]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/option.php
WordPress 4.1
[autoinstalls/wordpress.git] / wp-includes / option.php
1 <?php
2 /**
3  * Option API
4  *
5  * @package WordPress
6  * @subpackage Option
7  */
8
9 /**
10  * Retrieve option value based on name of option.
11  *
12  * If the option does not exist or does not have a value, then the return value
13  * will be false. This is useful to check whether you need to install an option
14  * and is commonly used during installation of plugin options and to test
15  * whether upgrading is required.
16  *
17  * If the option was serialized then it will be unserialized when it is returned.
18  *
19  * @since 1.5.0
20  *
21  * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
22  * @param mixed $default Optional. Default value to return if the option does not exist.
23  * @return mixed Value set for the option.
24  */
25 function get_option( $option, $default = false ) {
26         global $wpdb;
27
28         $option = trim( $option );
29         if ( empty( $option ) )
30                 return false;
31
32         /**
33          * Filter the value of an existing option before it is retrieved.
34          *
35          * The dynamic portion of the hook name, `$option`, refers to the option name.
36          *
37          * Passing a truthy value to the filter will short-circuit retrieving
38          * the option value, returning the passed value instead.
39          *
40          * @since 1.5.0
41          *
42          * @param bool|mixed $pre_option Value to return instead of the option value.
43          *                               Default false to skip it.
44          */
45         $pre = apply_filters( 'pre_option_' . $option, false );
46         if ( false !== $pre )
47                 return $pre;
48
49         if ( defined( 'WP_SETUP_CONFIG' ) )
50                 return false;
51
52         if ( ! defined( 'WP_INSTALLING' ) ) {
53                 // prevent non-existent options from triggering multiple queries
54                 $notoptions = wp_cache_get( 'notoptions', 'options' );
55                 if ( isset( $notoptions[ $option ] ) ) {
56                         /**
57                          * Filter the default value for an option.
58                          *
59                          * The dynamic portion of the hook name, `$option`, refers to the option name.
60                          *
61                          * @since 3.4.0
62                          *
63                          * @param mixed $default The default value to return if the option does not exist
64                          *                       in the database.
65                          */
66                         return apply_filters( 'default_option_' . $option, $default );
67                 }
68
69                 $alloptions = wp_load_alloptions();
70
71                 if ( isset( $alloptions[$option] ) ) {
72                         $value = $alloptions[$option];
73                 } else {
74                         $value = wp_cache_get( $option, 'options' );
75
76                         if ( false === $value ) {
77                                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
78
79                                 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
80                                 if ( is_object( $row ) ) {
81                                         $value = $row->option_value;
82                                         wp_cache_add( $option, $value, 'options' );
83                                 } else { // option does not exist, so we must cache its non-existence
84                                         $notoptions[$option] = true;
85                                         wp_cache_set( 'notoptions', $notoptions, 'options' );
86
87                                         /** This filter is documented in wp-includes/option.php */
88                                         return apply_filters( 'default_option_' . $option, $default );
89                                 }
90                         }
91                 }
92         } else {
93                 $suppress = $wpdb->suppress_errors();
94                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
95                 $wpdb->suppress_errors( $suppress );
96                 if ( is_object( $row ) ) {
97                         $value = $row->option_value;
98                 } else {
99                         /** This filter is documented in wp-includes/option.php */
100                         return apply_filters( 'default_option_' . $option, $default );
101                 }
102         }
103
104         // If home is not set use siteurl.
105         if ( 'home' == $option && '' == $value )
106                 return get_option( 'siteurl' );
107
108         if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
109                 $value = untrailingslashit( $value );
110
111         /**
112          * Filter the value of an existing option.
113          *
114          * The dynamic portion of the hook name, `$option`, refers to the option name.
115          *
116          * @since 1.5.0 As 'option_' . $setting
117          * @since 3.0.0
118          *
119          * @param mixed $value Value of the option. If stored serialized, it will be
120          *                     unserialized prior to being returned.
121          */
122         return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
123 }
124
125 /**
126  * Protect WordPress special option from being modified.
127  *
128  * Will die if $option is in protected list. Protected options are 'alloptions'
129  * and 'notoptions' options.
130  *
131  * @since 2.2.0
132  *
133  * @param string $option Option name.
134  */
135 function wp_protect_special_option( $option ) {
136         if ( 'alloptions' === $option || 'notoptions' === $option )
137                 wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
138 }
139
140 /**
141  * Print option value after sanitizing for forms.
142  *
143  * @since 1.5.0
144  *
145  * @param string $option Option name.
146  */
147 function form_option( $option ) {
148         echo esc_attr( get_option( $option ) );
149 }
150
151 /**
152  * Loads and caches all autoloaded options, if available or all options.
153  *
154  * @since 2.2.0
155  *
156  * @return array List of all options.
157  */
158 function wp_load_alloptions() {
159         global $wpdb;
160
161         if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
162                 $alloptions = wp_cache_get( 'alloptions', 'options' );
163         else
164                 $alloptions = false;
165
166         if ( !$alloptions ) {
167                 $suppress = $wpdb->suppress_errors();
168                 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
169                         $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
170                 $wpdb->suppress_errors($suppress);
171                 $alloptions = array();
172                 foreach ( (array) $alloptions_db as $o ) {
173                         $alloptions[$o->option_name] = $o->option_value;
174                 }
175                 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
176                         wp_cache_add( 'alloptions', $alloptions, 'options' );
177         }
178
179         return $alloptions;
180 }
181
182 /**
183  * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
184  *
185  * @since 3.0.0
186  *
187  * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
188  */
189 function wp_load_core_site_options( $site_id = null ) {
190         global $wpdb;
191
192         if ( !is_multisite() || wp_using_ext_object_cache() || defined( 'WP_INSTALLING' ) )
193                 return;
194
195         if ( empty($site_id) )
196                 $site_id = $wpdb->siteid;
197
198         $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled', 'ms_files_rewriting' );
199
200         $core_options_in = "'" . implode("', '", $core_options) . "'";
201         $options = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $site_id) );
202
203         foreach ( $options as $option ) {
204                 $key = $option->meta_key;
205                 $cache_key = "{$site_id}:$key";
206                 $option->meta_value = maybe_unserialize( $option->meta_value );
207
208                 wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
209         }
210 }
211
212 /**
213  * Update the value of an option that was already added.
214  *
215  * You do not need to serialize values. If the value needs to be serialized, then
216  * it will be serialized before it is inserted into the database. Remember,
217  * resources can not be serialized or added as an option.
218  *
219  * If the option does not exist, then the option will be added with the option
220  * value, but you will not be able to set whether it is autoloaded. If you want
221  * to set whether an option is autoloaded, then you need to use the add_option().
222  *
223  * @since 1.0.0
224  *
225  * @param string $option Option name. Expected to not be SQL-escaped.
226  * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
227  * @return bool False if value was not updated and true if value was updated.
228  */
229 function update_option( $option, $value ) {
230         global $wpdb;
231
232         $option = trim($option);
233         if ( empty($option) )
234                 return false;
235
236         wp_protect_special_option( $option );
237
238         if ( is_object( $value ) )
239                 $value = clone $value;
240
241         $value = sanitize_option( $option, $value );
242         $old_value = get_option( $option );
243
244         /**
245          * Filter a specific option before its value is (maybe) serialized and updated.
246          *
247          * The dynamic portion of the hook name, `$option`, refers to the option name.
248          *
249          * @since 2.6.0
250          *
251          * @param mixed $value     The new, unserialized option value.
252          * @param mixed $old_value The old option value.
253          */
254         $value = apply_filters( 'pre_update_option_' . $option, $value, $old_value );
255
256         /**
257          * Filter an option before its value is (maybe) serialized and updated.
258          *
259          * @since 3.9.0
260          *
261          * @param mixed  $value     The new, unserialized option value.
262          * @param string $option    Name of the option.
263          * @param mixed  $old_value The old option value.
264          */
265         $value = apply_filters( 'pre_update_option', $value, $option, $old_value );
266
267         // If the new and old values are the same, no need to update.
268         if ( $value === $old_value )
269                 return false;
270
271         if ( false === $old_value )
272                 return add_option( $option, $value );
273
274         $serialized_value = maybe_serialize( $value );
275
276         /**
277          * Fires immediately before an option value is updated.
278          *
279          * @since 2.9.0
280          *
281          * @param string $option    Name of the option to update.
282          * @param mixed  $old_value The old option value.
283          * @param mixed  $value     The new option value.
284          */
285         do_action( 'update_option', $option, $old_value, $value );
286
287         $result = $wpdb->update( $wpdb->options, array( 'option_value' => $serialized_value ), array( 'option_name' => $option ) );
288         if ( ! $result )
289                 return false;
290
291         $notoptions = wp_cache_get( 'notoptions', 'options' );
292         if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
293                 unset( $notoptions[$option] );
294                 wp_cache_set( 'notoptions', $notoptions, 'options' );
295         }
296
297         if ( ! defined( 'WP_INSTALLING' ) ) {
298                 $alloptions = wp_load_alloptions();
299                 if ( isset( $alloptions[$option] ) ) {
300                         $alloptions[ $option ] = $serialized_value;
301                         wp_cache_set( 'alloptions', $alloptions, 'options' );
302                 } else {
303                         wp_cache_set( $option, $serialized_value, 'options' );
304                 }
305         }
306
307         /**
308          * Fires after the value of a specific option has been successfully updated.
309          *
310          * The dynamic portion of the hook name, `$option`, refers to the option name.
311          *
312          * @since 2.0.1
313          *
314          * @param mixed $old_value The old option value.
315          * @param mixed $value     The new option value.
316          */
317         do_action( "update_option_{$option}", $old_value, $value );
318
319         /**
320          * Fires after the value of an option has been successfully updated.
321          *
322          * @since 2.9.0
323          *
324          * @param string $option    Name of the updated option.
325          * @param mixed  $old_value The old option value.
326          * @param mixed  $value     The new option value.
327          */
328         do_action( 'updated_option', $option, $old_value, $value );
329         return true;
330 }
331
332 /**
333  * Add a new option.
334  *
335  * You do not need to serialize values. If the value needs to be serialized, then
336  * it will be serialized before it is inserted into the database. Remember,
337  * resources can not be serialized or added as an option.
338  *
339  * You can create options without values and then update the values later.
340  * Existing options will not be updated and checks are performed to ensure that you
341  * aren't adding a protected WordPress option. Care should be taken to not name
342  * options the same as the ones which are protected.
343  *
344  * @since 1.0.0
345  *
346  * @param string         $option      Name of option to add. Expected to not be SQL-escaped.
347  * @param mixed          $value       Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
348  * @param string         $deprecated  Optional. Description. Not used anymore.
349  * @param string|bool    $autoload    Optional. Default is enabled. Whether to load the option when WordPress starts up.
350  * @return bool False if option was not added and true if option was added.
351  */
352 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
353         global $wpdb;
354
355         if ( !empty( $deprecated ) )
356                 _deprecated_argument( __FUNCTION__, '2.3' );
357
358         $option = trim($option);
359         if ( empty($option) )
360                 return false;
361
362         wp_protect_special_option( $option );
363
364         if ( is_object($value) )
365                 $value = clone $value;
366
367         $value = sanitize_option( $option, $value );
368
369         // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
370         $notoptions = wp_cache_get( 'notoptions', 'options' );
371         if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
372                 if ( false !== get_option( $option ) )
373                         return false;
374
375         $serialized_value = maybe_serialize( $value );
376         $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
377
378         /**
379          * Fires before an option is added.
380          *
381          * @since 2.9.0
382          *
383          * @param string $option Name of the option to add.
384          * @param mixed  $value  Value of the option.
385          */
386         do_action( 'add_option', $option, $value );
387
388         $result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $serialized_value, $autoload ) );
389         if ( ! $result )
390                 return false;
391
392         if ( ! defined( 'WP_INSTALLING' ) ) {
393                 if ( 'yes' == $autoload ) {
394                         $alloptions = wp_load_alloptions();
395                         $alloptions[ $option ] = $serialized_value;
396                         wp_cache_set( 'alloptions', $alloptions, 'options' );
397                 } else {
398                         wp_cache_set( $option, $serialized_value, 'options' );
399                 }
400         }
401
402         // This option exists now
403         $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
404         if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
405                 unset( $notoptions[$option] );
406                 wp_cache_set( 'notoptions', $notoptions, 'options' );
407         }
408
409         /**
410          * Fires after a specific option has been added.
411          *
412          * The dynamic portion of the hook name, `$option`, refers to the option name.
413          *
414          * @since 2.5.0 As "add_option_{$name}"
415          * @since 3.0.0
416          *
417          * @param string $option Name of the option to add.
418          * @param mixed  $value  Value of the option.
419          */
420         do_action( "add_option_{$option}", $option, $value );
421
422         /**
423          * Fires after an option has been added.
424          *
425          * @since 2.9.0
426          *
427          * @param string $option Name of the added option.
428          * @param mixed  $value  Value of the option.
429          */
430         do_action( 'added_option', $option, $value );
431         return true;
432 }
433
434 /**
435  * Removes option by name. Prevents removal of protected WordPress options.
436  *
437  * @since 1.2.0
438  *
439  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
440  * @return bool True, if option is successfully deleted. False on failure.
441  */
442 function delete_option( $option ) {
443         global $wpdb;
444
445         $option = trim( $option );
446         if ( empty( $option ) )
447                 return false;
448
449         wp_protect_special_option( $option );
450
451         // Get the ID, if no ID then return
452         $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
453         if ( is_null( $row ) )
454                 return false;
455
456         /**
457          * Fires immediately before an option is deleted.
458          *
459          * @since 2.9.0
460          *
461          * @param string $option Name of the option to delete.
462          */
463         do_action( 'delete_option', $option );
464
465         $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
466         if ( ! defined( 'WP_INSTALLING' ) ) {
467                 if ( 'yes' == $row->autoload ) {
468                         $alloptions = wp_load_alloptions();
469                         if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
470                                 unset( $alloptions[$option] );
471                                 wp_cache_set( 'alloptions', $alloptions, 'options' );
472                         }
473                 } else {
474                         wp_cache_delete( $option, 'options' );
475                 }
476         }
477         if ( $result ) {
478
479                 /**
480                  * Fires after a specific option has been deleted.
481                  *
482                  * The dynamic portion of the hook name, `$option`, refers to the option name.
483                  *
484                  * @since 3.0.0
485                  *
486                  * @param string $option Name of the deleted option.
487                  */
488                 do_action( "delete_option_$option", $option );
489
490                 /**
491                  * Fires after an option has been deleted.
492                  *
493                  * @since 2.9.0
494                  *
495                  * @param string $option Name of the deleted option.
496                  */
497                 do_action( 'deleted_option', $option );
498                 return true;
499         }
500         return false;
501 }
502
503 /**
504  * Delete a transient.
505  *
506  * @since 2.8.0
507  *
508  * @param string $transient Transient name. Expected to not be SQL-escaped.
509  * @return bool true if successful, false otherwise
510  */
511 function delete_transient( $transient ) {
512
513         /**
514          * Fires immediately before a specific transient is deleted.
515          *
516          * The dynamic portion of the hook name, `$transient`, refers to the transient name.
517          *
518          * @since 3.0.0
519          *
520          * @param string $transient Transient name.
521          */
522         do_action( 'delete_transient_' . $transient, $transient );
523
524         if ( wp_using_ext_object_cache() ) {
525                 $result = wp_cache_delete( $transient, 'transient' );
526         } else {
527                 $option_timeout = '_transient_timeout_' . $transient;
528                 $option = '_transient_' . $transient;
529                 $result = delete_option( $option );
530                 if ( $result )
531                         delete_option( $option_timeout );
532         }
533
534         if ( $result ) {
535
536                 /**
537                  * Fires after a transient is deleted.
538                  *
539                  * @since 3.0.0
540                  *
541                  * @param string $transient Deleted transient name.
542                  */
543                 do_action( 'deleted_transient', $transient );
544         }
545
546         return $result;
547 }
548
549 /**
550  * Get the value of a transient.
551  *
552  * If the transient does not exist, does not have a value, or has expired,
553  * then the return value will be false.
554  *
555  * @since 2.8.0
556  *
557  * @param string $transient Transient name. Expected to not be SQL-escaped.
558  * @return mixed Value of transient.
559  */
560 function get_transient( $transient ) {
561
562         /**
563          * Filter the value of an existing transient.
564          *
565          * The dynamic portion of the hook name, `$transient`, refers to the transient name.
566          *
567          * Passing a truthy value to the filter will effectively short-circuit retrieval
568          * of the transient, returning the passed value instead.
569          *
570          * @since 2.8.0
571          *
572          * @param mixed $pre_transient The default value to return if the transient does not exist.
573          *                             Any value other than false will short-circuit the retrieval
574          *                             of the transient, and return the returned value.
575          */
576         $pre = apply_filters( 'pre_transient_' . $transient, false );
577         if ( false !== $pre )
578                 return $pre;
579
580         if ( wp_using_ext_object_cache() ) {
581                 $value = wp_cache_get( $transient, 'transient' );
582         } else {
583                 $transient_option = '_transient_' . $transient;
584                 if ( ! defined( 'WP_INSTALLING' ) ) {
585                         // If option is not in alloptions, it is not autoloaded and thus has a timeout
586                         $alloptions = wp_load_alloptions();
587                         if ( !isset( $alloptions[$transient_option] ) ) {
588                                 $transient_timeout = '_transient_timeout_' . $transient;
589                                 if ( get_option( $transient_timeout ) < time() ) {
590                                         delete_option( $transient_option  );
591                                         delete_option( $transient_timeout );
592                                         $value = false;
593                                 }
594                         }
595                 }
596
597                 if ( ! isset( $value ) )
598                         $value = get_option( $transient_option );
599         }
600
601         /**
602          * Filter an existing transient's value.
603          *
604          * The dynamic portion of the hook name, `$transient`, refers to the transient name.
605          *
606          * @since 2.8.0
607          *
608          * @param mixed $value Value of transient.
609          */
610         return apply_filters( 'transient_' . $transient, $value );
611 }
612
613 /**
614  * Set/update the value of a transient.
615  *
616  * You do not need to serialize values. If the value needs to be serialized, then
617  * it will be serialized before it is set.
618  *
619  * @since 2.8.0
620  *
621  * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
622  *                           45 characters or fewer in length.
623  * @param mixed  $value      Transient value. Must be serializable if non-scalar.
624  *                           Expected to not be SQL-escaped.
625  * @param int    $expiration Optional. Time until expiration in seconds. Default 0.
626  * @return bool False if value was not set and true if value was set.
627  */
628 function set_transient( $transient, $value, $expiration = 0 ) {
629
630         /**
631          * Filter a specific transient before its value is set.
632          *
633          * The dynamic portion of the hook name, `$transient`, refers to the transient name.
634          *
635          * @since 3.0.0
636          *
637          * @param mixed $value New value of transient.
638          */
639         $value = apply_filters( 'pre_set_transient_' . $transient, $value );
640
641         $expiration = (int) $expiration;
642
643         if ( wp_using_ext_object_cache() ) {
644                 $result = wp_cache_set( $transient, $value, 'transient', $expiration );
645         } else {
646                 $transient_timeout = '_transient_timeout_' . $transient;
647                 $transient = '_transient_' . $transient;
648                 if ( false === get_option( $transient ) ) {
649                         $autoload = 'yes';
650                         if ( $expiration ) {
651                                 $autoload = 'no';
652                                 add_option( $transient_timeout, time() + $expiration, '', 'no' );
653                         }
654                         $result = add_option( $transient, $value, '', $autoload );
655                 } else {
656                         // If expiration is requested, but the transient has no timeout option,
657                         // delete, then re-create transient rather than update.
658                         $update = true;
659                         if ( $expiration ) {
660                                 if ( false === get_option( $transient_timeout ) ) {
661                                         delete_option( $transient );
662                                         add_option( $transient_timeout, time() + $expiration, '', 'no' );
663                                         $result = add_option( $transient, $value, '', 'no' );
664                                         $update = false;
665                                 } else {
666                                         update_option( $transient_timeout, time() + $expiration );
667                                 }
668                         }
669                         if ( $update ) {
670                                 $result = update_option( $transient, $value );
671                         }
672                 }
673         }
674
675         if ( $result ) {
676
677                 /**
678                  * Fires after the value for a specific transient has been set.
679                  *
680                  * The dynamic portion of the hook name, `$transient`, refers to the transient name.
681                  *
682                  * @since 3.0.0
683                  *
684                  * @param mixed $value      Transient value.
685                  * @param int   $expiration Time until expiration in seconds. Default 0.
686                  */
687                 do_action( 'set_transient_' . $transient, $value, $expiration );
688
689                 /**
690                  * Fires after the value for a transient has been set.
691                  *
692                  * @since 3.0.0
693                  *
694                  * @param string $transient  The name of the transient.
695                  * @param mixed  $value      Transient value.
696                  * @param int    $expiration Time until expiration in seconds. Default 0.
697                  */
698                 do_action( 'setted_transient', $transient, $value, $expiration );
699         }
700         return $result;
701 }
702
703 /**
704  * Saves and restores user interface settings stored in a cookie.
705  *
706  * Checks if the current user-settings cookie is updated and stores it. When no
707  * cookie exists (different browser used), adds the last saved cookie restoring
708  * the settings.
709  *
710  * @since 2.7.0
711  */
712 function wp_user_settings() {
713
714         if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
715                 return;
716         }
717
718         if ( ! $user_id = get_current_user_id() ) {
719                 return;
720         }
721
722         if ( is_super_admin() && ! is_user_member_of_blog() ) {
723                 return;
724         }
725
726         $settings = (string) get_user_option( 'user-settings', $user_id );
727
728         if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
729                 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
730
731                 // No change or both empty
732                 if ( $cookie == $settings )
733                         return;
734
735                 $last_saved = (int) get_user_option( 'user-settings-time', $user_id );
736                 $current = isset( $_COOKIE['wp-settings-time-' . $user_id]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user_id] ) : 0;
737
738                 // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
739                 if ( $current > $last_saved ) {
740                         update_user_option( $user_id, 'user-settings', $cookie, false );
741                         update_user_option( $user_id, 'user-settings-time', time() - 5, false );
742                         return;
743                 }
744         }
745
746         // The cookie is not set in the current browser or the saved value is newer.
747         $secure = ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) );
748         setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
749         setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
750         $_COOKIE['wp-settings-' . $user_id] = $settings;
751 }
752
753 /**
754  * Retrieve user interface setting value based on setting name.
755  *
756  * @since 2.7.0
757  *
758  * @param string $name The name of the setting.
759  * @param string $default Optional default value to return when $name is not set.
760  * @return mixed the last saved user setting or the default value/false if it doesn't exist.
761  */
762 function get_user_setting( $name, $default = false ) {
763         $all_user_settings = get_all_user_settings();
764
765         return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
766 }
767
768 /**
769  * Add or update user interface setting.
770  *
771  * Both $name and $value can contain only ASCII letters, numbers and underscores.
772  * This function has to be used before any output has started as it calls setcookie().
773  *
774  * @since 2.8.0
775  *
776  * @param string $name The name of the setting.
777  * @param string $value The value for the setting.
778  * @return null|bool true if set successfully/false if not.
779  */
780 function set_user_setting( $name, $value ) {
781
782         if ( headers_sent() ) {
783                 return false;
784         }
785
786         $all_user_settings = get_all_user_settings();
787         $all_user_settings[$name] = $value;
788
789         return wp_set_all_user_settings( $all_user_settings );
790 }
791
792 /**
793  * Delete user interface settings.
794  *
795  * Deleting settings would reset them to the defaults.
796  * This function has to be used before any output has started as it calls setcookie().
797  *
798  * @since 2.7.0
799  *
800  * @param string $names The name or array of names of the setting to be deleted.
801  * @return null|bool true if deleted successfully/false if not.
802  */
803 function delete_user_setting( $names ) {
804
805         if ( headers_sent() ) {
806                 return false;
807         }
808
809         $all_user_settings = get_all_user_settings();
810         $names = (array) $names;
811         $deleted = false;
812
813         foreach ( $names as $name ) {
814                 if ( isset( $all_user_settings[$name] ) ) {
815                         unset( $all_user_settings[$name] );
816                         $deleted = true;
817                 }
818         }
819
820         if ( $deleted ) {
821                 return wp_set_all_user_settings( $all_user_settings );
822         }
823
824         return false;
825 }
826
827 /**
828  * Retrieve all user interface settings.
829  *
830  * @since 2.7.0
831  *
832  * @return array the last saved user settings or empty array.
833  */
834 function get_all_user_settings() {
835         global $_updated_user_settings;
836
837         if ( ! $user_id = get_current_user_id() ) {
838                 return array();
839         }
840
841         if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
842                 return $_updated_user_settings;
843         }
844
845         $user_settings = array();
846
847         if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
848                 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
849
850                 if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
851                         parse_str( $cookie, $user_settings );
852                 }
853         } else {
854                 $option = get_user_option( 'user-settings', $user_id );
855
856                 if ( $option && is_string( $option ) ) {
857                         parse_str( $option, $user_settings );
858                 }
859         }
860
861         $_updated_user_settings = $user_settings;
862         return $user_settings;
863 }
864
865 /**
866  * Private. Set all user interface settings.
867  *
868  * @since 2.8.0
869  *
870  * @param array $user_settings
871  * @return null|bool
872  */
873 function wp_set_all_user_settings( $user_settings ) {
874         global $_updated_user_settings;
875
876         if ( ! $user_id = get_current_user_id() ) {
877                 return false;
878         }
879
880         if ( is_super_admin() && ! is_user_member_of_blog() ) {
881                 return;
882         }
883
884         $settings = '';
885         foreach ( $user_settings as $name => $value ) {
886                 $_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
887                 $_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value );
888
889                 if ( ! empty( $_name ) ) {
890                         $settings .= $_name . '=' . $_value . '&';
891                 }
892         }
893
894         $settings = rtrim( $settings, '&' );
895         parse_str( $settings, $_updated_user_settings );
896
897         update_user_option( $user_id, 'user-settings', $settings, false );
898         update_user_option( $user_id, 'user-settings-time', time(), false );
899
900         return true;
901 }
902
903 /**
904  * Delete the user settings of the current user.
905  *
906  * @since 2.7.0
907  */
908 function delete_all_user_settings() {
909         if ( ! $user_id = get_current_user_id() ) {
910                 return;
911         }
912
913         update_user_option( $user_id, 'user-settings', '', false );
914         setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
915 }
916
917 /**
918  * Retrieve site option value based on name of option.
919  *
920  * @since 2.8.0
921  *
922  * @see get_option()
923  *
924  * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
925  * @param mixed $default Optional value to return if option doesn't exist. Default false.
926  * @param bool $use_cache Whether to use cache. Multisite only. Default true.
927  * @return mixed Value set for the option.
928  */
929 function get_site_option( $option, $default = false, $use_cache = true ) {
930         global $wpdb;
931
932         /**
933          * Filter an existing site option before it is retrieved.
934          *
935          * The dynamic portion of the hook name, `$option`, refers to the option name.
936          *
937          * Passing a truthy value to the filter will effectively short-circuit retrieval,
938          * returning the passed value instead.
939          *
940          * @since 2.9.0 As 'pre_site_option_' . $key
941          * @since 3.0.0
942          *
943          * @param mixed $pre_option The default value to return if the option does not exist.
944          */
945         $pre = apply_filters( 'pre_site_option_' . $option, false );
946
947         if ( false !== $pre )
948                 return $pre;
949
950         // prevent non-existent options from triggering multiple queries
951         $notoptions_key = "{$wpdb->siteid}:notoptions";
952         $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
953
954         if ( isset( $notoptions[$option] ) ) {
955
956                 /**
957                  * Filter a specific default site option.
958                  *
959                  * The dynamic portion of the hook name, `$option`, refers to the option name.
960                  *
961                  * @since 3.4.0
962                  *
963                  * @param mixed $default The value to return if the site option does not exist
964                  *                       in the database.
965                  */
966                 return apply_filters( 'default_site_option_' . $option, $default );
967         }
968
969         if ( ! is_multisite() ) {
970
971                 /** This filter is documented in wp-includes/option.php */
972                 $default = apply_filters( 'default_site_option_' . $option, $default );
973                 $value = get_option($option, $default);
974         } else {
975                 $cache_key = "{$wpdb->siteid}:$option";
976                 if ( $use_cache )
977                         $value = wp_cache_get($cache_key, 'site-options');
978
979                 if ( !isset($value) || (false === $value) ) {
980                         $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
981
982                         // Has to be get_row instead of get_var because of funkiness with 0, false, null values
983                         if ( is_object( $row ) ) {
984                                 $value = $row->meta_value;
985                                 $value = maybe_unserialize( $value );
986                                 wp_cache_set( $cache_key, $value, 'site-options' );
987                         } else {
988                                 $notoptions[$option] = true;
989                                 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
990
991                                 /** This filter is documented in wp-includes/option.php */
992                                 $value = apply_filters( 'default_site_option_' . $option, $default );
993                         }
994                 }
995         }
996
997         /**
998          * Filter the value of an existing site option.
999          *
1000          * The dynamic portion of the hook name, `$option`, refers to the option name.
1001          *
1002          * @since 2.9.0 As 'site_option_' . $key
1003          * @since 3.0.0
1004          *
1005          * @param mixed $value Value of site option.
1006          */
1007         return apply_filters( 'site_option_' . $option, $value );
1008 }
1009
1010 /**
1011  * Add a new site option.
1012  *
1013  * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
1014  *
1015  * @since 2.8.0
1016  *
1017  * @see add_option()
1018  *
1019  * @param string $option Name of option to add. Expected to not be SQL-escaped.
1020  * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
1021  * @return bool False if option was not added and true if option was added.
1022  */
1023 function add_site_option( $option, $value ) {
1024         global $wpdb;
1025
1026         wp_protect_special_option( $option );
1027
1028         /**
1029          * Filter the value of a specific site option before it is added.
1030          *
1031          * The dynamic portion of the hook name, `$option`, refers to the option name.
1032          *
1033          * @since 2.9.0 As 'pre_add_site_option_' . $key
1034          * @since 3.0.0
1035          *
1036          * @param mixed $value Value of site option.
1037          */
1038         $value = apply_filters( 'pre_add_site_option_' . $option, $value );
1039
1040         $notoptions_key = "{$wpdb->siteid}:notoptions";
1041
1042         if ( !is_multisite() ) {
1043                 $result = add_option( $option, $value );
1044         } else {
1045                 $cache_key = "{$wpdb->siteid}:$option";
1046
1047                 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
1048                 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1049                 if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) )
1050                         if ( false !== get_site_option( $option ) )
1051                                 return false;
1052
1053                 $value = sanitize_option( $option, $value );
1054
1055                 $serialized_value = maybe_serialize( $value );
1056                 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
1057
1058                 if ( ! $result )
1059                         return false;
1060
1061                 wp_cache_set( $cache_key, $value, 'site-options' );
1062
1063                 // This option exists now
1064                 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
1065                 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1066                         unset( $notoptions[$option] );
1067                         wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1068                 }
1069         }
1070
1071         if ( $result ) {
1072
1073                 /**
1074                  * Fires after a specific site option has been successfully added.
1075                  *
1076                  * The dynamic portion of the hook name, `$option`, refers to the option name.
1077                  *
1078                  * @since 2.9.0 As "add_site_option_{$key}"
1079                  * @since 3.0.0
1080                  *
1081                  * @param string $option Name of site option.
1082                  * @param mixed  $value  Value of site option.
1083                  */
1084                 do_action( "add_site_option_{$option}", $option, $value );
1085
1086                 /**
1087                  * Fires after a site option has been successfully added.
1088                  *
1089                  * @since 3.0.0
1090                  *
1091                  * @param string $option Name of site option.
1092                  * @param mixed  $value  Value of site option.
1093                  */
1094                 do_action( "add_site_option", $option, $value );
1095
1096                 return true;
1097         }
1098         return false;
1099 }
1100
1101 /**
1102  * Removes site option by name.
1103  *
1104  * @since 2.8.0
1105  *
1106  * @see delete_option()
1107  *
1108  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1109  * @return bool True, if succeed. False, if failure.
1110  */
1111 function delete_site_option( $option ) {
1112         global $wpdb;
1113
1114         // ms_protect_special_option( $option ); @todo
1115
1116         /**
1117          * Fires immediately before a specific site option is deleted.
1118          *
1119          * The dynamic portion of the hook name, `$option`, refers to the option name.
1120          *
1121          * @since 3.0.0
1122          */
1123         do_action( 'pre_delete_site_option_' . $option );
1124
1125         if ( !is_multisite() ) {
1126                 $result = delete_option( $option );
1127         } else {
1128                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
1129                 if ( is_null( $row ) || !$row->meta_id )
1130                         return false;
1131                 $cache_key = "{$wpdb->siteid}:$option";
1132                 wp_cache_delete( $cache_key, 'site-options' );
1133
1134                 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
1135         }
1136
1137         if ( $result ) {
1138
1139                 /**
1140                  * Fires after a specific site option has been deleted.
1141                  *
1142                  * The dynamic portion of the hook name, `$option`, refers to the option name.
1143                  *
1144                  * @since 2.9.0 As "delete_site_option_{$key}"
1145                  * @since 3.0.0
1146                  *
1147                  * @param string $option Name of the site option.
1148                  */
1149                 do_action( "delete_site_option_{$option}", $option );
1150
1151                 /**
1152                  * Fires after a site option has been deleted.
1153                  *
1154                  * @since 3.0.0
1155                  *
1156                  * @param string $option Name of the site option.
1157                  */
1158                 do_action( "delete_site_option", $option );
1159
1160                 return true;
1161         }
1162         return false;
1163 }
1164
1165 /**
1166  * Update the value of a site option that was already added.
1167  *
1168  * @since 2.8.0
1169  *
1170  * @see update_option()
1171  *
1172  * @param string $option Name of option. Expected to not be SQL-escaped.
1173  * @param mixed $value Option value. Expected to not be SQL-escaped.
1174  * @return bool False if value was not updated and true if value was updated.
1175  */
1176 function update_site_option( $option, $value ) {
1177         global $wpdb;
1178
1179         wp_protect_special_option( $option );
1180
1181         $old_value = get_site_option( $option );
1182
1183         /**
1184          * Filter a specific site option before its value is updated.
1185          *
1186          * The dynamic portion of the hook name, `$option`, refers to the option name.
1187          *
1188          * @since 2.9.0 As 'pre_update_site_option_' . $key
1189          * @since 3.0.0
1190          *
1191          * @param mixed $value     New value of site option.
1192          * @param mixed $old_value Old value of site option.
1193          */
1194         $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value );
1195
1196         if ( $value === $old_value )
1197                 return false;
1198
1199         if ( false === $old_value )
1200                 return add_site_option( $option, $value );
1201
1202         $notoptions_key = "{$wpdb->siteid}:notoptions";
1203         $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1204         if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1205                 unset( $notoptions[$option] );
1206                 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1207         }
1208
1209         if ( !is_multisite() ) {
1210                 $result = update_option( $option, $value );
1211         } else {
1212                 $value = sanitize_option( $option, $value );
1213
1214                 $serialized_value = maybe_serialize( $value );
1215                 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
1216
1217                 if ( $result ) {
1218                         $cache_key = "{$wpdb->siteid}:$option";
1219                         wp_cache_set( $cache_key, $value, 'site-options' );
1220                 }
1221         }
1222
1223         if ( $result ) {
1224
1225                 /**
1226                  * Fires after the value of a specific site option has been successfully updated.
1227                  *
1228                  * The dynamic portion of the hook name, `$option`, refers to the option name.
1229                  *
1230                  * @since 2.9.0 As "update_site_option_{$key}"
1231                  * @since 3.0.0
1232                  *
1233                  * @param string $option    Name of site option.
1234                  * @param mixed  $value     Current value of site option.
1235                  * @param mixed  $old_value Old value of site option.
1236                  */
1237                 do_action( "update_site_option_{$option}", $option, $value, $old_value );
1238
1239                 /**
1240                  * Fires after the value of a site option has been successfully updated.
1241                  *
1242                  * @since 3.0.0
1243                  *
1244                  * @param string $option    Name of site option.
1245                  * @param mixed  $value     Current value of site option.
1246                  * @param mixed  $old_value Old value of site option.
1247                  */
1248                 do_action( "update_site_option", $option, $value, $old_value );
1249
1250                 return true;
1251         }
1252         return false;
1253 }
1254
1255 /**
1256  * Delete a site transient.
1257  *
1258  * @since 2.9.0
1259  *
1260  * @param string $transient Transient name. Expected to not be SQL-escaped.
1261  * @return bool True if successful, false otherwise
1262  */
1263 function delete_site_transient( $transient ) {
1264
1265         /**
1266          * Fires immediately before a specific site transient is deleted.
1267          *
1268          * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1269          *
1270          * @since 3.0.0
1271          *
1272          * @param string $transient Transient name.
1273          */
1274         do_action( 'delete_site_transient_' . $transient, $transient );
1275
1276         if ( wp_using_ext_object_cache() ) {
1277                 $result = wp_cache_delete( $transient, 'site-transient' );
1278         } else {
1279                 $option_timeout = '_site_transient_timeout_' . $transient;
1280                 $option = '_site_transient_' . $transient;
1281                 $result = delete_site_option( $option );
1282                 if ( $result )
1283                         delete_site_option( $option_timeout );
1284         }
1285         if ( $result ) {
1286
1287                 /**
1288                  * Fires after a transient is deleted.
1289                  *
1290                  * @since 3.0.0
1291                  *
1292                  * @param string $transient Deleted transient name.
1293                  */
1294                 do_action( 'deleted_site_transient', $transient );
1295         }
1296
1297         return $result;
1298 }
1299
1300 /**
1301  * Get the value of a site transient.
1302  *
1303  * If the transient does not exist, does not have a value, or has expired,
1304  * then the return value will be false.
1305  *
1306  * @since 2.9.0
1307  *
1308  * @see get_transient()
1309  *
1310  * @param string $transient Transient name. Expected to not be SQL-escaped.
1311  * @return mixed Value of transient.
1312  */
1313 function get_site_transient( $transient ) {
1314
1315         /**
1316          * Filter the value of an existing site transient.
1317          *
1318          * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1319          *
1320          * Passing a truthy value to the filter will effectively short-circuit retrieval,
1321          * returning the passed value instead.
1322          *
1323          * @since 2.9.0
1324          *
1325          * @param mixed $pre_site_transient The default value to return if the site transient does not exist.
1326          *                                  Any value other than false will short-circuit the retrieval
1327          *                                  of the transient, and return the returned value.
1328          */
1329         $pre = apply_filters( 'pre_site_transient_' . $transient, false );
1330
1331         if ( false !== $pre )
1332                 return $pre;
1333
1334         if ( wp_using_ext_object_cache() ) {
1335                 $value = wp_cache_get( $transient, 'site-transient' );
1336         } else {
1337                 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1338                 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
1339                 $transient_option = '_site_transient_' . $transient;
1340                 if ( ! in_array( $transient, $no_timeout ) ) {
1341                         $transient_timeout = '_site_transient_timeout_' . $transient;
1342                         $timeout = get_site_option( $transient_timeout );
1343                         if ( false !== $timeout && $timeout < time() ) {
1344                                 delete_site_option( $transient_option  );
1345                                 delete_site_option( $transient_timeout );
1346                                 $value = false;
1347                         }
1348                 }
1349
1350                 if ( ! isset( $value ) )
1351                         $value = get_site_option( $transient_option );
1352         }
1353
1354         /**
1355          * Filter the value of an existing site transient.
1356          *
1357          * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1358          *
1359          * @since 2.9.0
1360          *
1361          * @param mixed $value Value of site transient.
1362          */
1363         return apply_filters( 'site_transient_' . $transient, $value );
1364 }
1365
1366 /**
1367  * Set/update the value of a site transient.
1368  *
1369  * You do not need to serialize values, if the value needs to be serialize, then
1370  * it will be serialized before it is set.
1371  *
1372  * @since 2.9.0
1373  *
1374  * @see set_transient()
1375  *
1376  * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
1377  *                           40 characters or fewer in length.
1378  * @param mixed  $value      Transient value. Expected to not be SQL-escaped.
1379  * @param int    $expiration Optional. Time until expiration in seconds. Default 0.
1380  * @return bool False if value was not set and true if value was set.
1381  */
1382 function set_site_transient( $transient, $value, $expiration = 0 ) {
1383
1384         /**
1385          * Filter the value of a specific site transient before it is set.
1386          *
1387          * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1388          *
1389          * @since 3.0.0
1390          *
1391          * @param mixed $value Value of site transient.
1392          */
1393         $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
1394
1395         $expiration = (int) $expiration;
1396
1397         if ( wp_using_ext_object_cache() ) {
1398                 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1399         } else {
1400                 $transient_timeout = '_site_transient_timeout_' . $transient;
1401                 $option = '_site_transient_' . $transient;
1402                 if ( false === get_site_option( $option ) ) {
1403                         if ( $expiration )
1404                                 add_site_option( $transient_timeout, time() + $expiration );
1405                         $result = add_site_option( $option, $value );
1406                 } else {
1407                         if ( $expiration )
1408                                 update_site_option( $transient_timeout, time() + $expiration );
1409                         $result = update_site_option( $option, $value );
1410                 }
1411         }
1412         if ( $result ) {
1413
1414                 /**
1415                  * Fires after the value for a specific site transient has been set.
1416                  *
1417                  * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1418                  *
1419                  * @since 3.0.0
1420                  *
1421                  * @param mixed $value      Site transient value.
1422                  * @param int   $expiration Time until expiration in seconds. Default 0.
1423                  */
1424                 do_action( 'set_site_transient_' . $transient, $value, $expiration );
1425
1426                 /**
1427                  * Fires after the value for a site transient has been set.
1428                  *
1429                  * @since 3.0.0
1430                  *
1431                  * @param string $transient  The name of the site transient.
1432                  * @param mixed  $value      Site transient value.
1433                  * @param int    $expiration Time until expiration in seconds. Default 0.
1434                  */
1435                 do_action( 'setted_site_transient', $transient, $value, $expiration );
1436         }
1437         return $result;
1438 }