| @@ -2,15 +2,12 @@ | ||
| 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; | |
| 7 | -use Elementor\Modules\Variables\Storage\Exceptions\DuplicatedLabel; | |
| 6 | +use Elementor\Modules\AtomicWidgets\Utils; | |
| 8 | 7 | use Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound; |
| 9 | 8 | use Elementor\Modules\Variables\Storage\Exceptions\VariablesLimitReached; |
| 10 | 9 | use Elementor\Modules\Variables\Storage\Exceptions\FatalError; |
| 11 | -use Elementor\Modules\Variables\Storage\Exceptions\BatchOperationFailed; | |
| 12 | -use Exception; | |
| 13 | 10 | |
| 14 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | 12 | exit; // Exit if accessed directly. |
| 16 | 13 | } |
| @@ -15,8 +12,13 @@ | ||
| 15 | 12 | exit; // Exit if accessed directly. |
| 16 | 13 | } |
| 17 | 14 | |
| 18 | 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 | + | |
| 19 | 21 | private Kit $kit; |
| 20 | 22 | |
| 21 | 23 | public function __construct( Kit $kit ) { |
| 22 | 24 | $this->kit = $kit; |
| @@ -22,9 +24,9 @@ | ||
| 22 | 24 | $this->kit = $kit; |
| 23 | 25 | } |
| 24 | 26 | |
| 25 | 27 | /** |
| 26 | - * @throws VariablesLimitReached If database connection fails or query execution errors occur. | |
| 28 | + * @throws VariablesLimitReached | |
| 27 | 29 | */ |
| 28 | 30 | private function assert_if_variables_limit_reached( array $db_record ) { |
| 29 | 31 | $variables_in_use = 0; |
| 30 | 32 | |
| @@ -35,44 +37,15 @@ | ||
| 35 | 37 | |
| 36 | 38 | ++$variables_in_use; |
| 37 | 39 | } |
| 38 | 40 | |
| 39 | - if ( Constants::TOTAL_VARIABLES_COUNT < $variables_in_use ) { | |
| 41 | + if ( self::TOTAL_VARIABLES_COUNT < $variables_in_use ) { | |
| 40 | 42 | throw new VariablesLimitReached( 'Total variables count limit reached' ); |
| 41 | 43 | } |
| 42 | 44 | } |
| 43 | 45 | |
| 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 | 46 | public function load(): array { |
| 74 | - $db_record = $this->kit->get_json_meta( Constants::VARIABLES_META_KEY ); | |
| 47 | + $db_record = $this->kit->get_json_meta( static::VARIABLES_META_KEY ); | |
| 75 | 48 | |
| 76 | 49 | if ( is_array( $db_record ) && ! empty( $db_record ) ) { |
| 77 | 50 | return $db_record; |
| 78 | 51 | } |
| @@ -80,9 +53,9 @@ | ||
| 80 | 53 | return $this->get_default_meta(); |
| 81 | 54 | } |
| 82 | 55 | |
| 83 | 56 | /** |
| 84 | - * @throws FatalError If variable update fails or validation errors occur. | |
| 57 | + * @throws FatalError | |
| 85 | 58 | */ |
| 86 | 59 | public function create( array $variable ) { |
| 87 | 60 | $db_record = $this->load(); |
| 88 | 61 | |
| @@ -88,22 +61,15 @@ | ||
| 88 | 61 | |
| 89 | 62 | $list_of_variables = $db_record['data'] ?? []; |
| 90 | 63 | |
| 91 | 64 | $id = $this->new_id_for( $list_of_variables ); |
| 92 | - $new_variable = $this->extract_from( $variable, [ | |
| 65 | + | |
| 66 | + $list_of_variables[ $id ] = $this->extract_from( $variable, [ | |
| 93 | 67 | 'type', |
| 94 | 68 | 'label', |
| 95 | 69 | 'value', |
| 96 | - 'order', | |
| 97 | 70 | ] ); |
| 98 | 71 | |
| 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 | 72 | $db_record['data'] = $list_of_variables; |
| 107 | 73 | |
| 108 | 74 | $this->assert_if_variables_limit_reached( $db_record ); |
| 109 | 75 | |
| @@ -119,10 +85,10 @@ | ||
| 119 | 85 | ]; |
| 120 | 86 | } |
| 121 | 87 | |
| 122 | 88 | /** |
| 123 | - * @throws RecordNotFound If variable deletion fails or database errors occur. | |
| 124 | - * @throws FatalError If variable deletion fails or database errors occur. | |
| 89 | + * @throws RecordNotFound | |
| 90 | + * @throws FatalError | |
| 125 | 91 | */ |
| 126 | 92 | public function update( string $id, array $variable ) { |
| 127 | 93 | $db_record = $this->load(); |
| 128 | 94 | |
| @@ -131,17 +97,13 @@ | ||
| 131 | 97 | if ( ! isset( $list_of_variables[ $id ] ) ) { |
| 132 | 98 | throw new RecordNotFound( 'Variable not found' ); |
| 133 | 99 | } |
| 134 | 100 | |
| 135 | - $updated_variable = array_merge( $list_of_variables[ $id ], $this->extract_from( $variable, [ | |
| 101 | + $list_of_variables[ $id ] = array_merge( $list_of_variables[ $id ], $this->extract_from( $variable, [ | |
| 136 | 102 | 'label', |
| 137 | 103 | 'value', |
| 138 | - 'order', | |
| 139 | 104 | ] ) ); |
| 140 | 105 | |
| 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 | 106 | $db_record['data'] = $list_of_variables; |
| 145 | 107 | |
| 146 | 108 | $watermark = $this->save( $db_record ); |
| 147 | 109 | |
| @@ -155,10 +117,10 @@ | ||
| 155 | 117 | ]; |
| 156 | 118 | } |
| 157 | 119 | |
| 158 | 120 | /** |
| 159 | - * @throws RecordNotFound If bulk operation fails or validation errors occur. | |
| 160 | - * @throws FatalError If bulk operation fails or validation errors occur. | |
| 121 | + * @throws RecordNotFound | |
| 122 | + * @throws FatalError | |
| 161 | 123 | */ |
| 162 | 124 | public function delete( string $id ) { |
| 163 | 125 | $db_record = $this->load(); |
| 164 | 126 | |
| @@ -185,12 +147,12 @@ | ||
| 185 | 147 | ]; |
| 186 | 148 | } |
| 187 | 149 | |
| 188 | 150 | /** |
| 189 | - * @throws RecordNotFound If export operation fails or data serialization errors occur. | |
| 190 | - * @throws FatalError If export operation fails or data serialization errors occur. | |
| 151 | + * @throws RecordNotFound | |
| 152 | + * @throws FatalError | |
| 191 | 153 | */ |
| 192 | - public function restore( string $id, $overrides = [] ) { | |
| 154 | + public function restore( string $id ) { | |
| 193 | 155 | $db_record = $this->load(); |
| 194 | 156 | |
| 195 | 157 | $list_of_variables = $db_record['data'] ?? []; |
| 196 | 158 | |
| @@ -197,26 +159,14 @@ | ||
| 197 | 159 | if ( ! isset( $list_of_variables[ $id ] ) ) { |
| 198 | 160 | throw new RecordNotFound( 'Variable not found' ); |
| 199 | 161 | } |
| 200 | 162 | |
| 201 | - $restored_variable = $this->extract_from( $list_of_variables[ $id ], [ | |
| 163 | + $list_of_variables[ $id ] = $this->extract_from( $list_of_variables[ $id ], [ | |
| 202 | 164 | 'label', |
| 203 | 165 | 'value', |
| 204 | 166 | 'type', |
| 205 | - 'order', | |
| 206 | 167 | ] ); |
| 207 | 168 | |
| 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 | 169 | $db_record['data'] = $list_of_variables; |
| 220 | 170 | |
| 221 | 171 | $this->assert_if_variables_limit_reached( $db_record ); |
| 222 | 172 | |
| @@ -226,229 +176,13 @@ | ||
| 226 | 176 | throw new FatalError( 'Failed to restore variable' ); |
| 227 | 177 | } |
| 228 | 178 | |
| 229 | 179 | return [ |
| 230 | - 'variable' => array_merge( [ 'id' => $id ], $restored_variable ), | |
| 180 | + 'variable' => array_merge( [ 'id' => $id ], $list_of_variables[ $id ] ), | |
| 231 | 181 | 'watermark' => $watermark, |
| 232 | 182 | ]; |
| 233 | 183 | } |
| 234 | 184 | |
| 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 | 185 | private function save( array $db_record ) { |
| 452 | 186 | if ( PHP_INT_MAX === $db_record['watermark'] ) { |
| 453 | 187 | $db_record['watermark'] = 0; |
| 454 | 188 | } |
| @@ -454,9 +188,9 @@ | ||
| 454 | 188 | } |
| 455 | 189 | |
| 456 | 190 | ++$db_record['watermark']; |
| 457 | 191 | |
| 458 | - if ( $this->kit->update_json_meta( Constants::VARIABLES_META_KEY, $db_record ) ) { | |
| 192 | + if ( $this->kit->update_json_meta( static::VARIABLES_META_KEY, $db_record ) ) { | |
| 459 | 193 | return $db_record['watermark']; |
| 460 | 194 | } |
| 461 | 195 | |
| 462 | 196 | return false; |
| @@ -477,24 +211,8 @@ | ||
| 477 | 211 | private function get_default_meta(): array { |
| 478 | 212 | return [ |
| 479 | 213 | 'data' => [], |
| 480 | 214 | 'watermark' => 0, |
| 481 | - 'version' => Constants::FORMAT_VERSION_V1, | |
| 215 | + 'version' => self::FORMAT_VERSION_V1, | |
| 482 | 216 | ]; |
| 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 | 217 | } |
| 500 | 218 | } |