| 1 |
<?php |
| 2 |
namespace Elementor\Modules\DefaultValues\Data; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
use Elementor\Core\Utils\Collection; |
| 6 |
use Elementor\Core\Kits\Documents\Kit; |
| 7 |
use Elementor\Modules\DefaultValues\Module; |
| 8 |
use Elementor\Core\Kits\Exceptions\Kit_Not_Exists; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
class Repository { |
| 15 |
const DEFAULT_VALUES_META_KEY = '_elementor_default_values'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var static[] |
| 19 |
*/ |
| 20 |
private static $instances = []; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var Kit |
| 24 |
*/ |
| 25 |
private $kit; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var Collection|null |
| 29 |
*/ |
| 30 |
private $data = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* @param Kit|null $kit |
| 34 |
* |
| 35 |
* @return static |
| 36 |
* @throws Kit_Not_Exists |
| 37 |
*/ |
| 38 |
public static function instance( Kit $kit = null ) { |
| 39 |
$kit = $kit ? $kit : Plugin::$instance->kits_manager->get_active_kit(); |
| 40 |
|
| 41 |
if ( ! $kit->get_id() ) { |
| 42 |
throw new Kit_Not_Exists(); |
| 43 |
} |
| 44 |
|
| 45 |
if ( ! isset( static::$instances[ $kit->get_id() ] ) ) { |
| 46 |
static::$instances[ $kit->get_id() ] = new static( $kit ); |
| 47 |
} |
| 48 |
|
| 49 |
return static::$instances[ $kit->get_id() ]; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get all the defaults. |
| 54 |
* |
| 55 |
* @return Collection |
| 56 |
*/ |
| 57 |
public function all() { |
| 58 |
if ( ! $this->data ) { |
| 59 |
$this->refresh(); |
| 60 |
} |
| 61 |
|
| 62 |
return $this->data; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get specific default |
| 67 |
* |
| 68 |
* @param $type |
| 69 |
* |
| 70 |
* @return array |
| 71 |
* @throws \Exception |
| 72 |
*/ |
| 73 |
public function get( $type ) { |
| 74 |
$data = $this->all(); |
| 75 |
|
| 76 |
return $data->get( $type, [ |
| 77 |
'settings' => [], |
| 78 |
'elements' => [], |
| 79 |
] ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param $type |
| 84 |
* @param array $settings |
| 85 |
* |
| 86 |
* @return array |
| 87 |
* @throws \Exception |
| 88 |
*/ |
| 89 |
public function store( $type, array $settings ) { |
| 90 |
$data = $this->all(); |
| 91 |
|
| 92 |
$data[ $type ] = [ |
| 93 |
'settings' => $settings, |
| 94 |
'elements' => [], |
| 95 |
]; |
| 96 |
|
| 97 |
return $this->save( $data )->get( $type ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @param $type |
| 102 |
* |
| 103 |
* @return array |
| 104 |
* @throws \Exception |
| 105 |
*/ |
| 106 |
public function delete( $type ) { |
| 107 |
$data = $this->all(); |
| 108 |
|
| 109 |
unset( $data[ $type ] ); |
| 110 |
|
| 111 |
return $this->save( $data )->get( $type ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Make sure the data is loaded. |
| 116 |
* |
| 117 |
* @return $this |
| 118 |
*/ |
| 119 |
public function refresh() { |
| 120 |
$this->data = new Collection( |
| 121 |
$this->kit->get_json_meta( static::DEFAULT_VALUES_META_KEY ) |
| 122 |
); |
| 123 |
|
| 124 |
return $this; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* @param Collection $data |
| 129 |
* |
| 130 |
* @return $this |
| 131 |
*/ |
| 132 |
private function save( Collection $data ) { |
| 133 |
$this->kit->update_meta( static::DEFAULT_VALUES_META_KEY, wp_json_encode( $data->all() ) ); |
| 134 |
|
| 135 |
$this->refresh(); |
| 136 |
|
| 137 |
return $this; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Repository constructor. |
| 142 |
* |
| 143 |
* @param Kit $kit |
| 144 |
*/ |
| 145 |
public function __construct( Kit $kit ) { |
| 146 |
$this->kit = $kit; |
| 147 |
} |
| 148 |
} |
| 149 |
|