PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.0
Elementor Website Builder – more than just a page builder v4.1.0
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 4.1.0, at modules/variables/services/batch-operations/batch-processor.php

124 lines 3.1 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 $variable->validate();
46
47 $collection->add_variable( $variable );
48
49 // TODO: do we need to return all this payload maybe return what the clients want
50 return [
51 'type' => 'create',
52 'id' => $data['id'],
53 'temp_id' => $temp_id,
54 'variable' => $variable->to_array(),
55 ];
56 }
57
58 private function op_update( Variables_Collection $collection, array $operation ): array {
59 $id = $operation['id'];
60 $data = $operation['variable'];
61
62 $variable = $collection->find_or_fail( $id );
63
64 if ( isset( $data['label'] ) ) {
65 $collection->assert_label_is_unique( $data['label'], $id );
66 }
67
68 $variable->apply_changes( $data );
69
70 return [
71 'type' => 'update',
72 'id' => $id,
73 'variable' => $variable->to_array(),
74 ];
75 }
76
77 private function op_delete( Variables_Collection $collection, array $operation ): array {
78 $id = $operation['id'];
79 $variable = $collection->find_or_fail( $id );
80
81 $variable->soft_delete();
82
83 return [
84 'type' => 'delete',
85 'id' => $id,
86 'deleted' => true,
87 ];
88 }
89
90 private function op_restore( Variables_Collection $collection, array $operation ): array {
91 $id = $operation['id'];
92 $variable = $collection->find_or_fail( $id );
93
94 $collection->assert_limit_not_reached();
95
96 if ( isset( $operation['label'] ) ) {
97 $collection->assert_label_is_unique( $operation['label'], $id );
98 $variable->validate();
99 }
100
101 $variable->apply_changes( $operation );
102
103 $variable->restore();
104
105 return [
106 'type' => 'restore',
107 'id' => $id,
108 'variable' => $variable->to_array(),
109 ];
110 }
111
112 public function operation_id( array $operation, int $index ): string {
113 if ( 'create' === $operation['type'] && isset( $operation['variable']['id'] ) ) {
114 return $operation['variable']['id'];
115 }
116
117 if ( isset( $operation['id'] ) ) {
118 return $operation['id'];
119 }
120
121 return "operation_{$index}";
122 }
123 }
124