← All changes
|
modules/atomic-widgets/styles/atomic-widget-styles.php
+14
-58
4.1.0
→
3.32.2
View file →
| @@ -2,17 +2,15 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\AtomicWidgets\Styles; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Core\Base\Document; |
| 6 | -use Elementor\Modules\AtomicWidgets\Utils\Utils; | |
| 6 | +use Elementor\Modules\AtomicWidgets\Cache_Validity; | |
| 7 | +use Elementor\Modules\AtomicWidgets\Utils; | |
| 7 | 8 | use Elementor\Modules\GlobalClasses\Utils\Atomic_Elements_Utils; |
| 8 | -use Elementor\Utils as ElementorUtils; | |
| 9 | 9 | use Elementor\Plugin; |
| 10 | 10 | |
| 11 | 11 | class Atomic_Widget_Styles { |
| 12 | 12 | const STYLES_KEY = 'local'; |
| 13 | - const CONTEXT_FRONTEND = 'frontend'; | |
| 14 | - const CONTEXT_PREVIEW = 'preview'; | |
| 15 | 13 | |
| 16 | 14 | public function register_hooks() { |
| 17 | 15 | add_action( 'elementor/atomic-widgets/styles/register', function( Atomic_Styles_Manager $styles_manager, array $post_ids ) { |
| 18 | 16 | $this->register_styles( $styles_manager, $post_ids ); |
| @@ -18,10 +16,9 @@ | ||
| 18 | 16 | $this->register_styles( $styles_manager, $post_ids ); |
| 19 | 17 | }, 30, 2 ); |
| 20 | 18 | |
| 21 | 19 | add_action( 'elementor/document/after_save', fn( Document $document ) => $this->invalidate_cache( |
| 22 | - [ $document->get_main_post()->ID ], | |
| 23 | - $this->get_context( ! Utils::is_post_published( $document ) ) | |
| 20 | + [ $document->get_main_post()->ID ] | |
| 24 | 21 | ), 20, 2 ); |
| 25 | 22 | |
| 26 | 23 | add_action( |
| 27 | 24 | 'elementor/core/files/clear_cache', |
| @@ -26,25 +23,17 @@ | ||
| 26 | 23 | add_action( |
| 27 | 24 | 'elementor/core/files/clear_cache', |
| 28 | 25 | fn() => $this->invalidate_cache(), |
| 29 | 26 | ); |
| 30 | - | |
| 31 | - add_action( | |
| 32 | - 'deleted_post', | |
| 33 | - fn( $post_id ) => $this->invalidate_cache( [ $post_id ] ) | |
| 34 | - ); | |
| 35 | - | |
| 36 | - add_action( 'update_option__elementor_pro_license_v2_data', fn() => Plugin::$instance->files_manager->clear_cache() ); | |
| 37 | - add_action( 'delete_option__elementor_pro_license_v2_data', fn() => Plugin::$instance->files_manager->clear_cache() ); | |
| 38 | 27 | } |
| 39 | 28 | |
| 40 | 29 | private function register_styles( Atomic_Styles_Manager $styles_manager, array $post_ids ) { |
| 41 | - $context = $this->get_context( Plugin::$instance->preview->is_editor_or_preview() ); | |
| 42 | - | |
| 43 | 30 | foreach ( $post_ids as $post_id ) { |
| 44 | 31 | $get_styles = fn() => $this->parse_post_styles( $post_id ); |
| 45 | 32 | |
| 46 | - $styles_manager->register( [ self::STYLES_KEY, $post_id, $context ], $get_styles ); | |
| 33 | + $style_key = $this->get_style_key( $post_id ); | |
| 34 | + | |
| 35 | + $styles_manager->register( $style_key, $get_styles, [ self::STYLES_KEY, $post_id ] ); | |
| 47 | 36 | } |
| 48 | 37 | } |
| 49 | 38 | |
| 50 | 39 | private function parse_post_styles( $post_id ) { |
| @@ -53,9 +42,9 @@ | ||
| 53 | 42 | Utils::traverse_post_elements( $post_id, function( $element_data ) use ( &$post_styles ) { |
| 54 | 43 | $post_styles = array_merge( $post_styles, $this->parse_element_style( $element_data ) ); |
| 55 | 44 | } ); |
| 56 | 45 | |
| 57 | - return self::get_license_based_filtered_styles( $post_styles ); | |
| 46 | + return $post_styles; | |
| 58 | 47 | } |
| 59 | 48 | |
| 60 | 49 | private function parse_element_style( array $element_data ) { |
| 61 | 50 | $element_type = Atomic_Elements_Utils::get_element_type( $element_data ); |
| @@ -67,55 +56,22 @@ | ||
| 67 | 56 | |
| 68 | 57 | return $element_data['styles'] ?? []; |
| 69 | 58 | } |
| 70 | 59 | |
| 71 | - private function invalidate_cache( ?array $post_ids = null, ?string $context = null ) { | |
| 60 | + private function invalidate_cache( ?array $post_ids = null ) { | |
| 61 | + $cache_validity = new Cache_Validity(); | |
| 62 | + | |
| 72 | 63 | if ( empty( $post_ids ) ) { |
| 73 | - do_action( 'elementor/atomic-widgets/styles/clear', [ self::STYLES_KEY ] ); | |
| 64 | + $cache_validity->invalidate( [ self::STYLES_KEY ] ); | |
| 74 | 65 | |
| 75 | 66 | return; |
| 76 | 67 | } |
| 77 | 68 | |
| 78 | - $is_post_status_publish = self::CONTEXT_FRONTEND === $context; | |
| 79 | - | |
| 80 | - // When a user publishes a post, we should invalidate the styles of the draft too | |
| 81 | 69 | foreach ( $post_ids as $post_id ) { |
| 82 | - do_action( 'elementor/atomic-widgets/styles/clear', | |
| 83 | - empty( $context ) || $is_post_status_publish | |
| 84 | - ? [ self::STYLES_KEY, $post_id ] | |
| 85 | - : [ self::STYLES_KEY, $post_id, $context ] | |
| 86 | - ); | |
| 70 | + $cache_validity->invalidate( [ self::STYLES_KEY, $post_id ] ); | |
| 87 | 71 | } |
| 88 | 72 | } |
| 89 | 73 | |
| 90 | - private function get_context( bool $is_preview ) { | |
| 91 | - return $is_preview ? self::CONTEXT_PREVIEW : self::CONTEXT_FRONTEND; | |
| 92 | - } | |
| 93 | - | |
| 94 | - public static function get_license_based_filtered_styles( $styles ) { | |
| 95 | - if ( ElementorUtils::has_pro() && version_compare( ELEMENTOR_PRO_VERSION, '3.35', '<' ) ) { | |
| 96 | - return $styles; | |
| 97 | - } | |
| 98 | - | |
| 99 | - return apply_filters( | |
| 100 | - 'elementor/atomic_widgets/editor_data/element_styles', | |
| 101 | - self::remove_custom_css_from_styles( $styles ), | |
| 102 | - $styles | |
| 103 | - ); | |
| 104 | - } | |
| 105 | - | |
| 106 | - public static function remove_custom_css_from_styles( array $styles ) { | |
| 107 | - if ( empty( $styles ) ) { | |
| 108 | - return $styles; | |
| 109 | - } | |
| 110 | - | |
| 111 | - foreach ( $styles as $style_id => $style ) { | |
| 112 | - if ( isset( $style['variants'] ) && is_array( $style['variants'] ) ) { | |
| 113 | - foreach ( $style['variants'] as $variant_index => $variant ) { | |
| 114 | - unset( $styles[ $style_id ]['variants'][ $variant_index ]['custom_css'] ); | |
| 115 | - } | |
| 116 | - } | |
| 117 | - } | |
| 118 | - | |
| 119 | - return $styles; | |
| 74 | + private function get_style_key( $post_id ) { | |
| 75 | + return self::STYLES_KEY . '-' . $post_id; | |
| 120 | 76 | } |
| 121 | 77 | } |