-
Notifications
You must be signed in to change notification settings - Fork 218
/
database.php
502 lines (437 loc) · 14.1 KB
/
database.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
<?php if ( ! defined( 'FW' ) ) {
die( 'Forbidden' );
}
/** Theme Settings Options */
{
/**
* Get a theme settings option value from the database
*
* @param string|null $option_id Specific option id (accepts multikey). null - all options
* @param null|mixed $default_value If no option found in the database, this value will be returned
* @param null|bool $get_original_value Original value is that with no translations and other changes
*
* @return mixed|null
*/
function fw_get_db_settings_option( $option_id = null, $default_value = null, $get_original_value = null ) {
$value = FW_WP_Option::get(
'fw_theme_settings_options:' . fw()->theme->manifest->get_id(),
$option_id, $default_value, $get_original_value
);
if (
(!is_null($option_id) && is_null($value)) // a specific option_id was requested
||
(is_null($option_id) && empty($value)) // all options were requested but the db value is empty (this can happen after Reset)
) {
/**
* Maybe the options was never saved or the given option id does not exist
* Extract the default values from the options array and try to find there the option id
*/
$cache_key = 'fw_default_options_values/settings';
try {
$all_options_values = FW_Cache::get( $cache_key );
} catch ( FW_Cache_Not_Found_Exception $e ) {
// extract the default values from options array
$all_options_values = fw_get_options_values_from_input(
fw()->theme->get_settings_options(),
array()
);
FW_Cache::set( $cache_key, $all_options_values );
}
if ( empty( $option_id ) ) {
// option id not specified, return all options values
return $all_options_values;
} else {
return fw_akg( $option_id, $all_options_values, $default_value );
}
} else {
return $value;
}
}
/**
* Set a theme settings option value in database
*
* @param null $option_id Specific option id (accepts multikey). null - all options
* @param mixed $value
*/
function fw_set_db_settings_option( $option_id = null, $value ) {
FW_WP_Option::set(
'fw_theme_settings_options:' . fw()->theme->manifest->get_id(),
$option_id, $value
);
}
}
/** Post Options */
{
/**
* Get post option value from the database
*
* @param null|int $post_id
* @param string|null $option_id Specific option id (accepts multikey). null - all options
* @param null|mixed $default_value If no option found in the database, this value will be returned
* @param null|bool $get_original_value Original value is that with no translations and other changes
*
* @return mixed|null
*/
function fw_get_db_post_option( $post_id = null, $option_id = null, $default_value = null, $get_original_value = null ) {
if ( ! $post_id ) {
/** @var WP_Post $post */
global $post;
if ( ! $post ) {
return $default_value;
} else {
$post_id = $post->ID;
}
/**
* Check if is Preview and use the preview post_id instead of real/current post id
*
* Note: WordPress changes the global $post content on preview:
* 1. https://github.com/WordPress/WordPress/blob/2096b451c704715db3c4faf699a1184260deade9/wp-includes/query.php#L3573-L3583
* 2. https://github.com/WordPress/WordPress/blob/4a31dd6fe8b774d56f901a29e72dcf9523e9ce85/wp-includes/revision.php#L485-L528
*/
if (is_preview()) {
$preview = wp_get_post_autosave($post->ID);
if ( is_object($preview) ) {
$post_id = $preview->ID;
}
}
}
$option_id = 'fw_options' . ( $option_id !== null ? '/' . $option_id : '' );
return FW_WP_Meta::get( 'post', $post_id, $option_id, $default_value, $get_original_value );
}
/**
* Set post option value in database
*
* @param null|int $post_id
* @param string|null $option_id Specific option id (accepts multikey). null - all options
* @param $value
*/
function fw_set_db_post_option( $post_id = null, $option_id = null, $value ) {
$post_id = intval($post_id);
if ( ! $post_id ) {
/** @var WP_Post $post */
global $post;
if ( ! $post ) {
return;
} else {
$post_id = $post->ID;
}
}
$old_value = fw_get_db_post_option($post_id, $option_id);
$sub_keys = explode('/', $option_id);
$base_key = array_shift($sub_keys);
$option_id = 'fw_options' . ( $option_id !== null ? '/' . $option_id : '' );
FW_WP_Meta::set( 'post', $post_id, $option_id, $value );
fw()->backend->_sync_post_separate_meta($post_id);
/**
* @since 2.2.8
*/
do_action('fw_post_options_update',
$post_id,
/**
* Option id
* First level multi-key
*
* For e.g.
*
* if $option_id is 'hello/world/7'
* this will be 'hello'
*/
$base_key,
/**
* The remaining sub-keys
*
* For e.g.
*
* if $option_id is 'hello/world/7'
* $option_id_keys will be array('world', '7')
*
* if $option_id is 'hello'
* $option_id_keys will be array()
*/
$sub_keys,
/**
* Old post option(s) value
* @since 2.3.3
*/
$old_value
);
}
}
/** Terms Options */
{
/**
* Get term option value from the database
*
* @param int $term_id
* @param string $taxonomy
* @param string|null $option_id Specific option id (accepts multikey). null - all options
* @param null|mixed $default_value If no option found in the database, this value will be returned
* @param null|bool $get_original_value Original value is that with no translations and other changes
*
* @return mixed|null
*/
function fw_get_db_term_option( $term_id, $taxonomy, $option_id = null, $default_value = null, $get_original_value = null ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
return null;
}
$option_id = 'fw_options' . ( $option_id !== null ? '/' . $option_id : '' );
return FW_WP_Meta::get( 'fw_term', $term_id, $option_id, $default_value, $get_original_value );
}
/**
* Set term option value in database
*
* @param int $term_id
* @param string $taxonomy
* @param string|null $option_id Specific option id (accepts multikey). null - all options
* @param mixed $value
*
* @return null
*/
function fw_set_db_term_option( $term_id, $taxonomy, $option_id = null, $value ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
return null;
}
$option_id = 'fw_options' . ( $option_id !== null ? '/' . $option_id : '' );
FW_WP_Meta::set( 'fw_term', $term_id, $option_id, $value );
}
}
/**
* Extensions Data
*
* Used by extensions to store custom data in database.
* By using these functions, extension prevent database spam with wp options for each extension,
* because these functions store all data in one wp option.
*/
{
/**
* Get extension's data from the database
*
* @param string $extension_name
* @param string|null $multi_key The key of the data you want to get. null - all data
* @param null|mixed $default_value If no option found in the database, this value will be returned
* @param null|bool $get_original_value Original value is that with no translations and other changes
*
* @return mixed|null
*/
function fw_get_db_extension_data( $extension_name, $multi_key = null, $default_value = null, $get_original_value = null ) {
if ( ! fw()->extensions->get( $extension_name ) ) {
trigger_error( 'Invalid extension: ' . $extension_name, E_USER_WARNING );
return null;
}
if ( $multi_key ) {
$multi_key = $extension_name . '/' . $multi_key;
} else {
$multi_key = $extension_name;
}
return FW_WP_Option::get( 'fw_extensions', $multi_key, $default_value, $get_original_value );
}
/**
* Set some extension's data in database
*
* @param string $extension_name
* @param string|null $multi_key The key of the data you want to set. null - all data
* @param mixed $value
*/
function fw_set_db_extension_data( $extension_name, $multi_key = null, $value ) {
if ( ! fw()->extensions->get( $extension_name ) ) {
trigger_error( 'Invalid extension: ' . $extension_name, E_USER_WARNING );
return;
}
if ( $multi_key ) {
$multi_key = $extension_name . '/' . $multi_key;
} else {
$multi_key = $extension_name;
}
FW_WP_Option::set( 'fw_extensions', $multi_key, $value );
}
}
/**
* Extensions Settings Options
*/
{
/**
* Get extension's settings option value from the database
*
* @param string $extension_name
* @param string|null $option_id
* @param null|mixed $default_value If no option found in the database, this value will be returned
* @param null|bool $get_original_value Original value is that with no translations and other changes
*
* @return mixed|null
*/
function fw_get_db_ext_settings_option( $extension_name, $option_id = null, $default_value = null, $get_original_value = null ) {
if ( ! ( $extension = fw()->extensions->get( $extension_name ) ) ) {
trigger_error( 'Invalid extension: ' . $extension_name, E_USER_WARNING );
return null;
}
$value = FW_WP_Option::get( 'fw_ext_settings_options:' . $extension_name, $option_id, $default_value, $get_original_value );
if ( is_null( $value ) ) {
/**
* Maybe the options was never saved or the given option id does not exists
* Extract the default values from the options array and try to find there the option id
*/
$cache_key = 'fw_default_options_values/ext_settings:' . $extension_name;
try {
$all_options_values = FW_Cache::get( $cache_key );
} catch ( FW_Cache_Not_Found_Exception $e ) {
// extract the default values from options array
$all_options_values = fw_get_options_values_from_input(
$extension->get_settings_options(),
array()
);
FW_Cache::set( $cache_key, $all_options_values );
}
if ( empty( $option_id ) ) {
// option id not specified, return all options values
return $all_options_values;
} else {
return fw_akg( $option_id, $all_options_values, $default_value );
}
} else {
return $value;
}
}
/**
* Set extension's setting option value in database
*
* @param string $extension_name
* @param string|null $option_id
* @param mixed $value
*/
function fw_set_db_ext_settings_option( $extension_name, $option_id = null, $value ) {
if ( ! fw()->extensions->get( $extension_name ) ) {
trigger_error( 'Invalid extension: ' . $extension_name, E_USER_WARNING );
return;
}
FW_WP_Option::set( 'fw_ext_settings_options:' . $extension_name, $option_id, $value );
}
}
{
/**
* Get user meta set by specific extension
*
* @param int $user_id
* @param string $extension_name
* @param string $keys
*
* If the extension doesn't exist or is disabled, or meta key doesn't exist, returns null,
* else returns the meta key value
*
* @return mixed|null
*/
function fw_get_db_extension_user_data( $user_id, $extension_name, $keys = null ) {
if ( ! fw()->extensions->get( $extension_name ) ) {
trigger_error( 'Invalid extension: ' . $extension_name, E_USER_WARNING );
return null;
}
$data = get_user_meta( $user_id, 'fw_data', true );
if ( is_null( $keys ) ) {
return fw_akg( $extension_name, $data );
}
return fw_akg( $extension_name . '/' . $keys, $data );
}
/**
* @param int $user_id
* @param string $extension_name
* @param mixed $value
* @param string $keys
*
* In case the extension doesn't exist or is disabled, or the value is equal to previous, returns false
*
* @return bool|int
*/
function fw_set_db_extension_user_data( $user_id, $extension_name, $value, $keys = null ) {
if ( ! fw()->extensions->get( $extension_name ) ) {
trigger_error( 'Invalid extension: ' . $extension_name, E_USER_WARNING );
return false;
}
$data = get_user_meta( $user_id, 'fw_data', true );
if ( $keys == null ) {
fw_aks( $extension_name, $value, $data );
} else {
fw_aks( $extension_name . '/' . $keys, $value, $data );
}
return fw_update_user_meta( $user_id, 'fw_data', $data );
}
}
/** Customizer Framework Options */
{
/**
* Get a customizer framework option value from the database
*
* @param string|null $option_id Specific option id (accepts multikey). null - all options
* @param null|mixed $default_value If no option found in the database, this value will be returned
* // fixme: Maybe add this parameter? @ param null|bool $get_original_value Original value is that with no translations and other changes
*
* @return mixed|null
*/
function fw_get_db_customizer_option( $option_id = null, $default_value = null ) {
// note: this contains only changed controls/options
$db_values = get_theme_mod(FW_Option_Type::get_default_name_prefix(), null);
if (
!is_null($default_value)
&&
is_null($option_id ? fw_akg($option_id, $db_values) : $db_values)
) {
/**
* Default value was provided in case db value is empty.
*
* Do not extract default values from options files (below)
* maybe this function was called from options files and it will cause infinite loop,
* that's why the developer provided a default value to prevent that.
*/
return $default_value;
}
if (is_null($db_values)) {
$db_values = array();
}
if (
is_null($option_id)
||
(
($base_key = explode('/', $option_id)) // note: option_id can be a multi-key 'a/b/c'
&&
($base_key = array_shift($base_key))
&&
!array_key_exists($base_key, $db_values)
)
) {
// extract options default values
{
$cache_key = 'fw_default_options_values/customizer';
try {
$default_values = FW_Cache::get( $cache_key );
} catch ( FW_Cache_Not_Found_Exception $e ) {
// extract the default values from options array
$default_values = fw_get_options_values_from_input(
fw()->theme->get_customizer_options(),
array()
);
FW_Cache::set( $cache_key, $default_values );
}
}
$db_values = array_merge($default_values, $db_values);
}
return is_null($option_id)
? $db_values
: fw_akg($option_id, $db_values, $default_value);
}
/**
* Set a theme customizer option value in database
*
* @param null $option_id Specific option id (accepts multikey). null - all options
* @param mixed $value
*/
function fw_set_db_customizer_option( $option_id = null, $value ) {
$db_value = get_theme_mod(FW_Option_Type::get_default_name_prefix(), array());
if (is_null($option_id)) {
$db_value = $value;
} else {
fw_aks($option_id, $value, $db_value);
}
set_theme_mod(
FW_Option_Type::get_default_name_prefix(),
$db_value
);
}
}