class-option.php
9.78 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
<?php
/**
* The Option model
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.0.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\Core\Models
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\Core\Models;
// Abort if called directly.
defined( 'WPINC' ) || die;
use WPMUDEV_BLC\Core\Utils\Abstracts\Base;
use WPMUDEV_BLC\Core\Traits\Sanitize;
/**
* Class Options
*
* @package WPMUDEV_BLC\Core\Models
*/
class Option extends Base {
/**
* Use the Sanitize Trait.
*
* @since 2.0.0
*/
use Sanitize;
/**
* Option keys. In case the option contains an array of sub options, this prop can be used for storing the array
* keys for consistency along the application.
*
* @var array|null $option_keys
*
* @since 2.0.0
*/
public $option_keys = null;
/**
* The option_name.
*
* @var string $name
*
* @since 2.0.0
*/
protected $name;
/**
* The option value. It has to be an array, so it is more flexible.
*
* @var array $value
*
* @since 2.0.0
*/
protected $value;
/**
* Sets if option should autoload. Default is false.
*
* @var bool $autoload
*
* @since 2.0.0
*/
protected $autoload = false;
/**
* Sets if the entire option should be stored partially or not when option value is an array or json object. By default, it is false.
*
* @var bool $override
*
* @since 2.0.0
*/
protected $override = false;
/**
* The option default value.
*
* @var bool|null|string|numeric $default
*
* @since 2.0.0
*/
public $default = false;
/**
* When on multisite sets if option is network wide or not. Default true.
*
* @var bool $network_wide
*
* @since 2.0.0
*/
protected $network_wide = false;
/**
* Options constructor.
*
* Configure option data.
*
* @return void|object
* @since 2.0.0
*
*/
public function __construct( $props = array() ) {
if ( ! empty( $props ) ) {
$default_props = array(
'name' => '',
'value' => '',
'autoload' => false,
'override' => false,
'network_wide' => false,
'option_keys' => null,
'default' => false,
);
$props = wp_parse_args( $props, $default_props );
if ( ! empty( $props ) ) {
foreach ( $props as $property_name => $property_value ) {
if ( property_exists( $this, $property_name ) ) {
$this->__set( $property_name, $property_value );
}
}
}
}
}
/**
* Initiates the Option.
*
* @return void
* @since 2
*/
public function init() {
$this->value = $this->get( null, null, null, true );
}
/**
* Set object $this->value param. Option does not get stored in db yet. The $this->value param can be latter be stored/saved by calling $this->save().
*
* @param array|object $options An array or json object with the options to be saved. Should be in the form key
* => value.
* @param bool $override A boolean to set if function will override entire option by clearing all previous options.
*
* @return bool Returns true if options get set for Options object. Those values are not saved yet.
* @since 2.0.0
*/
public function set( array $options = array(), bool $override = false ) {
$_options = $options;
if ( $override ) {
$this->value = array();
}
// If not an array check if it is json and convert to array. Not a required checked, needs to be removed.
if ( ! is_array( $options ) ) {
$_options = json_decode( $options, true );
if ( json_last_error() !== JSON_ERROR_NONE ) {
$_options = array();
}
}
if ( ! empty( $_options ) ) {
if ( ! is_array( $this->value ) ) {
$this->value = array();
}
foreach ( $_options as $key => $value ) {
$this->value[ $key ] = $value;
}
} else {
return false;
}
return true;
}
/**
* Save Options in DB.
*
* @param array|null $options The options to save.
*
* @param bool $override Optional. Useful when option is an array or json object. If true it will replace the
* entire option value. By default, it will replace only the value of the specified $options array.
*
* @param string|null $option_name Optional nullable option_key.
*
* @param bool $autoload To autoload option or not. Default is false
*
* @param bool $network_wide Store option network wide or not in case site is multisite. Default is false.
*
* @return bool True if saved successfully.
* @since 2.0.0
*
*/
public function save(
$options = null, $override = false, $option_name = null, $autoload = false, $network_wide = false
) {
$option_name = ! is_null( $option_name ) ? $option_name : $this->name;
$options = ! is_null( $options ) ? $options : $this->value;
if ( ! is_array( $options ) ) {
return false;
}
if ( ! $override ) {
$options = wp_parse_args( $options, $this->get( null, $option_name ) );
}
$options = wp_json_encode( $this->sanitize_array( $options ) );
if ( is_multisite() ) {
$network_wide = is_bool( $network_wide ) ? $network_wide : $this->network_wide;
if ( $network_wide ) {
return update_site_option( $option_name, $options );
}
}
return update_option(
$option_name,
$options,
$autoload
);
}
/**
* Get Options from DB.
*
* @param string|null $settings_key Specific settings key. If null it returns all options.
*
* @param string|null $option_name Optional nullable option name.
* @param string|null|bool $default Optional default option value.
* @param bool $force Optional. Force fetch option from db. Default is false.
*
* @return array|string|null Returns an array with options.
* @since 2.0.0
*/
public function get( string $settings_key = null, string $option_name = null, $default = null, bool $force = false ) {
if ( ! empty( $this->value ) && ! $force ) {
$options = $this->value;
} else {
$option_name = ! is_null( $option_name ) ? $option_name : $this->name;
$default = ! is_null( $default ) ? $default : $this->default;
if ( is_array( $default ) ) {
$default = json_encode( $default );
}
// Options are expected to be stored as json.
$options = json_decode( get_option( $option_name, $default ), true );
if ( json_last_error() !== JSON_ERROR_NONE ) {
$options = array();
}
$this->value = $options;
}
if ( ! is_null( $settings_key ) ) {
$settings_key = sanitize_key( $settings_key );
$options = $options[ $settings_key ] ?? null;
}
return $options;
}
/**
* Delete Option from DB.
* Does NOT require calling $this->save() in order to finalise deletion.
*
* @param string|array $options Holds the option key or keys that need to be deleted from option json. It should
* be a string or single array.
*
* @param string|null $option_name Optional nullable option name.
*
* @param bool|null $network_wide Delete option network wide or not in case site is multisite. Default is false.
*
* @return bool
* @since 2.0.0
*
*/
public function delete(
$options = null, $option_name = null, $network_wide = null
) {
if ( ! is_null( $options ) ) {
return $this->delete_option_elements( $options, $option_name, $network_wide );
}
$option_name = ! is_null( $option_name ) ? $option_name : $this->name;
if ( ! is_string( $option_name ) ) {
return false;
}
if ( is_multisite() ) {
$network_wide = is_bool( $network_wide ) ? $network_wide : $this->network_wide;
if ( $network_wide ) {
return delete_site_option( $option_name );
}
}
return delete_option( $option_name );
}
/**
* Prepares Option elements to be deleted. Removes elements passed through the $options input from the Option.
* Does NOT require calling $this->save() in order to finalise deletion.
* Used by $this->delete().
*
* @param string|array $options Holds the option key or keys that need to be deleted from option json. It should
* be a string or single array.
*
* @param string|null $option_name Optional nullable option name.
*
* @param bool $network_wide Delete option network wide or not in case site is multisite. Default is false.
*
* @return bool
* @since 2.0.0
*/
public function delete_option_elements( $options = null, $option_name = null, $network_wide = false ) {
$option_name = ! is_null( $option_name ) ? $option_name : $this->name;
if ( ! is_string( $option_name ) ) {
return false;
}
if ( is_object( $options ) ) {
$options = ( array ) $options;
}
$current_options = $this->get( null, $option_name );
if ( is_array( $options ) ) {
// Using array_values in case the input $options is multidimensional array.
$current_options = array_filter(
$current_options,
function ( $key ) use ( $options ) {
return ! in_array( $key, array_values( $options ) );
},
ARRAY_FILTER_USE_KEY
);
} else {
unset( $current_options[ $options ] );
}
$options = wp_json_encode( $this->sanitize_array( $current_options ) );
if ( is_multisite() ) {
$network_wide = is_bool( $network_wide ) ? $network_wide : $this->network_wide;
if ( $network_wide ) {
return update_site_option( $option_name, $options );
}
}
return update_option( $option_name, $options );
}
/**
* Resets Option without deleting it.
*
* @param string|null $option_name Optional nullable option name.
*
* @param bool $network_wide Reset option network wide or not in case site is multisite. Default is false.
*
* @return bool
* @since 2.0.0
*
*/
public function reset_option( string $option_name = null, bool $network_wide = false) {
$option_name = ! is_null( $option_name ) ? $option_name : $this->name;
if ( ! is_string( $option_name ) ) {
return false;
}
if ( is_multisite() ) {
$network_wide = is_bool( $network_wide ) ? $network_wide : $this->network_wide;
if ( $network_wide ) {
return update_site_option( $option_name, '' );
}
}
return update_option( $option_name, '' );
}
}