| @@ -2,9 +2,8 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Name field |
| 4 | 4 | * |
| 5 | 5 | * @package Formidable |
| 6 | - * | |
| 7 | 6 | * @since 4.11 |
| 8 | 7 | */ |
| 9 | 8 | |
| 10 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -13,19 +12,11 @@ | ||
| 13 | 12 | |
| 14 | 13 | class FrmFieldName extends FrmFieldCombo { |
| 15 | 14 | |
| 16 | 15 | /** |
| 17 | - * Track the first name field ID in forms. | |
| 18 | - * | |
| 19 | - * @var array Array with keys are form ID and values are name field IDs. | |
| 20 | - */ | |
| 21 | - private static $first_name_field_ids = array(); | |
| 22 | - | |
| 23 | - /** | |
| 24 | 16 | * Field name. |
| 25 | 17 | * |
| 26 | 18 | * @var string |
| 27 | - * | |
| 28 | 19 | * @since 3.0 |
| 29 | 20 | */ |
| 30 | 21 | protected $type = 'name'; |
| 31 | 22 | |
| @@ -32,25 +23,20 @@ | ||
| 32 | 23 | /** |
| 33 | 24 | * Could this field hold email values? |
| 34 | 25 | * |
| 35 | 26 | * @var bool |
| 36 | - * | |
| 37 | 27 | * @since 3.0 |
| 38 | 28 | */ |
| 39 | 29 | protected $holds_email_values = true; |
| 40 | 30 | |
| 41 | - /** | |
| 42 | - * @param array|int|object $field | |
| 43 | - * @param string $type | |
| 44 | - */ | |
| 45 | - public function __construct( $field = 0, $type = '' ) { | |
| 31 | + public function __construct( $field = '', $type = '' ) { | |
| 46 | 32 | parent::__construct( $field, $type ); |
| 47 | 33 | |
| 48 | 34 | $this->register_sub_fields( |
| 49 | 35 | array( |
| 50 | - 'first' => __( 'First Name', 'formidable' ), | |
| 51 | - 'middle' => __( 'Middle Name', 'formidable' ), | |
| 52 | - 'last' => __( 'Last Name', 'formidable' ), | |
| 36 | + 'first' => __( 'First', 'formidable' ), | |
| 37 | + 'middle' => __( 'Middle', 'formidable' ), | |
| 38 | + 'last' => __( 'Last', 'formidable' ), | |
| 53 | 39 | ) |
| 54 | 40 | ); |
| 55 | 41 | } |
| 56 | 42 | |
| @@ -92,9 +78,8 @@ | ||
| 92 | 78 | * @return string |
| 93 | 79 | */ |
| 94 | 80 | protected function get_name_layout() { |
| 95 | 81 | $name_layout = FrmField::get_option( $this->field, 'name_layout' ); |
| 96 | - | |
| 97 | 82 | if ( ! $name_layout ) { |
| 98 | 83 | $name_layout = 'first_last'; |
| 99 | 84 | } |
| 100 | 85 | return $name_layout; |
| @@ -123,10 +108,8 @@ | ||
| 123 | 108 | * |
| 124 | 109 | * @since 4.0 |
| 125 | 110 | * |
| 126 | 111 | * @param array $args Includes 'field', 'display', and 'values'. |
| 127 | - * | |
| 128 | - * @return void | |
| 129 | 112 | */ |
| 130 | 113 | public function show_primary_options( $args ) { |
| 131 | 114 | $field = (array) $args['field']; |
| 132 | 115 | include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/name/primary-options.php'; |
| @@ -139,9 +122,8 @@ | ||
| 139 | 122 | * This also handles the shortcode output. Support [id], [id show=first], [id show=last], [id show=middle]. |
| 140 | 123 | * |
| 141 | 124 | * @param mixed $value Field value before processing. |
| 142 | 125 | * @param array $atts Shortcode attributes. |
| 143 | - * | |
| 144 | 126 | * @return string Most of cases, this will return string. |
| 145 | 127 | */ |
| 146 | 128 | protected function prepare_display_value( $value, $atts ) { |
| 147 | 129 | if ( ! is_array( $value ) ) { |
| @@ -150,9 +132,9 @@ | ||
| 150 | 132 | |
| 151 | 133 | $name_layout = $this->get_name_layout(); |
| 152 | 134 | |
| 153 | 135 | if ( ! empty( $atts['show'] ) ) { |
| 154 | - return $value[ $atts['show'] ] ?? ''; | |
| 136 | + return isset( $value[ $atts['show'] ] ) ? $value[ $atts['show'] ] : ''; | |
| 155 | 137 | } |
| 156 | 138 | |
| 157 | 139 | $value = wp_parse_args( |
| 158 | 140 | $value, |
| @@ -180,12 +162,8 @@ | ||
| 180 | 162 | } |
| 181 | 163 | |
| 182 | 164 | /** |
| 183 | 165 | * @since 4.0.04 |
| 184 | - * | |
| 185 | - * @param array|string $value | |
| 186 | - * | |
| 187 | - * @return void | |
| 188 | 166 | */ |
| 189 | 167 | public function sanitize_value( &$value ) { |
| 190 | 168 | FrmAppHelper::sanitize_value( 'sanitize_text_field', $value ); |
| 191 | 169 | } |
| @@ -193,9 +171,8 @@ | ||
| 193 | 171 | /** |
| 194 | 172 | * Validate field. |
| 195 | 173 | * |
| 196 | 174 | * @param array $args Arguments. Includes `errors`, `value`. |
| 197 | - * | |
| 198 | 175 | * @return array Errors array. |
| 199 | 176 | */ |
| 200 | 177 | public function validate( $args ) { |
| 201 | 178 | /** |
| @@ -218,10 +195,8 @@ | ||
| 218 | 195 | * @type array $shortcode_atts Shortcode attributes. |
| 219 | 196 | * @type array $errors Field errors. |
| 220 | 197 | * @type bool $remove_names Remove name attribute or not. |
| 221 | 198 | * } |
| 222 | - * | |
| 223 | - * @return void | |
| 224 | 199 | */ |
| 225 | 200 | protected function process_args_for_field_output( &$args ) { |
| 226 | 201 | parent::process_args_for_field_output( $args ); |
| 227 | 202 | |
| @@ -254,110 +229,7 @@ | ||
| 254 | 229 | protected function get_inputs_container_attrs() { |
| 255 | 230 | $attrs = parent::get_inputs_container_attrs(); |
| 256 | 231 | |
| 257 | 232 | $attrs['data-name-layout'] = $this->get_name_layout(); |
| 258 | - return $attrs; | |
| 259 | - } | |
| 260 | - | |
| 261 | - /** | |
| 262 | - * Maybe show a warning if a name field is using a description that is not descriptive enough. | |
| 263 | - * Prior to version 6.16, First and Last were the default values, but this has been updated to | |
| 264 | - * improve accessibility. | |
| 265 | - * | |
| 266 | - * @since 6.16 | |
| 267 | - * | |
| 268 | - * @param array $args | |
| 269 | - * | |
| 270 | - * @return void | |
| 271 | - */ | |
| 272 | - public function show_after_default( $args ) { | |
| 273 | - echo '<div class="frm-mt-xs">'; | |
| 274 | - parent::show_after_default( $args ); | |
| 275 | - echo '</div>'; | |
| 276 | - | |
| 277 | - /** | |
| 278 | - * @var array $field | |
| 279 | - */ | |
| 280 | - $field = $args['field']; | |
| 281 | - | |
| 282 | - $show_warning = false; | |
| 283 | - | |
| 284 | - foreach ( $this->sub_fields as $name => $sub_field ) { | |
| 285 | - $description = FrmField::get_option( $field, $sub_field['name'] . '_desc' ); | |
| 286 | - | |
| 287 | - if ( in_array( $description, array( 'First', 'Last' ), true ) ) { | |
| 288 | - $show_warning = true; | |
| 289 | - break; | |
| 290 | - } | |
| 291 | - } | |
| 292 | - | |
| 293 | - if ( ! $show_warning ) { | |
| 294 | - return; | |
| 295 | - } | |
| 296 | - ?> | |
| 297 | - <div class="frm_warning_style"> | |
| 298 | - <?php | |
| 299 | - FrmAppHelper::icon_by_class( 'frm_icon_font frm_alert_icon', array( 'style' => 'width:24px' ) ); | |
| 300 | - echo ' '; | |
| 301 | - esc_html_e( 'Subfield descriptions are read by screen readers. Enhance accessibility by using complete labels, like "First Name" instead of "First".', 'formidable' ); | |
| 302 | - ?> | |
| 303 | - </div> | |
| 304 | - <?php | |
| 305 | - } | |
| 306 | - | |
| 307 | - /** | |
| 308 | - * Tracks the first name field ID in a form. | |
| 309 | - * | |
| 310 | - * @since 6.26 | |
| 311 | - * | |
| 312 | - * @param object[] $fields Array of fields in a form. | |
| 313 | - * | |
| 314 | - * @return void | |
| 315 | - */ | |
| 316 | - public static function track_first_name_field( $fields ) { | |
| 317 | - foreach ( $fields as $field ) { | |
| 318 | - if ( 'name' === $field->type ) { | |
| 319 | - self::$first_name_field_ids[ $field->form_id ] = $field->id; | |
| 320 | - return; | |
| 321 | - } | |
| 322 | - } | |
| 323 | - } | |
| 324 | - | |
| 325 | - /** | |
| 326 | - * Gets subfield input attributes. | |
| 327 | - * | |
| 328 | - * @since 6.26 | |
| 329 | - * | |
| 330 | - * @param array $sub_field Subfield data. | |
| 331 | - * @param array $args Field output args. See {@see FrmFieldCombo::load_field_output()}. | |
| 332 | - * | |
| 333 | - * @return array | |
| 334 | - */ | |
| 335 | - protected function get_sub_field_input_attrs( $sub_field, $args ) { | |
| 336 | - $attrs = parent::get_sub_field_input_attrs( $sub_field, $args ); | |
| 337 | - | |
| 338 | - $form_id = (int) ( is_array( $args['field'] ) ? $args['field']['form_id'] : $args['field']->form_id ); | |
| 339 | - | |
| 340 | - if ( ! self::$first_name_field_ids || empty( self::$first_name_field_ids[ $form_id ] ) ) { | |
| 341 | - return $attrs; | |
| 342 | - } | |
| 343 | - | |
| 344 | - $parent_form_id = (int) FrmField::get_option( $args['field'], 'parent_form_id' ); | |
| 345 | - | |
| 346 | - if ( $form_id !== $parent_form_id ) { | |
| 347 | - // Do not add autocomplete attribute to a name field inside repeater. | |
| 348 | - return $attrs; | |
| 349 | - } | |
| 350 | - | |
| 351 | - $field_id = (int) ( is_array( $args['field'] ) ? $args['field']['id'] : $args['field']->id ); | |
| 352 | - | |
| 353 | - if ( intval( self::$first_name_field_ids[ $form_id ] ) === $field_id ) { | |
| 354 | - if ( 'first' === $sub_field['name'] ) { | |
| 355 | - $attrs['autocomplete'] = 'given-name'; | |
| 356 | - } elseif ( 'last' === $sub_field['name'] ) { | |
| 357 | - $attrs['autocomplete'] = 'family-name'; | |
| 358 | - } | |
| 359 | - } | |
| 360 | - | |
| 361 | 233 | return $attrs; |
| 362 | 234 | } |
| 363 | 235 | } |