PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.16
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.16
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/FrmFieldFormHtml.php +37 -130 6.306.16 View file →
@@ -7,16 +7,10 @@
7 7 * @since 3.0
8 8 */
9 9 class FrmFieldFormHtml {
10 10
11 - /**
12 - * @var string
13 - */
14 11 private $html;
15 12
16 - /**
17 - * @var string
18 - */
19 13 private $html_id;
20 14
21 15 /**
22 16 * @var FrmFieldType
@@ -22,21 +16,12 @@
22 16 * @var FrmFieldType
23 17 */
24 18 private $field_obj;
25 19
26 - /**
27 - * @var int|string
28 - */
29 20 private $field_id;
30 21
31 - /**
32 - * @var array|object
33 - */
34 22 private $form = array();
35 23
36 - /**
37 - * @var array
38 - */
39 24 private $pass_args = array();
40 25
41 26 /**
42 27 * @since 3.0
@@ -56,10 +41,8 @@
56 41 * @since 3.0
57 42 *
58 43 * @param string $param
59 44 * @param array $atts
60 - *
61 - * @return void
62 45 */
63 46 private function _set( $param, $atts ) {
64 47 if ( isset( $atts[ $param ] ) ) {
65 48 $this->{$param} = $atts[ $param ];
@@ -69,10 +52,8 @@
69 52 /**
70 53 * @since 3.0
71 54 *
72 55 * @param array $atts
73 - *
74 - * @return void
75 56 */
76 57 private function set_html( $atts ) {
77 58 $this->set_from_field(
78 59 $atts,
@@ -86,10 +67,8 @@
86 67 /**
87 68 * @since 3.0
88 69 *
89 70 * @param array $atts
90 - *
91 - * @return void
92 71 */
93 72 private function set_field_id( $atts ) {
94 73 $this->set_from_field(
95 74 $atts,
@@ -103,16 +82,13 @@
103 82 /**
104 83 * @since 3.0
105 84 *
106 85 * @param array $atts
107 - *
108 - * @return void
109 86 */
110 87 private function set_pass_args( $atts ) {
111 88 $this->pass_args = $atts;
112 89
113 90 $exclude = array( 'field_obj', 'html' );
114 -
115 91 foreach ( $exclude as $ex ) {
116 92 if ( isset( $atts[ $ex ] ) ) {
117 93 unset( $this->pass_args[ $ex ] );
118 94 }
@@ -123,10 +99,8 @@
123 99 * @since 3.0
124 100 *
125 101 * @param array $atts
126 102 * @param array $set
127 - *
128 - * @return void
129 103 */
130 104 private function set_from_field( $atts, $set ) {
131 105 if ( isset( $atts[ $set['param'] ] ) ) {
132 106 $this->{$set['param']} = $atts[ $set['param'] ];
@@ -134,11 +108,8 @@
134 108 $this->{$set['param']} = $this->field_obj->get_field_column( $set['default'] );
135 109 }
136 110 }
137 111
138 - /**
139 - * @return string
140 - */
141 112 public function get_html() {
142 113 $this->replace_shortcodes_before_input();
143 114 $this->replace_shortcodes_with_atts();
144 115 $this->replace_shortcodes_after_input();
@@ -147,10 +118,8 @@
147 118 }
148 119
149 120 /**
150 121 * @since 3.0
151 - *
152 - * @return void
153 122 */
154 123 private function replace_shortcodes_before_input() {
155 124 $this->html = apply_filters( 'frm_before_replace_shortcodes', $this->html, $this->field_obj->get_field(), $this->pass_args['errors'], $this->form );
156 125
@@ -172,16 +141,14 @@
172 141 }
173 142
174 143 /**
175 144 * @since 3.0
176 - *
177 - * @return void
178 145 */
179 146 private function replace_field_values() {
180 147 // Replace [id].
181 148 $this->html = str_replace( '[id]', $this->field_id, $this->html );
182 149
183 - // Set the label for
150 + // set the label for
184 151 $this->html = str_replace( 'field_[key]', $this->html_id, $this->html );
185 152
186 153 // Replace [key].
187 154 $this->html = str_replace( '[key]', $this->field_obj->get_field_column( 'field_key' ), $this->html );
@@ -191,10 +158,8 @@
191 158 }
192 159
193 160 /**
194 161 * @since 3.0
195 - *
196 - * @return void
197 162 */
198 163 private function replace_required_label_shortcode() {
199 164 $required = FrmField::is_required( $this->field_obj->get_field() ) ? $this->field_obj->get_field_column( 'required_indicator' ) : '';
200 165 FrmShortcodeHelper::remove_inline_conditions( ! empty( $required ), 'required_label', $required, $this->html );
@@ -204,17 +169,12 @@
204 169 * If this is an HTML field, the values are included in the description.
205 170 * In this case, we don't want to run the wp shortcodes with the description included.
206 171 *
207 172 * @since 3.0
208 - *
209 - * @param bool $wp_processed
210 - *
211 - * @return void
212 173 */
213 174 private function maybe_replace_description_shortcode( $wp_processed = false ) {
214 175 $is_html = 'html' === $this->field_obj->get_field_column( 'type' );
215 - $should_replace = $is_html ? $wp_processed : ! $wp_processed;
216 -
176 + $should_replace = ( $is_html && $wp_processed ) || ( ! $is_html && ! $wp_processed );
217 177 if ( $should_replace ) {
218 178 $this->replace_description_shortcode();
219 179 }
220 180 }
@@ -220,16 +180,13 @@
220 180 }
221 181
222 182 /**
223 183 * @since 3.0
224 - *
225 - * @return void
226 184 */
227 185 private function replace_description_shortcode() {
228 186 $this->maybe_add_description_id();
229 187 $description = FrmAppHelper::maybe_kses( $this->field_obj->get_field_column( 'description' ) );
230 - // phpcs:ignore Universal.Operators.StrictComparisons
231 - FrmShortcodeHelper::remove_inline_conditions( $description && $description != '', 'description', $description, $this->html );
188 + FrmShortcodeHelper::remove_inline_conditions( ( $description && $description != '' ), 'description', $description, $this->html );
232 189 }
233 190
234 191 /**
235 192 * Add an ID to the description for aria-describedby.
@@ -235,15 +192,11 @@
235 192 * Add an ID to the description for aria-describedby.
236 193 * This ID was added to the HTML in v3.0.
237 194 *
238 195 * @since 3.0
239 - *
240 - * @return void
241 196 */
242 197 private function maybe_add_description_id() {
243 198 $description = $this->field_obj->get_field_column( 'description' );
244 -
245 - // phpcs:ignore Universal.Operators.StrictComparisons
246 199 if ( $description != '' ) {
247 200 $this->add_element_id( 'description', 'desc' );
248 201 }
249 202 }
@@ -251,17 +204,11 @@
251 204 /**
252 205 * Insert an ID if it doesn't exist.
253 206 *
254 207 * @since 3.06.02
255 - *
256 - * @param string $param
257 - * @param string $id
258 - *
259 - * @return void
260 208 */
261 209 private function add_element_id( $param, $id ) {
262 210 preg_match_all( '/(\[if\s+' . $param . '\])(.*?)(\[\/if\s+' . $param . '\])/mis', $this->html, $inner_html );
263 -
264 211 if ( ! isset( $inner_html[2] ) ) {
265 212 return;
266 213 }
267 214
@@ -268,35 +215,27 @@
268 215 if ( ! is_string( $inner_html[2] ) && count( $inner_html[2] ) === 1 ) {
269 216 $inner_html[2] = $inner_html[2][0];
270 217 }
271 218
272 - if ( ! is_string( $inner_html[2] ) ) {
273 - return;
219 + if ( is_string( $inner_html[2] ) ) {
220 + $has_id = strpos( $inner_html[2], ' id=' );
221 + if ( ! $has_id ) {
222 + $id = 'frm_' . $id . '_' . $this->html_id;
223 + $this->html = str_replace( 'class="frm_' . $param, 'id="' . esc_attr( $id ) . '" class="frm_' . esc_attr( $param ), $this->html );
224 + }
274 225 }
275 -
276 - $has_id = str_contains( $inner_html[2], ' id=' );
277 -
278 - if ( $has_id ) {
279 - return;
280 - }
281 -
282 - $id = 'frm_' . $id . '_' . $this->html_id;
283 - $this->html = str_replace( 'class="frm_' . $param, 'id="' . esc_attr( $id ) . '" class="frm_' . esc_attr( $param ), $this->html );
284 226 }
285 227
286 228 /**
287 229 * @since 3.0
288 - *
289 - * @return void
290 230 */
291 231 private function replace_error_shortcode() {
292 232 $this->maybe_add_error_id();
293 - $error = $this->pass_args['errors'][ 'field' . $this->field_id ] ?? false;
233 + $error = isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ? $this->pass_args['errors'][ 'field' . $this->field_id ] : false;
294 234
295 - if ( $error && ! str_contains( $this->html, 'role="alert"' ) && FrmAppHelper::should_include_alert_role_on_field_errors() ) {
235 + if ( ! empty( $error ) && false === strpos( $this->html, 'role="alert"' ) && FrmAppHelper::should_include_alert_role_on_field_errors() ) {
296 236 $error_body = self::get_error_body( $this->html );
297 -
298 - if ( is_string( $error_body ) && ! str_contains( $error_body, 'role=' ) ) {
237 + if ( is_string( $error_body ) && false === strpos( $error_body, 'role=' ) ) {
299 238 $new_error_body = preg_replace( '/class="frm_error/', 'role="alert" class="frm_error', $error_body, 1 );
300 239 $this->html = str_replace( '[if error]' . $error_body . '[/if error]', '[if error]' . $new_error_body . '[/if error]', $this->html );
301 240 }
302 241 }
@@ -307,25 +246,23 @@
307 246 /**
308 247 * Pull the HTML between [if error] and [/if error] shortcodes.
309 248 *
310 249 * @param string $html
311 - *
312 250 * @return false|string
313 251 */
314 252 private static function get_error_body( $html ) {
315 253 $start = strpos( $html, '[if error]' );
316 -
317 254 if ( false === $start ) {
318 255 return false;
319 256 }
320 257
321 258 $end = strpos( $html, '[/if error]', $start );
322 -
323 259 if ( false === $end ) {
324 260 return false;
325 261 }
326 262
327 - return substr( $html, $start + 10, $end - $start - 10 );
263 + $error_body = substr( $html, $start + 10, $end - $start - 10 );
264 + return $error_body;
328 265 }
329 266
330 267 /**
331 268 * Add an ID to the error message for aria-describedby.
@@ -331,10 +268,8 @@
331 268 * Add an ID to the error message for aria-describedby.
332 269 * This ID was added to the HTML in v3.06.02.
333 270 *
334 271 * @since 3.06.02
335 - *
336 - * @return void
337 272 */
338 273 private function maybe_add_error_id() {
339 274 if ( ! isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ) {
340 275 return;
@@ -346,10 +281,8 @@
346 281 /**
347 282 * Replace [required_class]
348 283 *
349 284 * @since 3.0
350 - *
351 - * @return void
352 285 */
353 286 private function replace_required_class() {
354 287 $required_class = FrmField::is_required( $this->field_obj->get_field() ) ? ' frm_required_field' : '';
355 288 $this->html = str_replace( '[required_class]', $required_class, $this->html );
@@ -356,35 +289,29 @@
356 289 }
357 290
358 291 /**
359 292 * @since 3.0
360 - *
361 - * @return void
362 293 */
363 294 private function replace_form_shortcodes() {
364 - if ( ! $this->form ) {
365 - return;
366 - }
295 + if ( ! empty( $this->form ) ) {
296 + $form = (array) $this->form;
367 297
368 - $form = (array) $this->form;
298 + // Replace [form_key].
299 + $this->html = str_replace( '[form_key]', $form['form_key'], $this->html );
369 300
370 - // Replace [form_key].
371 - $this->html = str_replace( '[form_key]', $form['form_key'], $this->html );
372 -
373 - // Replace [form_name].
374 - $this->html = str_replace( '[form_name]', $form['name'], $this->html );
301 + // Replace [form_name].
302 + $this->html = str_replace( '[form_name]', $form['name'], $this->html );
303 + }
375 304 }
376 305
377 306 /**
378 307 * @since 3.0
379 - *
380 - * @return void
381 308 */
382 309 public function replace_shortcodes_after_input() {
383 310 $this->html .= "\n";
384 311
385 312 // Stop html filtering on confirmation field to prevent loop
386 - if ( $this->field_obj->get_field_column( 'conf_field' ) !== 'stop' ) {
313 + if ( $this->field_obj->get_field_column( 'conf_field' ) != 'stop' ) {
387 314 $this->filter_for_more_shortcodes();
388 315 }
389 316 }
390 317
@@ -389,10 +316,8 @@
389 316 }
390 317
391 318 /**
392 319 * @since 3.0
393 - *
394 - * @return void
395 320 */
396 321 private function filter_for_more_shortcodes() {
397 322 $atts = $this->pass_args;
398 323
@@ -411,13 +336,11 @@
411 336 *
412 337 * @since 3.0
413 338 *
414 339 * @param string $html
415 - *
416 - * @return void
417 340 */
418 341 public function remove_collapse_shortcode( &$html ) {
419 - if ( str_contains( $html, '[collapse_this]' ) ) {
342 + if ( strpos( $html, '[collapse_this]' ) ) {
420 343 $html = str_replace( '[collapse_this]', '', $html );
421 344 }
422 345 }
423 346
@@ -422,10 +345,8 @@
422 345 }
423 346
424 347 /**
425 348 * @since 3.0
426 - *
427 - * @return void
428 349 */
429 350 private function replace_shortcodes_with_atts() {
430 351 preg_match_all( "/\[(input|deletelink)\b(.*?)(?:(\/))?\]/s", $this->html, $shortcodes, PREG_PATTERN_ORDER );
431 352
@@ -431,10 +352,11 @@
431 352
432 353 foreach ( $shortcodes[0] as $short_key => $tag ) {
433 354 $shortcode_atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[2][ $short_key ] );
434 355 $tag = FrmShortcodeHelper::get_shortcode_tag( $shortcodes, $short_key );
435 - $replace_with = '';
436 356
357 + $replace_with = '';
358 +
437 359 if ( $tag === 'deletelink' && FrmAppHelper::pro_is_installed() ) {
438 360 $replace_with = FrmProEntriesController::entry_delete_link( $shortcode_atts );
439 361 } elseif ( $tag === 'input' ) {
440 362 $replace_with = $this->replace_input_shortcode( $shortcode_atts );
@@ -450,8 +372,9 @@
450 372 * @return string
451 373 */
452 374 private function replace_input_shortcode( $shortcode_atts ) {
453 375 $shortcode_atts = $this->prepare_input_shortcode_atts( $shortcode_atts );
376 +
454 377 return $this->field_obj->include_front_field_input( $this->pass_args, $shortcode_atts );
455 378 }
456 379
457 380 /**
@@ -463,9 +386,9 @@
463 386 if ( isset( $shortcode_atts['opt'] ) ) {
464 387 --$shortcode_atts['opt'];
465 388 }
466 389
467 - $field_class = $shortcode_atts['class'] ?? '';
390 + $field_class = isset( $shortcode_atts['class'] ) ? $shortcode_atts['class'] : '';
468 391 $this->field_obj->set_field_column( 'input_class', $field_class );
469 392
470 393 if ( isset( $shortcode_atts['class'] ) ) {
471 394 unset( $shortcode_atts['class'] );
@@ -470,9 +393,9 @@
470 393 if ( isset( $shortcode_atts['class'] ) ) {
471 394 unset( $shortcode_atts['class'] );
472 395 }
473 396
474 - $this->field_obj->set_aria_invalid_error( $shortcode_atts, $this->pass_args );
397 + $shortcode_atts['aria-invalid'] = isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ? 'true' : 'false';
475 398
476 399 $this->field_obj->set_field_column( 'shortcodes', $shortcode_atts );
477 400
478 401 return $shortcode_atts;
@@ -482,10 +405,8 @@
482 405 * Add the label position class into the HTML
483 406 * If the label position is inside, add a class to show the label if the field has a value.
484 407 *
485 408 * @since 3.0
486 - *
487 - * @return void
488 409 */
489 410 private function add_class_to_label() {
490 411 $label_class = $this->field_obj->get_label_class();
491 412 $this->html = str_replace( '[label_position]', $label_class, $this->html );
@@ -494,10 +415,8 @@
494 415 /**
495 416 * Replace [entry_key]
496 417 *
497 418 * @since 3.0
498 - *
499 - * @return void
500 419 */
501 420 private function replace_entry_key() {
502 421 $entry_key = FrmAppHelper::simple_get( 'entry', 'sanitize_title' );
503 422 $this->html = str_replace( '[entry_key]', $entry_key, $this->html );
@@ -506,18 +425,15 @@
506 425 /**
507 426 * Add classes to a field div
508 427 *
509 428 * @since 3.0
510 - *
511 - * @return void
512 429 */
513 430 private function add_field_div_classes() {
514 431 $classes = $this->get_field_div_classes();
515 432
516 - if ( in_array( $this->field_obj->get_field_column( 'type' ), array( 'html', 'summary' ), true ) && ! str_contains( $this->html, '[error_class]' ) ) {
517 - // There is no error_class shortcode for HTML fields
433 + if ( $this->field_obj->get_field_column( 'type' ) === 'html' && strpos( $this->html, '[error_class]' ) === false ) {
434 + // there is no error_class shortcode for HTML fields
518 435 $this->html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . esc_attr( $classes ), $this->html );
519 - return;
520 436 }
521 437
522 438 $this->html = str_replace( '[error_class]', esc_attr( $classes ), $this->html );
523 439 }
@@ -526,9 +442,9 @@
526 442 * Get the classes for a field div
527 443 *
528 444 * @since 3.0
529 445 *
530 - * @return string Classes.
446 + * @return string $classes
531 447 */
532 448 private function get_field_div_classes() {
533 449 // Add error class
534 450 $classes = isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ? ' frm_blank_field' : '';
@@ -534,10 +450,9 @@
534 450 $classes = isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ? ' frm_blank_field' : '';
535 451
536 452 // Add label position class
537 453 $settings = $this->field_obj->display_field_settings();
538 -
539 - if ( ! empty( $settings['label_position'] ) ) {
454 + if ( isset( $settings['label_position'] ) && $settings['label_position'] ) {
540 455 $label_position = $this->field_obj->get_field_column( 'label' );
541 456 $classes .= ' frm_' . $label_position . '_container';
542 457
543 458 // Add class if field has value, to be used for floating label styling.
@@ -547,14 +462,12 @@
547 462 }
548 463
549 464 // Add CSS layout classes
550 465 $extra_classes = $this->field_obj->get_field_column( 'classes' );
551 -
552 - if ( $extra_classes ) {
553 - if ( ! str_contains( $this->html, 'frm_form_field ' ) ) {
466 + if ( ! empty( $extra_classes ) ) {
467 + if ( ! strpos( $this->html, 'frm_form_field ' ) ) {
554 468 $classes .= ' frm_form_field';
555 469 }
556 -
557 470 $classes .= ' ' . $extra_classes;
558 471 }
559 472
560 473 $classes .= $this->field_obj->get_container_class();
@@ -562,9 +475,11 @@
562 475 // Get additional classes
563 476 $classes = apply_filters( 'frm_field_div_classes', $classes, $this->field_obj->get_field(), array( 'field_id' => $this->field_id ) );
564 477
565 478 // Remove unexpected characters from class.
566 - return implode( ' ', array_map( 'FrmFormsHelper::sanitize_layout_class', explode( ' ', $classes ) ) );
479 + $classes = implode( ' ', array_map( 'FrmFormsHelper::sanitize_layout_class', explode( ' ', $classes ) ) );
480 +
481 + return $classes;
567 482 }
568 483
569 484 /**
570 485 * This filters shortcodes in the field HTML
@@ -569,10 +484,8 @@
569 484 /**
570 485 * This filters shortcodes in the field HTML
571 486 *
572 487 * @since 3.0
573 - *
574 - * @return void
575 488 */
576 489 private function process_wp_shortcodes() {
577 490 if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
578 491 $this->html = do_shortcode( $this->html );
@@ -582,9 +495,8 @@
582 495 /**
583 496 * Adds multiple input attributes.
584 497 *
585 498 * @since 6.4.1
586 - *
587 499 * @return void
588 500 */
589 501 private function add_multiple_input_attributes() {
590 502 $field_type = $this->field_obj->get_field_column( 'type' );
@@ -610,13 +522,8 @@
610 522
611 523 // Add 'aria-required' attribute to the field if required.
612 524 if ( $is_radio && '1' === $field['required'] ) {
613 525 $attributes['aria-required'] = 'true';
614 - }
615 -
616 - // Add 'aria-invalid' attribute to the group if there are errors.
617 - if ( isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ) {
618 - $attributes['aria-invalid'] = 'true';
619 526 }
620 527
621 528 // Concatenate attributes into a string, and replace the role="group" in the HTML with the attributes string.
622 529 $html_attributes = FrmAppHelper::array_to_html_params( $attributes );