PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.0-beta1
Elementor Website Builder – more than just a page builder v4.1.0-beta1
4.3.3 4.3.2 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 All 456 releases
← All changes | modules/global-classes/global-classes-repository.php +91 -195 4.1.3 → 4.1.0-beta1 View file →
@@ -4,9 +4,8 @@
4 4 use Elementor\Core\Kits\Documents\Kit;
5 5 use Elementor\Modules\DesignSystemSync\Classes\Global_Classes_Sync_Map;
6 6 use Elementor\Modules\GlobalClasses\Concerns\Has_Kit_Dependency;
7 7 use Elementor\Modules\GlobalClasses\Concerns\Has_Preview_Context;
8 -use Elementor\Modules\GlobalClasses\Utils\Global_Class_Data_Normalizer;
9 8
10 9 if ( ! defined( 'ABSPATH' ) ) {
11 10 exit; // Exit if accessed directly.
12 11 }
@@ -70,15 +69,13 @@
70 69 return $this->labels()->get_ordered_labels();
71 70 }
72 71
73 72 public function get_order(): array {
74 - return Global_Classes_Order::make( $this->get_kit() )->set_preview( $this->is_preview() )->get_order();
73 + return Global_Classes_Order::make( $this->get_kit() )->get_order();
75 74 }
76 75
77 76 public function update_order_and_labels( array $order, array $new_labels ): void {
78 - Global_Classes_Order::make( $this->get_kit() )
79 - ->set_preview( $this->is_preview() )
80 - ->set_order( $order );
77 + Global_Classes_Order::make( $this->get_kit() )->set_order( $order );
81 78
82 79 $labels = $this->labels();
83 80 $existing_labels = $labels->get_labels();
84 81
@@ -87,16 +84,8 @@
87 84 }
88 85
89 86 $labels->set_labels( $existing_labels );
90 87
91 - if ( ! $this->is_preview() ) {
92 - Global_Classes_Order::make( $this->get_kit() )
93 - ->set_preview( true )
94 - ->set_order( $order );
95 -
96 - $this->clear_preview_labels_for_ids( array_keys( $new_labels ) );
97 - }
98 -
99 88 $this->cache = null;
100 89 }
101 90
102 91 private function labels(): Global_Classes_Labels {
@@ -113,17 +102,13 @@
113 102 if ( empty( $class_ids ) ) {
114 103 return [];
115 104 }
116 105
117 - $post_ids = Global_Classes_Post_IDs::make( $this->get_kit() )->get_post_ids( $class_ids );
118 106 $items = [];
119 107
120 - foreach ( $post_ids as $class_id => $post_id ) {
121 - $post = Global_Class_Post::from_post_id( $post_id, $this->is_preview() );
122 -
123 - if ( $post ) {
124 - $items[ $class_id ] = $post->to_array();
125 - }
108 + foreach ( $this->iterate_class_posts_for_ids( $class_ids ) as $class_post ) {
109 + $class_data = $class_post->to_array();
110 + $items[ $class_data['id'] ] = $class_data;
126 111 }
127 112
128 113 return $items;
129 114 }
@@ -145,28 +130,24 @@
145 130 $final_label_map[ $id ] = $before[ $id ];
146 131 }
147 132 }
148 133
149 - $affected_post_ids = $this->get_posts_affected_by_deletion( $to_delete );
150 -
151 134 $this->persist_class_batch_mutations( $to_delete, $to_create, $to_update, $touched_items, $is_preview );
152 135
153 - $classes_order = Global_Classes_Order::make( $this->get_kit() )->set_preview( $this->is_preview() );
136 + $classes_order = Global_Classes_Order::make( $this->get_kit() );
154 137 $classes_order->set_order( $order );
155 138 $labels->set_labels( $final_label_map );
156 139
157 140 if ( ! $is_preview ) {
158 141 Global_Classes_Sync_Map::make( $this->get_kit() )->apply_changes( $touched_items, $to_delete );
159 - Global_Classes_Order::make( $this->get_kit() )
160 - ->set_preview( true )
161 - ->set_order( $order );
162 142
163 - $this->bulk_clear_preview_meta( array_values( $to_update ) );
164 - $this->clear_preview_labels_for_ids( array_merge(
165 - array_values( $to_create ),
166 - array_values( $to_update ),
167 - array_values( $to_delete )
168 - ) );
143 + $this->each_class_id_batch( array_values( $to_update ), function ( string $class_id ) {
144 + $post = Global_Class_Post::find_by_class_id( $class_id, false );
145 + if ( $post ) {
146 + delete_post_meta( $post->get_post_id(), Global_Class_Post::META_KEY_DATA_PREVIEW );
147 + clean_post_cache( $post->get_post_id() );
148 + }
149 + } );
169 150 }
170 151
171 152 $this->cache = null;
172 153 $this->flush_runtime_cache();
@@ -175,18 +156,9 @@
175 156 'added' => $to_create,
176 157 'deleted' => $to_delete,
177 158 'modified' => $to_update,
178 159 'order' => $order_changed,
179 - 'affected_post_ids' => $affected_post_ids,
180 160 ] );
181 -
182 - if ( ! empty( $to_delete ) && ! $is_preview ) {
183 - do_action(
184 - 'elementor/global_classes/cleanup',
185 - $to_delete,
186 - $affected_post_ids
187 - );
188 - }
189 161 }
190 162
191 163 public function each_item( callable $cb, bool $skip_migration = false, int $batch_size = self::READ_BATCH_SIZE ): void {
192 164 $order = Global_Classes_Order::make( $this->get_kit() )->get_order();
@@ -202,69 +174,31 @@
202 174 }
203 175 }
204 176
205 177 public function put( array $items, array $order ) {
206 - $current_ids = Global_Classes_Order::make( $this->get_kit() )
207 - ->set_preview( $this->is_preview() )
208 - ->get_order();
178 + $current_ids = Global_Classes_Order::make( $this->get_kit() )->get_order();
209 179
210 180 $new_ids = array_keys( $items );
181 +
211 182 $current_order_string = implode( ';', $current_ids );
212 - $deleted_class_ids = array_values( array_diff( $current_ids, $new_ids ) );
213 183
214 184 $changes = [
215 185 'added' => array_values( array_diff( $new_ids, $current_ids ) ),
216 - 'deleted' => $deleted_class_ids,
186 + 'deleted' => array_values( array_diff( $current_ids, $new_ids ) ),
217 187 'modified' => array_values( array_intersect( $new_ids, $current_ids ) ),
218 188 'order' => implode( ';', $order ) !== $current_order_string,
219 189 ];
220 190
221 - /**
222 - * We collect all affected ids before the put_to_posts execution
223 - * as the update mechanism would handle the Global_Classes_Relations as it iterates over the update batches
224 - * So once we get to the cleanup phase - we would no longer have the relevant relations
225 - *
226 - * On top of that - by collecting all affected posts in advance, means we would iterate over each document's elements only once
227 - * (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)
228 - */
229 - $affected_post_ids = $this->get_posts_affected_by_deletion( $deleted_class_ids );
230 -
231 191 $this->put_to_posts( $items, $order, $current_ids );
232 192
233 193 $this->cache = null;
234 194
235 - $changes['affected_post_ids'] = $affected_post_ids;
236 -
237 195 do_action( 'elementor/global_classes/update', $this->get_context_key( 'event' ), $changes );
238 -
239 - if ( ! empty( $deleted_class_ids ) && ! $this->is_preview() ) {
240 - do_action(
241 - 'elementor/global_classes/cleanup',
242 - $deleted_class_ids,
243 - $affected_post_ids
244 - );
245 - }
246 196 }
247 197
248 - private function get_posts_affected_by_deletion( array $deleted_class_ids ): array {
249 - if ( empty( $deleted_class_ids ) ) {
250 - return [];
251 - }
252 -
253 - $relations = new Global_Classes_Relations();
254 - $post_ids = [];
255 -
256 - foreach ( $deleted_class_ids as $class_id ) {
257 - $post_ids[] = $relations->get_posts_by_style( $class_id );
258 - }
259 -
260 - return array_values( array_unique( array_merge( ...$post_ids ) ) );
261 - }
262 -
263 198 private function all_from_posts(): Global_Classes {
264 - $order = Global_Classes_Order::make( $this->get_kit() )
265 - ->set_preview( $this->is_preview() )
266 - ->get_order();
199 + $classes_order = Global_Classes_Order::make( $this->get_kit() );
200 + $order = $classes_order->get_order();
267 201
268 202 if ( empty( $order ) ) {
269 203 return Global_Classes::make( [], [] );
270 204 }
@@ -290,9 +224,9 @@
290 224 $to_update = array_intersect( $new_ids, $current_ids );
291 225
292 226 $this->persist_class_batch_mutations( $to_delete, $to_create, $to_update, $items, $is_preview );
293 227
294 - $classes_order = Global_Classes_Order::make( $this->get_kit() )->set_preview( $this->is_preview() );
228 + $classes_order = Global_Classes_Order::make( $this->get_kit() );
295 229 $classes_order->set_order( $order );
296 230
297 231 $label_map = [];
298 232 foreach ( $order as $id ) {
@@ -309,14 +243,15 @@
309 243 array_flip( $touched_ids )
310 244 );
311 245 Global_Classes_Sync_Map::make( $this->get_kit() )->apply_changes( $touched_items, array_values( $to_delete ) );
312 246
313 - $this->bulk_clear_preview_meta( array_values( $to_update ) );
314 - $this->clear_preview_labels_for_ids( array_merge(
315 - array_values( $to_create ),
316 - array_values( $to_update ),
317 - array_values( $to_delete )
318 - ) );
247 + $this->each_class_id_batch( array_values( $to_update ), function ( string $class_id ) {
248 + $post = Global_Class_Post::find_by_class_id( $class_id, false );
249 + if ( $post ) {
250 + delete_post_meta( $post->get_post_id(), Global_Class_Post::META_KEY_DATA_PREVIEW );
251 + clean_post_cache( $post->get_post_id() );
252 + }
253 + } );
319 254 }
320 255 }
321 256
322 257 private function iterate_class_posts_for_ids( array $class_ids ): \Generator {
@@ -333,13 +268,28 @@
333 268 ],
334 269 ],
335 270 ] );
336 271
272 + $seen_class_ids = [];
273 +
337 274 foreach ( $posts as $post ) {
275 + $class_id = get_post_meta( $post->ID, Global_Class_Post::META_KEY_ID, true );
276 +
277 + if ( $class_id && isset( $seen_class_ids[ $class_id ] ) ) {
278 + // if for some reason there's already a post meta to that class - delete any other ones
279 + // otherwise we may end up updating a stale post meta
280 + wp_delete_post( $post->ID, true );
281 + continue;
282 + }
283 +
284 + if ( $class_id ) {
285 + $seen_class_ids[ $class_id ] = true;
286 + }
287 +
338 288 yield Global_Class_Post::from_post( $post, $this->is_preview() );
339 289 clean_post_cache( $post->ID );
340 290 }
341 - unset( $posts );
291 + unset( $posts, $seen_class_ids );
342 292 $this->flush_runtime_cache();
343 293 }
344 294 }
345 295
@@ -350,28 +300,13 @@
350 300 array $items_by_id,
351 301 bool $is_preview
352 302 ): void {
353 303 $relations = new Global_Classes_Relations();
354 - $post_ids_map = Global_Classes_Post_IDs::make( $this->get_kit() );
355 304
356 - $ids_to_resolve = array_values( array_merge(
357 - array_values( $to_delete ),
358 - array_values( $to_update ),
359 - array_values( $to_create )
360 - ) );
361 - $post_ids = $post_ids_map->get_post_ids( $ids_to_resolve );
305 + $this->each_class_id_batch( array_values( $to_delete ), function ( string $class_id ) use ( $is_preview, $relations ) {
306 + $post = Global_Class_Post::find_by_class_id( $class_id, false );
362 307
363 - $this->each_class_id_batch(
364 - array_values( $to_delete ),
365 - function ( string $class_id ) use ( $is_preview, $relations, $post_ids ) {
366 - $post = isset( $post_ids[ $class_id ] )
367 - ? Global_Class_Post::from_post_id( $post_ids[ $class_id ], false )
368 - : null;
369 -
370 - if ( ! $post ) {
371 - return;
372 - }
373 -
308 + if ( $post ) {
374 309 if ( $is_preview ) {
375 310 $post->set_preview( true );
376 311 $post->update_data( [] );
377 312 clean_post_cache( $post->get_post_id() );
@@ -379,65 +314,57 @@
379 314 $relations->clear_class_relations( $class_id );
380 315 $post->delete();
381 316 }
382 317 }
383 - );
318 + } );
384 319
385 - $this->each_class_id_batch( $to_create, function ( string $class_id ) use ( $items_by_id, $is_preview, $post_ids, $post_ids_map ) {
320 + $this->each_class_id_batch( $to_create, function ( string $class_id ) use ( $items_by_id, $is_preview ) {
386 321 if ( ! isset( $items_by_id[ $class_id ] ) ) {
387 322 return;
388 323 }
389 324
390 325 $item = $items_by_id[ $class_id ];
391 - $data = Global_Class_Data_Normalizer::normalize_style_fields( $item );
392 - $kit = $this->get_kit();
393 - $existing_post_id = $post_ids[ $class_id ] ?? null;
394 - $existing_post = $existing_post_id ? Global_Class_Post::from_post_id( $existing_post_id, $is_preview ) : null;
326 + $data = $this->build_class_data_for_storage( $item );
395 327
396 - if ( $existing_post ) {
397 - $existing_post->update_data( $data );
328 + if ( $is_preview ) {
329 + $post = Global_Class_Post::find_by_class_id( $class_id, true );
398 330
399 - if ( ! $is_preview ) {
400 - $existing_post->update_label( $item['label'] );
331 + if ( $post ) {
332 + $post->set_preview( true );
333 + $post->update_data( $data );
334 + $post->update_label( $item['label'] );
335 + clean_post_cache( $post->get_post_id() );
336 + } else {
337 + $created = Global_Class_Post::create( $class_id, $item['label'], $data );
338 + if ( $created ) {
339 + clean_post_cache( $created->get_post_id() );
340 + }
401 341 }
342 + } else {
343 + $created = Global_Class_Post::create( $class_id, $item['label'], $data );
344 + if ( $created ) {
345 + clean_post_cache( $created->get_post_id() );
346 + }
347 + }
348 + } );
402 349
403 - clean_post_cache( $existing_post->get_post_id() );
404 -
350 + $this->each_class_id_batch( $to_update, function ( string $class_id ) use ( $items_by_id, $is_preview ) {
351 + if ( ! isset( $items_by_id[ $class_id ] ) ) {
405 352 return;
406 353 }
407 354
408 - $created = Global_Class_Post::create( $class_id, $item['label'], $data, $kit );
355 + $item = $items_by_id[ $class_id ];
356 + $post = Global_Class_Post::find_by_class_id( $class_id, $is_preview );
409 357
410 - if ( $created ) {
411 - $post_ids_map->set( $class_id, $created->get_post_id() );
412 - clean_post_cache( $created->get_post_id() );
358 + if ( ! $post ) {
359 + return;
413 360 }
361 +
362 + $data = $this->build_class_data_for_storage( $item );
363 + $post->update_data( $data );
364 + $post->update_label( $item['label'] );
365 + clean_post_cache( $post->get_post_id() );
414 366 } );
415 -
416 - $this->each_class_id_batch(
417 - $to_update,
418 - function ( string $class_id ) use ( $items_by_id, $is_preview, $post_ids ) {
419 - if ( ! isset( $items_by_id[ $class_id ] ) || ! isset( $post_ids[ $class_id ] ) ) {
420 - return;
421 - }
422 -
423 - $item = $items_by_id[ $class_id ];
424 - $post = Global_Class_Post::from_post_id( $post_ids[ $class_id ], $is_preview );
425 -
426 - if ( ! $post ) {
427 - return;
428 - }
429 -
430 - $data = Global_Class_Data_Normalizer::normalize_style_fields( $item );
431 - $post->update_data( $data );
432 -
433 - if ( ! $is_preview ) {
434 - $post->update_label( $item['label'] );
435 - }
436 -
437 - clean_post_cache( $post->get_post_id() );
438 - }
439 - );
440 367 }
441 368
442 369 public function delete_all(): void {
443 370 $order = $this->get_order();
@@ -453,52 +380,8 @@
453 380 Global_Classes_Order::make( $this->get_kit() )->set_order( [] );
454 381 $this->labels()->set_labels( [] );
455 382 }
456 383
457 - private function clear_preview_labels_for_ids( array $class_ids ): void {
458 - if ( empty( $class_ids ) ) {
459 - return;
460 - }
461 -
462 - $preview_labels = Global_Classes_Labels::make( $this->get_kit() )->set_preview( true );
463 - $labels_map = $preview_labels->get_labels();
464 -
465 - if ( empty( $labels_map ) ) {
466 - return;
467 - }
468 -
469 - $ids_to_remove = array_intersect( array_unique( $class_ids ), array_keys( $labels_map ) );
470 -
471 - if ( empty( $ids_to_remove ) ) {
472 - return;
473 - }
474 -
475 - foreach ( $ids_to_remove as $id ) {
476 - unset( $labels_map[ $id ] );
477 - }
478 -
479 - $preview_labels->set_labels( $labels_map );
480 - }
481 -
482 - private function bulk_clear_preview_meta( array $class_ids ): void {
483 - if ( empty( $class_ids ) ) {
484 - return;
485 - }
486 -
487 - $post_ids_map = Global_Classes_Post_IDs::make( $this->get_kit() );
488 -
489 - foreach ( array_chunk( $class_ids, self::PERSIST_BATCH_SIZE ) as $chunk ) {
490 - $post_ids = $post_ids_map->get_post_ids( $chunk );
491 -
492 - foreach ( $post_ids as $post_id ) {
493 - delete_post_meta( $post_id, Global_Class_Post::META_KEY_DATA_PREVIEW );
494 - clean_post_cache( $post_id );
495 - }
496 -
497 - $this->flush_runtime_cache();
498 - }
499 - }
500 -
501 384 private function each_class_id_batch( $class_ids, callable $callback, int $batch_size = self::PERSIST_BATCH_SIZE ): void {
502 385 $class_ids = is_array( $class_ids ) ? $class_ids : iterator_to_array( $class_ids, false );
503 386 foreach ( array_chunk( array_values( $class_ids ), $batch_size ) as $batch ) {
504 387 foreach ( $batch as $class_id ) {
@@ -511,6 +394,19 @@
511 394 private function flush_runtime_cache(): void {
512 395 if ( function_exists( 'wp_cache_flush_runtime' ) ) {
513 396 wp_cache_flush_runtime();
514 397 }
398 + }
399 +
400 + private function build_class_data_for_storage( array $item ): array {
401 + $data = [
402 + 'type' => $item['type'] ?? 'class',
403 + 'variants' => $item['variants'] ?? [],
404 + ];
405 +
406 + if ( array_key_exists( 'sync_to_v3', $item ) ) {
407 + $data['sync_to_v3'] = (bool) $item['sync_to_v3'];
408 + }
409 +
410 + return $data;
515 411 }
516 412 }