PluginProbe
Elementor Website Builder – more than just a page builder / 3.31.4
Elementor Website Builder – more than just a page builder v3.31.4
4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 All 454 releases
← All changes | modules/atomic-widgets/styles/atomic-styles-manager.php +26 -71 4.3.1 → 3.31.4 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;
@@ -31,10 +31,10 @@
31 31
32 32 private array $fonts = [];
33 33
34 34 public function __construct() {
35 + $this->css_files_manager = new CSS_Files_Manager();
35 36 $this->cache_validity = new Cache_Validity();
36 - $this->css_files_manager = new CSS_Files_Manager( $this->cache_validity );
37 37 }
38 38
39 39 public static function instance() {
40 40 if ( ! self::$instance ) {
@@ -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,21 +115,19 @@
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 - // `CSS_Files_Manager::get()` owns the per-breakpoint decision: it consults the
129 - // cache-validity leaf under `$breakpoint_path` (including the `should_exist`
130 - // meta), regenerates when the cache is invalid or when a should-be-present file
131 - // is missing on disk, and validates the leaf on success. See ED-24903.
132 121 $style_file = $this->css_files_manager->get(
133 - $this->convert_path_to_handle( $breakpoint_path ),
122 + $style_key . '-' . $breakpoint_key,
134 123 $breakpoint_media,
135 124 $render_css,
136 - $breakpoint_path
125 + $this->cache_validity->is_valid( $breakpoint_cache_keys )
137 126 );
138 127
128 + $this->cache_validity->validate( $breakpoint_cache_keys );
129 +
139 130 if ( ! $style_file ) {
140 131 continue;
141 132 }
142 133
@@ -143,9 +134,8 @@
143 134 wp_enqueue_style(
144 135 $style_file->get_handle(),
145 136 $style_file->get_url(),
146 137 [],
147 - $version,
148 138 $style_file->get_media()
149 139 );
150 140 }
151 141 }
@@ -155,10 +145,15 @@
155 145 $style_fonts = Style_Fonts::make( $style_key );
156 146
157 147 return Styles_Renderer::make(
158 148 Plugin::$instance->breakpoints->get_breakpoints_config()
159 - )->on_font_enqueue( fn( $font ) => $style_fonts->add( $font ) )
160 - ->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 );
161 156 }
162 157
163 158 private function get_breakpoint_media( string $breakpoint_key ): ?string {
164 159 $breakpoint_config = Plugin::$instance->breakpoints->get_breakpoints_config()[ $breakpoint_key ] ?? null;
@@ -178,24 +173,14 @@
178 173 return Collection::make( $styles )->reduce( function( $group, $style ) {
179 174 Collection::make( $style['variants'] )->each( function( $variant ) use ( &$group, $style ) {
180 175 $breakpoint = $variant['meta']['breakpoint'] ?? self::DEFAULT_BREAKPOINT;
181 176
182 - if ( empty( $breakpoint ) ) {
183 - $breakpoint = self::DEFAULT_BREAKPOINT;
184 - }
185 -
186 177 if ( ! isset( $group[ $breakpoint ][ $style['id'] ] ) ) {
187 - $style_for_breakpoint = [
178 + $group[ $breakpoint ][ $style['id'] ] = [
188 179 'id' => $style['id'],
189 180 'type' => $style['type'],
190 181 'variants' => [],
191 182 ];
192 -
193 - if ( isset( $style['cssName'] ) ) {
194 - $style_for_breakpoint['cssName'] = $style['cssName'];
195 - }
196 -
197 - $group[ $breakpoint ][ $style['id'] ] = $style_for_breakpoint;
198 183 }
199 184
200 185 $group[ $breakpoint ][ $style['id'] ]['variants'][] = $variant;
201 186 } );
@@ -234,36 +219,6 @@
234 219 private function enqueue_fonts() {
235 220 foreach ( $this->fonts as $font ) {
236 221 Plugin::instance()->frontend->enqueue_font( $font );
237 222 }
238 - }
239 -
240 - private function clear_styles( array $path ) {
241 - $node = $this->cache_validity->get_node( $path );
242 -
243 - $this->clear_styles_by_node( $path, $node );
244 -
245 - $this->cache_validity->invalidate( $path );
246 - }
247 -
248 - private function clear_styles_by_node( array $path, $node ) {
249 - if ( ! $node ) {
250 - return;
251 - }
252 -
253 - if ( true === $node || $node['state'] ) {
254 - $this->css_files_manager->delete( $this->convert_path_to_handle( $path ) );
255 - }
256 -
257 - if ( is_bool( $node ) || empty( $node['children'] ) ) {
258 - return;
259 - }
260 -
261 - foreach ( $node['children'] as $child_key => $child_node ) {
262 - $this->clear_styles_by_node( array_merge( $path, [ $child_key ] ), $child_node );
263 - }
264 - }
265 -
266 - private function convert_path_to_handle( array $path ) {
267 - return implode( '-', $path );
268 223 }
269 224 }