PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.6
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.6
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 +48 -146 6.306.6 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 - // Set the label for
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 - $should_replace = $is_html ? $wp_processed : ! $wp_processed;
216 -
177 + $should_replace = ( $is_html && $wp_processed ) || ( ! $is_html && ! $wp_processed );
217 178 if ( $should_replace ) {
218 179 $this->replace_description_shortcode();
219 180 }
220 181 }
@@ -220,16 +181,13 @@
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 - FrmShortcodeHelper::remove_inline_conditions( $description && $description != '', 'description', $description, $this->html );
189 + FrmShortcodeHelper::remove_inline_conditions( ( $description && $description != '' ), 'description', $description, $this->html );
232 190 }
233 191
234 192 /**
235 193 * Add an ID to the description for aria-describedby.
@@ -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
@@ -268,35 +216,27 @@
268 216 if ( ! is_string( $inner_html[2] ) && count( $inner_html[2] ) === 1 ) {
269 217 $inner_html[2] = $inner_html[2][0];
270 218 }
271 219
272 - if ( ! is_string( $inner_html[2] ) ) {
273 - return;
220 + if ( is_string( $inner_html[2] ) ) {
221 + $has_id = strpos( $inner_html[2], ' id=' );
222 + if ( ! $has_id ) {
223 + $id = 'frm_' . $id . '_' . $this->html_id;
224 + $this->html = str_replace( 'class="frm_' . $param, 'id="' . esc_attr( $id ) . '" class="frm_' . esc_attr( $param ), $this->html );
225 + }
274 226 }
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 227 }
285 228
286 229 /**
287 230 * @since 3.0
288 - *
289 - * @return void
290 231 */
291 232 private function replace_error_shortcode() {
292 233 $this->maybe_add_error_id();
293 - $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;
294 235
295 - 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() ) {
296 237 $error_body = self::get_error_body( $this->html );
297 -
298 - if ( is_string( $error_body ) && ! str_contains( $error_body, 'role=' ) ) {
238 + if ( is_string( $error_body ) && false === strpos( $error_body, 'role=' ) ) {
299 239 $new_error_body = preg_replace( '/class="frm_error/', 'role="alert" class="frm_error', $error_body, 1 );
300 240 $this->html = str_replace( '[if error]' . $error_body . '[/if error]', '[if error]' . $new_error_body . '[/if error]', $this->html );
301 241 }
302 242 }
@@ -307,25 +247,23 @@
307 247 /**
308 248 * Pull the HTML between [if error] and [/if error] shortcodes.
309 249 *
310 250 * @param string $html
311 - *
312 - * @return false|string
251 + * @return string|false
313 252 */
314 253 private static function get_error_body( $html ) {
315 254 $start = strpos( $html, '[if error]' );
316 -
317 255 if ( false === $start ) {
318 256 return false;
319 257 }
320 258
321 259 $end = strpos( $html, '[/if error]', $start );
322 -
323 260 if ( false === $end ) {
324 261 return false;
325 262 }
326 263
327 - return substr( $html, $start + 10, $end - $start - 10 );
264 + $error_body = substr( $html, $start + 10, $end - $start - 10 );
265 + return $error_body;
328 266 }
329 267
330 268 /**
331 269 * Add an ID to the error message for aria-describedby.
@@ -331,10 +269,8 @@
331 269 * Add an ID to the error message for aria-describedby.
332 270 * This ID was added to the HTML in v3.06.02.
333 271 *
334 272 * @since 3.06.02
335 - *
336 - * @return void
337 273 */
338 274 private function maybe_add_error_id() {
339 275 if ( ! isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ) {
340 276 return;
@@ -346,10 +282,8 @@
346 282 /**
347 283 * Replace [required_class]
348 284 *
349 285 * @since 3.0
350 - *
351 - * @return void
352 286 */
353 287 private function replace_required_class() {
354 288 $required_class = FrmField::is_required( $this->field_obj->get_field() ) ? ' frm_required_field' : '';
355 289 $this->html = str_replace( '[required_class]', $required_class, $this->html );
@@ -356,35 +290,29 @@
356 290 }
357 291
358 292 /**
359 293 * @since 3.0
360 - *
361 - * @return void
362 294 */
363 295 private function replace_form_shortcodes() {
364 - if ( ! $this->form ) {
365 - return;
366 - }
296 + if ( ! empty( $this->form ) ) {
297 + $form = (array) $this->form;
367 298
368 - $form = (array) $this->form;
299 + //replace [form_key]
300 + $this->html = str_replace( '[form_key]', $form['form_key'], $this->html );
369 301
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 );
302 + //replace [form_name]
303 + $this->html = str_replace( '[form_name]', $form['name'], $this->html );
304 + }
375 305 }
376 306
377 307 /**
378 308 * @since 3.0
379 - *
380 - * @return void
381 309 */
382 310 public function replace_shortcodes_after_input() {
383 311 $this->html .= "\n";
384 312
385 313 // Stop html filtering on confirmation field to prevent loop
386 - if ( $this->field_obj->get_field_column( 'conf_field' ) !== 'stop' ) {
314 + if ( $this->field_obj->get_field_column( 'conf_field' ) != 'stop' ) {
387 315 $this->filter_for_more_shortcodes();
388 316 }
389 317 }
390 318
@@ -389,15 +317,13 @@
389 317 }
390 318
391 319 /**
392 320 * @since 3.0
393 - *
394 - * @return void
395 321 */
396 322 private function filter_for_more_shortcodes() {
397 323 $atts = $this->pass_args;
398 324
399 - // If field is not in repeating section.
325 + //If field is not in repeating section
400 326 if ( empty( $atts['section_id'] ) ) {
401 327 $atts = array(
402 328 'errors' => $this->pass_args['errors'],
403 329 'form' => $this->form,
@@ -411,13 +337,11 @@
411 337 *
412 338 * @since 3.0
413 339 *
414 340 * @param string $html
415 - *
416 - * @return void
417 341 */
418 342 public function remove_collapse_shortcode( &$html ) {
419 - if ( str_contains( $html, '[collapse_this]' ) ) {
343 + if ( strpos( $html, '[collapse_this]' ) ) {
420 344 $html = str_replace( '[collapse_this]', '', $html );
421 345 }
422 346 }
423 347
@@ -422,10 +346,8 @@
422 346 }
423 347
424 348 /**
425 349 * @since 3.0
426 - *
427 - * @return void
428 350 */
429 351 private function replace_shortcodes_with_atts() {
430 352 preg_match_all( "/\[(input|deletelink)\b(.*?)(?:(\/))?\]/s", $this->html, $shortcodes, PREG_PATTERN_ORDER );
431 353
@@ -431,13 +353,14 @@
431 353
432 354 foreach ( $shortcodes[0] as $short_key => $tag ) {
433 355 $shortcode_atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[2][ $short_key ] );
434 356 $tag = FrmShortcodeHelper::get_shortcode_tag( $shortcodes, $short_key );
435 - $replace_with = '';
436 357
437 - if ( $tag === 'deletelink' && FrmAppHelper::pro_is_installed() ) {
358 + $replace_with = '';
359 +
360 + if ( $tag == 'deletelink' && FrmAppHelper::pro_is_installed() ) {
438 361 $replace_with = FrmProEntriesController::entry_delete_link( $shortcode_atts );
439 - } elseif ( $tag === 'input' ) {
362 + } elseif ( $tag == 'input' ) {
440 363 $replace_with = $this->replace_input_shortcode( $shortcode_atts );
441 364 }
442 365
443 366 $this->html = str_replace( $shortcodes[0][ $short_key ], $replace_with, $this->html );
@@ -450,8 +373,9 @@
450 373 * @return string
451 374 */
452 375 private function replace_input_shortcode( $shortcode_atts ) {
453 376 $shortcode_atts = $this->prepare_input_shortcode_atts( $shortcode_atts );
377 +
454 378 return $this->field_obj->include_front_field_input( $this->pass_args, $shortcode_atts );
455 379 }
456 380
457 381 /**
@@ -460,12 +384,12 @@
460 384 * @return array
461 385 */
462 386 private function prepare_input_shortcode_atts( $shortcode_atts ) {
463 387 if ( isset( $shortcode_atts['opt'] ) ) {
464 - --$shortcode_atts['opt'];
388 + $shortcode_atts['opt'] --;
465 389 }
466 390
467 - $field_class = $shortcode_atts['class'] ?? '';
391 + $field_class = isset( $shortcode_atts['class'] ) ? $shortcode_atts['class'] : '';
468 392 $this->field_obj->set_field_column( 'input_class', $field_class );
469 393
470 394 if ( isset( $shortcode_atts['class'] ) ) {
471 395 unset( $shortcode_atts['class'] );
@@ -470,9 +394,9 @@
470 394 if ( isset( $shortcode_atts['class'] ) ) {
471 395 unset( $shortcode_atts['class'] );
472 396 }
473 397
474 - $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';
475 399
476 400 $this->field_obj->set_field_column( 'shortcodes', $shortcode_atts );
477 401
478 402 return $shortcode_atts;
@@ -482,10 +406,8 @@
482 406 * Add the label position class into the HTML
483 407 * If the label position is inside, add a class to show the label if the field has a value.
484 408 *
485 409 * @since 3.0
486 - *
487 - * @return void
488 410 */
489 411 private function add_class_to_label() {
490 412 $label_class = $this->field_obj->get_label_class();
491 413 $this->html = str_replace( '[label_position]', $label_class, $this->html );
@@ -494,10 +416,8 @@
494 416 /**
495 417 * Replace [entry_key]
496 418 *
497 419 * @since 3.0
498 - *
499 - * @return void
500 420 */
501 421 private function replace_entry_key() {
502 422 $entry_key = FrmAppHelper::simple_get( 'entry', 'sanitize_title' );
503 423 $this->html = str_replace( '[entry_key]', $entry_key, $this->html );
@@ -506,21 +426,17 @@
506 426 /**
507 427 * Add classes to a field div
508 428 *
509 429 * @since 3.0
510 - *
511 - * @return void
512 430 */
513 431 private function add_field_div_classes() {
514 432 $classes = $this->get_field_div_classes();
515 433
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
518 - $this->html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . esc_attr( $classes ), $this->html );
519 - return;
434 + if ( $this->field_obj->get_field_column( 'type' ) == 'html' && strpos( $this->html, '[error_class]' ) === false ) {
435 + // there is no error_class shortcode for HTML fields
436 + $this->html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . $classes, $this->html );
520 437 }
521 -
522 - $this->html = str_replace( '[error_class]', esc_attr( $classes ), $this->html );
438 + $this->html = str_replace( '[error_class]', $classes, $this->html );
523 439 }
524 440
525 441 /**
526 442 * Get the classes for a field div
@@ -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,12 +450,11 @@
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 - $classes .= ' frm_' . $label_position . '_container';
456 + $classes .= ' frm_' . $label_position . '_container';
542 457
543 458 // Add class if field has value, to be used for floating label styling.
544 459 if ( 'inside' === $label_position && $this->field_obj->get_field_column( 'value' ) ) {
545 460 $classes .= ' frm_label_float_top';
@@ -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();
@@ -559,12 +472,9 @@
559 472
560 473 $classes .= $this->field_obj->get_container_class();
561 474
562 475 // Get additional classes
563 - $classes = apply_filters( 'frm_field_div_classes', $classes, $this->field_obj->get_field(), array( 'field_id' => $this->field_id ) );
564 -
565 - // Remove unexpected characters from class.
566 - 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 ) );
567 477 }
568 478
569 479 /**
570 480 * This filters shortcodes in the field HTML
@@ -569,10 +479,8 @@
569 479 /**
570 480 * This filters shortcodes in the field HTML
571 481 *
572 482 * @since 3.0
573 - *
574 - * @return void
575 483 */
576 484 private function process_wp_shortcodes() {
577 485 if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
578 486 $this->html = do_shortcode( $this->html );
@@ -582,9 +490,8 @@
582 490 /**
583 491 * Adds multiple input attributes.
584 492 *
585 493 * @since 6.4.1
586 - *
587 494 * @return void
588 495 */
589 496 private function add_multiple_input_attributes() {
590 497 $field_type = $this->field_obj->get_field_column( 'type' );
@@ -610,13 +517,8 @@
610 517
611 518 // Add 'aria-required' attribute to the field if required.
612 519 if ( $is_radio && '1' === $field['required'] ) {
613 520 $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 521 }
620 522
621 523 // Concatenate attributes into a string, and replace the role="group" in the HTML with the attributes string.
622 524 $html_attributes = FrmAppHelper::array_to_html_params( $attributes );