| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\Variables\Storage; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Core\Kits\Documents\Kit; |
| 6 | -use Elementor\Modules\AtomicWidgets\Utils\Utils; | |
| 6 | +use Elementor\Modules\AtomicWidgets\Utils; | |
| 7 | 7 | use Elementor\Modules\Variables\Storage\Exceptions\DuplicatedLabel; |
| 8 | 8 | use Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound; |
| 9 | 9 | use Elementor\Modules\Variables\Storage\Exceptions\VariablesLimitReached; |
| 10 | 10 | use Elementor\Modules\Variables\Storage\Exceptions\FatalError; |
| @@ -15,8 +15,11 @@ | ||
| 15 | 15 | exit; // Exit if accessed directly. |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | class Repository { |
| 19 | + const TOTAL_VARIABLES_COUNT = 100; | |
| 20 | + const FORMAT_VERSION_V1 = 1; | |
| 21 | + const VARIABLES_META_KEY = '_elementor_global_variables'; | |
| 19 | 22 | private Kit $kit; |
| 20 | 23 | |
| 21 | 24 | public function __construct( Kit $kit ) { |
| 22 | 25 | $this->kit = $kit; |
| @@ -22,9 +25,9 @@ | ||
| 22 | 25 | $this->kit = $kit; |
| 23 | 26 | } |
| 24 | 27 | |
| 25 | 28 | /** |
| 26 | - * @throws VariablesLimitReached If database connection fails or query execution errors occur. | |
| 29 | + * @throws VariablesLimitReached | |
| 27 | 30 | */ |
| 28 | 31 | private function assert_if_variables_limit_reached( array $db_record ) { |
| 29 | 32 | $variables_in_use = 0; |
| 30 | 33 | |
| @@ -35,15 +38,15 @@ | ||
| 35 | 38 | |
| 36 | 39 | ++$variables_in_use; |
| 37 | 40 | } |
| 38 | 41 | |
| 39 | - if ( Constants::TOTAL_VARIABLES_COUNT < $variables_in_use ) { | |
| 42 | + if ( self::TOTAL_VARIABLES_COUNT < $variables_in_use ) { | |
| 40 | 43 | throw new VariablesLimitReached( 'Total variables count limit reached' ); |
| 41 | 44 | } |
| 42 | 45 | } |
| 43 | 46 | |
| 44 | 47 | /** |
| 45 | - * @throws DuplicatedLabel If variable creation fails or validation errors occur. | |
| 48 | + * @throws DuplicatedLabel | |
| 46 | 49 | */ |
| 47 | 50 | private function assert_if_variable_label_is_duplicated( array $db_record, array $variable = [] ) { |
| 48 | 51 | foreach ( $db_record['data'] as $id => $existing_variable ) { |
| 49 | 52 | if ( isset( $existing_variable['deleted'] ) && $existing_variable['deleted'] ) { |
| @@ -70,9 +73,9 @@ | ||
| 70 | 73 | return $db_record['data'] ?? []; |
| 71 | 74 | } |
| 72 | 75 | |
| 73 | 76 | public function load(): array { |
| 74 | - $db_record = $this->kit->get_json_meta( Constants::VARIABLES_META_KEY ); | |
| 77 | + $db_record = $this->kit->get_json_meta( static::VARIABLES_META_KEY ); | |
| 75 | 78 | |
| 76 | 79 | if ( is_array( $db_record ) && ! empty( $db_record ) ) { |
| 77 | 80 | return $db_record; |
| 78 | 81 | } |
| @@ -80,9 +83,9 @@ | ||
| 80 | 83 | return $this->get_default_meta(); |
| 81 | 84 | } |
| 82 | 85 | |
| 83 | 86 | /** |
| 84 | - * @throws FatalError If variable update fails or validation errors occur. | |
| 87 | + * @throws FatalError | |
| 85 | 88 | */ |
| 86 | 89 | public function create( array $variable ) { |
| 87 | 90 | $db_record = $this->load(); |
| 88 | 91 | |
| @@ -92,15 +95,10 @@ | ||
| 92 | 95 | $new_variable = $this->extract_from( $variable, [ |
| 93 | 96 | 'type', |
| 94 | 97 | 'label', |
| 95 | 98 | 'value', |
| 96 | - 'order', | |
| 97 | 99 | ] ); |
| 98 | 100 | |
| 99 | - if ( ! isset( $new_variable['order'] ) ) { | |
| 100 | - $new_variable['order'] = $this->get_next_order( $list_of_variables ); | |
| 101 | - } | |
| 102 | - | |
| 103 | 101 | $this->assert_if_variable_label_is_duplicated( $db_record, $new_variable ); |
| 104 | 102 | |
| 105 | 103 | $list_of_variables[ $id ] = $new_variable; |
| 106 | 104 | $db_record['data'] = $list_of_variables; |
| @@ -119,10 +117,10 @@ | ||
| 119 | 117 | ]; |
| 120 | 118 | } |
| 121 | 119 | |
| 122 | 120 | /** |
| 123 | - * @throws RecordNotFound If variable deletion fails or database errors occur. | |
| 124 | - * @throws FatalError If variable deletion fails or database errors occur. | |
| 121 | + * @throws RecordNotFound | |
| 122 | + * @throws FatalError | |
| 125 | 123 | */ |
| 126 | 124 | public function update( string $id, array $variable ) { |
| 127 | 125 | $db_record = $this->load(); |
| 128 | 126 | |
| @@ -134,9 +132,8 @@ | ||
| 134 | 132 | |
| 135 | 133 | $updated_variable = array_merge( $list_of_variables[ $id ], $this->extract_from( $variable, [ |
| 136 | 134 | 'label', |
| 137 | 135 | 'value', |
| 138 | - 'order', | |
| 139 | 136 | ] ) ); |
| 140 | 137 | |
| 141 | 138 | $this->assert_if_variable_label_is_duplicated( $db_record, array_merge( $updated_variable, [ 'id' => $id ] ) ); |
| 142 | 139 | |
| @@ -155,10 +152,10 @@ | ||
| 155 | 152 | ]; |
| 156 | 153 | } |
| 157 | 154 | |
| 158 | 155 | /** |
| 159 | - * @throws RecordNotFound If bulk operation fails or validation errors occur. | |
| 160 | - * @throws FatalError If bulk operation fails or validation errors occur. | |
| 156 | + * @throws RecordNotFound | |
| 157 | + * @throws FatalError | |
| 161 | 158 | */ |
| 162 | 159 | public function delete( string $id ) { |
| 163 | 160 | $db_record = $this->load(); |
| 164 | 161 | |
| @@ -185,10 +182,10 @@ | ||
| 185 | 182 | ]; |
| 186 | 183 | } |
| 187 | 184 | |
| 188 | 185 | /** |
| 189 | - * @throws RecordNotFound If export operation fails or data serialization errors occur. | |
| 190 | - * @throws FatalError If export operation fails or data serialization errors occur. | |
| 186 | + * @throws RecordNotFound | |
| 187 | + * @throws FatalError | |
| 191 | 188 | */ |
| 192 | 189 | public function restore( string $id, $overrides = [] ) { |
| 193 | 190 | $db_record = $this->load(); |
| 194 | 191 | |
| @@ -201,9 +198,8 @@ | ||
| 201 | 198 | $restored_variable = $this->extract_from( $list_of_variables[ $id ], [ |
| 202 | 199 | 'label', |
| 203 | 200 | 'value', |
| 204 | 201 | 'type', |
| 205 | - 'order', | |
| 206 | 202 | ] ); |
| 207 | 203 | |
| 208 | 204 | if ( array_key_exists( 'label', $overrides ) ) { |
| 209 | 205 | $restored_variable['label'] = $overrides['label']; |
| @@ -234,10 +230,10 @@ | ||
| 234 | 230 | |
| 235 | 231 | /** |
| 236 | 232 | * Process multiple operations atomically |
| 237 | 233 | * |
| 238 | - * @throws BatchOperationFailed If batch operation fails or validation errors occur. | |
| 239 | - * @throws FatalError If batch operation fails or validation errors occur. | |
| 234 | + * @throws BatchOperationFailed | |
| 235 | + * @throws FatalError | |
| 240 | 236 | */ |
| 241 | 237 | public function process_atomic_batch( array $operations, int $expected_watermark ): array { |
| 242 | 238 | $db_record = $this->load(); |
| 243 | 239 | $results = []; |
| @@ -250,9 +246,8 @@ | ||
| 250 | 246 | } catch ( Exception $e ) { |
| 251 | 247 | $operation_id = $this->get_operation_identifier( $operation, $index ); |
| 252 | 248 | $errors[ $operation_id ] = [ |
| 253 | 249 | 'status' => $this->get_error_status_code( $e ), |
| 254 | - 'code' => $this->get_error_code( $e ), | |
| 255 | 250 | 'message' => $e->getMessage(), |
| 256 | 251 | ]; |
| 257 | 252 | } |
| 258 | 253 | } |
| @@ -262,9 +257,8 @@ | ||
| 262 | 257 | |
| 263 | 258 | foreach ( $errors as $operation_id => $error ) { |
| 264 | 259 | $error_details[ esc_html( $operation_id ) ] = [ |
| 265 | 260 | 'status' => (int) $error['status'], |
| 266 | - 'code' => $error['code'], | |
| 267 | 261 | 'message' => esc_html( $error['message'] ), |
| 268 | 262 | ]; |
| 269 | 263 | } |
| 270 | 264 | |
| @@ -307,14 +301,10 @@ | ||
| 307 | 301 | private function process_create_operation( array &$db_record, array $operation ): array { |
| 308 | 302 | $variable_data = $operation['variable']; |
| 309 | 303 | |
| 310 | 304 | $temp_id = $variable_data['id'] ?? null; |
| 311 | - $new_variable = $this->extract_from( $variable_data, [ 'type', 'label', 'value', 'order' ] ); | |
| 305 | + $new_variable = $this->extract_from( $variable_data, [ 'type', 'label', 'value' ] ); | |
| 312 | 306 | |
| 313 | - if ( ! isset( $new_variable['order'] ) ) { | |
| 314 | - $new_variable['order'] = $this->get_next_order( $db_record['data'] ); | |
| 315 | - } | |
| 316 | - | |
| 317 | 307 | $this->assert_if_variable_label_is_duplicated( $db_record, $new_variable ); |
| 318 | 308 | |
| 319 | 309 | $this->assert_if_variables_limit_reached( $db_record ); |
| 320 | 310 | |
| @@ -327,9 +317,8 @@ | ||
| 327 | 317 | $db_record['data'][ $id ] = $new_variable; |
| 328 | 318 | |
| 329 | 319 | return [ |
| 330 | 320 | 'id' => $id, |
| 331 | - 'type' => 'create', | |
| 332 | 321 | 'variable' => array_merge( [ 'id' => $id ], $new_variable ), |
| 333 | 322 | 'temp_id' => $temp_id, |
| 334 | 323 | ]; |
| 335 | 324 | } |
| @@ -338,12 +327,12 @@ | ||
| 338 | 327 | $id = $operation['id']; |
| 339 | 328 | $variable_data = $operation['variable']; |
| 340 | 329 | |
| 341 | 330 | if ( ! isset( $db_record['data'][ $id ] ) ) { |
| 342 | - throw new RecordNotFound( 'Variable not found' ); | |
| 331 | + throw new \Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound( 'Variable not found' ); | |
| 343 | 332 | } |
| 344 | 333 | |
| 345 | - $updated_fields = $this->extract_from( $variable_data, [ 'label', 'value', 'order' ] ); | |
| 334 | + $updated_fields = $this->extract_from( $variable_data, [ 'label', 'value' ] ); | |
| 346 | 335 | $updated_variable = array_merge( $db_record['data'][ $id ], $updated_fields ); |
| 347 | 336 | $updated_variable['updated_at'] = $this->now(); |
| 348 | 337 | |
| 349 | 338 | $this->assert_if_variable_label_is_duplicated( $db_record, array_merge( $updated_variable, [ 'id' => $id ] ) ); |
| @@ -351,9 +340,8 @@ | ||
| 351 | 340 | $db_record['data'][ $id ] = $updated_variable; |
| 352 | 341 | |
| 353 | 342 | return [ |
| 354 | 343 | 'id' => $id, |
| 355 | - 'type' => 'update', | |
| 356 | 344 | 'variable' => array_merge( [ 'id' => $id ], $updated_variable ), |
| 357 | 345 | ]; |
| 358 | 346 | } |
| 359 | 347 | |
| @@ -368,9 +356,8 @@ | ||
| 368 | 356 | $db_record['data'][ $id ]['deleted_at'] = $this->now(); |
| 369 | 357 | |
| 370 | 358 | return [ |
| 371 | 359 | 'id' => $id, |
| 372 | - 'type' => 'delete', | |
| 373 | 360 | 'deleted' => true, |
| 374 | 361 | ]; |
| 375 | 362 | } |
| 376 | 363 | |
| @@ -402,9 +389,8 @@ | ||
| 402 | 389 | $db_record['data'][ $id ] = $restored_variable; |
| 403 | 390 | |
| 404 | 391 | return [ |
| 405 | 392 | 'id' => $id, |
| 406 | - 'type' => 'restore', | |
| 407 | 393 | 'variable' => array_merge( [ 'id' => $id ], $restored_variable ), |
| 408 | 394 | ]; |
| 409 | 395 | } |
| 410 | 396 | |
| @@ -424,9 +410,10 @@ | ||
| 424 | 410 | if ( $e instanceof RecordNotFound ) { |
| 425 | 411 | return 404; |
| 426 | 412 | } |
| 427 | 413 | |
| 428 | - if ( $e instanceof DuplicatedLabel || $e instanceof VariablesLimitReached ) { | |
| 414 | + if ( $e instanceof DuplicatedLabel || | |
| 415 | + $e instanceof VariablesLimitReached ) { | |
| 429 | 416 | return 400; |
| 430 | 417 | } |
| 431 | 418 | |
| 432 | 419 | return 500; |
| @@ -431,24 +418,8 @@ | ||
| 431 | 418 | |
| 432 | 419 | return 500; |
| 433 | 420 | } |
| 434 | 421 | |
| 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 | 422 | private function save( array $db_record ) { |
| 452 | 423 | if ( PHP_INT_MAX === $db_record['watermark'] ) { |
| 453 | 424 | $db_record['watermark'] = 0; |
| 454 | 425 | } |
| @@ -454,9 +425,9 @@ | ||
| 454 | 425 | } |
| 455 | 426 | |
| 456 | 427 | ++$db_record['watermark']; |
| 457 | 428 | |
| 458 | - if ( $this->kit->update_json_meta( Constants::VARIABLES_META_KEY, $db_record ) ) { | |
| 429 | + if ( $this->kit->update_json_meta( static::VARIABLES_META_KEY, $db_record ) ) { | |
| 459 | 430 | return $db_record['watermark']; |
| 460 | 431 | } |
| 461 | 432 | |
| 462 | 433 | return false; |
| @@ -477,24 +448,8 @@ | ||
| 477 | 448 | private function get_default_meta(): array { |
| 478 | 449 | return [ |
| 479 | 450 | 'data' => [], |
| 480 | 451 | 'watermark' => 0, |
| 481 | - 'version' => Constants::FORMAT_VERSION_V1, | |
| 452 | + 'version' => self::FORMAT_VERSION_V1, | |
| 482 | 453 | ]; |
| 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 | 454 | } |
| 500 | 455 | } |