PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.7
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.7
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 +23 -140 6.306.7 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
@@ -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.
@@ -133,9 +124,8 @@
133 124 * This also handles the shortcode output. Support [id], [id show=first], [id show=last], [id show=middle].
134 125 *
135 126 * @param mixed $value Field value before processing.
136 127 * @param array $atts Shortcode attributes.
137 - *
138 128 * @return string Most of cases, this will return string.
139 129 */
140 130 protected function prepare_display_value( $value, $atts ) {
141 131 if ( ! is_array( $value ) ) {
@@ -141,10 +131,12 @@
141 131 if ( ! is_array( $value ) ) {
142 132 return $value;
143 133 }
144 134
135 + $name_layout = $this->get_name_layout();
136 +
145 137 if ( ! empty( $atts['show'] ) ) {
146 - return $value[ $atts['show'] ] ?? '';
138 + return isset( $value[ $atts['show'] ] ) ? $value[ $atts['show'] ] : '';
147 139 }
148 140
149 141 $value = wp_parse_args(
150 142 $value,
@@ -154,9 +146,9 @@
154 146 'last' => '',
155 147 )
156 148 );
157 149
158 - switch ( $this->get_name_layout() ) {
150 + switch ( $name_layout ) {
159 151 case 'last_first':
160 152 $value = $value['last'] . ' ' . $value['first'];
161 153 break;
162 154
@@ -173,10 +165,8 @@
173 165
174 166 /**
175 167 * @since 4.0.04
176 168 *
177 - * @param array|string $value
178 - *
179 169 * @return void
180 170 */
181 171 public function sanitize_value( &$value ) {
182 172 FrmAppHelper::sanitize_value( 'sanitize_text_field', $value );
@@ -185,9 +175,8 @@
185 175 /**
186 176 * Validate field.
187 177 *
188 178 * @param array $args Arguments. Includes `errors`, `value`.
189 - *
190 179 * @return array Errors array.
191 180 */
192 181 public function validate( $args ) {
193 182 /**
@@ -217,17 +206,15 @@
217 206 protected function process_args_for_field_output( &$args ) {
218 207 parent::process_args_for_field_output( $args );
219 208
220 209 // 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 - }
210 + if ( $this->should_print_hidden_sub_fields() && count( $args['sub_fields'] ) !== count( $this->sub_fields ) ) {
211 + $hidden_fields = array_diff_key( $this->sub_fields, $args['sub_fields'] );
212 + $args['sub_fields'] = $this->sub_fields;
224 213
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';
214 + foreach ( $hidden_fields as $name => $hidden_field ) {
215 + $args['sub_fields'][ $name ]['wrapper_classes'] .= ' frm_hidden';
216 + }
230 217 }
231 218 }
232 219
233 220 /**
@@ -248,111 +235,7 @@
248 235 protected function get_inputs_container_attrs() {
249 236 $attrs = parent::get_inputs_container_attrs();
250 237
251 238 $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 239 return $attrs;
357 240 }
358 241 }