PluginProbe
Elementor Website Builder – more than just a page builder / 3.31.4
Elementor Website Builder – more than just a page builder v3.31.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 / storage / repository.php

repository.php in Elementor Website Builder – more than just a page builder 3.31.4, at modules/variables/storage/repository.php

266 lines 6.4 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\Storage;
4
5 use Elementor\Core\Kits\Documents\Kit;
6 use Elementor\Modules\AtomicWidgets\Utils;
7 use Elementor\Modules\Variables\Classes\Variables;
8 use Elementor\Modules\Variables\Storage\Exceptions\DuplicatedLabel;
9 use Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound;
10 use Elementor\Modules\Variables\Storage\Exceptions\VariablesLimitReached;
11 use Elementor\Modules\Variables\Storage\Exceptions\FatalError;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16
17 class Repository {
18 const TOTAL_VARIABLES_COUNT = 100;
19
20 const FORMAT_VERSION_V1 = 1;
21 const VARIABLES_META_KEY = '_elementor_global_variables';
22
23 private Kit $kit;
24
25 public function __construct( Kit $kit ) {
26 $this->kit = $kit;
27 }
28
29 /**
30 * @throws VariablesLimitReached
31 */
32 private function assert_if_variables_limit_reached( array $db_record ) {
33 $variables_in_use = 0;
34
35 foreach ( $db_record['data'] as $variable ) {
36 if ( isset( $variable['deleted'] ) && $variable['deleted'] ) {
37 continue;
38 }
39
40 ++$variables_in_use;
41 }
42
43 if ( self::TOTAL_VARIABLES_COUNT < $variables_in_use ) {
44 throw new VariablesLimitReached( 'Total variables count limit reached' );
45 }
46 }
47
48 /**
49 * @throws DuplicatedLabel
50 */
51 private function assert_if_variable_label_is_duplicated( array $db_record, array $variable = [] ) {
52 foreach ( $db_record['data'] as $id => $existing_variable ) {
53 if ( isset( $existing_variable['deleted'] ) && $existing_variable['deleted'] ) {
54 continue;
55 }
56
57 if ( isset( $variable['id'] ) && $variable['id'] === $id ) {
58 continue;
59 }
60
61 if ( ! isset( $variable['label'] ) || ! isset( $existing_variable['label'] ) ) {
62 continue;
63 }
64
65 if ( strtolower( $existing_variable['label'] ) === strtolower( $variable['label'] ) ) {
66 throw new DuplicatedLabel( 'Variable label already exists' );
67 }
68 }
69 }
70
71 public function variables(): array {
72 $db_record = $this->load();
73
74 return $db_record['data'] ?? [];
75 }
76
77 public function load(): array {
78 $db_record = $this->kit->get_json_meta( static::VARIABLES_META_KEY );
79
80 if ( is_array( $db_record ) && ! empty( $db_record ) ) {
81 return $db_record;
82 }
83
84 return $this->get_default_meta();
85 }
86
87 /**
88 * @throws FatalError
89 */
90 public function create( array $variable ) {
91 $db_record = $this->load();
92
93 $list_of_variables = $db_record['data'] ?? [];
94
95 $id = $this->new_id_for( $list_of_variables );
96 $new_variable = $this->extract_from( $variable, [
97 'type',
98 'label',
99 'value',
100 ] );
101
102 $this->assert_if_variable_label_is_duplicated( $db_record, $new_variable );
103
104 $list_of_variables[ $id ] = $new_variable;
105 $db_record['data'] = $list_of_variables;
106
107 $this->assert_if_variables_limit_reached( $db_record );
108
109 $watermark = $this->save( $db_record );
110
111 if ( false === $watermark ) {
112 throw new FatalError( 'Failed to create variable' );
113 }
114
115 return [
116 'variable' => array_merge( [ 'id' => $id ], $list_of_variables[ $id ] ),
117 'watermark' => $watermark,
118 ];
119 }
120
121 /**
122 * @throws RecordNotFound
123 * @throws FatalError
124 */
125 public function update( string $id, array $variable ) {
126 $db_record = $this->load();
127
128 $list_of_variables = $db_record['data'] ?? [];
129
130 if ( ! isset( $list_of_variables[ $id ] ) ) {
131 throw new RecordNotFound( 'Variable not found' );
132 }
133
134 $updated_variable = array_merge( $list_of_variables[ $id ], $this->extract_from( $variable, [
135 'label',
136 'value',
137 ] ) );
138
139 $this->assert_if_variable_label_is_duplicated( $db_record, array_merge( $updated_variable, [ 'id' => $id ] ) );
140
141 $list_of_variables[ $id ] = $updated_variable;
142 $db_record['data'] = $list_of_variables;
143
144 $watermark = $this->save( $db_record );
145
146 if ( false === $watermark ) {
147 throw new FatalError( 'Failed to update variable' );
148 }
149
150 return [
151 'variable' => array_merge( [ 'id' => $id ], $list_of_variables[ $id ] ),
152 'watermark' => $watermark,
153 ];
154 }
155
156 /**
157 * @throws RecordNotFound
158 * @throws FatalError
159 */
160 public function delete( string $id ) {
161 $db_record = $this->load();
162
163 $list_of_variables = $db_record['data'] ?? [];
164
165 if ( ! isset( $list_of_variables[ $id ] ) ) {
166 throw new RecordNotFound( 'Variable not found' );
167 }
168
169 $list_of_variables[ $id ]['deleted'] = true;
170 $list_of_variables[ $id ]['deleted_at'] = $this->now();
171
172 $db_record['data'] = $list_of_variables;
173
174 $watermark = $this->save( $db_record );
175
176 if ( false === $watermark ) {
177 throw new FatalError( 'Failed to delete variable' );
178 }
179
180 return [
181 'variable' => array_merge( [ 'id' => $id ], $list_of_variables[ $id ] ),
182 'watermark' => $watermark,
183 ];
184 }
185
186 /**
187 * @throws RecordNotFound
188 * @throws FatalError
189 */
190 public function restore( string $id, $overrides = [] ) {
191 $db_record = $this->load();
192
193 $list_of_variables = $db_record['data'] ?? [];
194
195 if ( ! isset( $list_of_variables[ $id ] ) ) {
196 throw new RecordNotFound( 'Variable not found' );
197 }
198
199 $restored_variable = $this->extract_from( $list_of_variables[ $id ], [
200 'label',
201 'value',
202 'type',
203 ] );
204
205 if ( array_key_exists( 'label', $overrides ) ) {
206 $restored_variable['label'] = $overrides['label'];
207 }
208
209 if ( array_key_exists( 'value', $overrides ) ) {
210 $restored_variable['value'] = $overrides['value'];
211 }
212
213 $this->assert_if_variable_label_is_duplicated( $db_record, array_merge( $restored_variable, [ 'id' => $id ] ) );
214
215 $list_of_variables[ $id ] = $restored_variable;
216 $db_record['data'] = $list_of_variables;
217
218 $this->assert_if_variables_limit_reached( $db_record );
219
220 $watermark = $this->save( $db_record );
221
222 if ( false === $watermark ) {
223 throw new FatalError( 'Failed to restore variable' );
224 }
225
226 return [
227 'variable' => array_merge( [ 'id' => $id ], $restored_variable ),
228 'watermark' => $watermark,
229 ];
230 }
231
232 private function save( array $db_record ) {
233 if ( PHP_INT_MAX === $db_record['watermark'] ) {
234 $db_record['watermark'] = 0;
235 }
236
237 ++$db_record['watermark'];
238
239 if ( $this->kit->update_json_meta( static::VARIABLES_META_KEY, $db_record ) ) {
240 return $db_record['watermark'];
241 }
242
243 return false;
244 }
245
246 private function new_id_for( array $list_of_variables ): string {
247 return Utils::generate_id( 'e-gv-', array_keys( $list_of_variables ) );
248 }
249
250 private function now(): string {
251 return gmdate( 'Y-m-d H:i:s' );
252 }
253
254 private function extract_from( array $source, array $fields ): array {
255 return array_intersect_key( $source, array_flip( $fields ) );
256 }
257
258 private function get_default_meta(): array {
259 return [
260 'data' => [],
261 'watermark' => 0,
262 'version' => self::FORMAT_VERSION_V1,
263 ];
264 }
265 }
266