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/FrmStyle.php +142 -75 6.26 → trunk View file →
@@ -88,10 +88,9 @@
88 88 $all_instances = $this->get_all();
89 89 $css_scope_helper = new FrmCssScopeHelper();
90 90
91 91 if ( ! $id ) {
92 - $new_style = (array) $this->get_new();
93 - $all_instances[] = $new_style;
92 + $all_instances[] = (array) $this->get_new();
94 93 }
95 94
96 95 $action_ids = array();
97 96
@@ -98,9 +97,10 @@
98 97 foreach ( $all_instances as $new_instance ) {
99 98 $new_instance = (array) $new_instance;
100 99 $this->id = $new_instance['ID'];
101 100
102 - if ( $id != $this->id || ! $_POST || ! isset( $_POST['frm_style_setting'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
101 + // phpcs:ignore WordPress.Security.NonceVerification.Missing, Universal.Operators.StrictComparisons
102 + if ( $id != $this->id || ! $_POST || ! isset( $_POST['frm_style_setting'] ) ) {
103 103 // Don't continue if not saving this style.
104 104 continue;
105 105 }
106 106
@@ -114,24 +114,28 @@
114 114 // phpcs:ignore WordPress.Security.NonceVerification.Missing
115 115 $new_instance['post_title'] = sanitize_text_field( wp_unslash( $_POST['frm_style_setting']['post_title'] ) );
116 116 }
117 117
118 - $new_instance['post_content'] = isset( $_POST['frm_style_setting']['post_content'] ) ? $this->sanitize_post_content( wp_unslash( $_POST['frm_style_setting']['post_content'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
118 + $new_instance['post_content'] = isset( $_POST['frm_style_setting']['post_content'] ) ? $this->sanitize_post_content( wp_unslash( $_POST['frm_style_setting']['post_content'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing, SlevomatCodingStandard.Files.LineLength.LineTooLong
119 119 $new_instance['post_content']['custom_css'] = $custom_css;
120 120 unset( $custom_css );
121 121
122 - if ( ! empty( $new_instance['post_content']['single_style_custom_css'] ) ) {
123 - $css_scope = 'frm_style_' . $new_instance['post_name'];
124 - $new_instance['post_content']['single_style_custom_css'] = $css_scope_helper->nest( $new_instance['post_content']['single_style_custom_css'], $css_scope );
125 - }
126 -
127 122 $new_instance['post_type'] = FrmStylesController::$post_type;
128 123 $new_instance['post_status'] = 'publish';
129 124
130 125 if ( ! $id ) {
131 - $new_instance['post_name'] = $new_instance['post_title'];
126 + // For a new style (including a duplicate), the post_name is derived from the title.
127 + // Resolve slug uniqueness up front (WordPress appends -2, -3, etc. to duplicate slugs)
128 + // so the CSS scope below matches the slug WordPress will actually store.
129 + $slug = sanitize_title( $new_instance['post_title'] );
130 + $new_instance['post_name'] = wp_unique_post_slug( $slug, 0, $new_instance['post_status'], $new_instance['post_type'], 0 );
132 131 }
133 132
133 + if ( ! empty( $new_instance['post_content']['single_style_custom_css'] ) ) {
134 + $css_scope = 'frm_style_' . $new_instance['post_name'];
135 + $new_instance['post_content']['single_style_custom_css'] = $css_scope_helper->nest( $new_instance['post_content']['single_style_custom_css'], $css_scope );
136 + }
137 +
134 138 $default_settings = $this->get_defaults();
135 139
136 140 foreach ( $default_settings as $setting => $default ) {
137 141 if ( ! isset( $new_instance['post_content'][ $setting ] ) ) {
@@ -140,9 +144,9 @@
140 144
141 145 if ( $this->is_color( $setting ) ) {
142 146 $color_val = $new_instance['post_content'][ $setting ];
143 147
144 - if ( $color_val !== '' && false !== strpos( $color_val, 'rgb' ) ) {
148 + if ( $color_val !== '' && str_contains( $color_val, 'rgb' ) ) {
145 149 // Maybe sanitize if invalid rgba value is entered.
146 150 $this->maybe_sanitize_rgba_value( $color_val );
147 151 }
148 152 $new_instance['post_content'][ $setting ] = str_replace( '#', '', $color_val );
@@ -153,10 +157,9 @@
153 157 }
154 158 }
155 159
156 160 $new_instance['post_content'] = FrmStylesHelper::update_base_font_size( $new_instance['post_content'], $this->get_defaults() );
157 -
158 - $action_ids[] = $this->save( $new_instance );
161 + $action_ids[] = $this->save( $new_instance );
159 162 }//end foreach
160 163
161 164 $this->save_settings();
162 165
@@ -171,9 +174,9 @@
171 174 * @param string $color_val The color value, by reference.
172 175 *
173 176 * @return void
174 177 */
175 - private function maybe_sanitize_rgba_value( &$color_val ) {
178 + private function maybe_sanitize_rgba_value( &$color_val ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
176 179 if ( preg_match( '/(rgb|rgba)\(/', $color_val ) !== 1 ) {
177 180 return;
178 181 }
179 182
@@ -202,9 +205,9 @@
202 205 foreach ( explode( ',', $color_rgba ) as $index => $value ) {
203 206 $new_value = null;
204 207 $value_is_empty_string = '' === trim( $value ) || '' === $value;
205 208
206 - if ( 3 === $length_of_color_codes || ( $index !== $length_of_color_codes - 1 ) ) {
209 + if ( 3 === $length_of_color_codes || $index !== $length_of_color_codes - 1 ) {
207 210 // Insert a value for r, g, or b.
208 211 if ( $value < 0 ) {
209 212 $new_value = 0;
210 213 } elseif ( $value > 255 ) {
@@ -213,9 +216,9 @@
213 216 $new_value = 0;
214 217 } else {
215 218 $new_value = absint( $value );
216 219 }
217 - } else {
220 + } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
218 221 // Insert a value for alpha.
219 222 if ( $value_is_empty_string ) {
220 223 $new_value = 4 === $length_of_color_codes ? 1 : 0;
221 224 } elseif ( $value > 1 || $value < 0 ) {
@@ -247,9 +250,8 @@
247 250 $prefix = substr( $color_val, 0, strpos( $color_val, '(' ) + 1 );
248 251 // Limit the number of opening braces after rgb/rgba. There should only be one.
249 252 $prefix = rtrim( $prefix, '(' ) . '(';
250 253 $new_color = $prefix . $new_color . ')';
251 -
252 254 $color_val = $new_color;
253 255 }
254 256
255 257 /**
@@ -264,18 +266,15 @@
264 266 $valid_keys = array_keys( $defaults );
265 267 $sanitized_settings = array();
266 268
267 269 foreach ( $valid_keys as $key ) {
268 - if ( isset( $settings[ $key ] ) ) {
269 - $sanitized_settings[ $key ] = sanitize_textarea_field( $settings[ $key ] );
270 - } else {
271 - $sanitized_settings[ $key ] = $defaults[ $key ];
272 - }
270 + $sanitized_settings[ $key ] = isset( $settings[ $key ] ) ? sanitize_textarea_field( $settings[ $key ] ) : $defaults[ $key ];
273 271
274 272 if ( 'custom_css' !== $key && 'single_style_custom_css' !== $key ) {
275 273 $sanitized_settings[ $key ] = $this->strip_invalid_characters( $sanitized_settings[ $key ] );
276 274 }
277 275 }
276 +
278 277 return $sanitized_settings;
279 278 }
280 279
281 280 /**
@@ -290,9 +289,9 @@
290 289 private function strip_invalid_characters( $setting ) {
291 290 $characters_to_remove = array( '{', '}', ';', '[', ']' );
292 291
293 292 // RGB is handled instead in self::maybe_sanitize_rgba_value.
294 - if ( 0 !== strpos( $setting, 'rgb' ) ) {
293 + if ( ! str_starts_with( $setting, 'rgb' ) ) {
295 294 $setting = $this->maybe_fix_braces( $setting, $characters_to_remove );
296 295 }
297 296
298 297 return str_replace( $characters_to_remove, '', $setting );
@@ -341,12 +340,13 @@
341 340 // Remove extra braces from the end.
342 341 if ( in_array( substr( $output, -1 ), array( '(', ')' ), true ) ) {
343 342 $output = rtrim( $output, '()' );
344 343
345 - if ( false !== strpos( $output, '(' ) ) {
344 + if ( str_contains( $output, '(' ) ) {
346 345 $output .= ')';
347 346 }
348 347 }
348 +
349 349 return $output;
350 350 }
351 351
352 352 /**
@@ -356,9 +356,9 @@
356 356 *
357 357 * @return bool
358 358 */
359 359 private function should_remove_every_brace( $setting ) {
360 - if ( 0 === strpos( trim( $setting, '()' ), 'calc' ) ) {
360 + if ( str_starts_with( trim( $setting, '()' ), 'calc' ) ) {
361 361 // Support calc() sizes. We do not want to remove all braces when calc is used.
362 362 return false;
363 363 }
364 364
@@ -371,14 +371,9 @@
371 371
372 372 // Matches size values but also checks for unexpected ( and ).
373 373 // This is case insensitive so it will catch PX, PT, etc, as well.
374 374 $looks_like_a_size = preg_match( '/\(?[+-]?\d*\.?\d+(?:px|%|em|rem|ex|pt|pc|mm|cm|in)\)?/i', $setting );
375 -
376 - if ( $looks_like_a_size ) {
377 - return true;
378 - }
379 -
380 - return false;
375 + return (bool) $looks_like_a_size;
381 376 }
382 377
383 378 /**
384 379 * @since 3.01.01
@@ -387,10 +382,13 @@
387 382 *
388 383 * @return bool
389 384 */
390 385 private function is_color( $setting ) {
391 - $extra_colors = array( 'error_bg', 'error_border', 'error_text' );
392 - return strpos( $setting, 'color' ) !== false || in_array( $setting, $extra_colors, true );
386 + if ( str_contains( $setting, 'color' ) ) {
387 + return true;
388 + }
389 +
390 + return in_array( $setting, array( 'error_bg', 'error_border', 'error_text' ), true );
393 391 }
394 392
395 393 /**
396 394 * @since 3.01.01
@@ -397,11 +395,9 @@
397 395 *
398 396 * @return array
399 397 */
400 398 public function get_color_settings() {
401 - $defaults = $this->get_defaults();
402 - $settings = array_keys( $defaults );
403 -
399 + $settings = array_keys( $this->get_defaults() );
404 400 return array_filter( $settings, array( $this, 'is_color' ) );
405 401 }
406 402
407 403 /**
@@ -419,35 +415,95 @@
419 415
420 416 $this->clear_cache();
421 417
422 418 $css = $this->get_css_content( $filename );
423 - $create_file = new FrmCreateFile(
424 - array(
425 - 'file_name' => FrmStylesController::get_file_name(),
426 - 'new_file_path' => FrmAppHelper::plugin_path() . '/css',
427 - )
428 - );
419 + $create_file = new FrmCreateFile( self::get_create_style_file_args() );
429 420 $create_file->create_file( $css );
430 421
431 - update_option( 'frmpro_css', $css, 'no' );
422 + update_option( 'frmpro_css', $css, false );
432 423 set_transient( 'frmpro_css', $css, MONTH_IN_SECONDS );
433 424 }
434 425
435 426 /**
427 + * @since 6.32
428 + *
429 + * @return array
430 + */
431 + private static function get_create_style_file_args() {
432 + $add_css_to_uploads_dir = self::add_css_to_uploads_dir();
433 + $create_file_args = array(
434 + 'file_name' => FrmStylesController::get_file_name(),
435 + 'new_file_path' => self::get_generated_css_file_path( $add_css_to_uploads_dir ),
436 + );
437 +
438 + if ( $add_css_to_uploads_dir ) {
439 + $create_file_args['folder_name'] = 'formidable/css';
440 + }
441 +
442 + return $create_file_args;
443 + }
444 +
445 + /**
446 + * @since 6.32
447 + *
448 + * @param bool $add_css_to_uploads_dir
449 + *
450 + * @return string
451 + */
452 + public static function get_generated_css_file_path( $add_css_to_uploads_dir ) {
453 + if ( $add_css_to_uploads_dir ) {
454 + return self::target_css_uploads_dir();
455 + }
456 + return self::target_css_plugin_dir();
457 + }
458 +
459 + /**
460 + * Returns true if generated css file should be saved in the uploads directory.
461 + *
462 + * @since 6.32
463 + *
464 + * @return bool
465 + */
466 + public static function add_css_to_uploads_dir() {
467 + /**
468 + * @since 6.32
469 + *
470 + * @param bool $add_css_to_uploads_dir
471 + */
472 + return apply_filters( 'frm_add_css_to_uploads_dir', ! wp_is_file_mod_allowed( 'frm_save_css_to_plugin_folder' ) );
473 + }
474 +
475 + /**
476 + * @since 6.32
477 + *
478 + * @return string
479 + */
480 + private static function target_css_uploads_dir() {
481 + return wp_upload_dir()['basedir'] . '/formidable/css';
482 + }
483 +
484 + /**
485 + * @since 6.32
486 + *
487 + * @return string
488 + */
489 + private static function target_css_plugin_dir() {
490 + return FrmAppHelper::plugin_path() . '/css';
491 + }
492 +
493 + /**
436 494 * @param string $filename
437 495 *
438 496 * @return string
439 497 */
440 498 private function get_css_content( $filename ) {
441 - $css = '/* ' . __( 'WARNING: Any changes made to this file will be lost when your Formidable settings are updated', 'formidable' ) . ' */' . "\n";
442 -
499 + $css = '/* ' . __( 'WARNING: Any changes made to this file will be lost when your Formidable settings are updated', 'formidable' ) . ' */' . "\n";
443 500 $saving = true;
444 501 $frm_style = $this;
445 502
446 503 ob_start();
447 504 include $filename;
448 - $css .= preg_replace( '/\/\*(.|\s)*?\*\//', '', str_replace( array( "\r\n", "\r", "\n", "\t", ' ' ), '', ob_get_contents() ) );
449 - ob_end_clean();
505 + $css .= preg_replace( '/\/\*(.|\s)*?\*\//', '', str_replace( array( "\r\n", "\r", "\n", "\t", ' ' ), '', ob_get_clean() ) );
450 506
451 507 return FrmStylesController::replace_relative_url( $css );
452 508 }
453 509
@@ -488,13 +544,9 @@
488 544 public function get_one() {
489 545 if ( 'default' === $this->id ) {
490 546 $style = $this->get_default_style();
491 547
492 - if ( $style ) {
493 - $this->id = $style->ID;
494 - } else {
495 - $this->id = 0;
496 - }
548 + $this->id = $style ? $style->ID : 0;
497 549
498 550 return $style;
499 551 }
500 552
@@ -507,9 +559,9 @@
507 559 $style->post_content = FrmAppHelper::maybe_json_decode( $style->post_content );
508 560
509 561 $default_values = $this->get_defaults();
510 562
511 - // fill default values
563 + // Fill default values
512 564 $style->post_content = $this->override_defaults( $style->post_content );
513 565 $style->post_content = wp_parse_args( $style->post_content, $default_values );
514 566
515 567 return $style;
@@ -532,16 +584,16 @@
532 584 );
533 585
534 586 $temp_styles = FrmDb::check_cache( json_encode( $post_atts ), 'frm_styles', $post_atts, 'get_posts' );
535 587
536 - if ( empty( $temp_styles ) ) {
588 + if ( ! $temp_styles ) {
537 589 global $wpdb;
538 - // make sure there wasn't a conflict with the query
539 - $query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->posts . ' WHERE post_type=%s AND post_status=%s ORDER BY post_title ASC LIMIT 99', FrmStylesController::$post_type, 'publish' );
590 + // Make sure there wasn't a conflict with the query
591 + $query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->posts . ' WHERE post_type=%s AND post_status=%s ORDER BY post_title ASC LIMIT 99', FrmStylesController::$post_type, 'publish' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
540 592 $temp_styles = FrmDb::check_cache( 'frm_backup_style_check', 'frm_styles', $query, 'get_results' );
541 593
542 - if ( empty( $temp_styles ) ) {
543 - // create a new style if there are none
594 + if ( ! $temp_styles ) {
595 + // Create a new style if there are none
544 596 $new = $this->get_new();
545 597 $new->post_title = __( 'Formidable Style', 'formidable' );
546 598 $new->post_name = $new->post_title;
547 599 $new->menu_order = 1;
@@ -548,27 +600,25 @@
548 600 $new = $this->save( (array) $new );
549 601 $this->update( 'default' );
550 602
551 603 $post_atts['include'] = $new;
552 -
553 - $temp_styles = get_posts( $post_atts );
604 + $temp_styles = get_posts( $post_atts );
554 605 }
555 606 }
556 607
557 608 $default_values = $this->get_defaults();
558 609 $default_style = false;
610 + $styles = array();
559 611
560 - $styles = array();
561 -
562 612 foreach ( $temp_styles as $style ) {
563 613 $this->id = $style->ID;
564 614
565 615 if ( $style->menu_order ) {
566 616 if ( $default_style ) {
567 - // only return one default
617 + // Only return one default
568 618 $style->menu_order = 0;
569 619 } else {
570 - // check for a default style
620 + // Check for a default style
571 621 $default_style = $style->ID;
572 622 }
573 623 }
574 624
@@ -573,9 +623,9 @@
573 623 }
574 624
575 625 $style->post_content = FrmAppHelper::maybe_json_decode( $style->post_content );
576 626
577 - // fill default values
627 + // Fill default values
578 628 $style->post_content = $this->override_defaults( $style->post_content );
579 629 $style->post_content = wp_parse_args( $style->post_content, $default_values );
580 630
581 631 $styles[ $style->ID ] = $style;
@@ -581,10 +631,9 @@
581 631 $styles[ $style->ID ] = $style;
582 632 }//end foreach
583 633
584 634 if ( ! $default_style ) {
585 - $default_style = reset( $styles );
586 -
635 + $default_style = reset( $styles );
587 636 $styles[ $default_style->ID ]->menu_order = 1;
588 637 }
589 638
590 639 return $styles;
@@ -604,8 +653,10 @@
604 653 if ( $style->menu_order ) {
605 654 return $style;
606 655 }
607 656 }
657 +
658 + return null;
608 659 }
609 660
610 661 /**
611 662 * @param mixed $settings
@@ -616,9 +667,9 @@
616 667 if ( ! is_array( $settings ) ) {
617 668 return $settings;
618 669 }
619 670
620 - $settings['line_height'] = ! isset( $settings['field_height'] ) || $settings['field_height'] == '' || $settings['field_height'] === 'auto' ? 'normal' : $settings['field_height'];
671 + $settings['line_height'] = ! isset( $settings['field_height'] ) || $settings['field_height'] == '' || $settings['field_height'] === 'auto' ? 'normal' : $settings['field_height']; // phpcs:ignore Universal.Operators.StrictComparisons, SlevomatCodingStandard.Files.LineLength.LineTooLong
621 672
622 673 if ( ! isset( $settings['form_desc_size'] ) && isset( $settings['description_font_size'] ) ) {
623 674 $settings['form_desc_size'] = $settings['description_font_size'];
624 675 $settings['form_desc_color'] = $settings['description_color'];
@@ -630,14 +681,28 @@
630 681 $settings['section_border_color'] = $settings['border_color'];
631 682 }
632 683
633 684 if ( ! isset( $settings['submit_hover_bg_color'] ) && isset( $settings['submit_bg_color'] ) ) {
634 - $settings['submit_hover_bg_color'] = $settings['submit_bg_color'];
635 - $settings['submit_hover_color'] = $settings['submit_text_color'];
685 + $settings['submit_hover_bg_color'] = $settings['submit_bg_color'];
686 + }
687 +
688 + if ( ! isset( $settings['submit_hover_color'] ) && isset( $settings['submit_text_color'] ) ) {
689 + $settings['submit_hover_color'] = $settings['submit_text_color'];
690 + }
691 +
692 + if ( ! isset( $settings['submit_hover_border_color'] ) && isset( $settings['submit_border_color'] ) ) {
636 693 $settings['submit_hover_border_color'] = $settings['submit_border_color'];
694 + }
637 695
638 - $settings['submit_active_bg_color'] = $settings['submit_bg_color'];
639 - $settings['submit_active_color'] = $settings['submit_text_color'];
696 + if ( ! isset( $settings['submit_active_bg_color'] ) && isset( $settings['submit_bg_color'] ) ) {
697 + $settings['submit_active_bg_color'] = $settings['submit_bg_color'];
698 + }
699 +
700 + if ( ! isset( $settings['submit_active_color'] ) && isset( $settings['submit_text_color'] ) ) {
701 + $settings['submit_active_color'] = $settings['submit_text_color'];
702 + }
703 +
704 + if ( ! isset( $settings['submit_active_border_color'] ) && isset( $settings['submit_border_color'] ) ) {
640 705 $settings['submit_active_border_color'] = $settings['submit_border_color'];
641 706 }
642 707
643 708 return apply_filters( 'frm_override_default_styles', $settings );
@@ -800,9 +865,9 @@
800 865 *
801 866 * @return string
802 867 */
803 868 public function get_field_name( $field_name, $post_field = 'post_content' ) {
804 - return 'frm_style_setting' . ( empty( $post_field ) ? '' : '[' . $post_field . ']' ) . '[' . $field_name . ']';
869 + return 'frm_style_setting' . ( $post_field ? '[' . $post_field . ']' : '' ) . '[' . $field_name . ']';
805 870 }
806 871
807 872 /**
808 873 * @return array
@@ -832,9 +897,9 @@
832 897 $balanced_characters = array( '"', "'" );
833 898
834 899 foreach ( $balanced_characters as $char ) {
835 900 $char_count = substr_count( $value, $char );
836 - $is_balanced = $char_count % 2 == 0;
901 + $is_balanced = $char_count % 2 === 0;
837 902
838 903 if ( $is_balanced ) {
839 904 continue;
840 905 }
@@ -844,8 +909,9 @@
844 909 } else {
845 910 $value .= $char;
846 911 }
847 912 }
913 +
848 914 return $value;
849 915 }
850 916
851 917 /**
@@ -859,10 +925,11 @@
859 925 */
860 926 public function get_default_template_style( $style_id ) {
861 927 $default_template = get_post_meta( (int) $style_id, $this->default_template_style_meta_name, true );
862 928
863 - if ( empty( $default_template ) ) {
929 + if ( ! $default_template ) {
864 930 return FrmAppHelper::prepare_and_encode( $this->get_defaults() );
865 931 }
932 +
866 933 return $default_template;
867 934 }
868 935 }