-
Notifications
You must be signed in to change notification settings - Fork 24
/
class-imagify-abstract-options.php
623 lines (534 loc) · 14.3 KB
/
class-imagify-abstract-options.php
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
<?php
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
/**
* Abstract class to handle a part of the plugin options.
*
* @since 1.7
*/
abstract class Imagify_Abstract_Options {
/**
* Class version.
*
* @var string
* @since 1.7
*/
const VERSION = '1.0';
/**
* Suffix used in the name of the option.
*
* @var string
* @since 1.7
* @access protected
*/
protected $identifier;
/**
* The default values for the Imagify main options.
* These are the "zero state" values.
* Don't use null as value.
*
* @var array
* @since 1.7
* @access protected
*/
protected $default_values;
/**
* The Imagify main option values used when they are set the first time or reset.
* Values identical to default values are not listed.
*
* @var array
* @since 1.7
* @access protected
*/
protected $reset_values = array();
/**
* Tell if the option should be autoloaded by WP.
* Possible values are 'yes' and 'no'.
*
* @var string
* @since 1.7
* @access protected
*/
protected $autoload = 'yes';
/**
* Tell if the option should be a network option.
*
* @var bool
* @since 1.7
* @access protected
*/
protected $network_option = false;
/**
* Identifier used in the hook names.
*
* @var string
* @since 1.7
* @access private
*/
private $hook_identifier;
/**
* The single instance of the class.
*
* @var object
* @since 1.7
* @access protected
*/
protected static $_instance;
/**
* The constructor.
*
* @since 1.7
* @author Grégory Viguier
* @access protected
*/
protected function __construct() {
$this->hook_identifier = rtrim( strtolower( str_replace( 'Imagify_', '', get_class( $this ) ) ), 's' );
if ( ! is_string( $this->autoload ) ) {
$this->autoload = $this->autoload ? 'yes' : 'no';
}
$this->default_values = array_merge( array(
'version' => '',
), $this->default_values );
}
/**
* Get the main Instance.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @return object Main instance.
*/
public static function get_instance() {
if ( ! isset( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Launch the hooks.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*/
public function init() {
add_filter( 'sanitize_option_' . $this->get_option_name(), array( $this, 'sanitize_and_validate_on_update' ), 50 );
}
/** ----------------------------------------------------------------------------------------- */
/** GET/SET/DELETE OPTION(S) ================================================================ */
/** ----------------------------------------------------------------------------------------- */
/**
* Get an Imagify option.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @param string $key The option name.
* @return mixed The option value.
*/
public function get( $key ) {
$default_values = $this->get_default_values();
if ( ! isset( $default_values[ $key ] ) ) {
return null;
}
$default = $default_values[ $key ];
/**
* Pre-filter any Imagify option before read.
*
* @since 1.0
*
* @param mixed $value Value to return instead of the option value. Default null to skip it.
* @param mixed $default The default value.
*/
$value = apply_filters( 'pre_get_imagify_' . $this->get_hook_identifier() . '_' . $key, null, $default );
if ( isset( $value ) ) {
return $value;
}
// Get all values.
$values = $this->get_all();
// Sanitize and validate the value.
$value = $this->sanitize_and_validate( $key, $values[ $key ], $default );
/**
* Filter any Imagify option after read.
*
* @since 1.0
*
* @param mixed $value Value of the option.
* @param mixed $default The default value. Default false.
*/
return apply_filters( 'get_imagify_' . $this->get_hook_identifier() . '_' . $key, $value, $default );
}
/**
* Get all options (no cast, no sanitization, no validation).
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @return array The options.
*/
public function get_all() {
$values = $this->get_raw();
if ( ! $values ) {
return $this->get_reset_values();
}
return imagify_merge_intersect( $values, $this->get_default_values() );
}
/**
* Set one or multiple options.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @param array $values An array of option name / option value pairs.
*/
public function set( $values ) {
$args = func_get_args();
if ( isset( $args[1] ) && is_string( $args[0] ) ) {
$values = array( $args[0] => $args[1] );
}
if ( ! is_array( $values ) ) {
// PABKAC.
return;
}
$values = array_merge( $this->get_all(), $values );
$values = array_intersect_key( $values, $this->get_default_values() );
$this->set_raw( $values );
}
/**
* Delete one or multiple options.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @param array|string $keys An array of option names or a single option name.
*/
public function delete( $keys ) {
$values = $this->get_raw();
if ( ! $values ) {
if ( false !== $values ) {
$this->delete_raw();
}
return;
}
$keys = array_flip( (array) $keys );
$values = array_diff_key( $values, $keys );
$this->set_raw( $values );
}
/**
* Checks if the option with the given name exists or not.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @param string $key The option name.
* @return bool
*/
public function has( $key ) {
return null !== $this->get( $key );
}
/** ----------------------------------------------------------------------------------------- */
/** GET / UPDATE / DELETE RAW VALUES ======================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the name of the option that stores the settings.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @return string
*/
public function get_option_name() {
return IMAGIFY_SLUG . '_' . $this->identifier;
}
/**
* Get the identifier used in the hook names.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @return string
*/
public function get_hook_identifier() {
return $this->hook_identifier;
}
/**
* Tell if the option is autoloaded.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @return bool
*/
public function is_autoloaded() {
return 'yes' === $this->autoload;
}
/**
* Tell if the option is a network option.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @return bool
*/
public function is_network_option() {
return (bool) $this->network_option;
}
/**
* Get the raw value of all Imagify options.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @return array|bool The options. False if not set yet. An empty array if invalid.
*/
public function get_raw() {
$values = $this->is_network_option() ? get_site_option( $this->get_option_name() ) : get_option( $this->get_option_name() );
if ( false !== $values && ! is_array( $values ) ) {
return array();
}
return $values;
}
/**
* Update the Imagify options.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @param array $values An array of option name / option value pairs.
*/
public function set_raw( $values ) {
if ( ! $values ) {
// The option is empty: delete it.
$this->delete_raw();
} elseif ( $this->is_network_option() ) {
// Network option.
update_site_option( $this->get_option_name(), $values );
} elseif ( false === get_option( $this->get_option_name() ) ) {
// Compat' with WP < 4.2 + autoload: the option doesn't exist in the database.
add_option( $this->get_option_name(), $values, '', $this->autoload );
} else {
// Update the current value.
update_option( $this->get_option_name(), $values, $this->autoload );
}
}
/**
* Delete all Imagify options.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*/
public function delete_raw() {
$this->is_network_option() ? delete_site_option( $this->get_option_name() ) : delete_option( $this->get_option_name() );
}
/** ----------------------------------------------------------------------------------------- */
/** DEFAULT + RESET VALUES ================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get default option values.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @return array
*/
public function get_default_values() {
$default_values = $this->default_values;
if ( ! empty( $default_values['cached'] ) ) {
unset( $default_values['cached'] );
return $default_values;
}
/**
* Allow to add more default option values.
*
* @since 1.7
* @author Grégory Viguier
*
* @param array $new_values New default option values.
* @param array $default_values Plugin default option values.
*/
$new_values = apply_filters( 'imagify_default_' . $this->get_hook_identifier() . '_values', array(), $default_values );
$new_values = is_array( $new_values ) ? $new_values : array();
if ( $new_values ) {
// Don't allow new values to overwrite the plugin values.
$new_values = array_diff_key( $new_values, $default_values );
}
if ( $new_values ) {
$default_values = array_merge( $default_values, $new_values );
$this->default_values = $default_values;
}
$this->default_values['cached'] = 1;
return $default_values;
}
/**
* Get the values used when the option is empty.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @return array
*/
public function get_reset_values() {
$reset_values = $this->reset_values;
if ( ! empty( $reset_values['cached'] ) ) {
unset( $reset_values['cached'] );
return $reset_values;
}
$default_values = $this->get_default_values();
$reset_values = array_merge( $default_values, $reset_values );
/**
* Allow to filter the "reset" option values.
*
* @since 1.7
* @author Grégory Viguier
*
* @param array $reset_values Plugin reset option values.
*/
$new_values = apply_filters( 'imagify_reset_' . $this->get_hook_identifier() . '_values', $reset_values );
if ( $new_values && is_array( $new_values ) ) {
$reset_values = array_merge( $reset_values, $new_values );
}
$this->reset_values = $reset_values;
$this->reset_values['cached'] = 1;
return $reset_values;
}
/** ----------------------------------------------------------------------------------------- */
/** SANITIZATION, VALIDATION ================================================================ */
/** ----------------------------------------------------------------------------------------- */
/**
* Sanitize and validate an option value.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @param string $key The option key.
* @param mixed $value The value.
* @param mixed $default The default value.
* @return mixed
*/
public function sanitize_and_validate( $key, $value, $default = null ) {
if ( ! isset( $default ) ) {
$default_values = $this->get_default_values();
$default = $default_values[ $key ];
}
// Cast the value.
$value = self::cast( $value, $default );
if ( $value === $default ) {
return $value;
}
// Version.
if ( 'version' === $key ) {
return sanitize_text_field( $value );
}
return $this->sanitize_and_validate_value( $key, $value, $default );
}
/**
* Sanitize and validate an option value. Basic casts have been made.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @param string $key The option key.
* @param mixed $value The value.
* @param mixed $default The default value.
* @return mixed
*/
abstract public function sanitize_and_validate_value( $key, $value, $default );
/**
* Sanitize and validate Imagify's options before storing them.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @param string $values The option value.
* @return array
*/
public function sanitize_and_validate_on_update( $values ) {
$values = is_array( $values ) ? $values : array();
$default_values = $this->get_default_values();
if ( $values ) {
foreach ( $default_values as $key => $default ) {
if ( isset( $values[ $key ] ) ) {
$values[ $key ] = $this->sanitize_and_validate( $key, $values[ $key ], $default );
}
}
}
$values = array_intersect_key( $values, $default_values );
// Version.
if ( empty( $values['version'] ) ) {
$values['version'] = IMAGIFY_VERSION;
}
return $this->validate_values_on_update( $values );
}
/**
* Validate Imagify's options before storing them. Basic sanitization and validation have been made, row by row.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @param string $values The option value.
* @return array
*/
public function validate_values_on_update( $values ) {
return $values;
}
/** ----------------------------------------------------------------------------------------- */
/** TOOLS =================================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Cast a value, depending on its default value type.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @param mixed $value The value to cast.
* @param mixed $default The default value.
* @return mixed
*/
public static function cast( $value, $default ) {
if ( is_array( $default ) ) {
return is_array( $value ) ? $value : array();
}
if ( is_int( $default ) ) {
return (int) $value;
}
if ( is_bool( $default ) ) {
return (bool) $value;
}
if ( is_float( $default ) ) {
return round( (float) $value, 3 );
}
return $value;
}
/**
* Cast a float like 3.000 into an integer.
*
* @since 1.7
* @author Grégory Viguier
* @access public
*
* @param float $float The float.
* @return float|int
*/
public static function maybe_cast_float_as_int( $float ) {
return ( $float / (int) $float ) === (float) 1 ? (int) $float : $float;
}
}