PluginProbe
Elementor Website Builder – more than just a page builder / 3.34.3
Elementor Website Builder – more than just a page builder v3.34.3
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 / services / batch-operations / batch-processor.php

batch-processor.php in Elementor Website Builder – more than just a page builder 3.34.3, at modules/variables/services/batch-operations/batch-processor.php

122 lines 3.0 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\Services\Batch_Operations;
4
5 use Elementor\Modules\Variables\Storage\Entities\Variable;
6 use Elementor\Modules\Variables\Storage\Variables_Collection;
7 use Elementor\Modules\Variables\Storage\Exceptions\BatchOperationFailed;
8
9 class Batch_Processor {
10 private const OPERATION_MAP = [
11 'create' => 'op_create',
12 'update' => 'op_update',
13 'delete' => 'op_delete',
14 'restore' => 'op_restore',
15 ];
16
17 /**
18 * @throws BatchOperationFailed Invalid operation type.
19 */
20 public function apply_operation( Variables_Collection $collection, array $operation ): array {
21 $type = $operation['type'];
22
23 if ( ! isset( self::OPERATION_MAP[ $type ] ) ) {
24 throw new BatchOperationFailed( 'Invalid operation type: ' . esc_html( $type ), [] );
25 }
26
27 $method = self::OPERATION_MAP[ $type ];
28
29 return $this->$method( $collection, $operation );
30 }
31
32 private function op_create( Variables_Collection $collection, array $operation ): array {
33 $data = $operation['variable'];
34 $temp_id = $data['id'] ?? null;
35 $data['id'] = $collection->next_id();
36
37 $collection->assert_limit_not_reached();
38 $collection->assert_label_is_unique( $data['label'] );
39
40 if ( ! isset( $data['order'] ) ) {
41 $data['order'] = $collection->get_next_order();
42 }
43
44 $variable = Variable::create_new( $data );
45
46 $collection->add_variable( $variable );
47
48 // TODO: do we need to return all this payload maybe return what the clients want
49 return [
50 'type' => 'create',
51 'id' => $data['id'],
52 'temp_id' => $temp_id,
53 'variable' => $variable->to_array(),
54 ];
55 }
56
57 private function op_update( Variables_Collection $collection, array $operation ): array {
58 $id = $operation['id'];
59 $data = $operation['variable'];
60
61 $variable = $collection->find_or_fail( $id );
62
63 if ( isset( $data['label'] ) ) {
64 $collection->assert_label_is_unique( $data['label'], $id );
65 }
66
67 $variable->apply_changes( $data );
68
69 return [
70 'type' => 'update',
71 'id' => $id,
72 'variable' => $variable->to_array(),
73 ];
74 }
75
76 private function op_delete( Variables_Collection $collection, array $operation ): array {
77 $id = $operation['id'];
78 $variable = $collection->find_or_fail( $id );
79
80 $variable->soft_delete();
81
82 return [
83 'type' => 'delete',
84 'id' => $id,
85 'deleted' => true,
86 ];
87 }
88
89 private function op_restore( Variables_Collection $collection, array $operation ): array {
90 $id = $operation['id'];
91 $variable = $collection->find_or_fail( $id );
92
93 $collection->assert_limit_not_reached();
94
95 if ( isset( $operation['label'] ) ) {
96 $collection->assert_label_is_unique( $operation['label'], $id );
97 }
98
99 $variable->apply_changes( $operation );
100
101 $variable->restore();
102
103 return [
104 'type' => 'restore',
105 'id' => $id,
106 'variable' => $variable->to_array(),
107 ];
108 }
109
110 public function operation_id( array $operation, int $index ): string {
111 if ( 'create' === $operation['type'] && isset( $operation['variable']['id'] ) ) {
112 return $operation['variable']['id'];
113 }
114
115 if ( isset( $operation['id'] ) ) {
116 return $operation['id'];
117 }
118
119 return "operation_{$index}";
120 }
121 }
122