PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.4
Elementor Website Builder – more than just a page builder v4.2.4
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 / variables-service.php

variables-service.php in Elementor Website Builder – more than just a page builder 4.2.4, at modules/variables/services/variables-service.php

230 lines 5.8 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;
4
5 use Elementor\Modules\Variables\Services\Batch_Operations\Batch_Error_Formatter;
6 use Elementor\Modules\Variables\Services\Batch_Operations\Batch_Processor;
7 use Elementor\Modules\Variables\Storage\Entities\Variable;
8 use Elementor\Modules\Variables\Storage\Exceptions\BatchOperationFailed;
9 use Elementor\Modules\Variables\Storage\Variables_Repository;
10 use Elementor\Modules\Variables\Storage\Exceptions\FatalError;
11 use Elementor\Modules\Variables\PropTypes\Size_Variable_Prop_Type;
12 use Elementor\Utils as ElementorUtils;
13
14 class Variables_Service {
15 private Variables_Repository $repo;
16 private Batch_Processor $batch_processor;
17
18 public function __construct( Variables_Repository $repository, Batch_Processor $batch_processor ) {
19 $this->repo = $repository;
20 $this->batch_processor = $batch_processor;
21 }
22
23 public function get_variables_list(): array {
24 return $this->load()['data'];
25 }
26
27 public function find_by_label_or_id( string $needle ): ?array {
28 $needle = trim( $needle );
29 $needle = ltrim( $needle, '-' );
30
31 if ( '' === $needle ) {
32 return null;
33 }
34
35 $variables = $this->get_variables_list();
36
37 if ( isset( $variables[ $needle ] ) ) {
38 $variable = $variables[ $needle ];
39
40 if ( ! empty( $variable['deleted'] ) ) {
41 return null;
42 }
43
44 return array_merge( [ 'id' => $needle ], $variable );
45 }
46
47 foreach ( $variables as $id => $variable ) {
48 if ( ! empty( $variable['deleted'] ) ) {
49 continue;
50 }
51
52 if ( strcasecmp( $variable['label'] ?? '', $needle ) === 0 ) {
53 return array_merge( [ 'id' => $id ], $variable );
54 }
55 }
56
57 return null;
58 }
59
60 public function load() {
61 $collection = $this->repo->load()->serialize( true );
62 foreach ( $collection['data'] as $id => $variable ) {
63 if ( ! ElementorUtils::has_pro() && Size_Variable_Prop_Type::get_key() === $variable['type'] ) {
64 unset( $collection['data'][ $id ] );
65 }
66 }
67 return $collection;
68 }
69
70 /**
71 * @throws BatchOperationFailed Thrown when one of the operations fails.
72 * @throws FatalError Failed to save after batch.
73 */
74 public function process_batch( array $operations ) {
75 $collection = $this->repo->load();
76 $results = [];
77 $errors = [];
78
79 $error_formatter = new Batch_Error_Formatter();
80
81 foreach ( $operations as $index => $operation ) {
82 try {
83 $results[] = $this->batch_processor->apply_operation( $collection, $operation );
84
85 } catch ( \Exception $e ) {
86 $errors[ $this->batch_processor->operation_id( $operation, $index ) ] = [
87 'status' => $error_formatter->status_for( $e ),
88 'code' => $error_formatter->error_code_for( $e ),
89 'message' => $e->getMessage(),
90 ];
91 }
92 }
93
94 if ( ! empty( $errors ) ) {
95 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
96 throw new BatchOperationFailed( 'Batch failed', $errors );
97 }
98
99 $watermark = $this->repo->save( $collection );
100
101 if ( false === $watermark ) {
102 throw new FatalError( 'Failed to save batch operations' );
103 }
104
105 return [
106 'success' => true,
107 'results' => $results,
108 'watermark' => $watermark,
109 ];
110 }
111
112 /**
113 * @throws FatalError If variable create fails or validation errors occur.
114 */
115 public function create( array $data ): array {
116 $collection = $this->repo->load();
117
118 $collection->assert_limit_not_reached();
119 $collection->assert_label_is_unique( $data['label'] );
120
121 $id = $collection->next_id();
122 $data['id'] = $id;
123
124 // TODO: we need to look into this maybe we dont need order to be sent from client. Just implemented as it was
125 if ( ! isset( $data['order'] ) ) {
126 $data['order'] = $collection->get_next_order();
127 }
128
129 $variable = Variable::from_array( $data );
130 $variable->validate();
131
132 $collection->add_variable( $variable );
133
134 $watermark = $this->repo->save( $collection );
135
136 if ( false === $watermark ) {
137 throw new FatalError( 'Failed to create variable' );
138 }
139
140 return [
141 'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
142 'watermark' => $collection->watermark(),
143 ];
144 }
145
146 /**
147 * @throws FatalError If variable update fails.
148 */
149 public function update( string $id, array $data ): array {
150 $collection = $this->repo->load();
151 $variable = $collection->find_or_fail( $id );
152
153 if ( isset( $data['label'] ) ) {
154 $collection->assert_label_is_unique( $data['label'], $id );
155 }
156
157 $variable->apply_changes( $data );
158 $variable->validate();
159
160 $watermark = $this->repo->save( $collection );
161
162 if ( false === $watermark ) {
163 throw new FatalError( 'Failed to update variable' );
164 }
165
166 return [
167 'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
168 'watermark' => $watermark,
169 ];
170 }
171
172 /**
173 * @throws FatalError If variable delete fails.
174 */
175 public function delete( string $id ) {
176 $collection = $this->repo->load();
177 $variable = $collection->find_or_fail( $id );
178
179 $variable->soft_delete();
180
181 $watermark = $this->repo->save( $collection );
182
183 if ( false === $watermark ) {
184 throw new FatalError( 'Failed to delete variable' );
185 }
186
187 return [
188 'watermark' => $watermark,
189 'variable' => array_merge( [
190 'id' => $id,
191 'deleted' => true,
192 ], $variable->to_array() ),
193 ];
194 }
195
196 /**
197 * @throws FatalError If variable restore fails.
198 */
199 public function restore( string $id, $overrides = [] ) {
200 $collection = $this->repo->load();
201 $variable = $collection->find_or_fail( $id );
202
203 $label = $variable->label();
204
205 if ( isset( $overrides['label'] ) ) {
206 $label = $overrides['label'];
207 }
208
209 $collection->assert_limit_not_reached();
210
211 $collection->assert_label_is_unique( $label, $variable->id() );
212
213 $variable->apply_changes( $overrides );
214 $variable->validate();
215
216 $variable->restore();
217
218 $watermark = $this->repo->save( $collection );
219
220 if ( false === $watermark ) {
221 throw new FatalError( 'Failed to delete variable' );
222 }
223
224 return [
225 'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
226 'watermark' => $watermark,
227 ];
228 }
229 }
230