| @@ -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 | |
| @@ -60,12 +46,14 @@ | ||
| 60 | 46 | * |
| 61 | 47 | * @return array |
| 62 | 48 | */ |
| 63 | 49 | protected function get_processed_sub_fields() { |
| 64 | - $names = explode( '_', $this->get_name_layout() ); | |
| 65 | - $col_class = 'frm' . intval( 12 / count( $names ) ); | |
| 66 | - $result = array(); | |
| 50 | + $name_layout = $this->get_name_layout(); | |
| 51 | + $names = explode( '_', $name_layout ); | |
| 52 | + $col_class = 'frm' . intval( 12 / count( $names ) ); | |
| 67 | 53 | |
| 54 | + $result = array(); | |
| 55 | + | |
| 68 | 56 | foreach ( $names as $name ) { |
| 69 | 57 | if ( empty( $this->sub_fields[ $name ] ) ) { |
| 70 | 58 | continue; |
| 71 | 59 | } |
| @@ -90,9 +78,12 @@ | ||
| 90 | 78 | * @return string |
| 91 | 79 | */ |
| 92 | 80 | protected function get_name_layout() { |
| 93 | 81 | $name_layout = FrmField::get_option( $this->field, 'name_layout' ); |
| 94 | - return $name_layout ? $name_layout : 'first_last'; | |
| 82 | + if ( ! $name_layout ) { | |
| 83 | + $name_layout = 'first_last'; | |
| 84 | + } | |
| 85 | + return $name_layout; | |
| 95 | 86 | } |
| 96 | 87 | |
| 97 | 88 | /** |
| 98 | 89 | * Gets extra field options. |
| @@ -117,10 +108,8 @@ | ||
| 117 | 108 | * |
| 118 | 109 | * @since 4.0 |
| 119 | 110 | * |
| 120 | 111 | * @param array $args Includes 'field', 'display', and 'values'. |
| 121 | - * | |
| 122 | - * @return void | |
| 123 | 112 | */ |
| 124 | 113 | public function show_primary_options( $args ) { |
| 125 | 114 | $field = (array) $args['field']; |
| 126 | 115 | include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/name/primary-options.php'; |
| @@ -133,9 +122,8 @@ | ||
| 133 | 122 | * This also handles the shortcode output. Support [id], [id show=first], [id show=last], [id show=middle]. |
| 134 | 123 | * |
| 135 | 124 | * @param mixed $value Field value before processing. |
| 136 | 125 | * @param array $atts Shortcode attributes. |
| 137 | - * | |
| 138 | 126 | * @return string Most of cases, this will return string. |
| 139 | 127 | */ |
| 140 | 128 | protected function prepare_display_value( $value, $atts ) { |
| 141 | 129 | if ( ! is_array( $value ) ) { |
| @@ -141,10 +129,12 @@ | ||
| 141 | 129 | if ( ! is_array( $value ) ) { |
| 142 | 130 | return $value; |
| 143 | 131 | } |
| 144 | 132 | |
| 133 | + $name_layout = $this->get_name_layout(); | |
| 134 | + | |
| 145 | 135 | if ( ! empty( $atts['show'] ) ) { |
| 146 | - return $value[ $atts['show'] ] ?? ''; | |
| 136 | + return isset( $value[ $atts['show'] ] ) ? $value[ $atts['show'] ] : ''; | |
| 147 | 137 | } |
| 148 | 138 | |
| 149 | 139 | $value = wp_parse_args( |
| 150 | 140 | $value, |
| @@ -154,9 +144,9 @@ | ||
| 154 | 144 | 'last' => '', |
| 155 | 145 | ) |
| 156 | 146 | ); |
| 157 | 147 | |
| 158 | - switch ( $this->get_name_layout() ) { | |
| 148 | + switch ( $name_layout ) { | |
| 159 | 149 | case 'last_first': |
| 160 | 150 | $value = $value['last'] . ' ' . $value['first']; |
| 161 | 151 | break; |
| 162 | 152 | |
| @@ -172,12 +162,8 @@ | ||
| 172 | 162 | } |
| 173 | 163 | |
| 174 | 164 | /** |
| 175 | 165 | * @since 4.0.04 |
| 176 | - * | |
| 177 | - * @param array|string $value | |
| 178 | - * | |
| 179 | - * @return void | |
| 180 | 166 | */ |
| 181 | 167 | public function sanitize_value( &$value ) { |
| 182 | 168 | FrmAppHelper::sanitize_value( 'sanitize_text_field', $value ); |
| 183 | 169 | } |
| @@ -185,9 +171,8 @@ | ||
| 185 | 171 | /** |
| 186 | 172 | * Validate field. |
| 187 | 173 | * |
| 188 | 174 | * @param array $args Arguments. Includes `errors`, `value`. |
| 189 | - * | |
| 190 | 175 | * @return array Errors array. |
| 191 | 176 | */ |
| 192 | 177 | public function validate( $args ) { |
| 193 | 178 | /** |
| @@ -210,24 +195,20 @@ | ||
| 210 | 195 | * @type array $shortcode_atts Shortcode attributes. |
| 211 | 196 | * @type array $errors Field errors. |
| 212 | 197 | * @type bool $remove_names Remove name attribute or not. |
| 213 | 198 | * } |
| 214 | - * | |
| 215 | - * @return void | |
| 216 | 199 | */ |
| 217 | 200 | protected function process_args_for_field_output( &$args ) { |
| 218 | 201 | parent::process_args_for_field_output( $args ); |
| 219 | 202 | |
| 220 | 203 | // Show all subfields in form builder then use JS to show or hide them. |
| 221 | - if ( ! $this->should_print_hidden_sub_fields() || count( $args['sub_fields'] ) === count( $this->sub_fields ) ) { | |
| 222 | - return; | |
| 223 | - } | |
| 204 | + if ( $this->should_print_hidden_sub_fields() && count( $args['sub_fields'] ) !== count( $this->sub_fields ) ) { | |
| 205 | + $hidden_fields = array_diff_key( $this->sub_fields, $args['sub_fields'] ); | |
| 206 | + $args['sub_fields'] = $this->sub_fields; | |
| 224 | 207 | |
| 225 | - $hidden_fields = array_diff_key( $this->sub_fields, $args['sub_fields'] ); | |
| 226 | - $args['sub_fields'] = $this->sub_fields; | |
| 227 | - | |
| 228 | - foreach ( $hidden_fields as $name => $hidden_field ) { | |
| 229 | - $args['sub_fields'][ $name ]['wrapper_classes'] .= ' frm_hidden'; | |
| 208 | + foreach ( $hidden_fields as $name => $hidden_field ) { | |
| 209 | + $args['sub_fields'][ $name ]['wrapper_classes'] .= ' frm_hidden'; | |
| 210 | + } | |
| 230 | 211 | } |
| 231 | 212 | } |
| 232 | 213 | |
| 233 | 214 | /** |
| @@ -248,111 +229,7 @@ | ||
| 248 | 229 | protected function get_inputs_container_attrs() { |
| 249 | 230 | $attrs = parent::get_inputs_container_attrs(); |
| 250 | 231 | |
| 251 | 232 | $attrs['data-name-layout'] = $this->get_name_layout(); |
| 252 | - return $attrs; | |
| 253 | - } | |
| 254 | - | |
| 255 | - /** | |
| 256 | - * Maybe show a warning if a name field is using a description that is not descriptive enough. | |
| 257 | - * Prior to version 6.16, First and Last were the default values, but this has been updated to | |
| 258 | - * improve accessibility. | |
| 259 | - * | |
| 260 | - * @since 6.16 | |
| 261 | - * | |
| 262 | - * @param array $args | |
| 263 | - * | |
| 264 | - * @return void | |
| 265 | - */ | |
| 266 | - public function show_after_default( $args ) { | |
| 267 | - echo '<div class="frm-mt-xs">'; | |
| 268 | - parent::show_after_default( $args ); | |
| 269 | - echo '</div>'; | |
| 270 | - | |
| 271 | - /** | |
| 272 | - * @var array $field | |
| 273 | - */ | |
| 274 | - $field = $args['field']; | |
| 275 | - | |
| 276 | - $show_warning = false; | |
| 277 | - | |
| 278 | - foreach ( $this->sub_fields as $sub_field ) { | |
| 279 | - $description = FrmField::get_option( $field, $sub_field['name'] . '_desc' ); | |
| 280 | - | |
| 281 | - if ( in_array( $description, array( 'First', 'Last' ), true ) ) { | |
| 282 | - $show_warning = true; | |
| 283 | - break; | |
| 284 | - } | |
| 285 | - } | |
| 286 | - | |
| 287 | - if ( ! $show_warning ) { | |
| 288 | - return; | |
| 289 | - } | |
| 290 | - // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 291 | - ?> | |
| 292 | - <div class="frm_warning_style"> | |
| 293 | - <?php | |
| 294 | - FrmAppHelper::icon_by_class( 'frmfont frm_alert_icon', array( 'style' => 'width:24px' ) ); | |
| 295 | - echo ' '; | |
| 296 | - esc_html_e( 'Subfield descriptions are read by screen readers. Enhance accessibility by using complete labels, like "First Name" instead of "First".', 'formidable' ); | |
| 297 | - ?> | |
| 298 | - </div> | |
| 299 | - <?php | |
| 300 | - // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 301 | - } | |
| 302 | - | |
| 303 | - /** | |
| 304 | - * Tracks the first name field ID in a form. | |
| 305 | - * | |
| 306 | - * @since 6.26 | |
| 307 | - * | |
| 308 | - * @param object[] $fields Array of fields in a form. | |
| 309 | - * | |
| 310 | - * @return void | |
| 311 | - */ | |
| 312 | - public static function track_first_name_field( $fields ) { | |
| 313 | - foreach ( $fields as $field ) { | |
| 314 | - if ( 'name' === $field->type ) { | |
| 315 | - self::$first_name_field_ids[ $field->form_id ] = $field->id; | |
| 316 | - return; | |
| 317 | - } | |
| 318 | - } | |
| 319 | - } | |
| 320 | - | |
| 321 | - /** | |
| 322 | - * Gets subfield input attributes. | |
| 323 | - * | |
| 324 | - * @since 6.26 | |
| 325 | - * | |
| 326 | - * @param array $sub_field Subfield data. | |
| 327 | - * @param array $args Field output args. See {@see FrmFieldCombo::load_field_output()}. | |
| 328 | - * | |
| 329 | - * @return array | |
| 330 | - */ | |
| 331 | - protected function get_sub_field_input_attrs( $sub_field, $args ) { | |
| 332 | - $attrs = parent::get_sub_field_input_attrs( $sub_field, $args ); | |
| 333 | - $form_id = (int) ( is_array( $args['field'] ) ? $args['field']['form_id'] : $args['field']->form_id ); | |
| 334 | - | |
| 335 | - if ( ! self::$first_name_field_ids || empty( self::$first_name_field_ids[ $form_id ] ) ) { | |
| 336 | - return $attrs; | |
| 337 | - } | |
| 338 | - | |
| 339 | - $parent_form_id = (int) FrmField::get_option( $args['field'], 'parent_form_id' ); | |
| 340 | - | |
| 341 | - if ( $form_id !== $parent_form_id ) { | |
| 342 | - // Do not add autocomplete attribute to a name field inside repeater. | |
| 343 | - return $attrs; | |
| 344 | - } | |
| 345 | - | |
| 346 | - $field_id = (int) ( is_array( $args['field'] ) ? $args['field']['id'] : $args['field']->id ); | |
| 347 | - | |
| 348 | - if ( intval( self::$first_name_field_ids[ $form_id ] ) === $field_id ) { | |
| 349 | - if ( 'first' === $sub_field['name'] ) { | |
| 350 | - $attrs['autocomplete'] = 'given-name'; | |
| 351 | - } elseif ( 'last' === $sub_field['name'] ) { | |
| 352 | - $attrs['autocomplete'] = 'family-name'; | |
| 353 | - } | |
| 354 | - } | |
| 355 | - | |
| 356 | 233 | return $attrs; |
| 357 | 234 | } |
| 358 | 235 | } |