forked from cloudinary/cloudinary_wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-setting.php
More file actions
433 lines (377 loc) · 8.1 KB
/
Copy pathclass-setting.php
File metadata and controls
433 lines (377 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
/**
* Cloudinary represents a single setting node point.
*
* @package Cloudinary\Settings
*/
namespace Cloudinary\Settings;
use Cloudinary\Traits\Params_Trait;
use Cloudinary\Settings;
use Cloudinary\UI\Component;
/**
* Class Setting
*
* @package Cloudinary\Settings
*/
class Setting {
use Params_Trait;
/**
* Holds the settings component.
*
* @var Component
*/
protected $component;
/**
* Holds the setting type.
*
* @var string
*/
protected $type;
/**
* Holds the root parent.
*
* @var Settings
*/
protected $root;
/**
* Holds the slug.
*
* @var string
*/
protected $slug;
/**
* Holds the list of children.
*
* @var self[]
*/
protected $children = array();
/**
* Holds the parent.
*
* @var self
*/
protected $parent;
/**
* Setting constructor.
*
* @param string $slug The setting slug.
* @param Settings $root The root setting.
* @param array $params Optional Params.
*/
public function __construct( $slug, $root = null, $params = array() ) {
if ( is_null( $root ) ) {
$root = new Settings( $slug, $params );
}
$this->root = $root;
$this->slug = $slug;
if ( ! empty( $params ) ) {
$this->set_params( $params );
}
}
/**
* Set the parent setting.
*
* @param string $parent The slug of the parent setting.
*/
public function set_parent( $parent ) {
$this->parent = $parent;
}
/**
* Get the parent.
*
* @param int $depth The depth to go down.
*
* @return Settings|Setting
*/
public function get_parent( $depth = 1 ) {
$slug = $this->slug;
while ( 0 < $depth ) {
$slug = substr( $slug, 0, strrpos( $slug, $this->separator ) );
$depth --;
}
return $this->root->get_setting( $slug );
}
/**
* Set the setting type.
*
* @param string $type The parent setting.
*/
public function set_type( $type ) {
$this->type = $type;
}
/**
* Get the setting type.
*
* @return string
*/
public function get_type() {
return $this->type;
}
/**
* Get the settings component.
*
* @return Component
*/
public function get_component() {
if ( ! $this->component ) {
foreach ( $this->children as $child ) {
$child->get_component();
}
$this->component = Component::init( $this );
}
return $this->component;
}
/**
* Get the child settings.
*
* @return Setting[]
*/
public function get_settings() {
return $this->children;
}
/**
* Get the submitted value, if exists.
*
* @return mixed
*/
public function get_submitted_value() {
if ( ! empty( $this->children ) ) {
$value = array();
foreach ( $this->children as $slug => $child ) {
$child_value = $child->get_submitted_value();
if ( null !== $child_value ) {
$value[ $slug ] = $child_value;
}
}
return $value;
}
$raw_value = $this->root->get_submitted_value( $this->get_slug() );
$value = null;
if ( $raw_value ) {
$value = $this->get_component()->sanitize_value( $raw_value );
}
if ( is_null( $value ) && 'text' === $this->get_param( 'type' ) ) {
$value = $this->get_param( 'default', '' );
}
return $value;
}
/**
* Get a setting.
*
* @param string $slug The slug of the setting to get.
*
* @return Setting
*/
public function get_setting( $slug ) {
if ( isset( $this->children[ $slug ] ) ) {
return $this->children[ $slug ];
} elseif ( $this->has_setting( $slug ) ) {
foreach ( $this->children as $child ) {
if ( $child->has_setting( $slug ) ) {
return $child->get_setting( $slug );
}
}
}
$found = $this->root->get_setting( $this->slug . $this->separator . $slug );
if ( $found ) {
$found = $this->root->get_setting( $slug );
}
return $found;
}
/**
* Magic method to chain directly to the child settings by slug.
*
* @param string $name The name/slug of the child setting.
*
* @return Setting|null
*/
public function __get( $name ) {
$value = false;
if ( '_' === $name[0] ) {
$value = true;
$name = ltrim( $name, '_' );
}
if ( ! isset( $this->children[ $name ] ) ) {
$params = array(
Settings::META_KEYS['storage'] => $this->get_param( Settings::META_KEYS['storage'] ) . $this->separator . $name,
);
$this->children[ $name ] = $this->root->add( $this->slug . $this->separator . $name, array(), $params );
}
$return = $this->children[ $name ];
if ( $value ) {
$return = $return->get_value();
}
return $return;
}
/**
* Magic method to set a child setting's value.
*
* @param string $name The setting name being set.
* @param mixed $value The value to set.
*/
public function __set( $name, $value ) {
$this->{$name}->set_value( $value );
}
/**
* Add a child component.
*
* @param self $component The component to add.
*
* @return Setting|\WP_Error
*/
public function add( $component ) {
$parts = explode( $this->separator, $component->get_slug() );
$slug = array_pop( $parts );
$this->children[ $slug ] = $component;
return $component;
}
/**
* Get the component slug.
*
* @return string
*/
public function get_slug() {
return $this->slug;
}
/**
* Get a setting value.
*
* @param string $slug The slug to get.
*
* @return mixed
*/
public function get_value( $slug = null ) {
if ( null !== $slug ) {
if ( isset( $this->children[ $slug ] ) ) {
return $this->children[ $slug ]->get_value();
}
}
return $this->root->get_value( $slug ? $slug : $this->slug );
}
/**
* Set the value of the setting.
*
* @param mixed $value The value to set.
*/
public function set_value( $value ) {
$this->root->set_value( $this->slug, $value );
}
/**
* Pend a setting's value, for prep to update.
*
* @param mixed $value The value to set.
*/
public function set_pending( $value ) {
$this->root->set_pending( $this->slug, $value );
}
/**
* Get a setting's pending value for update.
*
* @return mixed
*/
public function get_pending() {
return $this->root->get_pending( $this->slug );
}
/**
* Check if a slug has a pending set of changes.
*
* @return bool
*/
public function has_pending() {
return $this->root->has_pending( $this->slug );
}
/**
* Remove a pending set.
*/
public function remove_pending() {
$this->root->remove_pending( $this->slug );
}
/**
* Check if the setting has a parent.
*
* @return bool
*/
public function has_parent() {
return ! empty( $this->parent );
}
/**
* Check if the setting has settings.
*
* @return bool
*/
public function has_settings() {
return ! empty( $this->children );
}
/**
* Check if the setting has a child setting with the slug.
*
* @param string $slug The slug to check for.
*
* @return bool
*/
public function has_setting( $slug ) {
if ( isset( $this->children[ $slug ] ) ) {
return true;
}
foreach ( $this->children as $child ) {
if ( $child->has_setting( $slug ) ) {
return true;
}
}
return false;
}
/**
* Get the main option name.
*
* @return string
*/
public function get_option_name() {
return explode( $this->separator, $this->get_param( Settings::META_KEYS['storage'] ), 2 )[0];
}
/**
* Get the option parent.
*
* @return Settings
*/
public function get_option_parent() {
$root = explode( $this->separator, $this->slug, 2 )[0];
return $this->root->get_setting( $root );
}
/**
* Get the root setting.
*
* @return Settings
*/
public function get_root_setting() {
return $this->root;
}
/**
* Find a setting.
*
* @param string $slug The slug to find.
*
* @return Setting
*/
public function find_setting( $slug ) {
return $this->root->find_setting( $slug );
}
/**
* Save the setting value.
*
* @param mixed $value The value to save.
*/
public function save_value( $value ) {
$this->root->set_pending( $this->slug, $value );
$this->root->save_setting( $this->get_option_name() );
}
/**
* Delete a settings data.
*
* @return bool
*/
public function delete() {
$this->root->set_pending( $this->slug, '--delete' );
$this->root->remove_pending( $this->slug );
return $this->root->delete( $this->get_param( Settings::META_KEYS['storage'] ) );
}
}