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