[], 'first_name' => [ 'type' => 'text', 'attribs' => [ 'placeholder' => 'First Name', 'required' => true ] ], 'last_name' => [ 'type' => 'text', 'attribs' => [ 'placeholder' => 'Last Name', 'required' => true ] ], 'street' => [ 'type' => 'text', 'attribs' => [ 'placeholder' => 'Street', 'required' => true ] ], 'zip' => [ 'type' => 'text', 'attribs' => [ 'placeholder' => 'Zip', 'required' => true ] ], 'city' => [ 'type' => 'text', 'attribs' => [ 'placeholder' => 'City', 'required' => true ] ], 'email' => [ 'type' => 'email', 'attribs' => [ 'placeholder' => 'Email', 'required' => true ] ], 'phone' => [ 'type' => 'phone', 'attribs' => [ 'placeholder' => 'Phone', 'required' => true ] ], ]; public static function get_fields() { if ( empty( static::$fields['prefix'] ) ) { $multi_options = []; if ( 'en' == FQP\FQP::get_curr_lang() ) { $multi_options = [ 'Mr' => 'Mr', 'Ms' => 'Ms', 'Mrs' => 'Mrs' ]; } else { foreach ( [ 'Mr', 'Ms' ] as $text ) { $text = fqp__( $text ); $multi_options[$text] = $text; } } $prefix = [ 'type' => 'radio', 'multi_options' => $multi_options, 'attribs' => [ 'required' => true ], ]; static::$fields['prefix'] = $prefix; } return static::$fields; } public static function sanitize_data( &$data ) { if ( !is_array( $data ) ) { $data = []; } $errors = []; $fields = [ 'prefix' => fqp__('Prefix'), 'first_name' => fqp__('First Name'), 'last_name' => fqp__('Last Name'), 'street' => fqp__('Street'), 'zip' => fqp__('Zip'), 'city' => fqp__('City'), 'email' => fqp__('Email'), 'phone' => fqp__('Phone'), ]; $field_messages = [ 'required' => fqp__('%s is required'), 'wrong_format' => fqp__('%s is in wrong format'), ]; foreach ( $fields as $name => $title ) { if ( !array_key_exists( $name, $data ) || empty( $data[$name] ) ) { $errors[] = esc_html( sprintf( $field_messages['required'], $title ) ); } else if ( in_array( $name, [ 'email' ] ) ) { if ( !preg_match( FQP\FQP::EMAIL_REGEX, $data[$name] ) ) { $errors[] = esc_html( sprintf( $field_messages['wrong_format'], $title ) ); } } else if ( in_array( $name, [ 'zip' ] ) && ! empty( $data[$name] ) ) { if ( !preg_match( FQP\FQP::NUMERIC_REGEX, $data[$name] ) ) { $errors[] = esc_html( sprintf( $field_messages['wrong_format'], $title ) ); } } } if ( $data['phone'] ) { $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); try { $number = $phoneUtil->parse( $data['phone'], 'CH' ); if ( !$phoneUtil->isValidNumber( $number ) ) { $errors[] = esc_html( sprintf( $field_messages['wrong_format'], $fields['phone'] ) ); } } catch ( \libphonenumber\NumberParseException $e ) { $errors[] = esc_html( sprintf( $field_messages['wrong_format'], $fields['phone'] ) ); } } return ( empty( $errors ) ) ? true : $errors; } }