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 / variables-service.php

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

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