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