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

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

197 lines 5.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;
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 load() {
28 $collection = $this->repo->load()->serialize( true );
29 foreach ( $collection['data'] as $id => $variable ) {
30 if ( ! ElementorUtils::has_pro() && Size_Variable_Prop_Type::get_key() === $variable['type'] ) {
31 unset( $collection['data'][ $id ] );
32 }
33 }
34 return $collection;
35 }
36
37 /**
38 * @throws BatchOperationFailed Thrown when one of the operations fails.
39 * @throws FatalError Failed to save after batch.
40 */
41 public function process_batch( array $operations ) {
42 $collection = $this->repo->load();
43 $results = [];
44 $errors = [];
45
46 $error_formatter = new Batch_Error_Formatter();
47
48 foreach ( $operations as $index => $operation ) {
49 try {
50 $results[] = $this->batch_processor->apply_operation( $collection, $operation );
51
52 } catch ( \Exception $e ) {
53 $errors[ $this->batch_processor->operation_id( $operation, $index ) ] = [
54 'status' => $error_formatter->status_for( $e ),
55 'code' => $error_formatter->error_code_for( $e ),
56 'message' => $e->getMessage(),
57 ];
58 }
59 }
60
61 if ( ! empty( $errors ) ) {
62 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
63 throw new BatchOperationFailed( 'Batch failed', $errors );
64 }
65
66 $watermark = $this->repo->save( $collection );
67
68 if ( false === $watermark ) {
69 throw new FatalError( 'Failed to save batch operations' );
70 }
71
72 return [
73 'success' => true,
74 'results' => $results,
75 'watermark' => $watermark,
76 ];
77 }
78
79 /**
80 * @throws FatalError If variable create fails or validation errors occur.
81 */
82 public function create( array $data ): array {
83 $collection = $this->repo->load();
84
85 $collection->assert_limit_not_reached();
86 $collection->assert_label_is_unique( $data['label'] );
87
88 $id = $collection->next_id();
89 $data['id'] = $id;
90
91 // TODO: we need to look into this maybe we dont need order to be sent from client. Just implemented as it was
92 if ( ! isset( $data['order'] ) ) {
93 $data['order'] = $collection->get_next_order();
94 }
95
96 $variable = Variable::from_array( $data );
97 $variable->validate();
98
99 $collection->add_variable( $variable );
100
101 $watermark = $this->repo->save( $collection );
102
103 if ( false === $watermark ) {
104 throw new FatalError( 'Failed to create variable' );
105 }
106
107 return [
108 'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
109 'watermark' => $collection->watermark(),
110 ];
111 }
112
113 /**
114 * @throws FatalError If variable update fails.
115 */
116 public function update( string $id, array $data ): array {
117 $collection = $this->repo->load();
118 $variable = $collection->find_or_fail( $id );
119
120 if ( isset( $data['label'] ) ) {
121 $collection->assert_label_is_unique( $data['label'], $id );
122 }
123
124 $variable->apply_changes( $data );
125 $variable->validate();
126
127 $watermark = $this->repo->save( $collection );
128
129 if ( false === $watermark ) {
130 throw new FatalError( 'Failed to update variable' );
131 }
132
133 return [
134 'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
135 'watermark' => $watermark,
136 ];
137 }
138
139 /**
140 * @throws FatalError If variable delete fails.
141 */
142 public function delete( string $id ) {
143 $collection = $this->repo->load();
144 $variable = $collection->find_or_fail( $id );
145
146 $variable->soft_delete();
147
148 $watermark = $this->repo->save( $collection );
149
150 if ( false === $watermark ) {
151 throw new FatalError( 'Failed to delete variable' );
152 }
153
154 return [
155 'watermark' => $watermark,
156 'variable' => array_merge( [
157 'id' => $id,
158 'deleted' => true,
159 ], $variable->to_array() ),
160 ];
161 }
162
163 /**
164 * @throws FatalError If variable restore fails.
165 */
166 public function restore( string $id, $overrides = [] ) {
167 $collection = $this->repo->load();
168 $variable = $collection->find_or_fail( $id );
169
170 $label = $variable->label();
171
172 if ( isset( $overrides['label'] ) ) {
173 $label = $overrides['label'];
174 }
175
176 $collection->assert_limit_not_reached();
177
178 $collection->assert_label_is_unique( $label, $variable->id() );
179
180 $variable->apply_changes( $overrides );
181 $variable->validate();
182
183 $variable->restore();
184
185 $watermark = $this->repo->save( $collection );
186
187 if ( false === $watermark ) {
188 throw new FatalError( 'Failed to delete variable' );
189 }
190
191 return [
192 'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
193 'watermark' => $watermark,
194 ];
195 }
196 }
197