| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Variables\Storage; |
| 4 |
|
| 5 |
use Elementor\Core\Kits\Documents\Kit; |
| 6 |
use Elementor\Modules\Variables\Adapters\Prop_Type_Adapter; |
| 7 |
|
| 8 |
class Variables_Repository { |
| 9 |
private const VARIABLES_META_KEY = '_elementor_global_variables'; |
| 10 |
|
| 11 |
private Kit $kit; |
| 12 |
|
| 13 |
public function __construct( Kit $kit ) { |
| 14 |
$this->kit = $kit; |
| 15 |
} |
| 16 |
|
| 17 |
public function load(): Variables_Collection { |
| 18 |
$db_record = $this->kit->get_json_meta( self::VARIABLES_META_KEY ); |
| 19 |
|
| 20 |
if ( is_array( $db_record ) && ! empty( $db_record ) ) { |
| 21 |
$collection = Variables_Collection::hydrate( $db_record ); |
| 22 |
|
| 23 |
Prop_Type_Adapter::from_storage( $collection ); |
| 24 |
|
| 25 |
return $collection; |
| 26 |
} |
| 27 |
|
| 28 |
return Variables_Collection::default(); |
| 29 |
} |
| 30 |
|
| 31 |
public function save( Variables_Collection $collection ) { |
| 32 |
$collection->increment_watermark(); |
| 33 |
|
| 34 |
$record = Prop_Type_Adapter::to_storage( $collection ); |
| 35 |
|
| 36 |
if ( $this->kit->update_json_meta( static::VARIABLES_META_KEY, $record ) ) { |
| 37 |
return $collection->watermark(); |
| 38 |
} |
| 39 |
|
| 40 |
return false; |
| 41 |
} |
| 42 |
} |
| 43 |
|