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 +49 -157 6.336.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 - // 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,51 +216,33 @@
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 ) {
296 - FrmShortcodeHelper::remove_inline_conditions( false, 'error', $error, $this->html );
297 - return;
298 - }
299 -
300 - $include_alert_role_on_field_errors = FrmAppHelper::should_include_alert_role_on_field_errors();
301 - $has_alert_role = str_contains( $this->html, 'role="alert"' );
302 -
303 - if ( ! $has_alert_role && $include_alert_role_on_field_errors ) {
236 + if ( ! empty( $error ) && false === strpos( $this->html, 'role="alert"' ) && FrmAppHelper::should_include_alert_role_on_field_errors() ) {
304 237 $error_body = self::get_error_body( $this->html );
305 -
306 - if ( is_string( $error_body ) && ! str_contains( $error_body, 'role=' ) ) {
238 + if ( is_string( $error_body ) && false === strpos( $error_body, 'role=' ) ) {
307 239 $new_error_body = preg_replace( '/class="frm_error/', 'role="alert" class="frm_error', $error_body, 1 );
308 240 $this->html = str_replace( '[if error]' . $error_body . '[/if error]', '[if error]' . $new_error_body . '[/if error]', $this->html );
309 241 }
310 - } elseif ( ! $include_alert_role_on_field_errors && $has_alert_role ) {
311 - $this->html = str_replace( 'role="alert"', '', $this->html );
312 242 }
313 243
314 - FrmShortcodeHelper::remove_inline_conditions( true, 'error', $error, $this->html );
244 + FrmShortcodeHelper::remove_inline_conditions( ! empty( $error ), 'error', $error, $this->html );
315 245 }
316 246
317 247 /**
318 248 * Pull the HTML between [if error] and [/if error] shortcodes.
@@ -317,25 +247,23 @@
317 247 /**
318 248 * Pull the HTML between [if error] and [/if error] shortcodes.
319 249 *
320 250 * @param string $html
321 - *
322 - * @return false|string
251 + * @return string|false
323 252 */
324 253 private static function get_error_body( $html ) {
325 254 $start = strpos( $html, '[if error]' );
326 -
327 255 if ( false === $start ) {
328 256 return false;
329 257 }
330 258
331 259 $end = strpos( $html, '[/if error]', $start );
332 -
333 260 if ( false === $end ) {
334 261 return false;
335 262 }
336 263
337 - return substr( $html, $start + 10, $end - $start - 10 );
264 + $error_body = substr( $html, $start + 10, $end - $start - 10 );
265 + return $error_body;
338 266 }
339 267
340 268 /**
341 269 * Add an ID to the error message for aria-describedby.
@@ -341,10 +269,8 @@
341 269 * Add an ID to the error message for aria-describedby.
342 270 * This ID was added to the HTML in v3.06.02.
343 271 *
344 272 * @since 3.06.02
345 - *
346 - * @return void
347 273 */
348 274 private function maybe_add_error_id() {
349 275 if ( ! isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ) {
350 276 return;
@@ -356,10 +282,8 @@
356 282 /**
357 283 * Replace [required_class]
358 284 *
359 285 * @since 3.0
360 - *
361 - * @return void
362 286 */
363 287 private function replace_required_class() {
364 288 $required_class = FrmField::is_required( $this->field_obj->get_field() ) ? ' frm_required_field' : '';
365 289 $this->html = str_replace( '[required_class]', $required_class, $this->html );
@@ -366,35 +290,29 @@
366 290 }
367 291
368 292 /**
369 293 * @since 3.0
370 - *
371 - * @return void
372 294 */
373 295 private function replace_form_shortcodes() {
374 - if ( ! $this->form ) {
375 - return;
376 - }
296 + if ( ! empty( $this->form ) ) {
297 + $form = (array) $this->form;
377 298
378 - $form = (array) $this->form;
299 + //replace [form_key]
300 + $this->html = str_replace( '[form_key]', $form['form_key'], $this->html );
379 301
380 - // Replace [form_key].
381 - $this->html = str_replace( '[form_key]', $form['form_key'], $this->html );
382 -
383 - // Replace [form_name].
384 - $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 + }
385 305 }
386 306
387 307 /**
388 308 * @since 3.0
389 - *
390 - * @return void
391 309 */
392 310 public function replace_shortcodes_after_input() {
393 311 $this->html .= "\n";
394 312
395 313 // Stop html filtering on confirmation field to prevent loop
396 - if ( $this->field_obj->get_field_column( 'conf_field' ) !== 'stop' ) {
314 + if ( $this->field_obj->get_field_column( 'conf_field' ) != 'stop' ) {
397 315 $this->filter_for_more_shortcodes();
398 316 }
399 317 }
400 318
@@ -399,15 +317,13 @@
399 317 }
400 318
401 319 /**
402 320 * @since 3.0
403 - *
404 - * @return void
405 321 */
406 322 private function filter_for_more_shortcodes() {
407 323 $atts = $this->pass_args;
408 324
409 - // If field is not in repeating section.
325 + //If field is not in repeating section
410 326 if ( empty( $atts['section_id'] ) ) {
411 327 $atts = array(
412 328 'errors' => $this->pass_args['errors'],
413 329 'form' => $this->form,
@@ -421,13 +337,11 @@
421 337 *
422 338 * @since 3.0
423 339 *
424 340 * @param string $html
425 - *
426 - * @return void
427 341 */
428 342 public function remove_collapse_shortcode( &$html ) {
429 - if ( str_contains( $html, '[collapse_this]' ) ) {
343 + if ( strpos( $html, '[collapse_this]' ) ) {
430 344 $html = str_replace( '[collapse_this]', '', $html );
431 345 }
432 346 }
433 347
@@ -432,10 +346,8 @@
432 346 }
433 347
434 348 /**
435 349 * @since 3.0
436 - *
437 - * @return void
438 350 */
439 351 private function replace_shortcodes_with_atts() {
440 352 preg_match_all( "/\[(input|deletelink)\b(.*?)(?:(\/))?\]/s", $this->html, $shortcodes, PREG_PATTERN_ORDER );
441 353
@@ -441,13 +353,14 @@
441 353
442 354 foreach ( $shortcodes[0] as $short_key => $tag ) {
443 355 $shortcode_atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[2][ $short_key ] );
444 356 $tag = FrmShortcodeHelper::get_shortcode_tag( $shortcodes, $short_key );
445 - $replace_with = '';
446 357
447 - if ( $tag === 'deletelink' && FrmAppHelper::pro_is_installed() ) {
358 + $replace_with = '';
359 +
360 + if ( $tag == 'deletelink' && FrmAppHelper::pro_is_installed() ) {
448 361 $replace_with = FrmProEntriesController::entry_delete_link( $shortcode_atts );
449 - } elseif ( $tag === 'input' ) {
362 + } elseif ( $tag == 'input' ) {
450 363 $replace_with = $this->replace_input_shortcode( $shortcode_atts );
451 364 }
452 365
453 366 $this->html = str_replace( $shortcodes[0][ $short_key ], $replace_with, $this->html );
@@ -460,8 +373,9 @@
460 373 * @return string
461 374 */
462 375 private function replace_input_shortcode( $shortcode_atts ) {
463 376 $shortcode_atts = $this->prepare_input_shortcode_atts( $shortcode_atts );
377 +
464 378 return $this->field_obj->include_front_field_input( $this->pass_args, $shortcode_atts );
465 379 }
466 380
467 381 /**
@@ -470,12 +384,12 @@
470 384 * @return array
471 385 */
472 386 private function prepare_input_shortcode_atts( $shortcode_atts ) {
473 387 if ( isset( $shortcode_atts['opt'] ) ) {
474 - --$shortcode_atts['opt'];
388 + $shortcode_atts['opt'] --;
475 389 }
476 390
477 - $field_class = $shortcode_atts['class'] ?? '';
391 + $field_class = isset( $shortcode_atts['class'] ) ? $shortcode_atts['class'] : '';
478 392 $this->field_obj->set_field_column( 'input_class', $field_class );
479 393
480 394 if ( isset( $shortcode_atts['class'] ) ) {
481 395 unset( $shortcode_atts['class'] );
@@ -480,9 +394,9 @@
480 394 if ( isset( $shortcode_atts['class'] ) ) {
481 395 unset( $shortcode_atts['class'] );
482 396 }
483 397
484 - $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';
485 399
486 400 $this->field_obj->set_field_column( 'shortcodes', $shortcode_atts );
487 401
488 402 return $shortcode_atts;
@@ -492,10 +406,8 @@
492 406 * Add the label position class into the HTML
493 407 * If the label position is inside, add a class to show the label if the field has a value.
494 408 *
495 409 * @since 3.0
496 - *
497 - * @return void
498 410 */
499 411 private function add_class_to_label() {
500 412 $label_class = $this->field_obj->get_label_class();
501 413 $this->html = str_replace( '[label_position]', $label_class, $this->html );
@@ -504,10 +416,8 @@
504 416 /**
505 417 * Replace [entry_key]
506 418 *
507 419 * @since 3.0
508 - *
509 - * @return void
510 420 */
511 421 private function replace_entry_key() {
512 422 $entry_key = FrmAppHelper::simple_get( 'entry', 'sanitize_title' );
513 423 $this->html = str_replace( '[entry_key]', $entry_key, $this->html );
@@ -516,21 +426,17 @@
516 426 /**
517 427 * Add classes to a field div
518 428 *
519 429 * @since 3.0
520 - *
521 - * @return void
522 430 */
523 431 private function add_field_div_classes() {
524 432 $classes = $this->get_field_div_classes();
525 433
526 - if ( in_array( $this->field_obj->get_field_column( 'type' ), array( 'html', 'summary' ), true ) && ! str_contains( $this->html, '[error_class]' ) ) {
527 - // There is no error_class shortcode for HTML fields
528 - $this->html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . esc_attr( $classes ), $this->html );
529 - 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 );
530 437 }
531 -
532 - $this->html = str_replace( '[error_class]', esc_attr( $classes ), $this->html );
438 + $this->html = str_replace( '[error_class]', $classes, $this->html );
533 439 }
534 440
535 441 /**
536 442 * Get the classes for a field div
@@ -536,9 +442,9 @@
536 442 * Get the classes for a field div
537 443 *
538 444 * @since 3.0
539 445 *
540 - * @return string Classes.
446 + * @return string $classes
541 447 */
542 448 private function get_field_div_classes() {
543 449 // Add error class
544 450 $classes = isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ? ' frm_blank_field' : '';
@@ -544,12 +450,11 @@
544 450 $classes = isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ? ' frm_blank_field' : '';
545 451
546 452 // Add label position class
547 453 $settings = $this->field_obj->display_field_settings();
548 -
549 - if ( ! empty( $settings['label_position'] ) ) {
454 + if ( isset( $settings['label_position'] ) && $settings['label_position'] ) {
550 455 $label_position = $this->field_obj->get_field_column( 'label' );
551 - $classes .= ' frm_' . $label_position . '_container';
456 + $classes .= ' frm_' . $label_position . '_container';
552 457
553 458 // Add class if field has value, to be used for floating label styling.
554 459 if ( 'inside' === $label_position && $this->field_obj->get_field_column( 'value' ) ) {
555 460 $classes .= ' frm_label_float_top';
@@ -557,14 +462,12 @@
557 462 }
558 463
559 464 // Add CSS layout classes
560 465 $extra_classes = $this->field_obj->get_field_column( 'classes' );
561 -
562 - if ( $extra_classes ) {
563 - if ( ! str_contains( $this->html, 'frm_form_field ' ) ) {
466 + if ( ! empty( $extra_classes ) ) {
467 + if ( ! strpos( $this->html, 'frm_form_field ' ) ) {
564 468 $classes .= ' frm_form_field';
565 469 }
566 -
567 470 $classes .= ' ' . $extra_classes;
568 471 }
569 472
570 473 $classes .= $this->field_obj->get_container_class();
@@ -569,12 +472,9 @@
569 472
570 473 $classes .= $this->field_obj->get_container_class();
571 474
572 475 // Get additional classes
573 - $classes = apply_filters( 'frm_field_div_classes', $classes, $this->field_obj->get_field(), array( 'field_id' => $this->field_id ) );
574 -
575 - // Remove unexpected characters from class.
576 - 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 ) );
577 477 }
578 478
579 479 /**
580 480 * This filters shortcodes in the field HTML
@@ -579,10 +479,8 @@
579 479 /**
580 480 * This filters shortcodes in the field HTML
581 481 *
582 482 * @since 3.0
583 - *
584 - * @return void
585 483 */
586 484 private function process_wp_shortcodes() {
587 485 if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
588 486 $this->html = do_shortcode( $this->html );
@@ -592,9 +490,8 @@
592 490 /**
593 491 * Adds multiple input attributes.
594 492 *
595 493 * @since 6.4.1
596 - *
597 494 * @return void
598 495 */
599 496 private function add_multiple_input_attributes() {
600 497 $field_type = $this->field_obj->get_field_column( 'type' );
@@ -620,13 +517,8 @@
620 517
621 518 // Add 'aria-required' attribute to the field if required.
622 519 if ( $is_radio && '1' === $field['required'] ) {
623 520 $attributes['aria-required'] = 'true';
624 - }
625 -
626 - // Add 'aria-invalid' attribute to the group if there are errors.
627 - if ( isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ) {
628 - $attributes['aria-invalid'] = 'true';
629 521 }
630 522
631 523 // Concatenate attributes into a string, and replace the role="group" in the HTML with the attributes string.
632 524 $html_attributes = FrmAppHelper::array_to_html_params( $attributes );