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