[label, autocomplete, css modifier]. * * @return array */ private function parts() { return array( 'street' => array( __( 'Street address', 'fillaro' ), 'address-line1', 'full' ), 'line2' => array( __( 'Apartment, suite, etc.', 'fillaro' ), 'address-line2', 'full' ), 'city' => array( __( 'City', 'fillaro' ), 'address-level2', 'half' ), 'state' => array( __( 'State / Province', 'fillaro' ), 'address-level1', 'half' ), 'zip' => array( __( 'ZIP / Postal code', 'fillaro' ), 'postal-code', 'half' ), 'country' => array( __( 'Country', 'fillaro' ), 'country-name', 'half' ), ); } /** {@inheritdoc} */ public function sanitize_value( $raw, array $field ) { $raw = is_array( $raw ) ? $raw : array(); $clean = array(); foreach ( array_keys( $this->parts() ) as $part ) { $clean[ $part ] = isset( $raw[ $part ] ) && is_scalar( $raw[ $part ] ) ? trim( sanitize_text_field( (string) $raw[ $part ] ) ) : ''; } return $clean; } /** {@inheritdoc} */ public function validate( $value, array $field ) { if ( ! empty( $field['required'] ) && ( '' === $value['street'] || '' === $value['city'] || '' === $value['country'] ) ) { return __( 'Enter street, city and country.', 'fillaro' ); } return ''; } /** {@inheritdoc} */ public function to_text( $value, array $field ) { if ( ! is_array( $value ) ) { return (string) $value; } $lines = array_filter( array( isset( $value['street'] ) ? $value['street'] : '', isset( $value['line2'] ) ? $value['line2'] : '', trim( implode( ' ', array_filter( array( isset( $value['city'] ) ? $value['city'] : '', isset( $value['state'] ) ? $value['state'] : '', isset( $value['zip'] ) ? $value['zip'] : '' ) ) ) ), isset( $value['country'] ) ? $value['country'] : '', ) ); return implode( ', ', $lines ); } /** {@inheritdoc} */ public function render_control( array $field, array $ctx ) { $value = is_array( $ctx['value'] ) ? $ctx['value'] : array(); $required = ! empty( $field['required'] ); $html = '
'; foreach ( $this->parts() as $part => $meta ) { $id = $ctx['id'] . '-' . $part; $req = $required && in_array( $part, array( 'street', 'city', 'country' ), true ); $attrs = array( 'type' => 'text', 'id' => $id, 'name' => $ctx['name'] . '[' . $part . ']', 'class' => 'pf-input', 'value' => isset( $value[ $part ] ) ? (string) $value[ $part ] : '', 'autocomplete' => $meta[1], 'required' => $req, 'aria-required' => $req ? 'true' : false, 'aria-describedby' => $ctx['describedby'], ); $html .= sprintf( '
', esc_attr( $meta[2] ), $this->attrs( $attrs ), esc_attr( $id ), esc_html( $meta[0] ) ); } return $html . '
'; } }