PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / trunk
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More vtrunk
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 +91 -21 6.25 → trunk View file →
@@ -2,8 +2,9 @@
2 2 /**
3 3 * Name field
4 4 *
5 5 * @package Formidable
6 + *
6 7 * @since 4.11
7 8 */
8 9
9 10 if ( ! defined( 'ABSPATH' ) ) {
@@ -12,11 +13,19 @@
12 13
13 14 class FrmFieldName extends FrmFieldCombo {
14 15
15 16 /**
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 + /**
16 24 * Field name.
17 25 *
18 26 * @var string
27 + *
19 28 * @since 3.0
20 29 */
21 30 protected $type = 'name';
22 31
@@ -23,8 +32,9 @@
23 32 /**
24 33 * Could this field hold email values?
25 34 *
26 35 * @var bool
36 + *
27 37 * @since 3.0
28 38 */
29 39 protected $holds_email_values = true;
30 40
@@ -50,14 +60,12 @@
50 60 *
51 61 * @return array
52 62 */
53 63 protected function get_processed_sub_fields() {
54 - $name_layout = $this->get_name_layout();
55 - $names = explode( '_', $name_layout );
56 - $col_class = 'frm' . intval( 12 / count( $names ) );
64 + $names = explode( '_', $this->get_name_layout() );
65 + $col_class = 'frm' . intval( 12 / count( $names ) );
66 + $result = array();
57 67
58 - $result = array();
59 -
60 68 foreach ( $names as $name ) {
61 69 if ( empty( $this->sub_fields[ $name ] ) ) {
62 70 continue;
63 71 }
@@ -82,12 +90,9 @@
82 90 * @return string
83 91 */
84 92 protected function get_name_layout() {
85 93 $name_layout = FrmField::get_option( $this->field, 'name_layout' );
86 - if ( ! $name_layout ) {
87 - $name_layout = 'first_last';
88 - }
89 - return $name_layout;
94 + return $name_layout ? $name_layout : 'first_last';
90 95 }
91 96
92 97 /**
93 98 * Gets extra field options.
@@ -128,8 +133,9 @@
128 133 * This also handles the shortcode output. Support [id], [id show=first], [id show=last], [id show=middle].
129 134 *
130 135 * @param mixed $value Field value before processing.
131 136 * @param array $atts Shortcode attributes.
137 + *
132 138 * @return string Most of cases, this will return string.
133 139 */
134 140 protected function prepare_display_value( $value, $atts ) {
135 141 if ( ! is_array( $value ) ) {
@@ -135,10 +141,8 @@
135 141 if ( ! is_array( $value ) ) {
136 142 return $value;
137 143 }
138 144
139 - $name_layout = $this->get_name_layout();
140 -
141 145 if ( ! empty( $atts['show'] ) ) {
142 146 return $value[ $atts['show'] ] ?? '';
143 147 }
144 148
@@ -150,9 +154,9 @@
150 154 'last' => '',
151 155 )
152 156 );
153 157
154 - switch ( $name_layout ) {
158 + switch ( $this->get_name_layout() ) {
155 159 case 'last_first':
156 160 $value = $value['last'] . ' ' . $value['first'];
157 161 break;
158 162
@@ -169,8 +173,10 @@
169 173
170 174 /**
171 175 * @since 4.0.04
172 176 *
177 + * @param array|string $value
178 + *
173 179 * @return void
174 180 */
175 181 public function sanitize_value( &$value ) {
176 182 FrmAppHelper::sanitize_value( 'sanitize_text_field', $value );
@@ -179,8 +185,9 @@
179 185 /**
180 186 * Validate field.
181 187 *
182 188 * @param array $args Arguments. Includes `errors`, `value`.
189 + *
183 190 * @return array Errors array.
184 191 */
185 192 public function validate( $args ) {
186 193 /**
@@ -210,15 +217,17 @@
210 217 protected function process_args_for_field_output( &$args ) {
211 218 parent::process_args_for_field_output( $args );
212 219
213 220 // Show all subfields in form builder then use JS to show or hide them.
214 - if ( $this->should_print_hidden_sub_fields() && count( $args['sub_fields'] ) !== count( $this->sub_fields ) ) {
215 - $hidden_fields = array_diff_key( $this->sub_fields, $args['sub_fields'] );
216 - $args['sub_fields'] = $this->sub_fields;
221 + if ( ! $this->should_print_hidden_sub_fields() || count( $args['sub_fields'] ) === count( $this->sub_fields ) ) {
222 + return;
223 + }
217 224
218 - foreach ( $hidden_fields as $name => $hidden_field ) {
219 - $args['sub_fields'][ $name ]['wrapper_classes'] .= ' frm_hidden';
220 - }
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';
221 230 }
222 231 }
223 232
224 233 /**
@@ -235,9 +244,9 @@
235 244 * Gets inputs container attributes.
236 245 *
237 246 * @return array
238 247 */
239 - protected function get_inputs_container_attrs() {
248 + public function get_inputs_container_attrs() {
240 249 $attrs = parent::get_inputs_container_attrs();
241 250
242 251 $attrs['data-name-layout'] = $this->get_name_layout();
243 252 return $attrs;
@@ -250,8 +259,9 @@
250 259 *
251 260 * @since 6.16
252 261 *
253 262 * @param array $args
263 + *
254 264 * @return void
255 265 */
256 266 public function show_after_default( $args ) {
257 267 echo '<div class="frm-mt-xs">';
@@ -263,10 +273,12 @@
263 273 */
264 274 $field = $args['field'];
265 275
266 276 $show_warning = false;
267 - foreach ( $this->sub_fields as $name => $sub_field ) {
277 +
278 + foreach ( $this->sub_fields as $sub_field ) {
268 279 $description = FrmField::get_option( $field, $sub_field['name'] . '_desc' );
280 +
269 281 if ( in_array( $description, array( 'First', 'Last' ), true ) ) {
270 282 $show_warning = true;
271 283 break;
272 284 }
@@ -274,15 +286,73 @@
274 286
275 287 if ( ! $show_warning ) {
276 288 return;
277 289 }
290 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
278 291 ?>
279 292 <div class="frm_warning_style">
280 293 <?php
281 - FrmAppHelper::icon_by_class( 'frm_icon_font frm_alert_icon', array( 'style' => 'width:24px' ) );
294 + FrmAppHelper::icon_by_class( 'frmfont frm_alert_icon', array( 'style' => 'width:24px' ) );
282 295 echo ' ';
283 296 esc_html_e( 'Subfield descriptions are read by screen readers. Enhance accessibility by using complete labels, like "First Name" instead of "First".', 'formidable' );
284 297 ?>
285 298 </div>
286 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 + return $attrs;
287 357 }
288 358 }