PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.0-beta3
Elementor Website Builder – more than just a page builder v4.1.0-beta3
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 +68 -122 4.1.0 → 4.1.0-beta3 View file →
@@ -103,17 +103,13 @@
103 103 if ( empty( $class_ids ) ) {
104 104 return [];
105 105 }
106 106
107 - $post_ids = Global_Classes_Post_IDs::make( $this->get_kit() )->get_post_ids( $class_ids );
108 107 $items = [];
109 108
110 - foreach ( $post_ids as $class_id => $post_id ) {
111 - $post = Global_Class_Post::from_post_id( $post_id, $this->is_preview() );
112 -
113 - if ( $post ) {
114 - $items[ $class_id ] = $post->to_array();
115 - }
109 + foreach ( $this->iterate_class_posts_for_ids( $class_ids ) as $class_post ) {
110 + $class_data = $class_post->to_array();
111 + $items[ $class_data['id'] ] = $class_data;
116 112 }
117 113
118 114 return $items;
119 115 }
@@ -144,14 +140,15 @@
144 140
145 141 if ( ! $is_preview ) {
146 142 Global_Classes_Sync_Map::make( $this->get_kit() )->apply_changes( $touched_items, $to_delete );
147 143
148 - $this->bulk_clear_preview_meta( array_values( $to_update ) );
149 - $this->clear_preview_labels_for_ids( array_merge(
150 - array_values( $to_create ),
151 - array_values( $to_update ),
152 - array_values( $to_delete )
153 - ) );
144 + $this->each_class_id_batch( array_values( $to_update ), function ( string $class_id ) {
145 + $post = Global_Class_Post::find_by_class_id( $class_id, false );
146 + if ( $post ) {
147 + delete_post_meta( $post->get_post_id(), Global_Class_Post::META_KEY_DATA_PREVIEW );
148 + clean_post_cache( $post->get_post_id() );
149 + }
150 + } );
154 151 }
155 152
156 153 $this->cache = null;
157 154 $this->flush_runtime_cache();
@@ -247,14 +244,15 @@
247 244 array_flip( $touched_ids )
248 245 );
249 246 Global_Classes_Sync_Map::make( $this->get_kit() )->apply_changes( $touched_items, array_values( $to_delete ) );
250 247
251 - $this->bulk_clear_preview_meta( array_values( $to_update ) );
252 - $this->clear_preview_labels_for_ids( array_merge(
253 - array_values( $to_create ),
254 - array_values( $to_update ),
255 - array_values( $to_delete )
256 - ) );
248 + $this->each_class_id_batch( array_values( $to_update ), function ( string $class_id ) {
249 + $post = Global_Class_Post::find_by_class_id( $class_id, false );
250 + if ( $post ) {
251 + delete_post_meta( $post->get_post_id(), Global_Class_Post::META_KEY_DATA_PREVIEW );
252 + clean_post_cache( $post->get_post_id() );
253 + }
254 + } );
257 255 }
258 256 }
259 257
260 258 private function iterate_class_posts_for_ids( array $class_ids ): \Generator {
@@ -271,13 +269,28 @@
271 269 ],
272 270 ],
273 271 ] );
274 272
273 + $seen_class_ids = [];
274 +
275 275 foreach ( $posts as $post ) {
276 + $class_id = get_post_meta( $post->ID, Global_Class_Post::META_KEY_ID, true );
277 +
278 + if ( $class_id && isset( $seen_class_ids[ $class_id ] ) ) {
279 + // if for some reason there's already a post meta to that class - delete any other ones
280 + // otherwise we may end up updating a stale post meta
281 + wp_delete_post( $post->ID, true );
282 + continue;
283 + }
284 +
285 + if ( $class_id ) {
286 + $seen_class_ids[ $class_id ] = true;
287 + }
288 +
276 289 yield Global_Class_Post::from_post( $post, $this->is_preview() );
277 290 clean_post_cache( $post->ID );
278 291 }
279 - unset( $posts );
292 + unset( $posts, $seen_class_ids );
280 293 $this->flush_runtime_cache();
281 294 }
282 295 }
283 296
@@ -288,28 +301,13 @@
288 301 array $items_by_id,
289 302 bool $is_preview
290 303 ): void {
291 304 $relations = new Global_Classes_Relations();
292 - $post_ids_map = Global_Classes_Post_IDs::make( $this->get_kit() );
293 305
294 - $ids_to_resolve = array_values( array_merge(
295 - array_values( $to_delete ),
296 - array_values( $to_update ),
297 - array_values( $to_create )
298 - ) );
299 - $post_ids = $post_ids_map->get_post_ids( $ids_to_resolve );
306 + $this->each_class_id_batch( array_values( $to_delete ), function ( string $class_id ) use ( $is_preview, $relations ) {
307 + $post = Global_Class_Post::find_by_class_id( $class_id, false );
300 308
301 - $this->each_class_id_batch(
302 - array_values( $to_delete ),
303 - function ( string $class_id ) use ( $is_preview, $relations, $post_ids ) {
304 - $post = isset( $post_ids[ $class_id ] )
305 - ? Global_Class_Post::from_post_id( $post_ids[ $class_id ], false )
306 - : null;
307 -
308 - if ( ! $post ) {
309 - return;
310 - }
311 -
309 + if ( $post ) {
312 310 if ( $is_preview ) {
313 311 $post->set_preview( true );
314 312 $post->update_data( [] );
315 313 clean_post_cache( $post->get_post_id() );
@@ -317,11 +315,11 @@
317 315 $relations->clear_class_relations( $class_id );
318 316 $post->delete();
319 317 }
320 318 }
321 - );
319 + } );
322 320
323 - $this->each_class_id_batch( $to_create, function ( string $class_id ) use ( $items_by_id, $is_preview, $post_ids, $post_ids_map ) {
321 + $this->each_class_id_batch( $to_create, function ( string $class_id ) use ( $items_by_id, $is_preview ) {
324 322 if ( ! isset( $items_by_id[ $class_id ] ) ) {
325 323 return;
326 324 }
327 325
@@ -326,56 +324,48 @@
326 324 }
327 325
328 326 $item = $items_by_id[ $class_id ];
329 327 $data = Global_Class_Data_Normalizer::normalize_style_fields( $item );
330 - $kit = $this->get_kit();
331 - $existing_post_id = $post_ids[ $class_id ] ?? null;
332 - $existing_post = $existing_post_id ? Global_Class_Post::from_post_id( $existing_post_id, $is_preview ) : null;
333 328
334 - if ( $existing_post ) {
335 - $existing_post->update_data( $data );
329 + if ( $is_preview ) {
330 + $post = Global_Class_Post::find_by_class_id( $class_id, true );
336 331
337 - if ( ! $is_preview ) {
338 - $existing_post->update_label( $item['label'] );
332 + if ( $post ) {
333 + $post->set_preview( true );
334 + $post->update_data( $data );
335 + $post->update_label( $item['label'] );
336 + clean_post_cache( $post->get_post_id() );
337 + } else {
338 + $created = Global_Class_Post::create( $class_id, $item['label'], $data );
339 + if ( $created ) {
340 + clean_post_cache( $created->get_post_id() );
341 + }
339 342 }
343 + } else {
344 + $created = Global_Class_Post::create( $class_id, $item['label'], $data );
345 + if ( $created ) {
346 + clean_post_cache( $created->get_post_id() );
347 + }
348 + }
349 + } );
340 350
341 - clean_post_cache( $existing_post->get_post_id() );
342 -
351 + $this->each_class_id_batch( $to_update, function ( string $class_id ) use ( $items_by_id, $is_preview ) {
352 + if ( ! isset( $items_by_id[ $class_id ] ) ) {
343 353 return;
344 354 }
345 355
346 - $created = Global_Class_Post::create( $class_id, $item['label'], $data, $kit );
356 + $item = $items_by_id[ $class_id ];
357 + $post = Global_Class_Post::find_by_class_id( $class_id, $is_preview );
347 358
348 - if ( $created ) {
349 - $post_ids_map->set( $class_id, $created->get_post_id() );
350 - clean_post_cache( $created->get_post_id() );
359 + if ( ! $post ) {
360 + return;
351 361 }
362 +
363 + $data = Global_Class_Data_Normalizer::normalize_style_fields( $item );
364 + $post->update_data( $data );
365 + $post->update_label( $item['label'] );
366 + clean_post_cache( $post->get_post_id() );
352 367 } );
353 -
354 - $this->each_class_id_batch(
355 - $to_update,
356 - function ( string $class_id ) use ( $items_by_id, $is_preview, $post_ids ) {
357 - if ( ! isset( $items_by_id[ $class_id ] ) || ! isset( $post_ids[ $class_id ] ) ) {
358 - return;
359 - }
360 -
361 - $item = $items_by_id[ $class_id ];
362 - $post = Global_Class_Post::from_post_id( $post_ids[ $class_id ], $is_preview );
363 -
364 - if ( ! $post ) {
365 - return;
366 - }
367 -
368 - $data = Global_Class_Data_Normalizer::normalize_style_fields( $item );
369 - $post->update_data( $data );
370 -
371 - if ( ! $is_preview ) {
372 - $post->update_label( $item['label'] );
373 - }
374 -
375 - clean_post_cache( $post->get_post_id() );
376 - }
377 - );
378 368 }
379 369
380 370 public function delete_all(): void {
381 371 $order = $this->get_order();
@@ -389,52 +379,8 @@
389 379 } );
390 380
391 381 Global_Classes_Order::make( $this->get_kit() )->set_order( [] );
392 382 $this->labels()->set_labels( [] );
393 - }
394 -
395 - private function clear_preview_labels_for_ids( array $class_ids ): void {
396 - if ( empty( $class_ids ) ) {
397 - return;
398 - }
399 -
400 - $preview_labels = Global_Classes_Labels::make( $this->get_kit() )->set_preview( true );
401 - $labels_map = $preview_labels->get_labels();
402 -
403 - if ( empty( $labels_map ) ) {
404 - return;
405 - }
406 -
407 - $ids_to_remove = array_intersect( array_unique( $class_ids ), array_keys( $labels_map ) );
408 -
409 - if ( empty( $ids_to_remove ) ) {
410 - return;
411 - }
412 -
413 - foreach ( $ids_to_remove as $id ) {
414 - unset( $labels_map[ $id ] );
415 - }
416 -
417 - $preview_labels->set_labels( $labels_map );
418 - }
419 -
420 - private function bulk_clear_preview_meta( array $class_ids ): void {
421 - if ( empty( $class_ids ) ) {
422 - return;
423 - }
424 -
425 - $post_ids_map = Global_Classes_Post_IDs::make( $this->get_kit() );
426 -
427 - foreach ( array_chunk( $class_ids, self::PERSIST_BATCH_SIZE ) as $chunk ) {
428 - $post_ids = $post_ids_map->get_post_ids( $chunk );
429 -
430 - foreach ( $post_ids as $post_id ) {
431 - delete_post_meta( $post_id, Global_Class_Post::META_KEY_DATA_PREVIEW );
432 - clean_post_cache( $post_id );
433 - }
434 -
435 - $this->flush_runtime_cache();
436 - }
437 383 }
438 384
439 385 private function each_class_id_batch( $class_ids, callable $callback, int $batch_size = self::PERSIST_BATCH_SIZE ): void {
440 386 $class_ids = is_array( $class_ids ) ? $class_ids : iterator_to_array( $class_ids, false );