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