PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.8
Elementor Website Builder – more than just a page builder v3.35.8
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 3.35.8, at modules/variables/services/variables-service.php

190 lines 5.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;
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
98 $collection->add_variable( $variable );
99
100 $watermark = $this->repo->save( $collection );
101
102 if ( false === $watermark ) {
103 throw new FatalError( 'Failed to create variable' );
104 }
105
106 return [
107 'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
108 'watermark' => $collection->watermark(),
109 ];
110 }
111
112 /**
113 * @throws FatalError If variable update fails.
114 */
115 public function update( string $id, array $data ): array {
116 $collection = $this->repo->load();
117 $variable = $collection->find_or_fail( $id );
118
119 if ( isset( $data['label'] ) ) {
120 $collection->assert_label_is_unique( $data['label'], $id );
121 }
122
123 $variable->apply_changes( $data );
124
125 $watermark = $this->repo->save( $collection );
126
127 if ( false === $watermark ) {
128 throw new FatalError( 'Failed to update variable' );
129 }
130
131 return [
132 'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
133 'watermark' => $watermark,
134 ];
135 }
136
137 /**
138 * @throws FatalError If variable delete fails.
139 */
140 public function delete( string $id ) {
141 $collection = $this->repo->load();
142 $variable = $collection->find_or_fail( $id );
143
144 $variable->soft_delete();
145
146 $watermark = $this->repo->save( $collection );
147
148 if ( false === $watermark ) {
149 throw new FatalError( 'Failed to delete variable' );
150 }
151
152 return [
153 'watermark' => $watermark,
154 'variable' => array_merge( [
155 'id' => $id,
156 'deleted' => true,
157 ], $variable->to_array() ),
158 ];
159 }
160
161 /**
162 * @throws FatalError If variable restore fails.
163 */
164 public function restore( string $id, $overrides = [] ) {
165 $collection = $this->repo->load();
166 $variable = $collection->find_or_fail( $id );
167
168 $collection->assert_limit_not_reached();
169
170 if ( isset( $overrides['label'] ) ) {
171 $collection->assert_label_is_unique( $overrides['label'], $variable->id() );
172 }
173
174 $variable->apply_changes( $overrides );
175
176 $variable->restore();
177
178 $watermark = $this->repo->save( $collection );
179
180 if ( false === $watermark ) {
181 throw new FatalError( 'Failed to delete variable' );
182 }
183
184 return [
185 'variable' => array_merge( [ 'id' => $id ], $variable->to_array() ),
186 'watermark' => $watermark,
187 ];
188 }
189 }
190