| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\DesignSystemSync\Classes; |
| 4 |
|
| 5 |
use Elementor\Core\Kits\Documents\Kit; |
| 6 |
use Elementor\Modules\GlobalClasses\Concerns\Has_Kit_Dependency; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Global_Classes_Sync_Map { |
| 13 |
use Has_Kit_Dependency; |
| 14 |
|
| 15 |
const META_KEY = '_elementor_global_classes_sync_to_v3'; |
| 16 |
|
| 17 |
private ?array $cache = null; |
| 18 |
|
| 19 |
private function __construct() { |
| 20 |
} |
| 21 |
|
| 22 |
public static function make( ?Kit $kit = null ): self { |
| 23 |
$instance = new self(); |
| 24 |
|
| 25 |
if ( ! $kit ) { |
| 26 |
return $instance; |
| 27 |
} |
| 28 |
|
| 29 |
return $instance->set_kit( $kit ); |
| 30 |
} |
| 31 |
|
| 32 |
public function get_synced_ids(): array { |
| 33 |
return array_keys( $this->read_stored() ); |
| 34 |
} |
| 35 |
|
| 36 |
public function is_synced( string $id ): bool { |
| 37 |
return isset( $this->read_stored()[ $id ] ); |
| 38 |
} |
| 39 |
|
| 40 |
public function set_map( array $id_to_true ): bool { |
| 41 |
$kit = $this->get_kit(); |
| 42 |
|
| 43 |
if ( ! $kit ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
$result = $kit->update_meta( self::META_KEY, $id_to_true ); |
| 48 |
$this->cache = $id_to_true; |
| 49 |
|
| 50 |
return false !== $result; |
| 51 |
} |
| 52 |
|
| 53 |
public function apply_changes( array $touched_items, array $deleted_ids ): bool { |
| 54 |
$current = $this->read_stored(); |
| 55 |
|
| 56 |
foreach ( $deleted_ids as $id ) { |
| 57 |
unset( $current[ $id ] ); |
| 58 |
} |
| 59 |
|
| 60 |
foreach ( $touched_items as $id => $item ) { |
| 61 |
if ( ! empty( $item['sync_to_v3'] ) && (bool) $item['sync_to_v3'] ) { |
| 62 |
$current[ $id ] = true; |
| 63 |
} else { |
| 64 |
unset( $current[ $id ] ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return $this->set_map( $current ); |
| 69 |
} |
| 70 |
|
| 71 |
private function read_stored(): array { |
| 72 |
if ( null !== $this->cache ) { |
| 73 |
return $this->cache; |
| 74 |
} |
| 75 |
|
| 76 |
$kit = $this->get_kit(); |
| 77 |
|
| 78 |
if ( ! $kit ) { |
| 79 |
$this->cache = []; |
| 80 |
|
| 81 |
return []; |
| 82 |
} |
| 83 |
|
| 84 |
$raw = $kit->get_meta( self::META_KEY ); |
| 85 |
$this->cache = is_array( $raw ) ? $raw : []; |
| 86 |
|
| 87 |
return $this->cache; |
| 88 |
} |
| 89 |
} |
| 90 |
|