]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/option.php
WordPress 3.9
[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 or does not have a value, then the return value
555  * 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.
624  * @param mixed $value Transient value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
625  * @param int $expiration 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() )
715                 return;
716
717         if ( defined('DOING_AJAX') )
718                 return;
719
720         if ( ! $user_id = get_current_user_id() )
721                 return;
722
723         if ( is_super_admin() && ! is_user_member_of_blog() )
724                 return;
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         setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
748         setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
749         $_COOKIE['wp-settings-' . $user_id] = $settings;
750 }
751
752 /**
753  * Retrieve user interface setting value based on setting name.
754  *
755  * @since 2.7.0
756  *
757  * @param string $name The name of the setting.
758  * @param string $default Optional default value to return when $name is not set.
759  * @return mixed the last saved user setting or the default value/false if it doesn't exist.
760  */
761 function get_user_setting( $name, $default = false ) {
762         $all_user_settings = get_all_user_settings();
763
764         return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
765 }
766
767 /**
768  * Add or update user interface setting.
769  *
770  * Both $name and $value can contain only ASCII letters, numbers and underscores.
771  * This function has to be used before any output has started as it calls setcookie().
772  *
773  * @since 2.8.0
774  *
775  * @param string $name The name of the setting.
776  * @param string $value The value for the setting.
777  * @return bool true if set successfully/false if not.
778  */
779 function set_user_setting( $name, $value ) {
780
781         if ( headers_sent() )
782                 return false;
783
784         $all_user_settings = get_all_user_settings();
785         $all_user_settings[$name] = $value;
786
787         return wp_set_all_user_settings( $all_user_settings );
788 }
789
790 /**
791  * Delete user interface settings.
792  *
793  * Deleting settings would reset them to the defaults.
794  * This function has to be used before any output has started as it calls setcookie().
795  *
796  * @since 2.7.0
797  *
798  * @param mixed $names The name or array of names of the setting to be deleted.
799  * @return bool true if deleted successfully/false if not.
800  */
801 function delete_user_setting( $names ) {
802
803         if ( headers_sent() )
804                 return false;
805
806         $all_user_settings = get_all_user_settings();
807         $names = (array) $names;
808         $deleted = false;
809
810         foreach ( $names as $name ) {
811                 if ( isset( $all_user_settings[$name] ) ) {
812                         unset( $all_user_settings[$name] );
813                         $deleted = true;
814                 }
815         }
816
817         if ( $deleted )
818                 return wp_set_all_user_settings( $all_user_settings );
819
820         return false;
821 }
822
823 /**
824  * Retrieve all user interface settings.
825  *
826  * @since 2.7.0
827  *
828  * @return array the last saved user settings or empty array.
829  */
830 function get_all_user_settings() {
831         global $_updated_user_settings;
832
833         if ( ! $user_id = get_current_user_id() )
834                 return array();
835
836         if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) )
837                 return $_updated_user_settings;
838
839         $user_settings = array();
840         if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
841                 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
842
843                 if ( $cookie && strpos( $cookie, '=' ) ) // '=' cannot be 1st char
844                         parse_str( $cookie, $user_settings );
845
846         } else {
847                 $option = get_user_option( 'user-settings', $user_id );
848                 if ( $option && is_string($option) )
849                         parse_str( $option, $user_settings );
850         }
851
852         $_updated_user_settings = $user_settings;
853         return $user_settings;
854 }
855
856 /**
857  * Private. Set all user interface settings.
858  *
859  * @since 2.8.0
860  *
861  * @param array $user_settings
862  * @return bool
863  */
864 function wp_set_all_user_settings( $user_settings ) {
865         global $_updated_user_settings;
866
867         if ( ! $user_id = get_current_user_id() )
868                 return false;
869
870         if ( is_super_admin() && ! is_user_member_of_blog() )
871                 return;
872
873         $settings = '';
874         foreach ( $user_settings as $name => $value ) {
875                 $_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
876                 $_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value );
877
878                 if ( ! empty( $_name ) )
879                         $settings .= $_name . '=' . $_value . '&';
880         }
881
882         $settings = rtrim($settings, '&');
883         parse_str( $settings, $_updated_user_settings );
884
885         update_user_option( $user_id, 'user-settings', $settings, false );
886         update_user_option( $user_id, 'user-settings-time', time(), false );
887
888         return true;
889 }
890
891 /**
892  * Delete the user settings of the current user.
893  *
894  * @since 2.7.0
895  */
896 function delete_all_user_settings() {
897         if ( ! $user_id = get_current_user_id() )
898                 return;
899
900         update_user_option( $user_id, 'user-settings', '', false );
901         setcookie('wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH);
902 }
903
904 /**
905  * Retrieve site option value based on name of option.
906  *
907  * @since 2.8.0
908  *
909  * @see get_option()
910  *
911  * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
912  * @param mixed $default Optional value to return if option doesn't exist. Default false.
913  * @param bool $use_cache Whether to use cache. Multisite only. Default true.
914  * @return mixed Value set for the option.
915  */
916 function get_site_option( $option, $default = false, $use_cache = true ) {
917         global $wpdb;
918
919         /**
920          * Filter an existing site option before it is retrieved.
921          *
922          * The dynamic portion of the hook name, $option, refers to the option name.
923          *
924          * Passing a truthy value to the filter will effectively short-circuit retrieval,
925          * returning the passed value instead.
926          *
927          * @since 2.9.0 As 'pre_site_option_' . $key
928          * @since 3.0.0
929          *
930          * @param mixed $pre_option The default value to return if the option does not exist.
931          */
932         $pre = apply_filters( 'pre_site_option_' . $option, false );
933
934         if ( false !== $pre )
935                 return $pre;
936
937         // prevent non-existent options from triggering multiple queries
938         $notoptions_key = "{$wpdb->siteid}:notoptions";
939         $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
940
941         if ( isset( $notoptions[$option] ) ) {
942
943                 /**
944                  * Filter a specific default site option.
945                  *
946                  * The dynamic portion of the hook name, $option, refers to the option name.
947                  *
948                  * @since 3.4.0
949                  *
950                  * @param mixed $default The value to return if the site option does not exist
951                  *                       in the database.
952                  */
953                 return apply_filters( 'default_site_option_' . $option, $default );
954         }
955
956         if ( ! is_multisite() ) {
957
958                 /** This filter is documented in wp-includes/option.php */
959                 $default = apply_filters( 'default_site_option_' . $option, $default );
960                 $value = get_option($option, $default);
961         } else {
962                 $cache_key = "{$wpdb->siteid}:$option";
963                 if ( $use_cache )
964                         $value = wp_cache_get($cache_key, 'site-options');
965
966                 if ( !isset($value) || (false === $value) ) {
967                         $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
968
969                         // Has to be get_row instead of get_var because of funkiness with 0, false, null values
970                         if ( is_object( $row ) ) {
971                                 $value = $row->meta_value;
972                                 $value = maybe_unserialize( $value );
973                                 wp_cache_set( $cache_key, $value, 'site-options' );
974                         } else {
975                                 $notoptions[$option] = true;
976                                 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
977
978                                 /** This filter is documented in wp-includes/option.php */
979                                 $value = apply_filters( 'default_site_option_' . $option, $default );
980                         }
981                 }
982         }
983
984         /**
985          * Filter the value of an existing site option.
986          *
987          * The dynamic portion of the hook name, $option, refers to the option name.
988          *
989          * @since 2.9.0 As 'site_option_' . $key
990          * @since 3.0.0
991          *
992          * @param mixed $value Value of site option.
993          */
994         return apply_filters( 'site_option_' . $option, $value );
995 }
996
997 /**
998  * Add a new site option.
999  *
1000  * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
1001  *
1002  * @since 2.8.0
1003  *
1004  * @see add_option()
1005  *
1006  * @param string $option Name of option to add. Expected to not be SQL-escaped.
1007  * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
1008  * @return bool False if option was not added and true if option was added.
1009  */
1010 function add_site_option( $option, $value ) {
1011         global $wpdb;
1012
1013         wp_protect_special_option( $option );
1014
1015         /**
1016          * Filter the value of a specific site option before it is added.
1017          *
1018          * The dynamic portion of the hook name, $option, refers to the option name.
1019          *
1020          * @since 2.9.0 As 'pre_add_site_option_' . $key
1021          * @since 3.0.0
1022          *
1023          * @param mixed $value Value of site option.
1024          */
1025         $value = apply_filters( 'pre_add_site_option_' . $option, $value );
1026
1027         $notoptions_key = "{$wpdb->siteid}:notoptions";
1028
1029         if ( !is_multisite() ) {
1030                 $result = add_option( $option, $value );
1031         } else {
1032                 $cache_key = "{$wpdb->siteid}:$option";
1033
1034                 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
1035                 $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1036                 if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) )
1037                         if ( false !== get_site_option( $option ) )
1038                                 return false;
1039
1040                 $value = sanitize_option( $option, $value );
1041
1042                 $serialized_value = maybe_serialize( $value );
1043                 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
1044
1045                 if ( ! $result )
1046                         return false;
1047
1048                 wp_cache_set( $cache_key, $value, 'site-options' );
1049
1050                 // This option exists now
1051                 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
1052                 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1053                         unset( $notoptions[$option] );
1054                         wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1055                 }
1056         }
1057
1058         if ( $result ) {
1059
1060                 /**
1061                  * Fires after a specific site option has been successfully added.
1062                  *
1063                  * The dynamic portion of the hook name, $option, refers to the option name.
1064                  *
1065                  * @since 2.9.0 As "add_site_option_{$key}"
1066                  * @since 3.0.0
1067                  *
1068                  * @param string $option Name of site option.
1069                  * @param mixed  $value  Value of site option.
1070                  */
1071                 do_action( "add_site_option_{$option}", $option, $value );
1072
1073                 /**
1074                  * Fires after a site option has been successfully added.
1075                  *
1076                  * @since 3.0.0
1077                  *
1078                  * @param string $option Name of site option.
1079                  * @param mixed  $value  Value of site option.
1080                  */
1081                 do_action( "add_site_option", $option, $value );
1082
1083                 return true;
1084         }
1085         return false;
1086 }
1087
1088 /**
1089  * Removes site option by name.
1090  *
1091  * @since 2.8.0
1092  *
1093  * @see delete_option()
1094  *
1095  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1096  * @return bool True, if succeed. False, if failure.
1097  */
1098 function delete_site_option( $option ) {
1099         global $wpdb;
1100
1101         // ms_protect_special_option( $option ); @todo
1102
1103         /**
1104          * Fires immediately before a specific site option is deleted.
1105          *
1106          * The dynamic portion of the hook name, $option, refers to the option name.
1107          *
1108          * @since 3.0.0
1109          */
1110         do_action( 'pre_delete_site_option_' . $option );
1111
1112         if ( !is_multisite() ) {
1113                 $result = delete_option( $option );
1114         } else {
1115                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
1116                 if ( is_null( $row ) || !$row->meta_id )
1117                         return false;
1118                 $cache_key = "{$wpdb->siteid}:$option";
1119                 wp_cache_delete( $cache_key, 'site-options' );
1120
1121                 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
1122         }
1123
1124         if ( $result ) {
1125
1126                 /**
1127                  * Fires after a specific site option has been deleted.
1128                  *
1129                  * The dynamic portion of the hook name, $option, refers to the option name.
1130                  *
1131                  * @since 2.9.0 As "delete_site_option_{$key}"
1132                  * @since 3.0.0
1133                  *
1134                  * @param string $option Name of the site option.
1135                  */
1136                 do_action( "delete_site_option_{$option}", $option );
1137
1138                 /**
1139                  * Fires after a site option has been deleted.
1140                  *
1141                  * @since 3.0.0
1142                  *
1143                  * @param string $option Name of the site option.
1144                  */
1145                 do_action( "delete_site_option", $option );
1146
1147                 return true;
1148         }
1149         return false;
1150 }
1151
1152 /**
1153  * Update the value of a site option that was already added.
1154  *
1155  * @since 2.8.0
1156  *
1157  * @see update_option()
1158  *
1159  * @param string $option Name of option. Expected to not be SQL-escaped.
1160  * @param mixed $value Option value. Expected to not be SQL-escaped.
1161  * @return bool False if value was not updated and true if value was updated.
1162  */
1163 function update_site_option( $option, $value ) {
1164         global $wpdb;
1165
1166         wp_protect_special_option( $option );
1167
1168         $old_value = get_site_option( $option );
1169
1170         /**
1171          * Filter a specific site option before its value is updated.
1172          *
1173          * The dynamic portion of the hook name, $option, refers to the option name.
1174          *
1175          * @since 2.9.0 As 'pre_update_site_option_' . $key
1176          * @since 3.0.0
1177          *
1178          * @param mixed $value     New value of site option.
1179          * @param mixed $old_value Old value of site option.
1180          */
1181         $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value );
1182
1183         if ( $value === $old_value )
1184                 return false;
1185
1186         if ( false === $old_value )
1187                 return add_site_option( $option, $value );
1188
1189         $notoptions_key = "{$wpdb->siteid}:notoptions";
1190         $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1191         if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
1192                 unset( $notoptions[$option] );
1193                 wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1194         }
1195
1196         if ( !is_multisite() ) {
1197                 $result = update_option( $option, $value );
1198         } else {
1199                 $value = sanitize_option( $option, $value );
1200
1201                 $serialized_value = maybe_serialize( $value );
1202                 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
1203
1204                 if ( $result ) {
1205                         $cache_key = "{$wpdb->siteid}:$option";
1206                         wp_cache_set( $cache_key, $value, 'site-options' );
1207                 }
1208         }
1209
1210         if ( $result ) {
1211
1212                 /**
1213                  * Fires after the value of a specific site option has been successfully updated.
1214                  *
1215                  * The dynamic portion of the hook name, $option, refers to the option name.
1216                  *
1217                  * @since 2.9.0 As "update_site_option_{$key}"
1218                  * @since 3.0.0
1219                  *
1220                  * @param string $option    Name of site option.
1221                  * @param mixed  $value     Current value of site option.
1222                  * @param mixed  $old_value Old value of site option.
1223                  */
1224                 do_action( "update_site_option_{$option}", $option, $value, $old_value );
1225
1226                 /**
1227                  * Fires after the value of a site option has been successfully updated.
1228                  *
1229                  * @since 3.0.0
1230                  *
1231                  * @param string $option    Name of site option.
1232                  * @param mixed  $value     Current value of site option.
1233                  * @param mixed  $old_value Old value of site option.
1234                  */
1235                 do_action( "update_site_option", $option, $value, $old_value );
1236
1237                 return true;
1238         }
1239         return false;
1240 }
1241
1242 /**
1243  * Delete a site transient.
1244  *
1245  * @since 2.9.0
1246  *
1247  * @param string $transient Transient name. Expected to not be SQL-escaped.
1248  * @return bool True if successful, false otherwise
1249  */
1250 function delete_site_transient( $transient ) {
1251
1252         /**
1253          * Fires immediately before a specific site transient is deleted.
1254          *
1255          * The dynamic portion of the hook name, $transient, refers to the transient name.
1256          *
1257          * @since 3.0.0
1258          *
1259          * @param string $transient Transient name.
1260          */
1261         do_action( 'delete_site_transient_' . $transient, $transient );
1262
1263         if ( wp_using_ext_object_cache() ) {
1264                 $result = wp_cache_delete( $transient, 'site-transient' );
1265         } else {
1266                 $option_timeout = '_site_transient_timeout_' . $transient;
1267                 $option = '_site_transient_' . $transient;
1268                 $result = delete_site_option( $option );
1269                 if ( $result )
1270                         delete_site_option( $option_timeout );
1271         }
1272         if ( $result ) {
1273
1274                 /**
1275                  * Fires after a transient is deleted.
1276                  *
1277                  * @since 3.0.0
1278                  *
1279                  * @param string $transient Deleted transient name.
1280                  */
1281                 do_action( 'deleted_site_transient', $transient );
1282         }
1283
1284         return $result;
1285 }
1286
1287 /**
1288  * Get the value of a site transient.
1289  *
1290  * If the transient does not exist or does not have a value, then the return value
1291  * will be false.
1292  *
1293  * @since 2.9.0
1294  *
1295  * @see get_transient()
1296  *
1297  * @param string $transient Transient name. Expected to not be SQL-escaped.
1298  * @return mixed Value of transient
1299  */
1300 function get_site_transient( $transient ) {
1301
1302         /**
1303          * Filter the value of an existing site transient.
1304          *
1305          * The dynamic portion of the hook name, $transient, refers to the transient name.
1306          *
1307          * Passing a truthy value to the filter will effectively short-circuit retrieval,
1308          * returning the passed value instead.
1309          *
1310          * @since 2.9.0
1311          *
1312          * @param mixed $pre_site_transient The default value to return if the site transient does not exist.
1313          *                                  Any value other than false will short-circuit the retrieval
1314          *                                  of the transient, and return the returned value.
1315          */
1316         $pre = apply_filters( 'pre_site_transient_' . $transient, false );
1317
1318         if ( false !== $pre )
1319                 return $pre;
1320
1321         if ( wp_using_ext_object_cache() ) {
1322                 $value = wp_cache_get( $transient, 'site-transient' );
1323         } else {
1324                 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1325                 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
1326                 $transient_option = '_site_transient_' . $transient;
1327                 if ( ! in_array( $transient, $no_timeout ) ) {
1328                         $transient_timeout = '_site_transient_timeout_' . $transient;
1329                         $timeout = get_site_option( $transient_timeout );
1330                         if ( false !== $timeout && $timeout < time() ) {
1331                                 delete_site_option( $transient_option  );
1332                                 delete_site_option( $transient_timeout );
1333                                 $value = false;
1334                         }
1335                 }
1336
1337                 if ( ! isset( $value ) )
1338                         $value = get_site_option( $transient_option );
1339         }
1340
1341         /**
1342          * Filter the value of an existing site transient.
1343          *
1344          * The dynamic portion of the hook name, $transient, refers to the transient name.
1345          *
1346          * @since 2.9.0
1347          *
1348          * @param mixed $value Value of site transient.
1349          */
1350         return apply_filters( 'site_transient_' . $transient, $value );
1351 }
1352
1353 /**
1354  * Set/update the value of a site transient.
1355  *
1356  * You do not need to serialize values, if the value needs to be serialize, then
1357  * it will be serialized before it is set.
1358  *
1359  * @since 2.9.0
1360  *
1361  * @see set_transient()
1362  *
1363  * @param string $transient Transient name. Expected to not be SQL-escaped.
1364  * @param mixed $value Transient value. Expected to not be SQL-escaped.
1365  * @param int $expiration Time until expiration in seconds, default 0
1366  * @return bool False if value was not set and true if value was set.
1367  */
1368 function set_site_transient( $transient, $value, $expiration = 0 ) {
1369
1370         /**
1371          * Filter the value of a specific site transient before it is set.
1372          *
1373          * The dynamic portion of the hook name, $transient, refers to the transient name.
1374          *
1375          * @since 3.0.0
1376          *
1377          * @param mixed $value Value of site transient.
1378          */
1379         $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
1380
1381         $expiration = (int) $expiration;
1382
1383         if ( wp_using_ext_object_cache() ) {
1384                 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1385         } else {
1386                 $transient_timeout = '_site_transient_timeout_' . $transient;
1387                 $option = '_site_transient_' . $transient;
1388                 if ( false === get_site_option( $option ) ) {
1389                         if ( $expiration )
1390                                 add_site_option( $transient_timeout, time() + $expiration );
1391                         $result = add_site_option( $option, $value );
1392                 } else {
1393                         if ( $expiration )
1394                                 update_site_option( $transient_timeout, time() + $expiration );
1395                         $result = update_site_option( $option, $value );
1396                 }
1397         }
1398         if ( $result ) {
1399
1400                 /**
1401                  * Fires after the value for a specific site transient has been set.
1402                  *
1403                  * The dynamic portion of the hook name, $transient, refers to the transient name.
1404                  *
1405                  * @since 3.0.0
1406                  *
1407                  * @param mixed $value      Site transient value.
1408                  * @param int   $expiration Time until expiration in seconds. Default 0.
1409                  */
1410                 do_action( 'set_site_transient_' . $transient, $value, $expiration );
1411
1412                 /**
1413                  * Fires after the value for a site transient has been set.
1414                  *
1415                  * @since 3.0.0
1416                  *
1417                  * @param string $transient  The name of the site transient.
1418                  * @param mixed  $value      Site transient value.
1419                  * @param int    $expiration Time until expiration in seconds. Default 0.
1420                  */
1421                 do_action( 'setted_site_transient', $transient, $value, $expiration );
1422         }
1423         return $result;
1424 }