← All changes
|
modules/global-classes/global-classes-repository.php
+82
-124
4.1.1
→
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 | } |
| @@ -103,17 +102,13 @@ | ||
| 103 | 102 | if ( empty( $class_ids ) ) { |
| 104 | 103 | return []; |
| 105 | 104 | } |
| 106 | 105 | |
| 107 | - $post_ids = Global_Classes_Post_IDs::make( $this->get_kit() )->get_post_ids( $class_ids ); | |
| 108 | 106 | $items = []; |
| 109 | 107 | |
| 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 | - } | |
| 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; | |
| 116 | 111 | } |
| 117 | 112 | |
| 118 | 113 | return $items; |
| 119 | 114 | } |
| @@ -144,14 +139,15 @@ | ||
| 144 | 139 | |
| 145 | 140 | if ( ! $is_preview ) { |
| 146 | 141 | Global_Classes_Sync_Map::make( $this->get_kit() )->apply_changes( $touched_items, $to_delete ); |
| 147 | 142 | |
| 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 | - ) ); | |
| 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 | + } ); | |
| 154 | 150 | } |
| 155 | 151 | |
| 156 | 152 | $this->cache = null; |
| 157 | 153 | $this->flush_runtime_cache(); |
| @@ -247,14 +243,15 @@ | ||
| 247 | 243 | array_flip( $touched_ids ) |
| 248 | 244 | ); |
| 249 | 245 | Global_Classes_Sync_Map::make( $this->get_kit() )->apply_changes( $touched_items, array_values( $to_delete ) ); |
| 250 | 246 | |
| 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 | - ) ); | |
| 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 | + } ); | |
| 257 | 254 | } |
| 258 | 255 | } |
| 259 | 256 | |
| 260 | 257 | private function iterate_class_posts_for_ids( array $class_ids ): \Generator { |
| @@ -271,13 +268,28 @@ | ||
| 271 | 268 | ], |
| 272 | 269 | ], |
| 273 | 270 | ] ); |
| 274 | 271 | |
| 272 | + $seen_class_ids = []; | |
| 273 | + | |
| 275 | 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 | + | |
| 276 | 288 | yield Global_Class_Post::from_post( $post, $this->is_preview() ); |
| 277 | 289 | clean_post_cache( $post->ID ); |
| 278 | 290 | } |
| 279 | - unset( $posts ); | |
| 291 | + unset( $posts, $seen_class_ids ); | |
| 280 | 292 | $this->flush_runtime_cache(); |
| 281 | 293 | } |
| 282 | 294 | } |
| 283 | 295 | |
| @@ -288,28 +300,13 @@ | ||
| 288 | 300 | array $items_by_id, |
| 289 | 301 | bool $is_preview |
| 290 | 302 | ): void { |
| 291 | 303 | $relations = new Global_Classes_Relations(); |
| 292 | - $post_ids_map = Global_Classes_Post_IDs::make( $this->get_kit() ); | |
| 293 | 304 | |
| 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 ); | |
| 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 ); | |
| 300 | 307 | |
| 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 | - | |
| 308 | + if ( $post ) { | |
| 312 | 309 | if ( $is_preview ) { |
| 313 | 310 | $post->set_preview( true ); |
| 314 | 311 | $post->update_data( [] ); |
| 315 | 312 | clean_post_cache( $post->get_post_id() ); |
| @@ -317,65 +314,57 @@ | ||
| 317 | 314 | $relations->clear_class_relations( $class_id ); |
| 318 | 315 | $post->delete(); |
| 319 | 316 | } |
| 320 | 317 | } |
| 321 | - ); | |
| 318 | + } ); | |
| 322 | 319 | |
| 323 | - $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 ) { | |
| 324 | 321 | if ( ! isset( $items_by_id[ $class_id ] ) ) { |
| 325 | 322 | return; |
| 326 | 323 | } |
| 327 | 324 | |
| 328 | 325 | $item = $items_by_id[ $class_id ]; |
| 329 | - $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; | |
| 326 | + $data = $this->build_class_data_for_storage( $item ); | |
| 333 | 327 | |
| 334 | - if ( $existing_post ) { | |
| 335 | - $existing_post->update_data( $data ); | |
| 328 | + if ( $is_preview ) { | |
| 329 | + $post = Global_Class_Post::find_by_class_id( $class_id, true ); | |
| 336 | 330 | |
| 337 | - if ( ! $is_preview ) { | |
| 338 | - $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 | + } | |
| 339 | 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 | + } ); | |
| 340 | 349 | |
| 341 | - clean_post_cache( $existing_post->get_post_id() ); | |
| 342 | - | |
| 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 ] ) ) { | |
| 343 | 352 | return; |
| 344 | 353 | } |
| 345 | 354 | |
| 346 | - $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 ); | |
| 347 | 357 | |
| 348 | - if ( $created ) { | |
| 349 | - $post_ids_map->set( $class_id, $created->get_post_id() ); | |
| 350 | - clean_post_cache( $created->get_post_id() ); | |
| 358 | + if ( ! $post ) { | |
| 359 | + return; | |
| 351 | 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() ); | |
| 352 | 366 | } ); |
| 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 | 367 | } |
| 379 | 368 | |
| 380 | 369 | public function delete_all(): void { |
| 381 | 370 | $order = $this->get_order(); |
| @@ -391,52 +380,8 @@ | ||
| 391 | 380 | Global_Classes_Order::make( $this->get_kit() )->set_order( [] ); |
| 392 | 381 | $this->labels()->set_labels( [] ); |
| 393 | 382 | } |
| 394 | 383 | |
| 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 | - } | |
| 438 | - | |
| 439 | 384 | private function each_class_id_batch( $class_ids, callable $callback, int $batch_size = self::PERSIST_BATCH_SIZE ): void { |
| 440 | 385 | $class_ids = is_array( $class_ids ) ? $class_ids : iterator_to_array( $class_ids, false ); |
| 441 | 386 | foreach ( array_chunk( array_values( $class_ids ), $batch_size ) as $batch ) { |
| 442 | 387 | foreach ( $batch as $class_id ) { |
| @@ -449,6 +394,19 @@ | ||
| 449 | 394 | private function flush_runtime_cache(): void { |
| 450 | 395 | if ( function_exists( 'wp_cache_flush_runtime' ) ) { |
| 451 | 396 | wp_cache_flush_runtime(); |
| 452 | 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; | |
| 453 | 411 | } |
| 454 | 412 | } |