| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Variables\Storage; |
| 4 |
|
| 5 |
use Elementor\Core\Kits\Documents\Kit; |
| 6 |
use Elementor\Modules\AtomicWidgets\Utils; |
| 7 |
use Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound; |
| 8 |
use Elementor\Modules\Variables\Storage\Exceptions\VariablesLimitReached; |
| 9 |
use Elementor\Modules\Variables\Storage\Exceptions\FatalError; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
class Repository { |
| 16 |
const TOTAL_VARIABLES_COUNT = 100; |
| 17 |
|
| 18 |
const FORMAT_VERSION_V1 = 1; |
| 19 |
const VARIABLES_META_KEY = '_elementor_global_variables'; |
| 20 |
|
| 21 |
private Kit $kit; |
| 22 |
|
| 23 |
public function __construct( Kit $kit ) { |
| 24 |
$this->kit = $kit; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @throws VariablesLimitReached |
| 29 |
*/ |
| 30 |
private function assert_if_variables_limit_reached( array $db_record ) { |
| 31 |
$variables_in_use = 0; |
| 32 |
|
| 33 |
foreach ( $db_record['data'] as $variable ) { |
| 34 |
if ( isset( $variable['deleted'] ) && $variable['deleted'] ) { |
| 35 |
continue; |
| 36 |
} |
| 37 |
|
| 38 |
++$variables_in_use; |
| 39 |
} |
| 40 |
|
| 41 |
if ( self::TOTAL_VARIABLES_COUNT < $variables_in_use ) { |
| 42 |
throw new VariablesLimitReached( 'Total variables count limit reached' ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public function load(): array { |
| 47 |
$db_record = $this->kit->get_json_meta( static::VARIABLES_META_KEY ); |
| 48 |
|
| 49 |
if ( is_array( $db_record ) && ! empty( $db_record ) ) { |
| 50 |
return $db_record; |
| 51 |
} |
| 52 |
|
| 53 |
return $this->get_default_meta(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @throws FatalError |
| 58 |
*/ |
| 59 |
public function create( array $variable ) { |
| 60 |
$db_record = $this->load(); |
| 61 |
|
| 62 |
$list_of_variables = $db_record['data'] ?? []; |
| 63 |
|
| 64 |
$id = $this->new_id_for( $list_of_variables ); |
| 65 |
|
| 66 |
$list_of_variables[ $id ] = $this->extract_from( $variable, [ |
| 67 |
'type', |
| 68 |
'label', |
| 69 |
'value', |
| 70 |
] ); |
| 71 |
|
| 72 |
$db_record['data'] = $list_of_variables; |
| 73 |
|
| 74 |
$this->assert_if_variables_limit_reached( $db_record ); |
| 75 |
|
| 76 |
$watermark = $this->save( $db_record ); |
| 77 |
|
| 78 |
if ( false === $watermark ) { |
| 79 |
throw new FatalError( 'Failed to create variable' ); |
| 80 |
} |
| 81 |
|
| 82 |
return [ |
| 83 |
'variable' => array_merge( [ 'id' => $id ], $list_of_variables[ $id ] ), |
| 84 |
'watermark' => $watermark, |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @throws RecordNotFound |
| 90 |
* @throws FatalError |
| 91 |
*/ |
| 92 |
public function update( string $id, array $variable ) { |
| 93 |
$db_record = $this->load(); |
| 94 |
|
| 95 |
$list_of_variables = $db_record['data'] ?? []; |
| 96 |
|
| 97 |
if ( ! isset( $list_of_variables[ $id ] ) ) { |
| 98 |
throw new RecordNotFound( 'Variable not found' ); |
| 99 |
} |
| 100 |
|
| 101 |
$list_of_variables[ $id ] = array_merge( $list_of_variables[ $id ], $this->extract_from( $variable, [ |
| 102 |
'label', |
| 103 |
'value', |
| 104 |
] ) ); |
| 105 |
|
| 106 |
$db_record['data'] = $list_of_variables; |
| 107 |
|
| 108 |
$watermark = $this->save( $db_record ); |
| 109 |
|
| 110 |
if ( false === $watermark ) { |
| 111 |
throw new FatalError( 'Failed to update variable' ); |
| 112 |
} |
| 113 |
|
| 114 |
return [ |
| 115 |
'variable' => array_merge( [ 'id' => $id ], $list_of_variables[ $id ] ), |
| 116 |
'watermark' => $watermark, |
| 117 |
]; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @throws RecordNotFound |
| 122 |
* @throws FatalError |
| 123 |
*/ |
| 124 |
public function delete( string $id ) { |
| 125 |
$db_record = $this->load(); |
| 126 |
|
| 127 |
$list_of_variables = $db_record['data'] ?? []; |
| 128 |
|
| 129 |
if ( ! isset( $list_of_variables[ $id ] ) ) { |
| 130 |
throw new RecordNotFound( 'Variable not found' ); |
| 131 |
} |
| 132 |
|
| 133 |
$list_of_variables[ $id ]['deleted'] = true; |
| 134 |
$list_of_variables[ $id ]['deleted_at'] = $this->now(); |
| 135 |
|
| 136 |
$db_record['data'] = $list_of_variables; |
| 137 |
|
| 138 |
$watermark = $this->save( $db_record ); |
| 139 |
|
| 140 |
if ( false === $watermark ) { |
| 141 |
throw new FatalError( 'Failed to delete variable' ); |
| 142 |
} |
| 143 |
|
| 144 |
return [ |
| 145 |
'variable' => array_merge( [ 'id' => $id ], $list_of_variables[ $id ] ), |
| 146 |
'watermark' => $watermark, |
| 147 |
]; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @throws RecordNotFound |
| 152 |
* @throws FatalError |
| 153 |
*/ |
| 154 |
public function restore( string $id ) { |
| 155 |
$db_record = $this->load(); |
| 156 |
|
| 157 |
$list_of_variables = $db_record['data'] ?? []; |
| 158 |
|
| 159 |
if ( ! isset( $list_of_variables[ $id ] ) ) { |
| 160 |
throw new RecordNotFound( 'Variable not found' ); |
| 161 |
} |
| 162 |
|
| 163 |
$list_of_variables[ $id ] = $this->extract_from( $list_of_variables[ $id ], [ |
| 164 |
'label', |
| 165 |
'value', |
| 166 |
'type', |
| 167 |
] ); |
| 168 |
|
| 169 |
$db_record['data'] = $list_of_variables; |
| 170 |
|
| 171 |
$this->assert_if_variables_limit_reached( $db_record ); |
| 172 |
|
| 173 |
$watermark = $this->save( $db_record ); |
| 174 |
|
| 175 |
if ( false === $watermark ) { |
| 176 |
throw new FatalError( 'Failed to restore variable' ); |
| 177 |
} |
| 178 |
|
| 179 |
return [ |
| 180 |
'variable' => array_merge( [ 'id' => $id ], $list_of_variables[ $id ] ), |
| 181 |
'watermark' => $watermark, |
| 182 |
]; |
| 183 |
} |
| 184 |
|
| 185 |
private function save( array $db_record ) { |
| 186 |
if ( PHP_INT_MAX === $db_record['watermark'] ) { |
| 187 |
$db_record['watermark'] = 0; |
| 188 |
} |
| 189 |
|
| 190 |
++$db_record['watermark']; |
| 191 |
|
| 192 |
if ( $this->kit->update_json_meta( static::VARIABLES_META_KEY, $db_record ) ) { |
| 193 |
return $db_record['watermark']; |
| 194 |
} |
| 195 |
|
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
private function new_id_for( array $list_of_variables ): string { |
| 200 |
return Utils::generate_id( 'e-gv-', array_keys( $list_of_variables ) ); |
| 201 |
} |
| 202 |
|
| 203 |
private function now(): string { |
| 204 |
return gmdate( 'Y-m-d H:i:s' ); |
| 205 |
} |
| 206 |
|
| 207 |
private function extract_from( array $source, array $fields ): array { |
| 208 |
return array_intersect_key( $source, array_flip( $fields ) ); |
| 209 |
} |
| 210 |
|
| 211 |
private function get_default_meta(): array { |
| 212 |
return [ |
| 213 |
'data' => [], |
| 214 |
'watermark' => 0, |
| 215 |
'version' => self::FORMAT_VERSION_V1, |
| 216 |
]; |
| 217 |
} |
| 218 |
} |
| 219 |
|