PluginProbe
Elementor Website Builder – more than just a page builder / 3.33.0-dev1
Elementor Website Builder – more than just a page builder v3.33.0-dev1
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 4.0.7 All 451 releases
elementor / modules / variables / storage / repository.php

repository.php in Elementor Website Builder – more than just a page builder 3.33.0-dev1, at modules/variables/storage/repository.php

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