| 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\DuplicatedLabel; |
| 8 |
use Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound; |
| 9 |
use Elementor\Modules\Variables\Storage\Exceptions\VariablesLimitReached; |
| 10 |
use Elementor\Modules\Variables\Storage\Exceptions\FatalError; |
| 11 |
use Elementor\Modules\Variables\Storage\Exceptions\BatchOperationFailed; |
| 12 |
use Exception; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
class Repository { |
| 19 |
const TOTAL_VARIABLES_COUNT = 100; |
| 20 |
const FORMAT_VERSION_V1 = 1; |
| 21 |
const VARIABLES_META_KEY = '_elementor_global_variables'; |
| 22 |
private Kit $kit; |
| 23 |
|
| 24 |
public function __construct( Kit $kit ) { |
| 25 |
$this->kit = $kit; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* @throws VariablesLimitReached |
| 30 |
*/ |
| 31 |
private function assert_if_variables_limit_reached( array $db_record ) { |
| 32 |
$variables_in_use = 0; |
| 33 |
|
| 34 |
foreach ( $db_record['data'] as $variable ) { |
| 35 |
if ( isset( $variable['deleted'] ) && $variable['deleted'] ) { |
| 36 |
continue; |
| 37 |
} |
| 38 |
|
| 39 |
++$variables_in_use; |
| 40 |
} |
| 41 |
|
| 42 |
if ( self::TOTAL_VARIABLES_COUNT < $variables_in_use ) { |
| 43 |
throw new VariablesLimitReached( 'Total variables count limit reached' ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @throws DuplicatedLabel |
| 49 |
*/ |
| 50 |
private function assert_if_variable_label_is_duplicated( array $db_record, array $variable = [] ) { |
| 51 |
foreach ( $db_record['data'] as $id => $existing_variable ) { |
| 52 |
if ( isset( $existing_variable['deleted'] ) && $existing_variable['deleted'] ) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
if ( isset( $variable['id'] ) && $variable['id'] === $id ) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! isset( $variable['label'] ) || ! isset( $existing_variable['label'] ) ) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
if ( strtolower( $existing_variable['label'] ) === strtolower( $variable['label'] ) ) { |
| 65 |
throw new DuplicatedLabel( 'Variable label already exists' ); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public function variables(): array { |
| 71 |
$db_record = $this->load(); |
| 72 |
|
| 73 |
return $db_record['data'] ?? []; |
| 74 |
} |
| 75 |
|
| 76 |
public function load(): array { |
| 77 |
$db_record = $this->kit->get_json_meta( static::VARIABLES_META_KEY ); |
| 78 |
|
| 79 |
if ( is_array( $db_record ) && ! empty( $db_record ) ) { |
| 80 |
return $db_record; |
| 81 |
} |
| 82 |
|
| 83 |
return $this->get_default_meta(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @throws FatalError |
| 88 |
*/ |
| 89 |
public function create( array $variable ) { |
| 90 |
$db_record = $this->load(); |
| 91 |
|
| 92 |
$list_of_variables = $db_record['data'] ?? []; |
| 93 |
|
| 94 |
$id = $this->new_id_for( $list_of_variables ); |
| 95 |
$new_variable = $this->extract_from( $variable, [ |
| 96 |
'type', |
| 97 |
'label', |
| 98 |
'value', |
| 99 |
] ); |
| 100 |
|
| 101 |
$this->assert_if_variable_label_is_duplicated( $db_record, $new_variable ); |
| 102 |
|
| 103 |
$list_of_variables[ $id ] = $new_variable; |
| 104 |
$db_record['data'] = $list_of_variables; |
| 105 |
|
| 106 |
$this->assert_if_variables_limit_reached( $db_record ); |
| 107 |
|
| 108 |
$watermark = $this->save( $db_record ); |
| 109 |
|
| 110 |
if ( false === $watermark ) { |
| 111 |
throw new FatalError( 'Failed to create 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 update( string $id, array $variable ) { |
| 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 |
$updated_variable = array_merge( $list_of_variables[ $id ], $this->extract_from( $variable, [ |
| 134 |
'label', |
| 135 |
'value', |
| 136 |
] ) ); |
| 137 |
|
| 138 |
$this->assert_if_variable_label_is_duplicated( $db_record, array_merge( $updated_variable, [ 'id' => $id ] ) ); |
| 139 |
|
| 140 |
$list_of_variables[ $id ] = $updated_variable; |
| 141 |
$db_record['data'] = $list_of_variables; |
| 142 |
|
| 143 |
$watermark = $this->save( $db_record ); |
| 144 |
|
| 145 |
if ( false === $watermark ) { |
| 146 |
throw new FatalError( 'Failed to update variable' ); |
| 147 |
} |
| 148 |
|
| 149 |
return [ |
| 150 |
'variable' => array_merge( [ 'id' => $id ], $list_of_variables[ $id ] ), |
| 151 |
'watermark' => $watermark, |
| 152 |
]; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @throws RecordNotFound |
| 157 |
* @throws FatalError |
| 158 |
*/ |
| 159 |
public function delete( string $id ) { |
| 160 |
$db_record = $this->load(); |
| 161 |
|
| 162 |
$list_of_variables = $db_record['data'] ?? []; |
| 163 |
|
| 164 |
if ( ! isset( $list_of_variables[ $id ] ) ) { |
| 165 |
throw new RecordNotFound( 'Variable not found' ); |
| 166 |
} |
| 167 |
|
| 168 |
$list_of_variables[ $id ]['deleted'] = true; |
| 169 |
$list_of_variables[ $id ]['deleted_at'] = $this->now(); |
| 170 |
|
| 171 |
$db_record['data'] = $list_of_variables; |
| 172 |
|
| 173 |
$watermark = $this->save( $db_record ); |
| 174 |
|
| 175 |
if ( false === $watermark ) { |
| 176 |
throw new FatalError( 'Failed to delete variable' ); |
| 177 |
} |
| 178 |
|
| 179 |
return [ |
| 180 |
'variable' => array_merge( [ 'id' => $id ], $list_of_variables[ $id ] ), |
| 181 |
'watermark' => $watermark, |
| 182 |
]; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @throws RecordNotFound |
| 187 |
* @throws FatalError |
| 188 |
*/ |
| 189 |
public function restore( string $id, $overrides = [] ) { |
| 190 |
$db_record = $this->load(); |
| 191 |
|
| 192 |
$list_of_variables = $db_record['data'] ?? []; |
| 193 |
|
| 194 |
if ( ! isset( $list_of_variables[ $id ] ) ) { |
| 195 |
throw new RecordNotFound( 'Variable not found' ); |
| 196 |
} |
| 197 |
|
| 198 |
$restored_variable = $this->extract_from( $list_of_variables[ $id ], [ |
| 199 |
'label', |
| 200 |
'value', |
| 201 |
'type', |
| 202 |
] ); |
| 203 |
|
| 204 |
if ( array_key_exists( 'label', $overrides ) ) { |
| 205 |
$restored_variable['label'] = $overrides['label']; |
| 206 |
} |
| 207 |
|
| 208 |
if ( array_key_exists( 'value', $overrides ) ) { |
| 209 |
$restored_variable['value'] = $overrides['value']; |
| 210 |
} |
| 211 |
|
| 212 |
$this->assert_if_variable_label_is_duplicated( $db_record, array_merge( $restored_variable, [ 'id' => $id ] ) ); |
| 213 |
|
| 214 |
$list_of_variables[ $id ] = $restored_variable; |
| 215 |
$db_record['data'] = $list_of_variables; |
| 216 |
|
| 217 |
$this->assert_if_variables_limit_reached( $db_record ); |
| 218 |
|
| 219 |
$watermark = $this->save( $db_record ); |
| 220 |
|
| 221 |
if ( false === $watermark ) { |
| 222 |
throw new FatalError( 'Failed to restore variable' ); |
| 223 |
} |
| 224 |
|
| 225 |
return [ |
| 226 |
'variable' => array_merge( [ 'id' => $id ], $restored_variable ), |
| 227 |
'watermark' => $watermark, |
| 228 |
]; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Process multiple operations atomically |
| 233 |
* |
| 234 |
* @throws BatchOperationFailed |
| 235 |
* @throws FatalError |
| 236 |
*/ |
| 237 |
public function process_atomic_batch( array $operations, int $expected_watermark ): array { |
| 238 |
$db_record = $this->load(); |
| 239 |
$results = []; |
| 240 |
$errors = []; |
| 241 |
|
| 242 |
foreach ( $operations as $index => $operation ) { |
| 243 |
try { |
| 244 |
$result = $this->process_single_operation( $db_record, $operation ); |
| 245 |
$results[] = $result; |
| 246 |
} catch ( Exception $e ) { |
| 247 |
$operation_id = $this->get_operation_identifier( $operation, $index ); |
| 248 |
$errors[ $operation_id ] = [ |
| 249 |
'status' => $this->get_error_status_code( $e ), |
| 250 |
'message' => $e->getMessage(), |
| 251 |
]; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if ( ! empty( $errors ) ) { |
| 256 |
$error_details = []; |
| 257 |
|
| 258 |
foreach ( $errors as $operation_id => $error ) { |
| 259 |
$error_details[ esc_html( $operation_id ) ] = [ |
| 260 |
'status' => (int) $error['status'], |
| 261 |
'message' => esc_html( $error['message'] ), |
| 262 |
]; |
| 263 |
} |
| 264 |
|
| 265 |
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 266 |
throw new BatchOperationFailed( 'Batch operation failed', $error_details ); |
| 267 |
} |
| 268 |
|
| 269 |
$watermark = $this->save( $db_record ); |
| 270 |
|
| 271 |
if ( false === $watermark ) { |
| 272 |
throw new FatalError( 'Failed to save batch operations' ); |
| 273 |
} |
| 274 |
|
| 275 |
return [ |
| 276 |
'success' => true, |
| 277 |
'watermark' => $watermark, |
| 278 |
'results' => $results, |
| 279 |
]; |
| 280 |
} |
| 281 |
|
| 282 |
private function process_single_operation( array &$db_record, array $operation ): array { |
| 283 |
switch ( $operation['type'] ) { |
| 284 |
case 'create': |
| 285 |
return $this->process_create_operation( $db_record, $operation ); |
| 286 |
|
| 287 |
case 'update': |
| 288 |
return $this->process_update_operation( $db_record, $operation ); |
| 289 |
|
| 290 |
case 'delete': |
| 291 |
return $this->process_delete_operation( $db_record, $operation ); |
| 292 |
|
| 293 |
case 'restore': |
| 294 |
return $this->process_restore_operation( $db_record, $operation ); |
| 295 |
|
| 296 |
default: |
| 297 |
throw new BatchOperationFailed( 'Invalid operation type: ' . esc_html( $operation['type'] ), [] ); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
private function process_create_operation( array &$db_record, array $operation ): array { |
| 302 |
$variable_data = $operation['variable']; |
| 303 |
|
| 304 |
$temp_id = $variable_data['id'] ?? null; |
| 305 |
$new_variable = $this->extract_from( $variable_data, [ 'type', 'label', 'value' ] ); |
| 306 |
|
| 307 |
$this->assert_if_variable_label_is_duplicated( $db_record, $new_variable ); |
| 308 |
|
| 309 |
$this->assert_if_variables_limit_reached( $db_record ); |
| 310 |
|
| 311 |
$id = $this->new_id_for( $db_record['data'] ); |
| 312 |
$now = $this->now(); |
| 313 |
|
| 314 |
$new_variable['created_at'] = $now; |
| 315 |
$new_variable['updated_at'] = $now; |
| 316 |
|
| 317 |
$db_record['data'][ $id ] = $new_variable; |
| 318 |
|
| 319 |
return [ |
| 320 |
'id' => $id, |
| 321 |
'variable' => array_merge( [ 'id' => $id ], $new_variable ), |
| 322 |
'temp_id' => $temp_id, |
| 323 |
]; |
| 324 |
} |
| 325 |
|
| 326 |
private function process_update_operation( array &$db_record, array $operation ): array { |
| 327 |
$id = $operation['id']; |
| 328 |
$variable_data = $operation['variable']; |
| 329 |
|
| 330 |
if ( ! isset( $db_record['data'][ $id ] ) ) { |
| 331 |
throw new \Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound( 'Variable not found' ); |
| 332 |
} |
| 333 |
|
| 334 |
$updated_fields = $this->extract_from( $variable_data, [ 'label', 'value' ] ); |
| 335 |
$updated_variable = array_merge( $db_record['data'][ $id ], $updated_fields ); |
| 336 |
$updated_variable['updated_at'] = $this->now(); |
| 337 |
|
| 338 |
$this->assert_if_variable_label_is_duplicated( $db_record, array_merge( $updated_variable, [ 'id' => $id ] ) ); |
| 339 |
|
| 340 |
$db_record['data'][ $id ] = $updated_variable; |
| 341 |
|
| 342 |
return [ |
| 343 |
'id' => $id, |
| 344 |
'variable' => array_merge( [ 'id' => $id ], $updated_variable ), |
| 345 |
]; |
| 346 |
} |
| 347 |
|
| 348 |
private function process_delete_operation( array &$db_record, array $operation ): array { |
| 349 |
$id = $operation['id']; |
| 350 |
|
| 351 |
if ( ! isset( $db_record['data'][ $id ] ) ) { |
| 352 |
throw new RecordNotFound( 'Variable not found' ); |
| 353 |
} |
| 354 |
|
| 355 |
$db_record['data'][ $id ]['deleted'] = true; |
| 356 |
$db_record['data'][ $id ]['deleted_at'] = $this->now(); |
| 357 |
|
| 358 |
return [ |
| 359 |
'id' => $id, |
| 360 |
'deleted' => true, |
| 361 |
]; |
| 362 |
} |
| 363 |
|
| 364 |
private function process_restore_operation( array &$db_record, array $operation ): array { |
| 365 |
$id = $operation['id']; |
| 366 |
|
| 367 |
if ( ! isset( $db_record['data'][ $id ] ) ) { |
| 368 |
throw new RecordNotFound( 'Variable not found' ); |
| 369 |
} |
| 370 |
|
| 371 |
$overrides = []; |
| 372 |
|
| 373 |
if ( isset( $operation['label'] ) ) { |
| 374 |
$overrides['label'] = $operation['label']; |
| 375 |
} |
| 376 |
|
| 377 |
if ( isset( $operation['value'] ) ) { |
| 378 |
$overrides['value'] = $operation['value']; |
| 379 |
} |
| 380 |
|
| 381 |
$restored_variable = $this->extract_from( $db_record['data'][ $id ], [ 'label', 'value', 'type' ] ); |
| 382 |
$restored_variable = array_merge( $restored_variable, $overrides ); |
| 383 |
$restored_variable['updated_at'] = $this->now(); |
| 384 |
|
| 385 |
$this->assert_if_variable_label_is_duplicated( $db_record, array_merge( $restored_variable, [ 'id' => $id ] ) ); |
| 386 |
|
| 387 |
$this->assert_if_variables_limit_reached( $db_record ); |
| 388 |
|
| 389 |
$db_record['data'][ $id ] = $restored_variable; |
| 390 |
|
| 391 |
return [ |
| 392 |
'id' => $id, |
| 393 |
'variable' => array_merge( [ 'id' => $id ], $restored_variable ), |
| 394 |
]; |
| 395 |
} |
| 396 |
|
| 397 |
private function get_operation_identifier( array $operation, int $index ): string { |
| 398 |
if ( 'create' === $operation['type'] && isset( $operation['variable']['id'] ) ) { |
| 399 |
return $operation['variable']['id']; |
| 400 |
} |
| 401 |
|
| 402 |
if ( isset( $operation['id'] ) ) { |
| 403 |
return $operation['id']; |
| 404 |
} |
| 405 |
|
| 406 |
return "operation_{$index}"; |
| 407 |
} |
| 408 |
|
| 409 |
private function get_error_status_code( Exception $e ): int { |
| 410 |
if ( $e instanceof RecordNotFound ) { |
| 411 |
return 404; |
| 412 |
} |
| 413 |
|
| 414 |
if ( $e instanceof DuplicatedLabel || |
| 415 |
$e instanceof VariablesLimitReached ) { |
| 416 |
return 400; |
| 417 |
} |
| 418 |
|
| 419 |
return 500; |
| 420 |
} |
| 421 |
|
| 422 |
private function save( array $db_record ) { |
| 423 |
if ( PHP_INT_MAX === $db_record['watermark'] ) { |
| 424 |
$db_record['watermark'] = 0; |
| 425 |
} |
| 426 |
|
| 427 |
++$db_record['watermark']; |
| 428 |
|
| 429 |
if ( $this->kit->update_json_meta( static::VARIABLES_META_KEY, $db_record ) ) { |
| 430 |
return $db_record['watermark']; |
| 431 |
} |
| 432 |
|
| 433 |
return false; |
| 434 |
} |
| 435 |
|
| 436 |
private function new_id_for( array $list_of_variables ): string { |
| 437 |
return Utils::generate_id( 'e-gv-', array_keys( $list_of_variables ) ); |
| 438 |
} |
| 439 |
|
| 440 |
private function now(): string { |
| 441 |
return gmdate( 'Y-m-d H:i:s' ); |
| 442 |
} |
| 443 |
|
| 444 |
private function extract_from( array $source, array $fields ): array { |
| 445 |
return array_intersect_key( $source, array_flip( $fields ) ); |
| 446 |
} |
| 447 |
|
| 448 |
private function get_default_meta(): array { |
| 449 |
return [ |
| 450 |
'data' => [], |
| 451 |
'watermark' => 0, |
| 452 |
'version' => self::FORMAT_VERSION_V1, |
| 453 |
]; |
| 454 |
} |
| 455 |
} |
| 456 |
|