PluginProbe
Elementor Website Builder – more than just a page builder / 3.31.4
Elementor Website Builder – more than just a page builder v3.31.4
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
← All changes | modules/variables/storage/repository.php +20 -255 4.2.43.31.4 View file →
@@ -2,15 +2,14 @@
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 +use Elementor\Modules\Variables\Classes\Variables;
7 8 use Elementor\Modules\Variables\Storage\Exceptions\DuplicatedLabel;
8 9 use Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound;
9 10 use Elementor\Modules\Variables\Storage\Exceptions\VariablesLimitReached;
10 11 use Elementor\Modules\Variables\Storage\Exceptions\FatalError;
11 -use Elementor\Modules\Variables\Storage\Exceptions\BatchOperationFailed;
12 -use Exception;
13 12
14 13 if ( ! defined( 'ABSPATH' ) ) {
15 14 exit; // Exit if accessed directly.
16 15 }
@@ -15,8 +14,13 @@
15 14 exit; // Exit if accessed directly.
16 15 }
17 16
18 17 class Repository {
18 + const TOTAL_VARIABLES_COUNT = 100;
19 +
20 + const FORMAT_VERSION_V1 = 1;
21 + const VARIABLES_META_KEY = '_elementor_global_variables';
22 +
19 23 private Kit $kit;
20 24
21 25 public function __construct( Kit $kit ) {
22 26 $this->kit = $kit;
@@ -22,9 +26,9 @@
22 26 $this->kit = $kit;
23 27 }
24 28
25 29 /**
26 - * @throws VariablesLimitReached If database connection fails or query execution errors occur.
30 + * @throws VariablesLimitReached
27 31 */
28 32 private function assert_if_variables_limit_reached( array $db_record ) {
29 33 $variables_in_use = 0;
30 34
@@ -35,15 +39,15 @@
35 39
36 40 ++$variables_in_use;
37 41 }
38 42
39 - if ( Constants::TOTAL_VARIABLES_COUNT < $variables_in_use ) {
43 + if ( self::TOTAL_VARIABLES_COUNT < $variables_in_use ) {
40 44 throw new VariablesLimitReached( 'Total variables count limit reached' );
41 45 }
42 46 }
43 47
44 48 /**
45 - * @throws DuplicatedLabel If variable creation fails or validation errors occur.
49 + * @throws DuplicatedLabel
46 50 */
47 51 private function assert_if_variable_label_is_duplicated( array $db_record, array $variable = [] ) {
48 52 foreach ( $db_record['data'] as $id => $existing_variable ) {
49 53 if ( isset( $existing_variable['deleted'] ) && $existing_variable['deleted'] ) {
@@ -70,9 +74,9 @@
70 74 return $db_record['data'] ?? [];
71 75 }
72 76
73 77 public function load(): array {
74 - $db_record = $this->kit->get_json_meta( Constants::VARIABLES_META_KEY );
78 + $db_record = $this->kit->get_json_meta( static::VARIABLES_META_KEY );
75 79
76 80 if ( is_array( $db_record ) && ! empty( $db_record ) ) {
77 81 return $db_record;
78 82 }
@@ -80,9 +84,9 @@
80 84 return $this->get_default_meta();
81 85 }
82 86
83 87 /**
84 - * @throws FatalError If variable update fails or validation errors occur.
88 + * @throws FatalError
85 89 */
86 90 public function create( array $variable ) {
87 91 $db_record = $this->load();
88 92
@@ -92,15 +96,10 @@
92 96 $new_variable = $this->extract_from( $variable, [
93 97 'type',
94 98 'label',
95 99 'value',
96 - 'order',
97 100 ] );
98 101
99 - if ( ! isset( $new_variable['order'] ) ) {
100 - $new_variable['order'] = $this->get_next_order( $list_of_variables );
101 - }
102 -
103 102 $this->assert_if_variable_label_is_duplicated( $db_record, $new_variable );
104 103
105 104 $list_of_variables[ $id ] = $new_variable;
106 105 $db_record['data'] = $list_of_variables;
@@ -119,10 +118,10 @@
119 118 ];
120 119 }
121 120
122 121 /**
123 - * @throws RecordNotFound If variable deletion fails or database errors occur.
124 - * @throws FatalError If variable deletion fails or database errors occur.
122 + * @throws RecordNotFound
123 + * @throws FatalError
125 124 */
126 125 public function update( string $id, array $variable ) {
127 126 $db_record = $this->load();
128 127
@@ -134,9 +133,8 @@
134 133
135 134 $updated_variable = array_merge( $list_of_variables[ $id ], $this->extract_from( $variable, [
136 135 'label',
137 136 'value',
138 - 'order',
139 137 ] ) );
140 138
141 139 $this->assert_if_variable_label_is_duplicated( $db_record, array_merge( $updated_variable, [ 'id' => $id ] ) );
142 140
@@ -155,10 +153,10 @@
155 153 ];
156 154 }
157 155
158 156 /**
159 - * @throws RecordNotFound If bulk operation fails or validation errors occur.
160 - * @throws FatalError If bulk operation fails or validation errors occur.
157 + * @throws RecordNotFound
158 + * @throws FatalError
161 159 */
162 160 public function delete( string $id ) {
163 161 $db_record = $this->load();
164 162
@@ -185,10 +183,10 @@
185 183 ];
186 184 }
187 185
188 186 /**
189 - * @throws RecordNotFound If export operation fails or data serialization errors occur.
190 - * @throws FatalError If export operation fails or data serialization errors occur.
187 + * @throws RecordNotFound
188 + * @throws FatalError
191 189 */
192 190 public function restore( string $id, $overrides = [] ) {
193 191 $db_record = $this->load();
194 192
@@ -201,9 +199,8 @@
201 199 $restored_variable = $this->extract_from( $list_of_variables[ $id ], [
202 200 'label',
203 201 'value',
204 202 'type',
205 - 'order',
206 203 ] );
207 204
208 205 if ( array_key_exists( 'label', $overrides ) ) {
209 206 $restored_variable['label'] = $overrides['label'];
@@ -231,224 +228,8 @@
231 228 'watermark' => $watermark,
232 229 ];
233 230 }
234 231
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 232 private function save( array $db_record ) {
452 233 if ( PHP_INT_MAX === $db_record['watermark'] ) {
453 234 $db_record['watermark'] = 0;
454 235 }
@@ -454,9 +235,9 @@
454 235 }
455 236
456 237 ++$db_record['watermark'];
457 238
458 - if ( $this->kit->update_json_meta( Constants::VARIABLES_META_KEY, $db_record ) ) {
239 + if ( $this->kit->update_json_meta( static::VARIABLES_META_KEY, $db_record ) ) {
459 240 return $db_record['watermark'];
460 241 }
461 242
462 243 return false;
@@ -477,24 +258,8 @@
477 258 private function get_default_meta(): array {
478 259 return [
479 260 'data' => [],
480 261 'watermark' => 0,
481 - 'version' => Constants::FORMAT_VERSION_V1,
262 + 'version' => self::FORMAT_VERSION_V1,
482 263 ];
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 264 }
500 265 }