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