33 $option = trim( $option ); |
33 $option = trim( $option ); |
34 if ( empty( $option ) ) { |
34 if ( empty( $option ) ) { |
35 return false; |
35 return false; |
36 } |
36 } |
37 |
37 |
|
38 /* |
|
39 * Until a proper _deprecated_option() function can be introduced, |
|
40 * redirect requests to deprecated keys to the new, correct ones. |
|
41 */ |
|
42 $deprecated_keys = array( |
|
43 'blacklist_keys' => 'disallowed_keys', |
|
44 'comment_whitelist' => 'comment_previously_approved', |
|
45 ); |
|
46 |
|
47 if ( ! wp_installing() && isset( $deprecated_keys[ $option ] ) ) { |
|
48 _deprecated_argument( |
|
49 __FUNCTION__, |
|
50 '5.5.0', |
|
51 sprintf( |
|
52 /* translators: 1: Deprecated option key, 2: New option key. */ |
|
53 __( 'The "%1$s" option key has been renamed to "%2$s".' ), |
|
54 $option, |
|
55 $deprecated_keys[ $option ] |
|
56 ) |
|
57 ); |
|
58 return get_option( $deprecated_keys[ $option ], $default ); |
|
59 } |
|
60 |
38 /** |
61 /** |
39 * Filters the value of an existing option before it is retrieved. |
62 * Filters the value of an existing option before it is retrieved. |
40 * |
63 * |
41 * The dynamic portion of the hook name, `$option`, refers to the option name. |
64 * The dynamic portion of the hook name, `$option`, refers to the option name. |
42 * |
65 * |
43 * Passing a truthy value to the filter will short-circuit retrieving |
66 * Returning a truthy value from the filter will effectively short-circuit retrieval |
44 * the option value, returning the passed value instead. |
67 * and return the passed value instead. |
45 * |
68 * |
46 * @since 1.5.0 |
69 * @since 1.5.0 |
47 * @since 4.4.0 The `$option` parameter was added. |
70 * @since 4.4.0 The `$option` parameter was added. |
48 * @since 4.9.0 The `$default` parameter was added. |
71 * @since 4.9.0 The `$default` parameter was added. |
49 * |
72 * |
50 * @param bool|mixed $pre_option The value to return instead of the option value. This differs from |
73 * @param mixed $pre_option The value to return instead of the option value. This differs |
51 * `$default`, which is used as the fallback value in the event the option |
74 * from `$default`, which is used as the fallback value in the event |
52 * doesn't exist elsewhere in get_option(). Default false (to skip past the |
75 * the option doesn't exist elsewhere in get_option(). |
53 * short-circuit). |
76 * Default false (to skip past the short-circuit). |
54 * @param string $option Option name. |
77 * @param string $option Option name. |
55 * @param mixed $default The fallback value to return if the option does not exist. |
78 * @param mixed $default The fallback value to return if the option does not exist. |
56 * Default is false. |
79 * Default false. |
57 */ |
80 */ |
58 $pre = apply_filters( "pre_option_{$option}", false, $option, $default ); |
81 $pre = apply_filters( "pre_option_{$option}", false, $option, $default ); |
59 |
82 |
60 if ( false !== $pre ) { |
83 if ( false !== $pre ) { |
61 return $pre; |
84 return $pre; |
97 $value = wp_cache_get( $option, 'options' ); |
121 $value = wp_cache_get( $option, 'options' ); |
98 |
122 |
99 if ( false === $value ) { |
123 if ( false === $value ) { |
100 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); |
124 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); |
101 |
125 |
102 // Has to be get_row instead of get_var because of funkiness with 0, false, null values |
126 // Has to be get_row() instead of get_var() because of funkiness with 0, false, null values. |
103 if ( is_object( $row ) ) { |
127 if ( is_object( $row ) ) { |
104 $value = $row->option_value; |
128 $value = $row->option_value; |
105 wp_cache_add( $option, $value, 'options' ); |
129 wp_cache_add( $option, $value, 'options' ); |
106 } else { // option does not exist, so we must cache its non-existence |
130 } else { // Option does not exist, so we must cache its non-existence. |
107 if ( ! is_array( $notoptions ) ) { |
131 if ( ! is_array( $notoptions ) ) { |
108 $notoptions = array(); |
132 $notoptions = array(); |
109 } |
133 } |
|
134 |
110 $notoptions[ $option ] = true; |
135 $notoptions[ $option ] = true; |
111 wp_cache_set( 'notoptions', $notoptions, 'options' ); |
136 wp_cache_set( 'notoptions', $notoptions, 'options' ); |
112 |
137 |
113 /** This filter is documented in wp-includes/option.php */ |
138 /** This filter is documented in wp-includes/option.php */ |
114 return apply_filters( "default_option_{$option}", $default, $option, $passed_default ); |
139 return apply_filters( "default_option_{$option}", $default, $option, $passed_default ); |
117 } |
142 } |
118 } else { |
143 } else { |
119 $suppress = $wpdb->suppress_errors(); |
144 $suppress = $wpdb->suppress_errors(); |
120 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); |
145 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); |
121 $wpdb->suppress_errors( $suppress ); |
146 $wpdb->suppress_errors( $suppress ); |
|
147 |
122 if ( is_object( $row ) ) { |
148 if ( is_object( $row ) ) { |
123 $value = $row->option_value; |
149 $value = $row->option_value; |
124 } else { |
150 } else { |
125 /** This filter is documented in wp-includes/option.php */ |
151 /** This filter is documented in wp-includes/option.php */ |
126 return apply_filters( "default_option_{$option}", $default, $option, $passed_default ); |
152 return apply_filters( "default_option_{$option}", $default, $option, $passed_default ); |
127 } |
153 } |
128 } |
154 } |
129 |
155 |
130 // If home is not set use siteurl. |
156 // If home is not set, use siteurl. |
131 if ( 'home' == $option && '' == $value ) { |
157 if ( 'home' === $option && '' === $value ) { |
132 return get_option( 'siteurl' ); |
158 return get_option( 'siteurl' ); |
133 } |
159 } |
134 |
160 |
135 if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) ) { |
161 if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ), true ) ) { |
136 $value = untrailingslashit( $value ); |
162 $value = untrailingslashit( $value ); |
137 } |
163 } |
138 |
164 |
139 /** |
165 /** |
140 * Filters the value of an existing option. |
166 * Filters the value of an existing option. |
181 |
213 |
182 /** |
214 /** |
183 * Loads and caches all autoloaded options, if available or all options. |
215 * Loads and caches all autoloaded options, if available or all options. |
184 * |
216 * |
185 * @since 2.2.0 |
217 * @since 2.2.0 |
|
218 * @since 5.3.1 The `$force_cache` parameter was added. |
186 * |
219 * |
187 * @global wpdb $wpdb WordPress database abstraction object. |
220 * @global wpdb $wpdb WordPress database abstraction object. |
188 * |
221 * |
|
222 * @param bool $force_cache Optional. Whether to force an update of the local cache |
|
223 * from the persistent cache. Default false. |
189 * @return array List of all options. |
224 * @return array List of all options. |
190 */ |
225 */ |
191 function wp_load_alloptions() { |
226 function wp_load_alloptions( $force_cache = false ) { |
192 global $wpdb; |
227 global $wpdb; |
193 |
228 |
194 if ( ! wp_installing() || ! is_multisite() ) { |
229 if ( ! wp_installing() || ! is_multisite() ) { |
195 $alloptions = wp_cache_get( 'alloptions', 'options' ); |
230 $alloptions = wp_cache_get( 'alloptions', 'options', $force_cache ); |
196 } else { |
231 } else { |
197 $alloptions = false; |
232 $alloptions = false; |
198 } |
233 } |
199 |
234 |
200 if ( ! $alloptions ) { |
235 if ( ! $alloptions ) { |
201 $suppress = $wpdb->suppress_errors(); |
236 $suppress = $wpdb->suppress_errors(); |
202 if ( ! $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) { |
237 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ); |
|
238 if ( ! $alloptions_db ) { |
203 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ); |
239 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ); |
204 } |
240 } |
205 $wpdb->suppress_errors( $suppress ); |
241 $wpdb->suppress_errors( $suppress ); |
206 |
242 |
207 $alloptions = array(); |
243 $alloptions = array(); |
265 wp_cache_set( $cache_key, $option->meta_value, 'site-options' ); |
302 wp_cache_set( $cache_key, $option->meta_value, 'site-options' ); |
266 } |
303 } |
267 } |
304 } |
268 |
305 |
269 /** |
306 /** |
270 * Update the value of an option that was already added. |
307 * Updates the value of an option that was already added. |
271 * |
308 * |
272 * You do not need to serialize values. If the value needs to be serialized, then |
309 * You do not need to serialize values. If the value needs to be serialized, |
273 * it will be serialized before it is inserted into the database. Remember, |
310 * then it will be serialized before it is inserted into the database. |
274 * resources can not be serialized or added as an option. |
311 * Remember, resources cannot be serialized or added as an option. |
275 * |
312 * |
276 * If the option does not exist, then the option will be added with the option value, |
313 * If the option does not exist, it will be created. |
277 * with an `$autoload` value of 'yes'. |
314 |
|
315 * This function is designed to work with or without a logged-in user. In terms of security, |
|
316 * plugin developers should check the current user's capabilities before updating any options. |
278 * |
317 * |
279 * @since 1.0.0 |
318 * @since 1.0.0 |
280 * @since 4.2.0 The `$autoload` parameter was added. |
319 * @since 4.2.0 The `$autoload` parameter was added. |
281 * |
320 * |
282 * @global wpdb $wpdb WordPress database abstraction object. |
321 * @global wpdb $wpdb WordPress database abstraction object. |
283 * |
322 * |
284 * @param string $option Option name. Expected to not be SQL-escaped. |
323 * @param string $option Name of the option to update. Expected to not be SQL-escaped. |
285 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. |
324 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. |
286 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options, |
325 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options, |
287 * `$autoload` can only be updated using `update_option()` if `$value` is also changed. |
326 * `$autoload` can only be updated using `update_option()` if `$value` is also changed. |
288 * Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options, |
327 * Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options, |
289 * the default value is 'yes'. Default null. |
328 * the default value is 'yes'. Default null. |
290 * @return bool False if value was not updated and true if value was updated. |
329 * @return bool True if the value was updated, false otherwise. |
291 */ |
330 */ |
292 function update_option( $option, $value, $autoload = null ) { |
331 function update_option( $option, $value, $autoload = null ) { |
293 global $wpdb; |
332 global $wpdb; |
294 |
333 |
295 $option = trim( $option ); |
334 $option = trim( $option ); |
296 if ( empty( $option ) ) { |
335 if ( empty( $option ) ) { |
297 return false; |
336 return false; |
|
337 } |
|
338 |
|
339 /* |
|
340 * Until a proper _deprecated_option() function can be introduced, |
|
341 * redirect requests to deprecated keys to the new, correct ones. |
|
342 */ |
|
343 $deprecated_keys = array( |
|
344 'blacklist_keys' => 'disallowed_keys', |
|
345 'comment_whitelist' => 'comment_previously_approved', |
|
346 ); |
|
347 |
|
348 if ( ! wp_installing() && isset( $deprecated_keys[ $option ] ) ) { |
|
349 _deprecated_argument( |
|
350 __FUNCTION__, |
|
351 '5.5.0', |
|
352 sprintf( |
|
353 /* translators: 1: Deprecated option key, 2: New option key. */ |
|
354 __( 'The "%1$s" option key has been renamed to "%2$s".' ), |
|
355 $option, |
|
356 $deprecated_keys[ $option ] |
|
357 ) |
|
358 ); |
|
359 return update_option( $deprecated_keys[ $option ], $value, $autoload ); |
298 } |
360 } |
299 |
361 |
300 wp_protect_special_option( $option ); |
362 wp_protect_special_option( $option ); |
301 |
363 |
302 if ( is_object( $value ) ) { |
364 if ( is_object( $value ) ) { |
418 * @param string $option Name of the updated option. |
481 * @param string $option Name of the updated option. |
419 * @param mixed $old_value The old option value. |
482 * @param mixed $old_value The old option value. |
420 * @param mixed $value The new option value. |
483 * @param mixed $value The new option value. |
421 */ |
484 */ |
422 do_action( 'updated_option', $option, $old_value, $value ); |
485 do_action( 'updated_option', $option, $old_value, $value ); |
|
486 |
423 return true; |
487 return true; |
424 } |
488 } |
425 |
489 |
426 /** |
490 /** |
427 * Add a new option. |
491 * Adds a new option. |
428 * |
492 * |
429 * You do not need to serialize values. If the value needs to be serialized, then |
493 * You do not need to serialize values. If the value needs to be serialized, |
430 * it will be serialized before it is inserted into the database. Remember, |
494 * then it will be serialized before it is inserted into the database. |
431 * resources can not be serialized or added as an option. |
495 * Remember, resources cannot be serialized or added as an option. |
432 * |
496 * |
433 * You can create options without values and then update the values later. |
497 * You can create options without values and then update the values later. |
434 * Existing options will not be updated and checks are performed to ensure that you |
498 * Existing options will not be updated and checks are performed to ensure that you |
435 * aren't adding a protected WordPress option. Care should be taken to not name |
499 * aren't adding a protected WordPress option. Care should be taken to not name |
436 * options the same as the ones which are protected. |
500 * options the same as the ones which are protected. |
437 * |
501 * |
438 * @since 1.0.0 |
502 * @since 1.0.0 |
439 * |
503 * |
440 * @global wpdb $wpdb WordPress database abstraction object. |
504 * @global wpdb $wpdb WordPress database abstraction object. |
441 * |
505 * |
442 * @param string $option Name of option to add. Expected to not be SQL-escaped. |
506 * @param string $option Name of the option to add. Expected to not be SQL-escaped. |
443 * @param mixed $value Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. |
507 * @param mixed $value Optional. Option value. Must be serializable if non-scalar. |
444 * @param string $deprecated Optional. Description. Not used anymore. |
508 * Expected to not be SQL-escaped. |
445 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. |
509 * @param string $deprecated Optional. Description. Not used anymore. |
446 * Default is enabled. Accepts 'no' to disable for legacy reasons. |
510 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. |
447 * @return bool False if option was not added and true if option was added. |
511 * Default is enabled. Accepts 'no' to disable for legacy reasons. |
|
512 * @return bool True if the option was added, false otherwise. |
448 */ |
513 */ |
449 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) { |
514 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) { |
450 global $wpdb; |
515 global $wpdb; |
451 |
516 |
452 if ( ! empty( $deprecated ) ) { |
517 if ( ! empty( $deprecated ) ) { |
456 $option = trim( $option ); |
521 $option = trim( $option ); |
457 if ( empty( $option ) ) { |
522 if ( empty( $option ) ) { |
458 return false; |
523 return false; |
459 } |
524 } |
460 |
525 |
|
526 /* |
|
527 * Until a proper _deprecated_option() function can be introduced, |
|
528 * redirect requests to deprecated keys to the new, correct ones. |
|
529 */ |
|
530 $deprecated_keys = array( |
|
531 'blacklist_keys' => 'disallowed_keys', |
|
532 'comment_whitelist' => 'comment_previously_approved', |
|
533 ); |
|
534 |
|
535 if ( ! wp_installing() && isset( $deprecated_keys[ $option ] ) ) { |
|
536 _deprecated_argument( |
|
537 __FUNCTION__, |
|
538 '5.5.0', |
|
539 sprintf( |
|
540 /* translators: 1: Deprecated option key, 2: New option key. */ |
|
541 __( 'The "%1$s" option key has been renamed to "%2$s".' ), |
|
542 $option, |
|
543 $deprecated_keys[ $option ] |
|
544 ) |
|
545 ); |
|
546 return add_option( $deprecated_keys[ $option ], $value, $deprecated, $autoload ); |
|
547 } |
|
548 |
461 wp_protect_special_option( $option ); |
549 wp_protect_special_option( $option ); |
462 |
550 |
463 if ( is_object( $value ) ) { |
551 if ( is_object( $value ) ) { |
464 $value = clone $value; |
552 $value = clone $value; |
465 } |
553 } |
466 |
554 |
467 $value = sanitize_option( $option, $value ); |
555 $value = sanitize_option( $option, $value ); |
468 |
556 |
469 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query |
557 // Make sure the option doesn't already exist. |
|
558 // We can check the 'notoptions' cache before we ask for a DB query. |
470 $notoptions = wp_cache_get( 'notoptions', 'options' ); |
559 $notoptions = wp_cache_get( 'notoptions', 'options' ); |
|
560 |
471 if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) { |
561 if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) { |
472 /** This filter is documented in wp-includes/option.php */ |
562 /** This filter is documented in wp-includes/option.php */ |
473 if ( apply_filters( "default_option_{$option}", false, $option, false ) !== get_option( $option ) ) { |
563 if ( apply_filters( "default_option_{$option}", false, $option, false ) !== get_option( $option ) ) { |
474 return false; |
564 return false; |
475 } |
565 } |
492 if ( ! $result ) { |
582 if ( ! $result ) { |
493 return false; |
583 return false; |
494 } |
584 } |
495 |
585 |
496 if ( ! wp_installing() ) { |
586 if ( ! wp_installing() ) { |
497 if ( 'yes' == $autoload ) { |
587 if ( 'yes' === $autoload ) { |
498 $alloptions = wp_load_alloptions(); |
588 $alloptions = wp_load_alloptions( true ); |
499 $alloptions[ $option ] = $serialized_value; |
589 $alloptions[ $option ] = $serialized_value; |
500 wp_cache_set( 'alloptions', $alloptions, 'options' ); |
590 wp_cache_set( 'alloptions', $alloptions, 'options' ); |
501 } else { |
591 } else { |
502 wp_cache_set( $option, $serialized_value, 'options' ); |
592 wp_cache_set( $option, $serialized_value, 'options' ); |
503 } |
593 } |
504 } |
594 } |
505 |
595 |
506 // This option exists now |
596 // This option exists now. |
507 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh |
597 $notoptions = wp_cache_get( 'notoptions', 'options' ); // Yes, again... we need it to be fresh. |
|
598 |
508 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { |
599 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { |
509 unset( $notoptions[ $option ] ); |
600 unset( $notoptions[ $option ] ); |
510 wp_cache_set( 'notoptions', $notoptions, 'options' ); |
601 wp_cache_set( 'notoptions', $notoptions, 'options' ); |
511 } |
602 } |
512 |
603 |
667 * @return mixed Value of transient. |
764 * @return mixed Value of transient. |
668 */ |
765 */ |
669 function get_transient( $transient ) { |
766 function get_transient( $transient ) { |
670 |
767 |
671 /** |
768 /** |
672 * Filters the value of an existing transient. |
769 * Filters the value of an existing transient before it is retrieved. |
673 * |
770 * |
674 * The dynamic portion of the hook name, `$transient`, refers to the transient name. |
771 * The dynamic portion of the hook name, `$transient`, refers to the transient name. |
675 * |
772 * |
676 * Passing a truthy value to the filter will effectively short-circuit retrieval |
773 * Returning a truthy value from the filter will effectively short-circuit retrieval |
677 * of the transient, returning the passed value instead. |
774 * and return the passed value instead. |
678 * |
775 * |
679 * @since 2.8.0 |
776 * @since 2.8.0 |
680 * @since 4.4.0 The `$transient` parameter was added |
777 * @since 4.4.0 The `$transient` parameter was added |
681 * |
778 * |
682 * @param mixed $pre_transient The default value to return if the transient does not exist. |
779 * @param mixed $pre_transient The default value to return if the transient does not exist. |
683 * Any value other than false will short-circuit the retrieval |
780 * Any value other than false will short-circuit the retrieval |
684 * of the transient, and return the returned value. |
781 * of the transient, and return that value. |
685 * @param string $transient Transient name. |
782 * @param string $transient Transient name. |
686 */ |
783 */ |
687 $pre = apply_filters( "pre_transient_{$transient}", false, $transient ); |
784 $pre = apply_filters( "pre_transient_{$transient}", false, $transient ); |
|
785 |
688 if ( false !== $pre ) { |
786 if ( false !== $pre ) { |
689 return $pre; |
787 return $pre; |
690 } |
788 } |
691 |
789 |
692 if ( wp_using_ext_object_cache() ) { |
790 if ( wp_using_ext_object_cache() ) { |
693 $value = wp_cache_get( $transient, 'transient' ); |
791 $value = wp_cache_get( $transient, 'transient' ); |
694 } else { |
792 } else { |
695 $transient_option = '_transient_' . $transient; |
793 $transient_option = '_transient_' . $transient; |
696 if ( ! wp_installing() ) { |
794 if ( ! wp_installing() ) { |
697 // If option is not in alloptions, it is not autoloaded and thus has a timeout |
795 // If option is not in alloptions, it is not autoloaded and thus has a timeout. |
698 $alloptions = wp_load_alloptions(); |
796 $alloptions = wp_load_alloptions(); |
699 if ( ! isset( $alloptions[ $transient_option ] ) ) { |
797 if ( ! isset( $alloptions[ $transient_option ] ) ) { |
700 $transient_timeout = '_transient_timeout_' . $transient; |
798 $transient_timeout = '_transient_timeout_' . $transient; |
701 $timeout = get_option( $transient_timeout ); |
799 $timeout = get_option( $transient_timeout ); |
702 if ( false !== $timeout && $timeout < time() ) { |
800 if ( false !== $timeout && $timeout < time() ) { |
725 */ |
823 */ |
726 return apply_filters( "transient_{$transient}", $value, $transient ); |
824 return apply_filters( "transient_{$transient}", $value, $transient ); |
727 } |
825 } |
728 |
826 |
729 /** |
827 /** |
730 * Set/update the value of a transient. |
828 * Sets/updates the value of a transient. |
731 * |
829 * |
732 * You do not need to serialize values. If the value needs to be serialized, then |
830 * You do not need to serialize values. If the value needs to be serialized, |
733 * it will be serialized before it is set. |
831 * then it will be serialized before it is set. |
734 * |
832 * |
735 * @since 2.8.0 |
833 * @since 2.8.0 |
736 * |
834 * |
737 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be |
835 * @param string $transient Transient name. Expected to not be SQL-escaped. |
738 * 172 characters or fewer in length. |
836 * Must be 172 characters or fewer in length. |
739 * @param mixed $value Transient value. Must be serializable if non-scalar. |
837 * @param mixed $value Transient value. Must be serializable if non-scalar. |
740 * Expected to not be SQL-escaped. |
838 * Expected to not be SQL-escaped. |
741 * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration). |
839 * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration). |
742 * @return bool False if value was not set and true if value was set. |
840 * @return bool True if the value was set, false otherwise. |
743 */ |
841 */ |
744 function set_transient( $transient, $value, $expiration = 0 ) { |
842 function set_transient( $transient, $value, $expiration = 0 ) { |
745 |
843 |
746 $expiration = (int) $expiration; |
844 $expiration = (int) $expiration; |
747 |
845 |
787 $result = add_option( $transient_option, $value, '', $autoload ); |
886 $result = add_option( $transient_option, $value, '', $autoload ); |
788 } else { |
887 } else { |
789 // If expiration is requested, but the transient has no timeout option, |
888 // If expiration is requested, but the transient has no timeout option, |
790 // delete, then re-create transient rather than update. |
889 // delete, then re-create transient rather than update. |
791 $update = true; |
890 $update = true; |
|
891 |
792 if ( $expiration ) { |
892 if ( $expiration ) { |
793 if ( false === get_option( $transient_timeout ) ) { |
893 if ( false === get_option( $transient_timeout ) ) { |
794 delete_option( $transient_option ); |
894 delete_option( $transient_option ); |
795 add_option( $transient_timeout, time() + $expiration, '', 'no' ); |
895 add_option( $transient_timeout, time() + $expiration, '', 'no' ); |
796 $result = add_option( $transient_option, $value, '', 'no' ); |
896 $result = add_option( $transient_option, $value, '', 'no' ); |
797 $update = false; |
897 $update = false; |
798 } else { |
898 } else { |
799 update_option( $transient_timeout, time() + $expiration ); |
899 update_option( $transient_timeout, time() + $expiration ); |
800 } |
900 } |
801 } |
901 } |
|
902 |
802 if ( $update ) { |
903 if ( $update ) { |
803 $result = update_option( $transient_option, $value ); |
904 $result = update_option( $transient_option, $value ); |
804 } |
905 } |
805 } |
906 } |
806 } |
907 } |
924 $settings = (string) get_user_option( 'user-settings', $user_id ); |
1027 $settings = (string) get_user_option( 'user-settings', $user_id ); |
925 |
1028 |
926 if ( isset( $_COOKIE[ 'wp-settings-' . $user_id ] ) ) { |
1029 if ( isset( $_COOKIE[ 'wp-settings-' . $user_id ] ) ) { |
927 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE[ 'wp-settings-' . $user_id ] ); |
1030 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE[ 'wp-settings-' . $user_id ] ); |
928 |
1031 |
929 // No change or both empty |
1032 // No change or both empty. |
930 if ( $cookie == $settings ) { |
1033 if ( $cookie == $settings ) { |
931 return; |
1034 return; |
932 } |
1035 } |
933 |
1036 |
934 $last_saved = (int) get_user_option( 'user-settings-time', $user_id ); |
1037 $last_saved = (int) get_user_option( 'user-settings-time', $user_id ); |
935 $current = isset( $_COOKIE[ 'wp-settings-time-' . $user_id ] ) ? preg_replace( '/[^0-9]/', '', $_COOKIE[ 'wp-settings-time-' . $user_id ] ) : 0; |
1038 $current = isset( $_COOKIE[ 'wp-settings-time-' . $user_id ] ) ? preg_replace( '/[^0-9]/', '', $_COOKIE[ 'wp-settings-time-' . $user_id ] ) : 0; |
936 |
1039 |
937 // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is |
1040 // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is. |
938 if ( $current > $last_saved ) { |
1041 if ( $current > $last_saved ) { |
939 update_user_option( $user_id, 'user-settings', $cookie, false ); |
1042 update_user_option( $user_id, 'user-settings', $cookie, false ); |
940 update_user_option( $user_id, 'user-settings-time', time() - 5, false ); |
1043 update_user_option( $user_id, 'user-settings-time', time() - 5, false ); |
941 return; |
1044 return; |
942 } |
1045 } |
948 setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure ); |
1051 setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure ); |
949 $_COOKIE[ 'wp-settings-' . $user_id ] = $settings; |
1052 $_COOKIE[ 'wp-settings-' . $user_id ] = $settings; |
950 } |
1053 } |
951 |
1054 |
952 /** |
1055 /** |
953 * Retrieve user interface setting value based on setting name. |
1056 * Retrieves user interface setting value based on setting name. |
954 * |
1057 * |
955 * @since 2.7.0 |
1058 * @since 2.7.0 |
956 * |
1059 * |
957 * @param string $name The name of the setting. |
1060 * @param string $name The name of the setting. |
958 * @param string $default Optional default value to return when $name is not set. |
1061 * @param string $default Optional default value to return when $name is not set. |
959 * @return mixed the last saved user setting or the default value/false if it doesn't exist. |
1062 * @return mixed The last saved user setting or the default value/false if it doesn't exist. |
960 */ |
1063 */ |
961 function get_user_setting( $name, $default = false ) { |
1064 function get_user_setting( $name, $default = false ) { |
962 $all_user_settings = get_all_user_settings(); |
1065 $all_user_settings = get_all_user_settings(); |
963 |
1066 |
964 return isset( $all_user_settings[ $name ] ) ? $all_user_settings[ $name ] : $default; |
1067 return isset( $all_user_settings[ $name ] ) ? $all_user_settings[ $name ] : $default; |
965 } |
1068 } |
966 |
1069 |
967 /** |
1070 /** |
968 * Add or update user interface setting. |
1071 * Adds or updates user interface setting. |
969 * |
1072 * |
970 * Both $name and $value can contain only ASCII letters, numbers and underscores. |
1073 * Both $name and $value can contain only ASCII letters, numbers, hyphens, and underscores. |
971 * |
1074 * |
972 * This function has to be used before any output has started as it calls setcookie(). |
1075 * This function has to be used before any output has started as it calls setcookie(). |
973 * |
1076 * |
974 * @since 2.8.0 |
1077 * @since 2.8.0 |
975 * |
1078 * |
976 * @param string $name The name of the setting. |
1079 * @param string $name The name of the setting. |
977 * @param string $value The value for the setting. |
1080 * @param string $value The value for the setting. |
978 * @return bool|null True if set successfully, false if not. Null if the current user can't be established. |
1081 * @return bool|null True if set successfully, false otherwise. |
|
1082 * Null if the current user is not a member of the site. |
979 */ |
1083 */ |
980 function set_user_setting( $name, $value ) { |
1084 function set_user_setting( $name, $value ) { |
981 if ( headers_sent() ) { |
1085 if ( headers_sent() ) { |
982 return false; |
1086 return false; |
983 } |
1087 } |
1126 * @since 4.4.0 The `$use_cache` parameter was deprecated. |
1234 * @since 4.4.0 The `$use_cache` parameter was deprecated. |
1127 * @since 4.4.0 Modified into wrapper for get_network_option() |
1235 * @since 4.4.0 Modified into wrapper for get_network_option() |
1128 * |
1236 * |
1129 * @see get_network_option() |
1237 * @see get_network_option() |
1130 * |
1238 * |
1131 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
1239 * @param string $option Name of the option to retrieve. Expected to not be SQL-escaped. |
1132 * @param mixed $default Optional value to return if option doesn't exist. Default false. |
1240 * @param mixed $default Optional. Value to return if the option doesn't exist. Default false. |
1133 * @param bool $deprecated Whether to use cache. Multisite only. Always set to true. |
1241 * @param bool $deprecated Whether to use cache. Multisite only. Always set to true. |
1134 * @return mixed Value set for the option. |
1242 * @return mixed Value set for the option. |
1135 */ |
1243 */ |
1136 function get_site_option( $option, $default = false, $deprecated = true ) { |
1244 function get_site_option( $option, $default = false, $deprecated = true ) { |
1137 return get_network_option( null, $option, $default ); |
1245 return get_network_option( null, $option, $default ); |
1138 } |
1246 } |
1139 |
1247 |
1140 /** |
1248 /** |
1141 * Add a new option for the current network. |
1249 * Adds a new option for the current network. |
1142 * |
1250 * |
1143 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case. |
1251 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case. |
1144 * |
1252 * |
1145 * @since 2.8.0 |
1253 * @since 2.8.0 |
1146 * @since 4.4.0 Modified into wrapper for add_network_option() |
1254 * @since 4.4.0 Modified into wrapper for add_network_option() |
1147 * |
1255 * |
1148 * @see add_network_option() |
1256 * @see add_network_option() |
1149 * |
1257 * |
1150 * @param string $option Name of option to add. Expected to not be SQL-escaped. |
1258 * @param string $option Name of the option to add. Expected to not be SQL-escaped. |
1151 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped. |
1259 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped. |
1152 * @return bool False if the option was not added. True if the option was added. |
1260 * @return bool True if the option was added, false otherwise. |
1153 */ |
1261 */ |
1154 function add_site_option( $option, $value ) { |
1262 function add_site_option( $option, $value ) { |
1155 return add_network_option( null, $option, $value ); |
1263 return add_network_option( null, $option, $value ); |
1156 } |
1264 } |
1157 |
1265 |
1161 * @since 2.8.0 |
1269 * @since 2.8.0 |
1162 * @since 4.4.0 Modified into wrapper for delete_network_option() |
1270 * @since 4.4.0 Modified into wrapper for delete_network_option() |
1163 * |
1271 * |
1164 * @see delete_network_option() |
1272 * @see delete_network_option() |
1165 * |
1273 * |
1166 * @param string $option Name of option to remove. Expected to not be SQL-escaped. |
1274 * @param string $option Name of the option to delete. Expected to not be SQL-escaped. |
1167 * @return bool True, if succeed. False, if failure. |
1275 * @return bool True if the option was deleted, false otherwise. |
1168 */ |
1276 */ |
1169 function delete_site_option( $option ) { |
1277 function delete_site_option( $option ) { |
1170 return delete_network_option( null, $option ); |
1278 return delete_network_option( null, $option ); |
1171 } |
1279 } |
1172 |
1280 |
1173 /** |
1281 /** |
1174 * Update the value of an option that was already added for the current network. |
1282 * Updates the value of an option that was already added for the current network. |
1175 * |
1283 * |
1176 * @since 2.8.0 |
1284 * @since 2.8.0 |
1177 * @since 4.4.0 Modified into wrapper for update_network_option() |
1285 * @since 4.4.0 Modified into wrapper for update_network_option() |
1178 * |
1286 * |
1179 * @see update_network_option() |
1287 * @see update_network_option() |
1180 * |
1288 * |
1181 * @param string $option Name of option. Expected to not be SQL-escaped. |
1289 * @param string $option Name of the option. Expected to not be SQL-escaped. |
1182 * @param mixed $value Option value. Expected to not be SQL-escaped. |
1290 * @param mixed $value Option value. Expected to not be SQL-escaped. |
1183 * @return bool False if value was not updated. True if value was updated. |
1291 * @return bool True if the value was updated, false otherwise. |
1184 */ |
1292 */ |
1185 function update_site_option( $option, $value ) { |
1293 function update_site_option( $option, $value ) { |
1186 return update_network_option( null, $option, $value ); |
1294 return update_network_option( null, $option, $value ); |
1187 } |
1295 } |
1188 |
1296 |
1189 /** |
1297 /** |
1190 * Retrieve a network's option value based on the option name. |
1298 * Retrieves a network's option value based on the option name. |
1191 * |
1299 * |
1192 * @since 4.4.0 |
1300 * @since 4.4.0 |
1193 * |
1301 * |
1194 * @see get_option() |
1302 * @see get_option() |
1195 * |
1303 * |
1196 * @global wpdb $wpdb WordPress database abstraction object. |
1304 * @global wpdb $wpdb WordPress database abstraction object. |
1197 * |
1305 * |
1198 * @param int $network_id ID of the network. Can be null to default to the current network ID. |
1306 * @param int $network_id ID of the network. Can be null to default to the current network ID. |
1199 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
1307 * @param string $option Name of the option to retrieve. Expected to not be SQL-escaped. |
1200 * @param mixed $default Optional. Value to return if the option doesn't exist. Default false. |
1308 * @param mixed $default Optional. Value to return if the option doesn't exist. Default false. |
1201 * @return mixed Value set for the option. |
1309 * @return mixed Value set for the option. |
1202 */ |
1310 */ |
1203 function get_network_option( $network_id, $option, $default = false ) { |
1311 function get_network_option( $network_id, $option, $default = false ) { |
1204 global $wpdb; |
1312 global $wpdb; |
1205 |
1313 |
1213 if ( ! $network_id ) { |
1321 if ( ! $network_id ) { |
1214 $network_id = get_current_network_id(); |
1322 $network_id = get_current_network_id(); |
1215 } |
1323 } |
1216 |
1324 |
1217 /** |
1325 /** |
1218 * Filters an existing network option before it is retrieved. |
1326 * Filters the value of an existing network option before it is retrieved. |
1219 * |
1327 * |
1220 * The dynamic portion of the hook name, `$option`, refers to the option name. |
1328 * The dynamic portion of the hook name, `$option`, refers to the option name. |
1221 * |
1329 * |
1222 * Passing a truthy value to the filter will effectively short-circuit retrieval, |
1330 * Returning a truthy value from the filter will effectively short-circuit retrieval |
1223 * returning the passed value instead. |
1331 * and return the passed value instead. |
1224 * |
1332 * |
1225 * @since 2.9.0 As 'pre_site_option_' . $key |
1333 * @since 2.9.0 As 'pre_site_option_' . $key |
1226 * @since 3.0.0 |
1334 * @since 3.0.0 |
1227 * @since 4.4.0 The `$option` parameter was added. |
1335 * @since 4.4.0 The `$option` parameter was added. |
1228 * @since 4.7.0 The `$network_id` parameter was added. |
1336 * @since 4.7.0 The `$network_id` parameter was added. |
1229 * @since 4.9.0 The `$default` parameter was added. |
1337 * @since 4.9.0 The `$default` parameter was added. |
1230 * |
1338 * |
1231 * @param mixed $pre_option The value to return instead of the option value. This differs from |
1339 * @param mixed $pre_option The value to return instead of the option value. This differs |
1232 * `$default`, which is used as the fallback value in the event the |
1340 * from `$default`, which is used as the fallback value in the event |
1233 * option doesn't exist elsewhere in get_network_option(). Default |
1341 * the option doesn't exist elsewhere in get_network_option(). |
1234 * is false (to skip past the short-circuit). |
1342 * Default false (to skip past the short-circuit). |
1235 * @param string $option Option name. |
1343 * @param string $option Option name. |
1236 * @param int $network_id ID of the network. |
1344 * @param int $network_id ID of the network. |
1237 * @param mixed $default The fallback value to return if the option does not exist. |
1345 * @param mixed $default The fallback value to return if the option does not exist. |
1238 * Default is false. |
1346 * Default false. |
1239 */ |
1347 */ |
1240 $pre = apply_filters( "pre_site_option_{$option}", false, $option, $network_id, $default ); |
1348 $pre = apply_filters( "pre_site_option_{$option}", false, $option, $network_id, $default ); |
1241 |
1349 |
1242 if ( false !== $pre ) { |
1350 if ( false !== $pre ) { |
1243 return $pre; |
1351 return $pre; |
1244 } |
1352 } |
1245 |
1353 |
1246 // prevent non-existent options from triggering multiple queries |
1354 // Prevent non-existent options from triggering multiple queries. |
1247 $notoptions_key = "$network_id:notoptions"; |
1355 $notoptions_key = "$network_id:notoptions"; |
1248 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); |
1356 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); |
1249 |
1357 |
1250 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { |
1358 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { |
1251 |
1359 |
1275 $value = wp_cache_get( $cache_key, 'site-options' ); |
1383 $value = wp_cache_get( $cache_key, 'site-options' ); |
1276 |
1384 |
1277 if ( ! isset( $value ) || false === $value ) { |
1385 if ( ! isset( $value ) || false === $value ) { |
1278 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); |
1386 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); |
1279 |
1387 |
1280 // Has to be get_row instead of get_var because of funkiness with 0, false, null values |
1388 // Has to be get_row() instead of get_var() because of funkiness with 0, false, null values. |
1281 if ( is_object( $row ) ) { |
1389 if ( is_object( $row ) ) { |
1282 $value = $row->meta_value; |
1390 $value = $row->meta_value; |
1283 $value = maybe_unserialize( $value ); |
1391 $value = maybe_unserialize( $value ); |
1284 wp_cache_set( $cache_key, $value, 'site-options' ); |
1392 wp_cache_set( $cache_key, $value, 'site-options' ); |
1285 } else { |
1393 } else { |
1286 if ( ! is_array( $notoptions ) ) { |
1394 if ( ! is_array( $notoptions ) ) { |
1287 $notoptions = array(); |
1395 $notoptions = array(); |
1288 } |
1396 } |
|
1397 |
1289 $notoptions[ $option ] = true; |
1398 $notoptions[ $option ] = true; |
1290 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); |
1399 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); |
1291 |
1400 |
1292 /** This filter is documented in wp-includes/option.php */ |
1401 /** This filter is documented in wp-includes/option.php */ |
1293 $value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id ); |
1402 $value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id ); |
1725 * @return mixed Value of transient. |
1840 * @return mixed Value of transient. |
1726 */ |
1841 */ |
1727 function get_site_transient( $transient ) { |
1842 function get_site_transient( $transient ) { |
1728 |
1843 |
1729 /** |
1844 /** |
1730 * Filters the value of an existing site transient. |
1845 * Filters the value of an existing site transient before it is retrieved. |
1731 * |
1846 * |
1732 * The dynamic portion of the hook name, `$transient`, refers to the transient name. |
1847 * The dynamic portion of the hook name, `$transient`, refers to the transient name. |
1733 * |
1848 * |
1734 * Passing a truthy value to the filter will effectively short-circuit retrieval, |
1849 * Returning a truthy value from the filter will effectively short-circuit retrieval |
1735 * returning the passed value instead. |
1850 * and return the passed value instead. |
1736 * |
1851 * |
1737 * @since 2.9.0 |
1852 * @since 2.9.0 |
1738 * @since 4.4.0 The `$transient` parameter was added. |
1853 * @since 4.4.0 The `$transient` parameter was added. |
1739 * |
1854 * |
1740 * @param mixed $pre_site_transient The default value to return if the site transient does not exist. |
1855 * @param mixed $pre_site_transient The default value to return if the site transient does not exist. |
1741 * Any value other than false will short-circuit the retrieval |
1856 * Any value other than false will short-circuit the retrieval |
1742 * of the transient, and return the returned value. |
1857 * of the transient, and return that value. |
1743 * @param string $transient Transient name. |
1858 * @param string $transient Transient name. |
1744 */ |
1859 */ |
1745 $pre = apply_filters( "pre_site_transient_{$transient}", false, $transient ); |
1860 $pre = apply_filters( "pre_site_transient_{$transient}", false, $transient ); |
1746 |
1861 |
1747 if ( false !== $pre ) { |
1862 if ( false !== $pre ) { |
1782 */ |
1897 */ |
1783 return apply_filters( "site_transient_{$transient}", $value, $transient ); |
1898 return apply_filters( "site_transient_{$transient}", $value, $transient ); |
1784 } |
1899 } |
1785 |
1900 |
1786 /** |
1901 /** |
1787 * Set/update the value of a site transient. |
1902 * Sets/updates the value of a site transient. |
1788 * |
1903 * |
1789 * You do not need to serialize values, if the value needs to be serialize, then |
1904 * You do not need to serialize values. If the value needs to be serialized, |
1790 * it will be serialized before it is set. |
1905 * then it will be serialized before it is set. |
1791 * |
1906 * |
1792 * @since 2.9.0 |
1907 * @since 2.9.0 |
1793 * |
1908 * |
1794 * @see set_transient() |
1909 * @see set_transient() |
1795 * |
1910 * |
1796 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be |
1911 * @param string $transient Transient name. Expected to not be SQL-escaped. Must be |
1797 * 167 characters or fewer in length. |
1912 * 167 characters or fewer in length. |
1798 * @param mixed $value Transient value. Expected to not be SQL-escaped. |
1913 * @param mixed $value Transient value. Expected to not be SQL-escaped. |
1799 * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration). |
1914 * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration). |
1800 * @return bool False if value was not set and true if value was set. |
1915 * @return bool True if the value was set, false otherwise. |
1801 */ |
1916 */ |
1802 function set_site_transient( $transient, $value, $expiration = 0 ) { |
1917 function set_site_transient( $transient, $value, $expiration = 0 ) { |
1803 |
1918 |
1804 /** |
1919 /** |
1805 * Filters the value of a specific site transient before it is set. |
1920 * Filters the value of a specific site transient before it is set. |
2062 'schema' => array( |
2180 'schema' => array( |
2063 'enum' => array( 'open', 'closed' ), |
2181 'enum' => array( 'open', 'closed' ), |
2064 ), |
2182 ), |
2065 ), |
2183 ), |
2066 'type' => 'string', |
2184 'type' => 'string', |
2067 'description' => __( 'Allow people to post comments on new articles.' ), |
2185 'description' => __( 'Allow people to submit comments on new posts.' ), |
2068 ) |
2186 ) |
2069 ); |
2187 ); |
2070 } |
2188 } |
2071 |
2189 |
2072 /** |
2190 /** |
2073 * Register a setting and its data. |
2191 * Registers a setting and its data. |
2074 * |
2192 * |
2075 * @since 2.7.0 |
2193 * @since 2.7.0 |
2076 * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`. |
2194 * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`. |
2077 * |
2195 * @since 5.5.0 `$new_whitelist_options` was renamed to `$new_allowed_options`. |
2078 * @global array $new_whitelist_options |
2196 * Please consider writing more inclusive code. |
|
2197 * |
|
2198 * @global array $new_allowed_options |
2079 * @global array $wp_registered_settings |
2199 * @global array $wp_registered_settings |
2080 * |
2200 * |
2081 * @param string $option_group A settings group name. Should correspond to a whitelisted option key name. |
2201 * @param string $option_group A settings group name. Should correspond to an allowed option key name. |
2082 * Default whitelisted option key names include "general," "discussion," and "reading," among others. |
2202 * Default allowed option key names include 'general', 'discussion', 'media', |
|
2203 * 'reading', 'writing', 'misc', 'options', and 'privacy'. |
2083 * @param string $option_name The name of an option to sanitize and save. |
2204 * @param string $option_name The name of an option to sanitize and save. |
2084 * @param array $args { |
2205 * @param array $args { |
2085 * Data used to describe the setting when registered. |
2206 * Data used to describe the setting when registered. |
2086 * |
2207 * |
2087 * @type string $type The type of data associated with this setting. |
2208 * @type string $type The type of data associated with this setting. |
2088 * Valid values are 'string', 'boolean', 'integer', and 'number'. |
2209 * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. |
2089 * @type string $description A description of the data attached to this setting. |
2210 * @type string $description A description of the data attached to this setting. |
2090 * @type callable $sanitize_callback A callback function that sanitizes the option's value. |
2211 * @type callable $sanitize_callback A callback function that sanitizes the option's value. |
2091 * @type bool $show_in_rest Whether data associated with this setting should be included in the REST API. |
2212 * @type bool|array $show_in_rest Whether data associated with this setting should be included in the REST API. |
2092 * @type mixed $default Default value when calling `get_option()`. |
2213 * When registering complex settings, this argument may optionally be an |
|
2214 * array with a 'schema' key. |
|
2215 * @type mixed $default Default value when calling `get_option()`. |
2093 * } |
2216 * } |
2094 */ |
2217 */ |
2095 function register_setting( $option_group, $option_name, $args = array() ) { |
2218 function register_setting( $option_group, $option_name, $args = array() ) { |
2096 global $new_whitelist_options, $wp_registered_settings; |
2219 global $new_allowed_options, $wp_registered_settings; |
|
2220 |
|
2221 /* |
|
2222 * In 5.5.0, the `$new_whitelist_options` global variable was renamed to `$new_allowed_options`. |
|
2223 * Please consider writing more inclusive code. |
|
2224 */ |
|
2225 $GLOBALS['new_whitelist_options'] = &$new_allowed_options; |
2097 |
2226 |
2098 $defaults = array( |
2227 $defaults = array( |
2099 'type' => 'string', |
2228 'type' => 'string', |
2100 'group' => $option_group, |
2229 'group' => $option_group, |
2101 'description' => '', |
2230 'description' => '', |
2119 * @param array $defaults Array of default arguments. |
2248 * @param array $defaults Array of default arguments. |
2120 * @param string $option_group Setting group. |
2249 * @param string $option_group Setting group. |
2121 * @param string $option_name Setting name. |
2250 * @param string $option_name Setting name. |
2122 */ |
2251 */ |
2123 $args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name ); |
2252 $args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name ); |
|
2253 |
2124 $args = wp_parse_args( $args, $defaults ); |
2254 $args = wp_parse_args( $args, $defaults ); |
|
2255 |
|
2256 // Require an item schema when registering settings with an array type. |
|
2257 if ( false !== $args['show_in_rest'] && 'array' === $args['type'] && ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) ) { |
|
2258 _doing_it_wrong( __FUNCTION__, __( 'When registering an "array" setting to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items".' ), '5.4.0' ); |
|
2259 } |
2125 |
2260 |
2126 if ( ! is_array( $wp_registered_settings ) ) { |
2261 if ( ! is_array( $wp_registered_settings ) ) { |
2127 $wp_registered_settings = array(); |
2262 $wp_registered_settings = array(); |
2128 } |
2263 } |
2129 |
2264 |
2130 if ( 'misc' == $option_group ) { |
2265 if ( 'misc' === $option_group ) { |
2131 _deprecated_argument( |
2266 _deprecated_argument( |
2132 __FUNCTION__, |
2267 __FUNCTION__, |
2133 '3.0.0', |
2268 '3.0.0', |
2134 /* translators: %s: misc */ |
|
2135 sprintf( |
2269 sprintf( |
|
2270 /* translators: %s: misc */ |
2136 __( 'The "%s" options group has been removed. Use another settings group.' ), |
2271 __( 'The "%s" options group has been removed. Use another settings group.' ), |
2137 'misc' |
2272 'misc' |
2138 ) |
2273 ) |
2139 ); |
2274 ); |
2140 $option_group = 'general'; |
2275 $option_group = 'general'; |
2141 } |
2276 } |
2142 |
2277 |
2143 if ( 'privacy' == $option_group ) { |
2278 if ( 'privacy' === $option_group ) { |
2144 _deprecated_argument( |
2279 _deprecated_argument( |
2145 __FUNCTION__, |
2280 __FUNCTION__, |
2146 '3.5.0', |
2281 '3.5.0', |
2147 /* translators: %s: privacy */ |
|
2148 sprintf( |
2282 sprintf( |
|
2283 /* translators: %s: privacy */ |
2149 __( 'The "%s" options group has been removed. Use another settings group.' ), |
2284 __( 'The "%s" options group has been removed. Use another settings group.' ), |
2150 'privacy' |
2285 'privacy' |
2151 ) |
2286 ) |
2152 ); |
2287 ); |
2153 $option_group = 'reading'; |
2288 $option_group = 'reading'; |
2154 } |
2289 } |
2155 |
2290 |
2156 $new_whitelist_options[ $option_group ][] = $option_name; |
2291 $new_allowed_options[ $option_group ][] = $option_name; |
|
2292 |
2157 if ( ! empty( $args['sanitize_callback'] ) ) { |
2293 if ( ! empty( $args['sanitize_callback'] ) ) { |
2158 add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] ); |
2294 add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] ); |
2159 } |
2295 } |
2160 if ( array_key_exists( 'default', $args ) ) { |
2296 if ( array_key_exists( 'default', $args ) ) { |
2161 add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 ); |
2297 add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 ); |
2162 } |
2298 } |
2163 |
2299 |
|
2300 /** |
|
2301 * Fires immediately before the setting is registered but after its filters are in place. |
|
2302 * |
|
2303 * @since 5.5.0 |
|
2304 * |
|
2305 * @param string $option_group Setting group. |
|
2306 * @param string $option_name Setting name. |
|
2307 * @param array $args Array of setting registration arguments. |
|
2308 */ |
|
2309 do_action( 'register_setting', $option_group, $option_name, $args ); |
|
2310 |
2164 $wp_registered_settings[ $option_name ] = $args; |
2311 $wp_registered_settings[ $option_name ] = $args; |
2165 } |
2312 } |
2166 |
2313 |
2167 /** |
2314 /** |
2168 * Unregister a setting. |
2315 * Unregisters a setting. |
2169 * |
2316 * |
2170 * @since 2.7.0 |
2317 * @since 2.7.0 |
2171 * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead. |
2318 * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead. |
2172 * |
2319 * @since 5.5.0 `$new_whitelist_options` was renamed to `$new_allowed_options`. |
2173 * @global array $new_whitelist_options |
2320 * Please consider writing more inclusive code. |
|
2321 * |
|
2322 * @global array $new_allowed_options |
2174 * @global array $wp_registered_settings |
2323 * @global array $wp_registered_settings |
2175 * |
2324 * |
2176 * @param string $option_group The settings group name used during registration. |
2325 * @param string $option_group The settings group name used during registration. |
2177 * @param string $option_name The name of the option to unregister. |
2326 * @param string $option_name The name of the option to unregister. |
2178 * @param callable $deprecated Deprecated. |
2327 * @param callable $deprecated Deprecated. |
2179 */ |
2328 */ |
2180 function unregister_setting( $option_group, $option_name, $deprecated = '' ) { |
2329 function unregister_setting( $option_group, $option_name, $deprecated = '' ) { |
2181 global $new_whitelist_options, $wp_registered_settings; |
2330 global $new_allowed_options, $wp_registered_settings; |
2182 |
2331 |
2183 if ( 'misc' == $option_group ) { |
2332 /* |
|
2333 * In 5.5.0, the `$new_whitelist_options` global variable was renamed to `$new_allowed_options`. |
|
2334 * Please consider writing more inclusive code. |
|
2335 */ |
|
2336 $GLOBALS['new_whitelist_options'] = &$new_allowed_options; |
|
2337 |
|
2338 if ( 'misc' === $option_group ) { |
2184 _deprecated_argument( |
2339 _deprecated_argument( |
2185 __FUNCTION__, |
2340 __FUNCTION__, |
2186 '3.0.0', |
2341 '3.0.0', |
2187 /* translators: %s: misc */ |
|
2188 sprintf( |
2342 sprintf( |
|
2343 /* translators: %s: misc */ |
2189 __( 'The "%s" options group has been removed. Use another settings group.' ), |
2344 __( 'The "%s" options group has been removed. Use another settings group.' ), |
2190 'misc' |
2345 'misc' |
2191 ) |
2346 ) |
2192 ); |
2347 ); |
2193 $option_group = 'general'; |
2348 $option_group = 'general'; |
2194 } |
2349 } |
2195 |
2350 |
2196 if ( 'privacy' == $option_group ) { |
2351 if ( 'privacy' === $option_group ) { |
2197 _deprecated_argument( |
2352 _deprecated_argument( |
2198 __FUNCTION__, |
2353 __FUNCTION__, |
2199 '3.5.0', |
2354 '3.5.0', |
2200 /* translators: %s: privacy */ |
|
2201 sprintf( |
2355 sprintf( |
|
2356 /* translators: %s: privacy */ |
2202 __( 'The "%s" options group has been removed. Use another settings group.' ), |
2357 __( 'The "%s" options group has been removed. Use another settings group.' ), |
2203 'privacy' |
2358 'privacy' |
2204 ) |
2359 ) |
2205 ); |
2360 ); |
2206 $option_group = 'reading'; |
2361 $option_group = 'reading'; |
2207 } |
2362 } |
2208 |
2363 |
2209 $pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ] ); |
2364 $pos = array_search( $option_name, (array) $new_allowed_options[ $option_group ], true ); |
2210 if ( $pos !== false ) { |
2365 |
2211 unset( $new_whitelist_options[ $option_group ][ $pos ] ); |
2366 if ( false !== $pos ) { |
2212 } |
2367 unset( $new_allowed_options[ $option_group ][ $pos ] ); |
|
2368 } |
|
2369 |
2213 if ( '' !== $deprecated ) { |
2370 if ( '' !== $deprecated ) { |
2214 _deprecated_argument( |
2371 _deprecated_argument( |
2215 __FUNCTION__, |
2372 __FUNCTION__, |
2216 '4.7.0', |
2373 '4.7.0', |
2217 /* translators: 1: $sanitize_callback, 2: register_setting() */ |
|
2218 sprintf( |
2374 sprintf( |
|
2375 /* translators: 1: $sanitize_callback, 2: register_setting() */ |
2219 __( '%1$s is deprecated. The callback from %2$s is used instead.' ), |
2376 __( '%1$s is deprecated. The callback from %2$s is used instead.' ), |
2220 '<code>$sanitize_callback</code>', |
2377 '<code>$sanitize_callback</code>', |
2221 '<code>register_setting()</code>' |
2378 '<code>register_setting()</code>' |
2222 ) |
2379 ) |
2223 ); |
2380 ); |