format_of( $field ); } /** * Name format. * * @param mixed $field Field config. * @return string "split" or "full". */ private function format_of( $field ) { return ( is_array( $field ) && isset( $field['format'] ) && 'full' === $field['format'] ) ? 'full' : 'split'; } /** {@inheritdoc} */ public function sanitize_config( array $raw ) { return array( 'format' => $this->format_of( $raw ), 'firstPlaceholder' => isset( $raw['firstPlaceholder'] ) ? sanitize_text_field( (string) $raw['firstPlaceholder'] ) : '', 'lastPlaceholder' => isset( $raw['lastPlaceholder'] ) ? sanitize_text_field( (string) $raw['lastPlaceholder'] ) : '', ); } /** {@inheritdoc} */ public function sanitize_value( $raw, array $field ) { if ( 'full' === $this->format_of( $field ) ) { return parent::sanitize_value( $raw, $field ); } $raw = is_array( $raw ) ? $raw : array(); return array( 'first' => isset( $raw['first'] ) && is_scalar( $raw['first'] ) ? trim( sanitize_text_field( (string) $raw['first'] ) ) : '', 'last' => isset( $raw['last'] ) && is_scalar( $raw['last'] ) ? trim( sanitize_text_field( (string) $raw['last'] ) ) : '', ); } /** {@inheritdoc} */ public function validate( $value, array $field ) { if ( is_array( $value ) && ! empty( $field['required'] ) && ( '' === $value['first'] || '' === $value['last'] ) ) { return __( 'Enter your first and last name.', 'fillaro' ); } return ''; } /** {@inheritdoc} */ public function to_text( $value, array $field ) { if ( is_array( $value ) ) { return trim( ( isset( $value['first'] ) ? $value['first'] : '' ) . ' ' . ( isset( $value['last'] ) ? $value['last'] : '' ) ); } return (string) $value; } /** {@inheritdoc} */ public function render_control( array $field, array $ctx ) { $required = ! empty( $field['required'] ); if ( 'full' === $this->format_of( $field ) ) { $attrs = $this->base_attrs( $field, $ctx ); $attrs['type'] = 'text'; $attrs['value'] = is_scalar( $ctx['value'] ) ? (string) $ctx['value'] : ''; $attrs['autocomplete'] = 'name'; return 'attrs( $attrs ) . ' />'; } $value = is_array( $ctx['value'] ) ? $ctx['value'] : array(); $parts = array( 'first' => array( __( 'First name', 'fillaro' ), 'given-name', $field['firstPlaceholder'] ), 'last' => array( __( 'Last name', 'fillaro' ), 'family-name', $field['lastPlaceholder'] ), ); $html = '
'; foreach ( $parts as $part => $meta ) { $id = $ctx['id'] . '-' . $part; $attrs = array( 'type' => 'text', 'id' => $id, 'name' => $ctx['name'] . '[' . $part . ']', 'class' => 'pf-input', 'value' => isset( $value[ $part ] ) ? (string) $value[ $part ] : '', 'autocomplete' => $meta[1], 'placeholder' => $meta[2], 'required' => $required, 'aria-required' => $required ? 'true' : false, 'aria-describedby' => $ctx['describedby'], ); $html .= sprintf( '
', $this->attrs( $attrs ), esc_attr( $id ), esc_html( $meta[0] ) ); } return $html . '
'; } }