| @@ -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 | |
| @@ -119,20 +118,24 @@ | ||
| 119 | 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 |
| 120 | 119 | $new_instance['post_content']['custom_css'] = $custom_css; |
| 121 | 120 | unset( $custom_css ); |
| 122 | 121 | |
| 123 | - if ( ! empty( $new_instance['post_content']['single_style_custom_css'] ) ) { | |
| 124 | - $css_scope = 'frm_style_' . $new_instance['post_name']; | |
| 125 | - $new_instance['post_content']['single_style_custom_css'] = $css_scope_helper->nest( $new_instance['post_content']['single_style_custom_css'], $css_scope ); | |
| 126 | - } | |
| 127 | - | |
| 128 | 122 | $new_instance['post_type'] = FrmStylesController::$post_type; |
| 129 | 123 | $new_instance['post_status'] = 'publish'; |
| 130 | 124 | |
| 131 | 125 | if ( ! $id ) { |
| 132 | - $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 ); | |
| 133 | 131 | } |
| 134 | 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 | + | |
| 135 | 138 | $default_settings = $this->get_defaults(); |
| 136 | 139 | |
| 137 | 140 | foreach ( $default_settings as $setting => $default ) { |
| 138 | 141 | if ( ! isset( $new_instance['post_content'][ $setting ] ) ) { |
| @@ -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 ) { |
| @@ -379,10 +382,13 @@ | ||
| 379 | 382 | * |
| 380 | 383 | * @return bool |
| 381 | 384 | */ |
| 382 | 385 | private function is_color( $setting ) { |
| 383 | - $extra_colors = array( 'error_bg', 'error_border', 'error_text' ); | |
| 384 | - return str_contains( $setting, 'color' ) || 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 ); | |
| 385 | 391 | } |
| 386 | 392 | |
| 387 | 393 | /** |
| 388 | 394 | * @since 3.01.01 |
| @@ -389,11 +395,9 @@ | ||
| 389 | 395 | * |
| 390 | 396 | * @return array |
| 391 | 397 | */ |
| 392 | 398 | public function get_color_settings() { |
| 393 | - $defaults = $this->get_defaults(); | |
| 394 | - $settings = array_keys( $defaults ); | |
| 395 | - | |
| 399 | + $settings = array_keys( $this->get_defaults() ); | |
| 396 | 400 | return array_filter( $settings, array( $this, 'is_color' ) ); |
| 397 | 401 | } |
| 398 | 402 | |
| 399 | 403 | /** |
| @@ -411,21 +415,83 @@ | ||
| 411 | 415 | |
| 412 | 416 | $this->clear_cache(); |
| 413 | 417 | |
| 414 | 418 | $css = $this->get_css_content( $filename ); |
| 415 | - $create_file = new FrmCreateFile( | |
| 416 | - array( | |
| 417 | - 'file_name' => FrmStylesController::get_file_name(), | |
| 418 | - 'new_file_path' => FrmAppHelper::plugin_path() . '/css', | |
| 419 | - ) | |
| 420 | - ); | |
| 419 | + $create_file = new FrmCreateFile( self::get_create_style_file_args() ); | |
| 421 | 420 | $create_file->create_file( $css ); |
| 422 | 421 | |
| 423 | - update_option( 'frmpro_css', $css, 'no' ); | |
| 422 | + update_option( 'frmpro_css', $css, false ); | |
| 424 | 423 | set_transient( 'frmpro_css', $css, MONTH_IN_SECONDS ); |
| 425 | 424 | } |
| 426 | 425 | |
| 427 | 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 | + /** | |
| 428 | 494 | * @param string $filename |
| 429 | 495 | * |
| 430 | 496 | * @return string |
| 431 | 497 | */ |
| @@ -493,9 +559,9 @@ | ||
| 493 | 559 | $style->post_content = FrmAppHelper::maybe_json_decode( $style->post_content ); |
| 494 | 560 | |
| 495 | 561 | $default_values = $this->get_defaults(); |
| 496 | 562 | |
| 497 | - // fill default values | |
| 563 | + // Fill default values | |
| 498 | 564 | $style->post_content = $this->override_defaults( $style->post_content ); |
| 499 | 565 | $style->post_content = wp_parse_args( $style->post_content, $default_values ); |
| 500 | 566 | |
| 501 | 567 | return $style; |
| @@ -520,14 +586,14 @@ | ||
| 520 | 586 | $temp_styles = FrmDb::check_cache( json_encode( $post_atts ), 'frm_styles', $post_atts, 'get_posts' ); |
| 521 | 587 | |
| 522 | 588 | if ( ! $temp_styles ) { |
| 523 | 589 | global $wpdb; |
| 524 | - // make sure there wasn't a conflict with the query | |
| 590 | + // Make sure there wasn't a conflict with the query | |
| 525 | 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 |
| 526 | 592 | $temp_styles = FrmDb::check_cache( 'frm_backup_style_check', 'frm_styles', $query, 'get_results' ); |
| 527 | 593 | |
| 528 | 594 | if ( ! $temp_styles ) { |
| 529 | - // create a new style if there are none | |
| 595 | + // Create a new style if there are none | |
| 530 | 596 | $new = $this->get_new(); |
| 531 | 597 | $new->post_title = __( 'Formidable Style', 'formidable' ); |
| 532 | 598 | $new->post_name = $new->post_title; |
| 533 | 599 | $new->menu_order = 1; |
| @@ -547,12 +613,12 @@ | ||
| 547 | 613 | $this->id = $style->ID; |
| 548 | 614 | |
| 549 | 615 | if ( $style->menu_order ) { |
| 550 | 616 | if ( $default_style ) { |
| 551 | - // only return one default | |
| 617 | + // Only return one default | |
| 552 | 618 | $style->menu_order = 0; |
| 553 | 619 | } else { |
| 554 | - // check for a default style | |
| 620 | + // Check for a default style | |
| 555 | 621 | $default_style = $style->ID; |
| 556 | 622 | } |
| 557 | 623 | } |
| 558 | 624 | |
| @@ -557,9 +623,9 @@ | ||
| 557 | 623 | } |
| 558 | 624 | |
| 559 | 625 | $style->post_content = FrmAppHelper::maybe_json_decode( $style->post_content ); |
| 560 | 626 | |
| 561 | - // fill default values | |
| 627 | + // Fill default values | |
| 562 | 628 | $style->post_content = $this->override_defaults( $style->post_content ); |
| 563 | 629 | $style->post_content = wp_parse_args( $style->post_content, $default_values ); |
| 564 | 630 | |
| 565 | 631 | $styles[ $style->ID ] = $style; |
| @@ -615,14 +681,28 @@ | ||
| 615 | 681 | $settings['section_border_color'] = $settings['border_color']; |
| 616 | 682 | } |
| 617 | 683 | |
| 618 | 684 | if ( ! isset( $settings['submit_hover_bg_color'] ) && isset( $settings['submit_bg_color'] ) ) { |
| 619 | - $settings['submit_hover_bg_color'] = $settings['submit_bg_color']; | |
| 620 | - $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'] ) ) { | |
| 621 | 693 | $settings['submit_hover_border_color'] = $settings['submit_border_color']; |
| 694 | + } | |
| 622 | 695 | |
| 623 | - $settings['submit_active_bg_color'] = $settings['submit_bg_color']; | |
| 624 | - $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'] ) ) { | |
| 625 | 705 | $settings['submit_active_border_color'] = $settings['submit_border_color']; |
| 626 | 706 | } |
| 627 | 707 | |
| 628 | 708 | return apply_filters( 'frm_override_default_styles', $settings ); |
| @@ -785,9 +865,9 @@ | ||
| 785 | 865 | * |
| 786 | 866 | * @return string |
| 787 | 867 | */ |
| 788 | 868 | public function get_field_name( $field_name, $post_field = 'post_content' ) { |
| 789 | - return 'frm_style_setting' . ( empty( $post_field ) ? '' : '[' . $post_field . ']' ) . '[' . $field_name . ']'; | |
| 869 | + return 'frm_style_setting' . ( $post_field ? '[' . $post_field . ']' : '' ) . '[' . $field_name . ']'; | |
| 790 | 870 | } |
| 791 | 871 | |
| 792 | 872 | /** |
| 793 | 873 | * @return array |