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