← All changes
|
modules/atomic-widgets/styles/atomic-styles-manager.php
+23
-56
4.2.1
→
3.32.0-dev2
View file →
| @@ -4,10 +4,10 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Elementor\Core\Base\Document; |
| 6 | 6 | use Elementor\Core\Breakpoints\Breakpoint; |
| 7 | 7 | use Elementor\Core\Utils\Collection; |
| 8 | -use Elementor\Modules\AtomicWidgets\Utils\Memo; | |
| 9 | -use Elementor\Modules\AtomicWidgets\Styles\CacheValidity\Cache_Validity; | |
| 8 | +use Elementor\Modules\AtomicWidgets\Memo; | |
| 9 | +use Elementor\Modules\AtomicWidgets\Cache_Validity; | |
| 10 | 10 | use Elementor\Plugin; |
| 11 | 11 | |
| 12 | 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | 13 | exit; // Exit if accessed directly. |
| @@ -16,9 +16,9 @@ | ||
| 16 | 16 | class Atomic_Styles_Manager { |
| 17 | 17 | private static ?self $instance = null; |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * @var array<string, array{styles: callable, path: array<string>}> | |
| 20 | + * @var array<string, array{styles: callable, cache_keys: array<string>}> | |
| 21 | 21 | */ |
| 22 | 22 | private array $registered_styles_by_key = []; |
| 23 | 23 | |
| 24 | 24 | private Cache_Validity $cache_validity; |
| @@ -45,22 +45,17 @@ | ||
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | public function register_hooks() { |
| 48 | 48 | add_action( 'elementor/frontend/after_enqueue_post_styles', fn() => $this->enqueue_styles() ); |
| 49 | - | |
| 50 | 49 | add_action( 'elementor/post/render', function( $post_id ) { |
| 51 | 50 | $this->post_ids[] = $post_id; |
| 52 | 51 | } ); |
| 53 | - | |
| 54 | - add_action( 'elementor/atomic-widgets/styles/clear', fn( array $path ) => $this->clear_styles( $path ) ); | |
| 55 | 52 | } |
| 56 | 53 | |
| 57 | - public function register( array $path, callable $get_style_defs ) { | |
| 58 | - $key = $this->convert_path_to_handle( $path ); | |
| 59 | - | |
| 54 | + public function register( string $key, callable $get_style_defs, array $cache_keys ) { | |
| 60 | 55 | $this->registered_styles_by_key[ $key ] = [ |
| 61 | 56 | 'get_styles' => $get_style_defs, |
| 62 | - 'path' => $path, | |
| 57 | + 'cache_keys' => $cache_keys, | |
| 63 | 58 | ]; |
| 64 | 59 | } |
| 65 | 60 | |
| 66 | 61 | private function enqueue_styles() { |
| @@ -75,9 +70,9 @@ | ||
| 75 | 70 | $styles_by_key = Collection::make( $this->registered_styles_by_key ) |
| 76 | 71 | ->map_with_keys( fn ( $style_params, $style_key ) => [ |
| 77 | 72 | $style_key => [ |
| 78 | 73 | 'get_styles' => $get_styles_memo->memoize( $style_key, $style_params['get_styles'] ), |
| 79 | - 'path' => $style_params['path'], | |
| 74 | + 'cache_keys' => $style_params['cache_keys'], | |
| 80 | 75 | ], |
| 81 | 76 | ]) |
| 82 | 77 | ->all(); |
| 83 | 78 | |
| @@ -91,19 +86,19 @@ | ||
| 91 | 86 | private function before_render( array $styles_by_key ) { |
| 92 | 87 | $this->fonts = []; |
| 93 | 88 | |
| 94 | 89 | foreach ( $styles_by_key as $style_key => $style_params ) { |
| 95 | - $path = $style_params['path']; | |
| 90 | + $cache_keys = $style_params['cache_keys']; | |
| 96 | 91 | |
| 97 | 92 | // This cache validity check is of the general style, and used to reset dependencies that can only be evaluated |
| 98 | 93 | // upon the style rendering flow (i.e. when cache is invalid). |
| 99 | 94 | // (the corresponding css files cache validity includes also the file's breakpoint in the cache keys array) |
| 100 | - if ( ! $this->cache_validity->is_valid( $path ) ) { | |
| 95 | + if ( ! $this->cache_validity->is_valid( $cache_keys ) ) { | |
| 101 | 96 | Style_Fonts::make( $style_key )->clear(); |
| 97 | + } | |
| 102 | 98 | |
| 103 | - // We should validate it after this iteration | |
| 104 | - $this->cache_validity->validate( $path, uniqid() ); | |
| 105 | - } | |
| 99 | + // We should validate it after this iteration | |
| 100 | + $this->cache_validity->validate( $cache_keys ); | |
| 106 | 101 | } |
| 107 | 102 | } |
| 108 | 103 | |
| 109 | 104 | private function render( array $styles_by_key ) { |
| @@ -111,13 +106,11 @@ | ||
| 111 | 106 | $breakpoints = $this->get_breakpoints(); |
| 112 | 107 | |
| 113 | 108 | foreach ( $breakpoints as $breakpoint_key ) { |
| 114 | 109 | foreach ( $styles_by_key as $style_key => $style_params ) { |
| 115 | - $path = $style_params['path']; | |
| 110 | + $cache_keys = $style_params['cache_keys']; | |
| 116 | 111 | $render_css = fn() => $this->render_css_by_breakpoints( $style_params['get_styles'], $style_key, $breakpoint_key, $group_by_breakpoint_memo ); |
| 117 | 112 | |
| 118 | - $version = $this->cache_validity->get_meta( $path ); | |
| 119 | - | |
| 120 | 113 | $breakpoint_media = $this->get_breakpoint_media( $breakpoint_key ); |
| 121 | 114 | |
| 122 | 115 | if ( ! $breakpoint_media ) { |
| 123 | 116 | continue; |
| @@ -122,18 +115,18 @@ | ||
| 122 | 115 | if ( ! $breakpoint_media ) { |
| 123 | 116 | continue; |
| 124 | 117 | } |
| 125 | 118 | |
| 126 | - $breakpoint_path = array_merge( $path, [ $breakpoint_key ] ); | |
| 119 | + $breakpoint_cache_keys = array_merge( $cache_keys, [ $breakpoint_key ] ); | |
| 127 | 120 | |
| 128 | 121 | $style_file = $this->css_files_manager->get( |
| 129 | - $this->convert_path_to_handle( $breakpoint_path ), | |
| 122 | + $style_key . '-' . $breakpoint_key, | |
| 130 | 123 | $breakpoint_media, |
| 131 | 124 | $render_css, |
| 132 | - $this->cache_validity->is_valid( $breakpoint_path ) | |
| 125 | + $this->cache_validity->is_valid( $breakpoint_cache_keys ) | |
| 133 | 126 | ); |
| 134 | 127 | |
| 135 | - $this->cache_validity->validate( $breakpoint_path ); | |
| 128 | + $this->cache_validity->validate( $breakpoint_cache_keys ); | |
| 136 | 129 | |
| 137 | 130 | if ( ! $style_file ) { |
| 138 | 131 | continue; |
| 139 | 132 | } |
| @@ -141,9 +134,8 @@ | ||
| 141 | 134 | wp_enqueue_style( |
| 142 | 135 | $style_file->get_handle(), |
| 143 | 136 | $style_file->get_url(), |
| 144 | 137 | [], |
| 145 | - $version, | |
| 146 | 138 | $style_file->get_media() |
| 147 | 139 | ); |
| 148 | 140 | } |
| 149 | 141 | } |
| @@ -153,10 +145,15 @@ | ||
| 153 | 145 | $style_fonts = Style_Fonts::make( $style_key ); |
| 154 | 146 | |
| 155 | 147 | return Styles_Renderer::make( |
| 156 | 148 | Plugin::$instance->breakpoints->get_breakpoints_config() |
| 157 | - )->on_font_enqueue( fn( $font ) => $style_fonts->add( $font ) ) | |
| 158 | - ->render( $styles ); | |
| 149 | + )->on_prop_transform( function( $key, $value ) use ( $style_fonts ) { | |
| 150 | + if ( 'font-family' !== $key ) { | |
| 151 | + return; | |
| 152 | + } | |
| 153 | + | |
| 154 | + $style_fonts->add( $value ); | |
| 155 | + } )->render( $styles ); | |
| 159 | 156 | } |
| 160 | 157 | |
| 161 | 158 | private function get_breakpoint_media( string $breakpoint_key ): ?string { |
| 162 | 159 | $breakpoint_config = Plugin::$instance->breakpoints->get_breakpoints_config()[ $breakpoint_key ] ?? null; |
| @@ -222,36 +219,6 @@ | ||
| 222 | 219 | private function enqueue_fonts() { |
| 223 | 220 | foreach ( $this->fonts as $font ) { |
| 224 | 221 | Plugin::instance()->frontend->enqueue_font( $font ); |
| 225 | 222 | } |
| 226 | - } | |
| 227 | - | |
| 228 | - private function clear_styles( array $path ) { | |
| 229 | - $node = $this->cache_validity->get_node( $path ); | |
| 230 | - | |
| 231 | - $this->clear_styles_by_node( $path, $node ); | |
| 232 | - | |
| 233 | - $this->cache_validity->invalidate( $path ); | |
| 234 | - } | |
| 235 | - | |
| 236 | - private function clear_styles_by_node( array $path, $node ) { | |
| 237 | - if ( ! $node ) { | |
| 238 | - return; | |
| 239 | - } | |
| 240 | - | |
| 241 | - if ( true === $node || $node['state'] ) { | |
| 242 | - $this->css_files_manager->delete( $this->convert_path_to_handle( $path ) ); | |
| 243 | - } | |
| 244 | - | |
| 245 | - if ( is_bool( $node ) || empty( $node['children'] ) ) { | |
| 246 | - return; | |
| 247 | - } | |
| 248 | - | |
| 249 | - foreach ( $node['children'] as $child_key => $child_node ) { | |
| 250 | - $this->clear_styles_by_node( array_merge( $path, [ $child_key ] ), $child_node ); | |
| 251 | - } | |
| 252 | - } | |
| 253 | - | |
| 254 | - private function convert_path_to_handle( array $path ) { | |
| 255 | - return implode( '-', $path ); | |
| 256 | 223 | } |
| 257 | 224 | } |