PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.0-dev2
Elementor Website Builder – more than just a page builder v3.32.0-dev2
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 4.1.0-beta1 4.1.0-dev1 All 452 releases
← All changes | modules/global-classes/global-classes-repository.php +40 -465 4.2.13.32.0-dev2 View file →
@@ -1,12 +1,13 @@
1 1 <?php
2 2 namespace Elementor\Modules\GlobalClasses;
3 3
4 -use Elementor\Core\Kits\Documents\Kit;
5 -use Elementor\Modules\DesignSystemSync\Classes\Global_Classes_Sync_Map;
6 -use Elementor\Modules\GlobalClasses\Concerns\Has_Kit_Dependency;
7 -use Elementor\Modules\GlobalClasses\Concerns\Has_Preview_Context;
8 -use Elementor\Modules\GlobalClasses\Utils\Global_Class_Data_Normalizer;
4 +use Elementor\Core\Base\Document;
5 +use Elementor\Core\Utils\Collection;
6 +use Elementor\Modules\AtomicWidgets\Elements\Atomic_Element_Base;
7 +use Elementor\Modules\AtomicWidgets\Elements\Atomic_Widget_Base;
8 +use Elementor\Modules\GlobalClasses\Utils\Atomic_Elements_Utils;
9 +use Elementor\Plugin;
9 10
10 11 if ( ! defined( 'ABSPATH' ) ) {
11 12 exit; // Exit if accessed directly.
12 13 }
@@ -11,10 +12,8 @@
11 12 exit; // Exit if accessed directly.
12 13 }
13 14
14 15 class Global_Classes_Repository {
15 - use Has_Kit_Dependency;
16 - use Has_Preview_Context;
17 16
18 17 const META_KEY_FRONTEND = '_elementor_global_classes';
19 18 const META_KEY_PREVIEW = '_elementor_global_classes_preview';
20 19
@@ -20,493 +19,69 @@
20 19
21 20 const CONTEXT_FRONTEND = 'frontend';
22 21 const CONTEXT_PREVIEW = 'preview';
23 22
24 - const READ_BATCH_SIZE = 100;
25 - const PERSIST_BATCH_SIZE = 100;
23 + private string $context = self::CONTEXT_FRONTEND;
26 24
27 - protected array $context_keys = [
28 - 'event' => [
29 - 'frontend' => self::CONTEXT_FRONTEND,
30 - 'preview' => self::CONTEXT_PREVIEW,
31 - ],
32 - ];
33 -
34 - private ?Global_Classes $cache = null;
35 -
36 - public function __construct( ?Kit $kit = null ) {
37 - if ( null !== $kit ) {
38 - $this->set_kit( $kit );
39 - }
25 + public static function make(): Global_Classes_Repository {
26 + return new self();
40 27 }
41 28
42 - public static function make( ?Kit $kit = null ): Global_Classes_Repository {
43 - return new self( $kit );
44 - }
29 + public function context( string $context ): self {
30 + $this->context = $context;
45 31
46 - protected function on_preview_change(): void {
47 - $this->cache = null;
32 + return $this;
48 33 }
49 34
50 - /**
51 - * This method may be too heavy to use
52 - * Be mindful as this call would cause the server to freeze for as much time as needed until it fetches
53 - * all global classes
54 - */
55 - public function all( bool $force = false ): Global_Classes {
56 - if ( ! $force && null !== $this->cache ) {
57 - return $this->cache;
58 - }
35 + public function all() {
36 + $meta_key = $this->get_meta_key();
37 + $all = $this->get_kit()->get_json_meta( $meta_key );
59 38
60 - $this->cache = $this->all_from_posts();
39 + $is_preview = static::META_KEY_PREVIEW === $meta_key;
40 + $is_empty = empty( $all );
61 41
62 - return $this->cache;
63 - }
64 -
65 - public function all_labels(): array {
66 - return $this->labels()->get_ordered_labels();
67 - }
68 -
69 - public function get_order(): array {
70 - return Global_Classes_Order::make( $this->get_kit() )->set_preview( $this->is_preview() )->get_order();
71 - }
72 -
73 - public function update_order_and_labels( array $order, array $new_labels ): void {
74 - Global_Classes_Order::make( $this->get_kit() )
75 - ->set_preview( $this->is_preview() )
76 - ->set_order( $order );
77 -
78 - $labels = $this->labels();
79 - $existing_labels = $labels->get_labels();
80 -
81 - foreach ( $new_labels as $id => $label ) {
82 - $existing_labels[ $id ] = $label;
42 + if ( $is_preview && $is_empty ) {
43 + $all = $this->get_kit()->get_json_meta( static::META_KEY_FRONTEND );
83 44 }
84 45
85 - $labels->set_labels( $existing_labels );
86 -
87 - if ( ! $this->is_preview() ) {
88 - Global_Classes_Order::make( $this->get_kit() )
89 - ->set_preview( true )
90 - ->set_order( $order );
91 -
92 - $this->clear_preview_labels_for_ids( array_keys( $new_labels ) );
93 - }
94 -
95 - $this->cache = null;
46 + return Global_Classes::make( $all['items'] ?? [], $all['order'] ?? [] );
96 47 }
97 48
98 - private function labels(): Global_Classes_Labels {
99 - return Global_Classes_Labels::make( $this->get_kit() )->set_preview( $this->is_preview() );
100 - }
101 -
102 - public function get( string $class_id ): ?array {
103 - $post = Global_Class_Post::find_by_class_id( $class_id, $this->is_preview(), $this->get_kit() );
104 -
105 - return $post ? $post->to_array() : null;
106 - }
107 -
108 - public function get_by_ids( array $class_ids ): array {
109 - if ( empty( $class_ids ) ) {
110 - return [];
111 - }
112 -
113 - $post_ids = Global_Classes_Post_IDs::make( $this->get_kit() )->get_post_ids( $class_ids );
114 - $items = [];
115 -
116 - foreach ( $post_ids as $class_id => $post_id ) {
117 - $post = Global_Class_Post::from_post_id( $post_id, $this->is_preview() );
118 -
119 - if ( $post ) {
120 - $items[ $class_id ] = $post->to_array();
121 - }
122 - }
123 -
124 - return $items;
125 - }
126 -
127 - public function apply_changes( array $touched_items, array $changes, array $order ): void {
128 - $labels = $this->labels();
129 - $before = $labels->get_labels();
130 - $is_preview = $this->is_preview();
131 - $to_delete = $changes['deleted'] ?? [];
132 - $to_create = $changes['added'] ?? [];
133 - $to_update = $changes['modified'] ?? [];
134 - $order_changed = isset( $changes['order'] ) && $changes['order'];
135 -
136 - $final_label_map = [];
137 - foreach ( $order as $id ) {
138 - if ( isset( $touched_items[ $id ] ) ) {
139 - $final_label_map[ $id ] = $touched_items[ $id ]['label'];
140 - } elseif ( isset( $before[ $id ] ) ) {
141 - $final_label_map[ $id ] = $before[ $id ];
142 - }
143 - }
144 -
145 - $affected_post_ids = $this->get_posts_affected_by_deletion( $to_delete );
146 -
147 - $this->persist_class_batch_mutations( $to_delete, $to_create, $to_update, $touched_items, $is_preview );
148 -
149 - $classes_order = Global_Classes_Order::make( $this->get_kit() )->set_preview( $this->is_preview() );
150 - $classes_order->set_order( $order );
151 - $labels->set_labels( $final_label_map );
152 -
153 - if ( ! $is_preview ) {
154 - Global_Classes_Sync_Map::make( $this->get_kit() )->apply_changes( $touched_items, $to_delete );
155 - Global_Classes_Order::make( $this->get_kit() )
156 - ->set_preview( true )
157 - ->set_order( $order );
158 -
159 - $this->bulk_clear_preview_meta( array_values( $to_update ) );
160 - $this->clear_preview_labels_for_ids( array_merge(
161 - array_values( $to_create ),
162 - array_values( $to_update ),
163 - array_values( $to_delete )
164 - ) );
165 - }
166 -
167 - $this->cache = null;
168 - $this->flush_runtime_cache();
169 -
170 - do_action( 'elementor/global_classes/update', $this->get_context_key( 'event' ), [
171 - 'added' => $to_create,
172 - 'deleted' => $to_delete,
173 - 'modified' => $to_update,
174 - 'order' => $order_changed,
175 - 'affected_post_ids' => $affected_post_ids,
176 - ] );
177 -
178 - if ( ! empty( $to_delete ) && ! $is_preview ) {
179 - do_action(
180 - 'elementor/global_classes/cleanup',
181 - $to_delete,
182 - $affected_post_ids
183 - );
184 - }
185 - }
186 -
187 - public function each_item( callable $cb, bool $skip_migration = false, int $batch_size = self::READ_BATCH_SIZE ): void {
188 - $order = Global_Classes_Order::make( $this->get_kit() )->get_order();
189 -
190 - if ( empty( $order ) ) {
191 - return;
192 - }
193 -
194 - foreach ( array_chunk( $order, $batch_size ) as $chunk ) {
195 - foreach ( $this->iterate_class_posts_for_ids( $chunk ) as $class_post ) {
196 - $cb( $class_post->to_array( $skip_migration ) );
197 - }
198 - }
199 - }
200 -
201 49 public function put( array $items, array $order ) {
202 - $current_ids = Global_Classes_Order::make( $this->get_kit() )
203 - ->set_preview( $this->is_preview() )
204 - ->get_order();
50 + $current_value = $this->all()->get();
205 51
206 - $new_ids = array_keys( $items );
207 - $current_order_string = implode( ';', $current_ids );
208 - $deleted_class_ids = array_values( array_diff( $current_ids, $new_ids ) );
209 -
210 - $changes = [
211 - 'added' => array_values( array_diff( $new_ids, $current_ids ) ),
212 - 'deleted' => $deleted_class_ids,
213 - 'modified' => array_values( array_intersect( $new_ids, $current_ids ) ),
214 - 'order' => implode( ';', $order ) !== $current_order_string,
52 + $updated_value = [
53 + 'items' => $items,
54 + 'order' => $order,
215 55 ];
216 56
217 - /**
218 - * We collect all affected ids before the put_to_posts execution
219 - * as the update mechanism would handle the Global_Classes_Relations as it iterates over the update batches
220 - * So once we get to the cleanup phase - we would no longer have the relevant relations
221 - *
222 - * On top of that - by collecting all affected posts in advance, means we would iterate over each document's elements only once
223 - * (as the alternative would be to trigger the cleanup per removed class, but that means we may end up iterating over the same document N times, if all N styles are used in it)
224 - */
225 - $affected_post_ids = $this->get_posts_affected_by_deletion( $deleted_class_ids );
226 -
227 - $this->put_to_posts( $items, $order, $current_ids );
228 -
229 - $this->cache = null;
230 -
231 - $changes['affected_post_ids'] = $affected_post_ids;
232 -
233 - do_action( 'elementor/global_classes/update', $this->get_context_key( 'event' ), $changes );
234 -
235 - if ( ! empty( $deleted_class_ids ) && ! $this->is_preview() ) {
236 - do_action(
237 - 'elementor/global_classes/cleanup',
238 - $deleted_class_ids,
239 - $affected_post_ids
240 - );
241 - }
242 - }
243 -
244 - private function get_posts_affected_by_deletion( array $deleted_class_ids ): array {
245 - if ( empty( $deleted_class_ids ) ) {
246 - return [];
247 - }
248 -
249 - $relations = new Global_Classes_Relations();
250 - $post_ids = [];
251 -
252 - foreach ( $deleted_class_ids as $class_id ) {
253 - $post_ids[] = $relations->get_posts_by_style( $class_id );
254 - }
255 -
256 - return array_values( array_unique( array_merge( ...$post_ids ) ) );
257 - }
258 -
259 - private function all_from_posts(): Global_Classes {
260 - $order = Global_Classes_Order::make( $this->get_kit() )
261 - ->set_preview( $this->is_preview() )
262 - ->get_order();
263 -
264 - if ( empty( $order ) ) {
265 - return Global_Classes::make( [], [] );
266 - }
267 -
268 - $items = [];
269 -
270 - foreach ( $this->iterate_class_posts_for_ids( $order ) as $class_post ) {
271 - $class_data = $class_post->to_array();
272 - $items[ $class_data['id'] ] = $class_data;
273 - }
274 -
275 - $order = Global_Classes_Parser::sanitize_order( $items, $order );
276 -
277 - return Global_Classes::make( $items, $order );
278 - }
279 -
280 - private function put_to_posts( array $items, array $order, array $current_ids ): void {
281 - $is_preview = $this->is_preview();
282 - $new_ids = array_keys( $items );
283 -
284 - $to_delete = array_diff( $current_ids, $new_ids );
285 - $to_create = array_diff( $new_ids, $current_ids );
286 - $to_update = array_intersect( $new_ids, $current_ids );
287 -
288 - $this->persist_class_batch_mutations( $to_delete, $to_create, $to_update, $items, $is_preview );
289 -
290 - $classes_order = Global_Classes_Order::make( $this->get_kit() )->set_preview( $this->is_preview() );
291 - $classes_order->set_order( $order );
292 -
293 - $label_map = [];
294 - foreach ( $order as $id ) {
295 - if ( isset( $items[ $id ]['label'] ) ) {
296 - $label_map[ $id ] = $items[ $id ]['label'];
297 - }
298 - }
299 - $this->labels()->set_labels( $label_map );
300 -
301 - if ( ! $is_preview ) {
302 - $touched_ids = array_merge( array_values( $to_create ), array_values( $to_update ) );
303 - $touched_items = array_intersect_key(
304 - $items,
305 - array_flip( $touched_ids )
306 - );
307 - Global_Classes_Sync_Map::make( $this->get_kit() )->apply_changes( $touched_items, array_values( $to_delete ) );
308 -
309 - $this->bulk_clear_preview_meta( array_values( $to_update ) );
310 - $this->clear_preview_labels_for_ids( array_merge(
311 - array_values( $to_create ),
312 - array_values( $to_update ),
313 - array_values( $to_delete )
314 - ) );
315 - }
316 - }
317 -
318 - private function iterate_class_posts_for_ids( array $class_ids ): \Generator {
319 - foreach ( array_chunk( $class_ids, self::READ_BATCH_SIZE ) as $chunk ) {
320 - $posts = get_posts( [
321 - 'post_type' => Global_Class_Post_Type::CPT,
322 - 'post_status' => 'publish',
323 - 'posts_per_page' => -1,
324 - 'meta_query' => [
325 - [
326 - 'key' => Global_Class_Post::META_KEY_ID,
327 - 'value' => $chunk,
328 - 'compare' => 'IN',
329 - ],
330 - ],
331 - ] );
332 -
333 - foreach ( $posts as $post ) {
334 - yield Global_Class_Post::from_post( $post, $this->is_preview() );
335 - clean_post_cache( $post->ID );
336 - }
337 - unset( $posts );
338 - $this->flush_runtime_cache();
339 - }
340 - }
341 -
342 - private function persist_class_batch_mutations(
343 - array $to_delete,
344 - array $to_create,
345 - array $to_update,
346 - array $items_by_id,
347 - bool $is_preview
348 - ): void {
349 - $relations = new Global_Classes_Relations();
350 - $post_ids_map = Global_Classes_Post_IDs::make( $this->get_kit() );
351 -
352 - $ids_to_resolve = array_values( array_merge(
353 - array_values( $to_delete ),
354 - array_values( $to_update ),
355 - array_values( $to_create )
356 - ) );
357 - $post_ids = $post_ids_map->get_post_ids( $ids_to_resolve );
358 -
359 - $this->each_class_id_batch(
360 - array_values( $to_delete ),
361 - function ( string $class_id ) use ( $is_preview, $relations, $post_ids ) {
362 - $post = isset( $post_ids[ $class_id ] )
363 - ? Global_Class_Post::from_post_id( $post_ids[ $class_id ], false )
364 - : null;
365 -
366 - if ( ! $post ) {
367 - return;
368 - }
369 -
370 - if ( $is_preview ) {
371 - $post->set_preview( true );
372 - $post->update_data( [] );
373 - clean_post_cache( $post->get_post_id() );
374 - } else {
375 - $relations->clear_class_relations( $class_id );
376 - $post->delete();
377 - }
378 - }
379 - );
380 -
381 - $this->each_class_id_batch( $to_create, function ( string $class_id ) use ( $items_by_id, $is_preview, $post_ids, $post_ids_map ) {
382 - if ( ! isset( $items_by_id[ $class_id ] ) ) {
383 - return;
384 - }
385 -
386 - $item = $items_by_id[ $class_id ];
387 - $data = Global_Class_Data_Normalizer::normalize_style_fields( $item );
388 - $kit = $this->get_kit();
389 - $existing_post_id = $post_ids[ $class_id ] ?? null;
390 - $existing_post = $existing_post_id ? Global_Class_Post::from_post_id( $existing_post_id, $is_preview ) : null;
391 -
392 - if ( $existing_post ) {
393 - $existing_post->update_data( $data );
394 -
395 - if ( ! $is_preview ) {
396 - $existing_post->update_label( $item['label'] );
397 - }
398 -
399 - clean_post_cache( $existing_post->get_post_id() );
400 -
401 - return;
402 - }
403 -
404 - $created = Global_Class_Post::create( $class_id, $item['label'], $data, $kit );
405 -
406 - if ( $created ) {
407 - $post_ids_map->set( $class_id, $created->get_post_id() );
408 - clean_post_cache( $created->get_post_id() );
409 - }
410 - } );
411 -
412 - $this->each_class_id_batch(
413 - $to_update,
414 - function ( string $class_id ) use ( $items_by_id, $is_preview, $post_ids ) {
415 - if ( ! isset( $items_by_id[ $class_id ] ) || ! isset( $post_ids[ $class_id ] ) ) {
416 - return;
417 - }
418 -
419 - $item = $items_by_id[ $class_id ];
420 - $post = Global_Class_Post::from_post_id( $post_ids[ $class_id ], $is_preview );
421 -
422 - if ( ! $post ) {
423 - return;
424 - }
425 -
426 - $data = Global_Class_Data_Normalizer::normalize_style_fields( $item );
427 - $post->update_data( $data );
428 -
429 - if ( ! $is_preview ) {
430 - $post->update_label( $item['label'] );
431 - }
432 -
433 - clean_post_cache( $post->get_post_id() );
434 - }
435 - );
436 - }
437 -
438 - public function delete_all(): void {
439 - $order = $this->get_order();
440 -
441 - $this->each_class_id_batch( $order, function ( string $class_id ) {
442 - $post = Global_Class_Post::find_by_class_id( $class_id, false, $this->get_kit() );
443 -
444 - if ( $post ) {
445 - $post->delete();
446 - }
447 - } );
448 -
449 - Global_Classes_Order::make( $this->get_kit() )->set_order( [] );
450 - $this->labels()->set_labels( [] );
451 - }
452 -
453 - private function clear_preview_labels_for_ids( array $class_ids ): void {
454 - if ( empty( $class_ids ) ) {
57 + // `update_metadata` fails for identical data, so we skip it.
58 + if ( $current_value === $updated_value ) {
455 59 return;
456 60 }
457 61
458 - $preview_labels = Global_Classes_Labels::make( $this->get_kit() )->set_preview( true );
459 - $labels_map = $preview_labels->get_labels();
62 + $meta_key = $this->get_meta_key();
63 + $value = $this->get_kit()->update_json_meta( $meta_key, $updated_value );
460 64
461 - if ( empty( $labels_map ) ) {
462 - return;
463 - }
65 + $should_delete_preview = static::META_KEY_FRONTEND === $meta_key;
464 66
465 - $ids_to_remove = array_intersect( array_unique( $class_ids ), array_keys( $labels_map ) );
466 -
467 - if ( empty( $ids_to_remove ) ) {
468 - return;
67 + if ( $should_delete_preview ) {
68 + $this->get_kit()->delete_meta( static::META_KEY_PREVIEW );
469 69 }
470 70
471 - foreach ( $ids_to_remove as $id ) {
472 - unset( $labels_map[ $id ] );
71 + if ( ! $value ) {
72 + throw new \Exception( 'Failed to update global classes' );
473 73 }
474 74
475 - $preview_labels->set_labels( $labels_map );
75 + do_action( 'elementor/global_classes/update', $this->context, $updated_value, $current_value );
476 76 }
477 77
478 - private function bulk_clear_preview_meta( array $class_ids ): void {
479 - if ( empty( $class_ids ) ) {
480 - return;
481 - }
482 -
483 - $post_ids_map = Global_Classes_Post_IDs::make( $this->get_kit() );
484 -
485 - foreach ( array_chunk( $class_ids, self::PERSIST_BATCH_SIZE ) as $chunk ) {
486 - $post_ids = $post_ids_map->get_post_ids( $chunk );
487 -
488 - foreach ( $post_ids as $post_id ) {
489 - delete_post_meta( $post_id, Global_Class_Post::META_KEY_DATA_PREVIEW );
490 - clean_post_cache( $post_id );
491 - }
492 -
493 - $this->flush_runtime_cache();
494 - }
78 + private function get_meta_key(): string {
79 + return static::CONTEXT_FRONTEND === $this->context
80 + ? static::META_KEY_FRONTEND
81 + : static::META_KEY_PREVIEW;
495 82 }
496 83
497 - private function each_class_id_batch( $class_ids, callable $callback, int $batch_size = self::PERSIST_BATCH_SIZE ): void {
498 - $class_ids = is_array( $class_ids ) ? $class_ids : iterator_to_array( $class_ids, false );
499 - foreach ( array_chunk( array_values( $class_ids ), $batch_size ) as $batch ) {
500 - foreach ( $batch as $class_id ) {
501 - $callback( $class_id );
502 - }
503 - $this->flush_runtime_cache();
504 - }
505 - }
506 -
507 - private function flush_runtime_cache(): void {
508 - if ( function_exists( 'wp_cache_flush_runtime' ) ) {
509 - wp_cache_flush_runtime();
510 - }
84 + private function get_kit() {
85 + return Plugin::$instance->kits_manager->get_active_kit();
511 86 }
512 87 }