add_control( 'minlength', array( 'label' => __( 'Minimum length', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => '', 'inline' => true ) );
$this->add_control( 'maxlength', array( 'label' => __( 'Maximum length', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => '', 'inline' => true ) );
$this->add_control(
'pattern',
array(
'label' => __( 'Pattern (regular expression)', 'fieldwren' ),
'type' => Controls::TEXT,
'placeholder' => '^[A-Z]{2}[0-9]{4}$',
)
);
$this->add_control(
'pattern_message',
array(
'label' => __( 'Pattern message', 'fieldwren' ),
'type' => Controls::TEXT,
'condition' => array( 'pattern!' => '' ),
)
);
if ( $this->supports_unique() ) {
$this->add_control(
'unique',
array(
'label' => __( 'Unique value', 'fieldwren' ),
'type' => Controls::SWITCHER,
'default' => '',
'description' => __( 'Reject a value already submitted in another entry of this form.', 'fieldwren' ),
)
);
$this->add_control(
'unique_message',
array(
'label' => __( 'Unique message', 'fieldwren' ),
'type' => Controls::TEXT,
'default' => __( 'This value has already been submitted.', 'fieldwren' ),
'condition' => array( 'unique' => 'yes' ),
)
);
}
}
protected function schema_extra( array $s ) {
return array(
'minlength' => '' !== (string) ( $s['minlength'] ?? '' ) ? (int) $s['minlength'] : null,
'maxlength' => '' !== (string) ( $s['maxlength'] ?? '' ) ? (int) $s['maxlength'] : null,
'pattern' => (string) ( $s['pattern'] ?? '' ),
'pattern_message' => (string) ( $s['pattern_message'] ?? '' ),
'unique' => 'yes' === ( $s['unique'] ?? '' ),
'unique_message' => (string) ( $s['unique_message'] ?? '' ),
);
}
public function validate( $value, array $f ) {
$errors = array();
$value = is_array( $value ) ? '' : (string) $value;
if ( '' === $value ) {
return $errors;
}
$len = function_exists( 'mb_strlen' ) ? mb_strlen( $value ) : strlen( $value );
if ( null !== ( $f['minlength'] ?? null ) && $len < $f['minlength'] ) {
/* translators: %d characters */
$errors[] = sprintf( __( 'Please enter at least %d characters.', 'fieldwren' ), $f['minlength'] );
}
if ( ! empty( $f['maxlength'] ) && $len > $f['maxlength'] ) {
/* translators: %d characters */
$errors[] = sprintf( __( 'Please enter no more than %d characters.', 'fieldwren' ), $f['maxlength'] );
}
if ( ! empty( $f['pattern'] ) ) {
$re = '/' . str_replace( '/', '\/', $f['pattern'] ) . '/u';
if ( false !== @preg_match( $re, '' ) && ! preg_match( $re, $value ) ) { // phpcs:ignore
$errors[] = $f['pattern_message'] ? $f['pattern_message'] : __( 'Please match the requested format.', 'fieldwren' );
}
}
if ( ! empty( $f['unique'] ) && ! empty( $f['form_id'] ) && Entries::value_exists( $f['form_id'], $f['name'], $value ) ) {
$errors[] = $f['unique_message'] ? $f['unique_message'] : __( 'This value has already been submitted.', 'fieldwren' );
}
return $errors;
}
/** Length / pattern attributes for the input. */
protected function validation_attrs( array $s ) {
$a = array();
if ( '' !== (string) ( $s['maxlength'] ?? '' ) ) {
$a['maxlength'] = (int) $s['maxlength'];
}
if ( '' !== (string) ( $s['minlength'] ?? '' ) ) {
$a['minlength'] = (int) $s['minlength'];
}
if ( '' !== (string) ( $s['pattern'] ?? '' ) ) {
$a['pattern'] = $s['pattern'];
if ( '' !== (string) ( $s['pattern_message'] ?? '' ) ) {
$a['data-pattern-message'] = $s['pattern_message'];
}
}
return $a;
}
}
/** Single line text. */
class Text_Field extends Text_Like {
public function get_name() {
return 'text';
}
public function get_title() {
return __( 'Text', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'editor-textcolor' );
}
public function get_keywords() {
return array( 'input', 'text', 'single line' );
}
protected function register_type_controls() {
$this->add_control(
'mask',
array(
'label' => __( 'Input mask', 'fieldwren' ),
'type' => Controls::TEXT,
'placeholder' => '(999) 999-9999',
'description' => __( '9 = digit, a = letter, * = letter or digit. Leave empty for free text.', 'fieldwren' ),
)
);
$this->add_control(
'autocomplete',
array(
'label' => __( 'Autocomplete hint', 'fieldwren' ),
'type' => Controls::SELECT,
'default' => '',
'options' => array(
'' => __( 'None', 'fieldwren' ),
'name' => 'name',
'organization' => 'organization',
'organization-title' => 'organization-title',
'street-address' => 'street-address',
'postal-code' => 'postal-code',
'off' => 'off',
),
)
);
}
protected function render_input( array $s, $key, $id ) {
$extra = $this->validation_attrs( $s );
if ( '' !== (string) ( $s['mask'] ?? '' ) ) {
$extra['data-fwren-mask'] = $s['mask'];
}
if ( '' !== (string) ( $s['autocomplete'] ?? '' ) ) {
$extra['autocomplete'] = $s['autocomplete'];
}
$this->print_input( 'text', $s, $key, $id, $extra );
}
}
/** Email address. */
class Email_Field extends Text_Like {
public function get_name() {
return 'email';
}
public function get_title() {
return __( 'Email', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'email' );
}
protected function default_label() {
return __( 'Email', 'fieldwren' );
}
protected function register_type_controls() {
$this->add_control(
'confirm',
array(
'label' => __( 'Ask to confirm the email', 'fieldwren' ),
'type' => Controls::SWITCHER,
'default' => '',
)
);
$this->add_control(
'confirm_label',
array(
'label' => __( 'Confirmation label', 'fieldwren' ),
'type' => Controls::TEXT,
'default' => __( 'Confirm email', 'fieldwren' ),
'condition' => array( 'confirm' => 'yes' ),
)
);
}
protected function register_validation_controls() {
parent::register_validation_controls();
$this->add_control(
'allowed_domains',
array(
'label' => __( 'Allowed domains', 'fieldwren' ),
'type' => Controls::TEXT,
'placeholder' => 'example.com, company.org',
)
);
$this->add_control(
'blocked_domains',
array(
'label' => __( 'Blocked domains', 'fieldwren' ),
'type' => Controls::TEXT,
'placeholder' => 'gmail.com, yahoo.com',
'description' => __( 'Block free email providers on B2B forms, e.g. gmail.com, yahoo.com, outlook.com, hotmail.com.', 'fieldwren' ),
)
);
$this->add_control(
'verify_mx',
array(
'label' => __( 'Verify the email domain', 'fieldwren' ),
'type' => Controls::SWITCHER,
'default' => '',
'description' => __( 'Rejects addresses whose domain has no mail server (DNS MX / A record), e.g. typos like gmial.con.', 'fieldwren' ),
)
);
$this->add_control(
'block_disposable',
array(
'label' => __( 'Block disposable email addresses', 'fieldwren' ),
'type' => Controls::SWITCHER,
'default' => '',
'description' => __( 'Mailinator, Guerrilla Mail, 10 Minute Mail, YOPmail and other throwaway inboxes.', 'fieldwren' ),
)
);
}
/** @return string[] Common disposable email domains (filter: fwren/disposable_domains). */
public static function disposable_domains() {
return apply_filters(
'fwren/disposable_domains',
array( 'mailinator.com', 'guerrillamail.com', 'guerrillamail.net', 'sharklasers.com', 'grr.la', '10minutemail.com', '10minutemail.net', 'tempmail.com', 'temp-mail.org', 'tempmailo.com', 'yopmail.com', 'yopmail.net', 'trashmail.com', 'trashmail.de', 'getnada.com', 'nada.email', 'dispostable.com', 'maildrop.cc', 'mailnesia.com', 'mintemail.com', 'throwawaymail.com', 'fakeinbox.com', 'emailondeck.com', 'mohmal.com', 'tempinbox.com', 'mailcatch.com', 'spamgourmet.com', 'moakt.com', 'burnermail.io', 'inboxkitten.com', 'mail.tm', 'tmail.ws', 'tmpmail.org', 'tmpmail.net', 'discard.email', 'spambox.us', 'mytemp.email', 'emailfake.com', 'luxusmail.org', 'temporary-mail.net' )
);
}
protected function schema_extra( array $s ) {
return array_merge(
parent::schema_extra( $s ),
array(
'confirm' => 'yes' === ( $s['confirm'] ?? '' ),
'verify_mx' => 'yes' === ( $s['verify_mx'] ?? '' ),
'block_disposable'=> 'yes' === ( $s['block_disposable'] ?? '' ),
'allowed_domains' => self::domains( $s['allowed_domains'] ?? '' ),
'blocked_domains' => self::domains( $s['blocked_domains'] ?? '' ),
)
);
}
private static function domains( $s ) {
return array_values( array_filter( array_map( static function ( $d ) {
return strtolower( trim( ltrim( trim( $d ), '@' ) ) );
}, explode( ',', (string) $s ) ) ) );
}
public function sanitize( $raw, array $f ) {
return sanitize_email( is_array( $raw ) ? '' : (string) $raw );
}
public function validate( $value, array $f ) {
if ( '' === (string) $value ) {
return array();
}
if ( ! is_email( $value ) ) {
return array( __( 'Please enter a valid email address.', 'fieldwren' ) );
}
$domain = strtolower( substr( strrchr( $value, '@' ), 1 ) );
if ( ! empty( $f['allowed_domains'] ) && ! in_array( $domain, $f['allowed_domains'], true ) ) {
return array( __( 'Please use an email address from an allowed domain.', 'fieldwren' ) );
}
if ( ! empty( $f['blocked_domains'] ) && in_array( $domain, $f['blocked_domains'], true ) ) {
return array( __( 'Please use your work email address.', 'fieldwren' ) );
}
if ( ! empty( $f['block_disposable'] ) && in_array( $domain, self::disposable_domains(), true ) ) {
return array( __( 'Please use a permanent email address.', 'fieldwren' ) );
}
if ( ! empty( $f['verify_mx'] ) && function_exists( 'checkdnsrr' ) && ! checkdnsrr( $domain . '.', 'MX' ) && ! checkdnsrr( $domain . '.', 'A' ) ) {
return array( __( 'This email domain does not accept mail. Please check for typos.', 'fieldwren' ) );
}
if ( ! empty( $f['confirm'] ) && isset( $f['_confirm_value'] ) && strtolower( (string) $f['_confirm_value'] ) !== strtolower( (string) $value ) ) {
return array( __( 'The email addresses do not match.', 'fieldwren' ) );
}
return parent::validate( $value, $f );
}
public function format( $value, array $f, $mode = 'html' ) {
$value = (string) $value;
return 'html' === $mode && $value ? '' . esc_html( $value ) . '' : $value;
}
protected function render_input( array $s, $key, $id ) {
$this->print_input( 'email', $s, $key, $id, array_merge( $this->validation_attrs( $s ), array( 'autocomplete' => 'email', 'inputmode' => 'email' ) ) );
if ( 'yes' === ( $s['confirm'] ?? '' ) ) {
echo '';
echo '';
}
}
}
/** Website URL. */
class Url_Field extends Text_Like {
public function get_name() {
return 'url';
}
public function get_title() {
return __( 'Website URL', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'admin-links' );
}
protected function supports_unique() {
return false;
}
public function sanitize( $raw, array $f ) {
$raw = trim( is_array( $raw ) ? '' : (string) $raw );
if ( '' !== $raw && ! preg_match( '#^[a-z][a-z0-9+.-]*://#i', $raw ) ) {
$raw = 'https://' . $raw;
}
return esc_url_raw( $raw );
}
public function validate( $value, array $f ) {
if ( '' !== (string) $value && ! wp_http_validate_url( $value ) ) {
return array( __( 'Please enter a valid URL.', 'fieldwren' ) );
}
return parent::validate( $value, $f );
}
public function format( $value, array $f, $mode = 'html' ) {
$value = (string) $value;
return 'html' === $mode && $value ? '' . esc_html( $value ) . '' : $value;
}
protected function render_input( array $s, $key, $id ) {
$this->print_input( 'url', $s, $key, $id, array( 'inputmode' => 'url', 'autocomplete' => 'url' ) );
}
}
/** Password (not stored by default). */
class Password_Field extends Text_Like {
public function get_name() {
return 'password';
}
public function get_title() {
return __( 'Password', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'lock' );
}
public function get_category() {
return 'fwren-advanced';
}
protected function supports_unique() {
return false;
}
protected function has_default() {
return false;
}
protected function register_type_controls() {
$this->add_control( 'strength', array( 'label' => __( 'Show strength meter', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => 'yes' ) );
$this->add_control( 'store', array( 'label' => __( 'Store in entries', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '', 'description' => __( 'Off: the value is only passed to actions such as user registration and never saved.', 'fieldwren' ) ) );
}
protected function schema_extra( array $s ) {
return array_merge( parent::schema_extra( $s ), array( 'sensitive' => 'yes' !== ( $s['store'] ?? '' ) ) );
}
public function sanitize( $raw, array $f ) {
return is_array( $raw ) ? '' : (string) $raw; // Passwords are not altered.
}
public function format( $value, array $f, $mode = 'html' ) {
return '' === (string) $value ? '' : '••••••••';
}
protected function render_input( array $s, $key, $id ) {
$this->print_input( 'password', $s, $key, $id, array_merge( $this->validation_attrs( $s ), array( 'autocomplete' => 'new-password', 'data-fwren-strength' => 'yes' === ( $s['strength'] ?? '' ) ? '1' : false ) ) );
if ( 'yes' === ( $s['strength'] ?? '' ) ) {
echo '
';
}
}
}
/** Number. */
class Number_Field extends Field_Block {
public function get_name() {
return 'number';
}
public function get_title() {
return __( 'Number', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'calculator' );
}
protected function has_validation() {
return true;
}
protected function register_type_controls() {
$this->add_control( 'prefix', array( 'label' => __( 'Prefix', 'fieldwren' ), 'type' => Controls::TEXT, 'placeholder' => '$' ) );
$this->add_control( 'suffix', array( 'label' => __( 'Suffix', 'fieldwren' ), 'type' => Controls::TEXT, 'placeholder' => 'kg' ) );
}
protected function register_validation_controls() {
$this->add_control( 'min', array( 'label' => __( 'Minimum', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => '', 'inline' => true ) );
$this->add_control( 'max', array( 'label' => __( 'Maximum', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => '', 'inline' => true ) );
$this->add_control( 'step', array( 'label' => __( 'Step', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => 'any' ) );
$this->add_control( 'integer', array( 'label' => __( 'Whole numbers only', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) );
}
protected function schema_extra( array $s ) {
return array(
'min' => is_numeric( $s['min'] ?? '' ) ? (float) $s['min'] : null,
'max' => is_numeric( $s['max'] ?? '' ) ? (float) $s['max'] : null,
'integer' => 'yes' === ( $s['integer'] ?? '' ),
'prefix' => (string) ( $s['prefix'] ?? '' ),
'suffix' => (string) ( $s['suffix'] ?? '' ),
);
}
public function sanitize( $raw, array $f ) {
$raw = is_array( $raw ) ? '' : str_replace( array( ',', ' ' ), '', (string) $raw );
return '' === $raw ? '' : ( is_numeric( $raw ) ? $raw + 0 : $raw );
}
public function validate( $value, array $f ) {
if ( '' === $value ) {
return array();
}
if ( ! is_numeric( $value ) ) {
return array( __( 'Please enter a number.', 'fieldwren' ) );
}
if ( ! empty( $f['integer'] ) && floor( (float) $value ) != $value ) { // phpcs:ignore
return array( __( 'Please enter a whole number.', 'fieldwren' ) );
}
if ( null !== $f['min'] && $value < $f['min'] ) {
/* translators: %s minimum */
return array( sprintf( __( 'Please enter a value of at least %s.', 'fieldwren' ), $f['min'] ) );
}
if ( null !== $f['max'] && $value > $f['max'] ) {
/* translators: %s maximum */
return array( sprintf( __( 'Please enter a value no greater than %s.', 'fieldwren' ), $f['max'] ) );
}
return array();
}
public function format( $value, array $f, $mode = 'html' ) {
if ( '' === $value || null === $value ) {
return '';
}
$out = ( $f['prefix'] ?? '' ) . $value . ( $f['suffix'] ?? '' );
return 'html' === $mode ? esc_html( $out ) : $out;
}
protected function render_input( array $s, $key, $id ) {
$extra = array(
'min' => is_numeric( $s['min'] ?? '' ) ? $s['min'] : false,
'max' => is_numeric( $s['max'] ?? '' ) ? $s['max'] : false,
'step' => $s['step'] ? $s['step'] : 'any',
'inputmode' => 'yes' === ( $s['integer'] ?? '' ) ? 'numeric' : 'decimal',
);
$pre = (string) ( $s['prefix'] ?? '' );
$suf = (string) ( $s['suffix'] ?? '' );
if ( $pre || $suf ) {
echo '';
if ( $pre ) {
echo '' . esc_html( $pre ) . '';
}
}
$this->print_input( 'number', $s, $key, $id, $extra );
if ( $pre || $suf ) {
if ( $suf ) {
echo '' . esc_html( $suf ) . '';
}
echo '
';
}
}
}
/** Multi-line text. */
class Textarea_Field extends Text_Like {
public function get_name() {
return 'textarea';
}
public function get_title() {
return __( 'Textarea', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'text' );
}
public function get_keywords() {
return array( 'message', 'paragraph', 'multi line' );
}
protected function supports_unique() {
return false;
}
protected function register_type_controls() {
$this->add_control( 'rows', array( 'label' => __( 'Rows', 'fieldwren' ), 'type' => Controls::NUMBER, 'default' => 4, 'inline' => true ) );
$this->add_control( 'counter', array( 'label' => __( 'Show character counter', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => '' ) );
$this->add_control( 'autogrow', array( 'label' => __( 'Grow with content', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => 'yes' ) );
}
public function sanitize( $raw, array $f ) {
return sanitize_textarea_field( is_array( $raw ) ? '' : (string) $raw );
}
protected function render_input( array $s, $key, $id ) {
$extra = $this->validation_attrs( $s );
$extra['rows'] = max( 1, (int) $s['rows'] );
if ( 'yes' === ( $s['autogrow'] ?? '' ) ) {
$extra['data-fwren-autogrow'] = '1';
}
echo ''; // phpcs:ignore
if ( 'yes' === ( $s['counter'] ?? '' ) ) {
echo '';
}
}
}
/** Hidden value (smartcodes: UTM, referrer, user data…). */
class Hidden_Field extends Field_Block {
public function get_name() {
return 'hidden';
}
public function get_title() {
return __( 'Hidden', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'hidden' );
}
public function get_category() {
return 'fwren-advanced';
}
protected function has_placeholder() {
return false;
}
protected function has_input_style() {
return false;
}
protected function has_conditions() {
return false;
}
protected function register_type_controls() {
$this->add_control(
'capture',
array(
'label' => __( 'Quick value', 'fieldwren' ),
'type' => Controls::SELECT,
'default' => '',
'options' => array(
'' => __( 'Use default value', 'fieldwren' ),
'{get.utm_source}' => 'UTM source',
'{get.utm_medium}' => 'UTM medium',
'{get.utm_campaign}' => 'UTM campaign',
'{get.utm_term}' => 'UTM term',
'{get.utm_content}' => 'UTM content',
'{get.gclid}' => 'Google Ads click id (gclid)',
'{http_referer}' => __( 'Referrer', 'fieldwren' ),
'{embed_post.permalink}' => __( 'Page URL', 'fieldwren' ),
'{embed_post.title}' => __( 'Page title', 'fieldwren' ),
'{user.email}' => __( 'User email', 'fieldwren' ),
'{user.ID}' => __( 'User ID', 'fieldwren' ),
'{ip}' => __( 'IP address', 'fieldwren' ),
'{browser.name}' => __( 'Browser', 'fieldwren' ),
),
)
);
}
protected function schema_extra( array $s ) {
return array( 'default' => (string) ( $s['capture'] ? $s['capture'] : ( $s['default_value'] ?? '' ) ) );
}
protected function render( $content = '' ) {
$s = $this->settings;
$key = $this->field_key( $s );
if ( Renderer::is_editor() ) {
echo '' . \FWREN\Icons::svg( 'hidden', 16 ) . ' ' . esc_html( $s['label'] ? $s['label'] : $key ) . ' ' . esc_html( $s['capture'] ? $s['capture'] : $s['default_value'] ) . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Icons::svg() returns static bundled SVG markup.
return;
}
$v = $s['capture'] ? $s['capture'] : (string) $s['default_value'];
echo '';
}
protected function add_render_attributes() {
parent::add_render_attributes();
$this->add_render_attribute( '_wrapper', 'class', 'fwren-hidden-field' );
}
}
/** Phone with country code. */
class Phone_Field extends Text_Like {
public function get_name() {
return 'phone';
}
public function get_title() {
return __( 'Phone', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'phone' );
}
protected function default_label() {
return __( 'Phone', 'fieldwren' );
}
protected function register_type_controls() {
$this->add_control( 'country_code', array( 'label' => __( 'Country code picker', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => 'yes' ) );
$this->add_control(
'default_country',
array(
'label' => __( 'Default country', 'fieldwren' ),
'type' => Controls::SELECT,
'default' => 'auto',
'options' => array( 'auto' => __( 'Detect from browser language', 'fieldwren' ) ) + \FWREN\Countries::names(),
'condition' => array( 'country_code' => 'yes' ),
)
);
$this->add_control(
'countries',
array(
'label' => __( 'Only these countries', 'fieldwren' ),
'type' => Controls::SELECT2,
'default' => array(),
'options' => \FWREN\Countries::names(),
'condition' => array( 'country_code' => 'yes' ),
'description' => __( 'Empty = all countries.', 'fieldwren' ),
)
);
}
protected function schema_extra( array $s ) {
return array_merge( parent::schema_extra( $s ), array( 'country_code' => 'yes' === ( $s['country_code'] ?? '' ) ) );
}
public function sanitize( $raw, array $f ) {
return preg_replace( '/[^0-9+()\-\s.x]/i', '', is_array( $raw ) ? '' : (string) $raw );
}
public function validate( $value, array $f ) {
if ( '' === (string) $value ) {
return array();
}
$digits = preg_replace( '/\D/', '', $value );
if ( strlen( $digits ) < 6 || strlen( $digits ) > 16 ) {
return array( __( 'Please enter a valid phone number.', 'fieldwren' ) );
}
return parent::validate( $value, $f );
}
public function format( $value, array $f, $mode = 'html' ) {
$value = (string) $value;
return 'html' === $mode && $value ? '' . esc_html( $value ) . '' : $value;
}
protected function render_input( array $s, $key, $id ) {
if ( 'yes' !== ( $s['country_code'] ?? '' ) ) {
$this->print_input( 'tel', $s, $key, $id, array( 'autocomplete' => 'tel', 'inputmode' => 'tel' ) );
return;
}
$list = \FWREN\Countries::dial_codes();
$only = array_filter( (array) ( $s['countries'] ?? array() ) );
$def = (string) ( $s['default_country'] ?? 'auto' );
echo '';
echo '';
echo '';
echo '';
echo '
';
}
}
/** Name: first / middle / last. */
class Name_Field extends Field_Block {
const PARTS = array( 'first', 'middle', 'last' );
public function get_name() {
return 'name';
}
public function get_title() {
return __( 'Name', 'fieldwren' );
}
public function get_icon() {
return \FWREN\Icons::svg( 'admin-users' );
}
public function key_prefix() {
return 'names';
}
protected function has_placeholder() {
return false;
}
protected function has_default() {
return false;
}
protected function register_type_controls() {
$labels = array(
'first' => __( 'First name', 'fieldwren' ),
'middle' => __( 'Middle name', 'fieldwren' ),
'last' => __( 'Last name', 'fieldwren' ),
);
$this->add_control(
'name_mode',
array(
'label' => __( 'Format', 'fieldwren' ),
'type' => Controls::SELECT,
'default' => 'first_last',
'options' => array(
'full' => __( 'Full name (one input)', 'fieldwren' ),
'first_last' => __( 'First and last', 'fieldwren' ),
'all' => __( 'First, middle and last', 'fieldwren' ),
),
)
);
foreach ( self::PARTS as $p ) {
$cond = 'middle' === $p ? array( 'name_mode' => 'all' ) : array( 'name_mode!' => 'full' );
$this->add_control( "{$p}_label", array( 'label' => $labels[ $p ] . ' – ' . __( 'label', 'fieldwren' ), 'type' => Controls::TEXT, 'default' => $labels[ $p ], 'condition' => $cond ) );
$this->add_control( "{$p}_placeholder", array( 'label' => $labels[ $p ] . ' – ' . __( 'placeholder', 'fieldwren' ), 'type' => Controls::TEXT, 'condition' => $cond ) );
}
$this->add_control( 'full_placeholder', array( 'label' => __( 'Placeholder', 'fieldwren' ), 'type' => Controls::TEXT, 'condition' => array( 'name_mode' => 'full' ) ) );
$this->add_control( 'sublabels', array( 'label' => __( 'Show sub-labels', 'fieldwren' ), 'type' => Controls::SWITCHER, 'default' => 'yes', 'condition' => array( 'name_mode!' => 'full' ) ) );
}
private function parts( array $s ) {
$mode = $s['name_mode'] ?? 'first_last';
return 'full' === $mode ? array( 'full' ) : ( 'all' === $mode ? self::PARTS : array( 'first', 'last' ) );
}
protected function schema_extra( array $s ) {
return array( 'parts' => $this->parts( $s ) );
}
public function sanitize( $raw, array $f ) {
if ( ! is_array( $raw ) ) {
return array( 'full' => sanitize_text_field( (string) $raw ) );
}
$out = array();
foreach ( $f['parts'] as $p ) {
$out[ $p ] = sanitize_text_field( (string) ( $raw[ $p ] ?? '' ) );
}
return $out;
}
public function validate( $value, array $f ) {
if ( ! empty( $f['required'] ) && is_array( $value ) ) {
foreach ( array( 'first', 'full' ) as $p ) {
if ( in_array( $p, $f['parts'], true ) && '' === trim( (string) ( $value[ $p ] ?? '' ) ) ) {
return array( $f['required_message'] ? $f['required_message'] : __( 'Please enter your name.', 'fieldwren' ) );
}
}
}
return array();
}
public function format( $value, array $f, $mode = 'html' ) {
$v = is_array( $value ) ? trim( implode( ' ', array_filter( array_map( 'strval', $value ) ) ) ) : (string) $value;
return 'html' === $mode ? esc_html( $v ) : $v;
}
protected function render_input( array $s, $key, $id ) {
$parts = $this->parts( $s );
$auto = array( 'first' => 'given-name', 'middle' => 'additional-name', 'last' => 'family-name', 'full' => 'name' );
echo '';
foreach ( $parts as $i => $p ) {
$pid = 0 === $i ? $id : $id . '-' . $p;
$ph = (string) ( $s[ "{$p}_placeholder" ] ?? '' );
echo '
';
if ( 'full' !== $p && 'yes' === ( $s['sublabels'] ?? '' ) ) {
echo '';
}
$req = 'yes' === ( $s['required'] ?? '' ) && in_array( $p, array( 'first', 'full', 'last' ), true );
echo '';
echo '
';
}
echo '
';
}
}