]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/option.php
Wordpress 3.5.2
[autoinstalls/wordpress.git] / wp-includes / option.php
1 <?php
2 /**
3  * Option API
4  *
5  * @package WordPress
6  */
7
8 /**
9  * Retrieve option value based on name of option.
10  *
11  * If the option does not exist or does not have a value, then the return value
12  * will be false. This is useful to check whether you need to install an option
13  * and is commonly used during installation of plugin options and to test
14  * whether upgrading is required.
15  *
16  * If the option was serialized then it will be unserialized when it is returned.
17  *
18  * @since 1.5.0
19  * @package WordPress
20  * @subpackage Option
21  * @uses apply_filters() Calls 'pre_option_$option' before checking the option.
22  *      Any value other than false will "short-circuit" the retrieval of the option
23  *      and return the returned value. You should not try to override special options,
24  *      but you will not be prevented from doing so.
25  * @uses apply_filters() Calls 'option_$option', after checking the option, with
26  *      the option value.
27  *
28  * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
29  * @param mixed $default Optional. Default value to return if the option does not exist.
30  * @return mixed Value set for the option.
31  */
32 function get_option( $option, $default = false ) {
33         global $wpdb;
34
35         $option = trim( $option );
36         if ( empty( $option ) )
37                 return false;
38
39         // Allow plugins to short-circuit options.
40         $pre = apply_filters( 'pre_option_' . $option, false );
41         if ( false !== $pre )
42                 return $pre;
43
44         if ( defined( 'WP_SETUP_CONFIG' ) )
45                 return false;
46
47         if ( ! defined( 'WP_INSTALLING' ) ) {
48                 // prevent non-existent options from triggering multiple queries
49                 $notoptions = wp_cache_get( 'notoptions', 'options' );
50                 if ( isset( $notoptions[$option] ) )
51                         return apply_filters( 'default_option_' . $option, $default );
52
53                 $alloptions = wp_load_alloptions();
54
55                 if ( isset( $alloptions[$option] ) ) {
56                         $value = $alloptions[$option];
57                 } else {
58                         $value = wp_cache_get( $option, 'options' );
59
60                         if ( false === $value ) {
61                                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
62
63                                 // Has to be get_row instead of get_var because of funkiness with 0, false, null values
64                                 if ( is_object( $row ) ) {
65                                         $value = $row->option_value;
66                                         wp_cache_add( $option, $value, 'options' );
67                                 } else { // option does not exist, so we must cache its non-existence
68                                         $notoptions[$option] = true;
69                                         wp_cache_set( 'notoptions', $notoptions, 'options' );
70                                         return apply_filters( 'default_option_' . $option, $default );
71                                 }
72                         }
73                 }
74         } else {
75                 $suppress = $wpdb->suppress_errors();
76                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
77                 $wpdb->suppress_errors( $suppress );
78                 if ( is_object( $row ) )
79                         $value = $row->option_value;
80                 else
81                         return apply_filters( 'default_option_' . $option, $default );
82         }
83
84         // If home is not set use siteurl.
85         if ( 'home' == $option && '' == $value )
86                 return get_option( 'siteurl' );
87
88         if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
89                 $value = untrailingslashit( $value );
90
91         return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
92 }
93
94 /**
95  * Protect WordPress special option from being modified.
96  *
97  * Will die if $option is in protected list. Protected options are 'alloptions'
98  * and 'notoptions' options.
99  *
100  * @since 2.2.0
101  * @package WordPress
102  * @subpackage Option
103  *
104  * @param string $option Option name.
105  */
106 function wp_protect_special_option( $option ) {
107         $protected = array( 'alloptions', 'notoptions' );
108         if ( in_array( $option, $protected ) )
109                 wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
110 }
111
112 /**
113  * Print option value after sanitizing for forms.
114  *
115  * @uses attr Sanitizes value.
116  * @since 1.5.0
117  * @package WordPress
118  * @subpackage Option
119  *
120  * @param string $option Option name.
121  */
122 function form_option( $option ) {
123         echo esc_attr( get_option( $option ) );
124 }
125
126 /**
127  * Loads and caches all autoloaded options, if available or all options.
128  *
129  * @since 2.2.0
130  * @package WordPress
131  * @subpackage Option
132  *
133  * @return array List of all options.
134  */
135 function wp_load_alloptions() {
136         global $wpdb;
137
138         if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
139                 $alloptions = wp_cache_get( 'alloptions', 'options' );
140         else
141                 $alloptions = false;
142
143         if ( !$alloptions ) {
144                 $suppress = $wpdb->suppress_errors();
145                 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
146                         $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
147                 $wpdb->suppress_errors($suppress);
148                 $alloptions = array();
149                 foreach ( (array) $alloptions_db as $o ) {
150                         $alloptions[$o->option_name] = $o->option_value;
151                 }
152                 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
153                         wp_cache_add( 'alloptions', $alloptions, 'options' );
154         }
155
156         return $alloptions;
157 }
158
159 /**
160  * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
161  *
162  * @since 3.0.0
163  * @package WordPress
164  * @subpackage Option
165  *
166  * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
167  */
168 function wp_load_core_site_options( $site_id = null ) {
169         global $wpdb, $_wp_using_ext_object_cache;
170
171         if ( !is_multisite() || $_wp_using_ext_object_cache || defined( 'WP_INSTALLING' ) )
172                 return;
173
174         if ( empty($site_id) )
175                 $site_id = $wpdb->siteid;
176
177         $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' );
178
179         $core_options_in = "'" . implode("', '", $core_options) . "'";
180         $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) );
181
182         foreach ( $options as $option ) {
183                 $key = $option->meta_key;
184                 $cache_key = "{$site_id}:$key";
185                 $option->meta_value = maybe_unserialize( $option->meta_value );
186
187                 wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
188         }
189 }
190
191 /**
192  * Update the value of an option that was already added.
193  *
194  * You do not need to serialize values. If the value needs to be serialized, then
195  * it will be serialized before it is inserted into the database. Remember,
196  * resources can not be serialized or added as an option.
197  *
198  * If the option does not exist, then the option will be added with the option
199  * value, but you will not be able to set whether it is autoloaded. If you want
200  * to set whether an option is autoloaded, then you need to use the add_option().
201  *
202  * @since 1.0.0
203  * @package WordPress
204  * @subpackage Option
205  *
206  * @uses apply_filters() Calls 'pre_update_option_$option' hook to allow overwriting the
207  *      option value to be stored.
208  * @uses do_action() Calls 'update_option' hook before updating the option.
209  * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success.
210  *
211  * @param string $option Option name. Expected to not be SQL-escaped.
212  * @param mixed $newvalue Option value. Expected to not be SQL-escaped.
213  * @return bool False if value was not updated and true if value was updated.
214  */
215 function update_option( $option, $newvalue ) {
216         global $wpdb;
217
218         $option = trim($option);
219         if ( empty($option) )
220                 return false;
221
222         wp_protect_special_option( $option );
223
224         if ( is_object($newvalue) )
225                 $newvalue = clone $newvalue;
226
227         $newvalue = sanitize_option( $option, $newvalue );
228         $oldvalue = get_option( $option );
229         $newvalue = apply_filters( 'pre_update_option_' . $option, $newvalue, $oldvalue );
230
231         // If the new and old values are the same, no need to update.
232         if ( $newvalue === $oldvalue )
233                 return false;
234
235         if ( false === $oldvalue )
236                 return add_option( $option, $newvalue );
237
238         $notoptions = wp_cache_get( 'notoptions', 'options' );
239         if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
240                 unset( $notoptions[$option] );
241                 wp_cache_set( 'notoptions', $notoptions, 'options' );
242         }
243
244         $_newvalue = $newvalue;
245         $newvalue = maybe_serialize( $newvalue );
246
247         do_action( 'update_option', $option, $oldvalue, $_newvalue );
248         if ( ! defined( 'WP_INSTALLING' ) ) {
249                 $alloptions = wp_load_alloptions();
250                 if ( isset( $alloptions[$option] ) ) {
251                         $alloptions[$option] = $_newvalue;
252                         wp_cache_set( 'alloptions', $alloptions, 'options' );
253                 } else {
254                         wp_cache_set( $option, $_newvalue, 'options' );
255                 }
256         }
257
258         $result = $wpdb->update( $wpdb->options, array( 'option_value' => $newvalue ), array( 'option_name' => $option ) );
259
260         if ( $result ) {
261                 do_action( "update_option_{$option}", $oldvalue, $_newvalue );
262                 do_action( 'updated_option', $option, $oldvalue, $_newvalue );
263                 return true;
264         }
265         return false;
266 }
267
268 /**
269  * Add a new option.
270  *
271  * You do not need to serialize values. If the value needs to be serialized, then
272  * it will be serialized before it is inserted into the database. Remember,
273  * resources can not be serialized or added as an option.
274  *
275  * You can create options without values and then update the values later.
276  * Existing options will not be updated and checks are performed to ensure that you
277  * aren't adding a protected WordPress option. Care should be taken to not name
278  * options the same as the ones which are protected.
279  *
280  * @package WordPress
281  * @subpackage Option
282  * @since 1.0.0
283  *
284  * @uses do_action() Calls 'add_option' hook before adding the option.
285  * @uses do_action() Calls 'add_option_$option' and 'added_option' hooks on success.
286  *
287  * @param string $option Name of option to add. Expected to not be SQL-escaped.
288  * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
289  * @param mixed $deprecated Optional. Description. Not used anymore.
290  * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
291  * @return bool False if option was not added and true if option was added.
292  */
293 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
294         global $wpdb;
295
296         if ( !empty( $deprecated ) )
297                 _deprecated_argument( __FUNCTION__, '2.3' );
298
299         $option = trim($option);
300         if ( empty($option) )
301                 return false;
302
303         wp_protect_special_option( $option );
304
305         if ( is_object($value) )
306                 $value = clone $value;
307
308         $value = sanitize_option( $option, $value );
309
310         // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
311         $notoptions = wp_cache_get( 'notoptions', 'options' );
312         if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
313                 if ( false !== get_option( $option ) )
314                         return false;
315
316         $_value = $value;
317         $value = maybe_serialize( $value );
318         $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
319         do_action( 'add_option', $option, $_value );
320         if ( ! defined( 'WP_INSTALLING' ) ) {
321                 if ( 'yes' == $autoload ) {
322                         $alloptions = wp_load_alloptions();
323                         $alloptions[$option] = $value;
324                         wp_cache_set( 'alloptions', $alloptions, 'options' );
325                 } else {
326                         wp_cache_set( $option, $value, 'options' );
327                 }
328         }
329
330         // This option exists now
331         $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
332         if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
333                 unset( $notoptions[$option] );
334                 wp_cache_set( 'notoptions', $notoptions, 'options' );
335         }
336
337         $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, $value, $autoload ) );
338
339         if ( $result ) {
340                 do_action( "add_option_{$option}", $option, $_value );
341                 do_action( 'added_option', $option, $_value );
342                 return true;
343         }
344         return false;
345 }
346
347 /**
348  * Removes option by name. Prevents removal of protected WordPress options.
349  *
350  * @package WordPress
351  * @subpackage Option
352  * @since 1.2.0
353  *
354  * @uses do_action() Calls 'delete_option' hook before option is deleted.
355  * @uses do_action() Calls 'deleted_option' and 'delete_option_$option' hooks on success.
356  *
357  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
358  * @return bool True, if option is successfully deleted. False on failure.
359  */
360 function delete_option( $option ) {
361         global $wpdb;
362
363         wp_protect_special_option( $option );
364
365         // Get the ID, if no ID then return
366         $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
367         if ( is_null( $row ) )
368                 return false;
369         do_action( 'delete_option', $option );
370         $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
371         if ( ! defined( 'WP_INSTALLING' ) ) {
372                 if ( 'yes' == $row->autoload ) {
373                         $alloptions = wp_load_alloptions();
374                         if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
375                                 unset( $alloptions[$option] );
376                                 wp_cache_set( 'alloptions', $alloptions, 'options' );
377                         }
378                 } else {
379                         wp_cache_delete( $option, 'options' );
380                 }
381         }
382         if ( $result ) {
383                 do_action( "delete_option_$option", $option );
384                 do_action( 'deleted_option', $option );
385                 return true;
386         }
387         return false;
388 }
389
390 /**
391  * Delete a transient.
392  *
393  * @since 2.8.0
394  * @package WordPress
395  * @subpackage Transient
396  *
397  * @uses do_action() Calls 'delete_transient_$transient' hook before transient is deleted.
398  * @uses do_action() Calls 'deleted_transient' hook on success.
399  *
400  * @param string $transient Transient name. Expected to not be SQL-escaped.
401  * @return bool true if successful, false otherwise
402  */
403 function delete_transient( $transient ) {
404         global $_wp_using_ext_object_cache;
405
406         do_action( 'delete_transient_' . $transient, $transient );
407
408         if ( $_wp_using_ext_object_cache ) {
409                 $result = wp_cache_delete( $transient, 'transient' );
410         } else {
411                 $option_timeout = '_transient_timeout_' . $transient;
412                 $option = '_transient_' . $transient;
413                 $result = delete_option( $option );
414                 if ( $result )
415                         delete_option( $option_timeout );
416         }
417
418         if ( $result )
419                 do_action( 'deleted_transient', $transient );
420         return $result;
421 }
422
423 /**
424  * Get the value of a transient.
425  *
426  * If the transient does not exist or does not have a value, then the return value
427  * will be false.
428  *
429  * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient.
430  *      Any value other than false will "short-circuit" the retrieval of the transient
431  *      and return the returned value.
432  * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with
433  *      the transient value.
434  *
435  * @since 2.8.0
436  * @package WordPress
437  * @subpackage Transient
438  *
439  * @param string $transient Transient name. Expected to not be SQL-escaped
440  * @return mixed Value of transient
441  */
442 function get_transient( $transient ) {
443         global $_wp_using_ext_object_cache;
444
445         $pre = apply_filters( 'pre_transient_' . $transient, false );
446         if ( false !== $pre )
447                 return $pre;
448
449         if ( $_wp_using_ext_object_cache ) {
450                 $value = wp_cache_get( $transient, 'transient' );
451         } else {
452                 $transient_option = '_transient_' . $transient;
453                 if ( ! defined( 'WP_INSTALLING' ) ) {
454                         // If option is not in alloptions, it is not autoloaded and thus has a timeout
455                         $alloptions = wp_load_alloptions();
456                         if ( !isset( $alloptions[$transient_option] ) ) {
457                                 $transient_timeout = '_transient_timeout_' . $transient;
458                                 if ( get_option( $transient_timeout ) < time() ) {
459                                         delete_option( $transient_option  );
460                                         delete_option( $transient_timeout );
461                                         return false;
462                                 }
463                         }
464                 }
465
466                 $value = get_option( $transient_option );
467         }
468
469         return apply_filters( 'transient_' . $transient, $value );
470 }
471
472 /**
473  * Set/update the value of a transient.
474  *
475  * You do not need to serialize values. If the value needs to be serialized, then
476  * it will be serialized before it is set.
477  *
478  * @since 2.8.0
479  * @package WordPress
480  * @subpackage Transient
481  *
482  * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
483  *      transient value to be stored.
484  * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
485  *
486  * @param string $transient Transient name. Expected to not be SQL-escaped.
487  * @param mixed $value Transient value. Expected to not be SQL-escaped.
488  * @param int $expiration Time until expiration in seconds, default 0
489  * @return bool False if value was not set and true if value was set.
490  */
491 function set_transient( $transient, $value, $expiration = 0 ) {
492         global $_wp_using_ext_object_cache;
493
494         $value = apply_filters( 'pre_set_transient_' . $transient, $value );
495
496         if ( $_wp_using_ext_object_cache ) {
497                 $result = wp_cache_set( $transient, $value, 'transient', $expiration );
498         } else {
499                 $transient_timeout = '_transient_timeout_' . $transient;
500                 $transient = '_transient_' . $transient;
501                 if ( false === get_option( $transient ) ) {
502                         $autoload = 'yes';
503                         if ( $expiration ) {
504                                 $autoload = 'no';
505                                 add_option( $transient_timeout, time() + $expiration, '', 'no' );
506                         }
507                         $result = add_option( $transient, $value, '', $autoload );
508                 } else {
509                         if ( $expiration )
510                                 update_option( $transient_timeout, time() + $expiration );
511                         $result = update_option( $transient, $value );
512                 }
513         }
514         if ( $result ) {
515                 do_action( 'set_transient_' . $transient );
516                 do_action( 'setted_transient', $transient );
517         }
518         return $result;
519 }
520
521 /**
522  * Saves and restores user interface settings stored in a cookie.
523  *
524  * Checks if the current user-settings cookie is updated and stores it. When no
525  * cookie exists (different browser used), adds the last saved cookie restoring
526  * the settings.
527  *
528  * @package WordPress
529  * @subpackage Option
530  * @since 2.7.0
531  */
532 function wp_user_settings() {
533
534         if ( ! is_admin() )
535                 return;
536
537         if ( defined('DOING_AJAX') )
538                 return;
539
540         if ( ! $user = wp_get_current_user() )
541                 return;
542
543         if ( is_super_admin( $user->ID ) &&
544                 ! in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user->ID ) ) )
545                 )
546                 return;
547
548         $settings = get_user_option( 'user-settings', $user->ID );
549
550         if ( isset( $_COOKIE['wp-settings-' . $user->ID] ) ) {
551                 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
552
553                 if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) {
554                         if ( $cookie == $settings )
555                                 return;
556
557                         $last_time = (int) get_user_option( 'user-settings-time', $user->ID );
558                         $saved = isset( $_COOKIE['wp-settings-time-' . $user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user->ID] ) : 0;
559
560                         if ( $saved > $last_time ) {
561                                 update_user_option( $user->ID, 'user-settings', $cookie, false );
562                                 update_user_option( $user->ID, 'user-settings-time', time() - 5, false );
563                                 return;
564                         }
565                 }
566         }
567
568         setcookie( 'wp-settings-' . $user->ID, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
569         setcookie( 'wp-settings-time-' . $user->ID, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
570         $_COOKIE['wp-settings-' . $user->ID] = $settings;
571 }
572
573 /**
574  * Retrieve user interface setting value based on setting name.
575  *
576  * @package WordPress
577  * @subpackage Option
578  * @since 2.7.0
579  *
580  * @param string $name The name of the setting.
581  * @param string $default Optional default value to return when $name is not set.
582  * @return mixed the last saved user setting or the default value/false if it doesn't exist.
583  */
584 function get_user_setting( $name, $default = false ) {
585
586         $all = get_all_user_settings();
587
588         return isset($all[$name]) ? $all[$name] : $default;
589 }
590
591 /**
592  * Add or update user interface setting.
593  *
594  * Both $name and $value can contain only ASCII letters, numbers and underscores.
595  * This function has to be used before any output has started as it calls setcookie().
596  *
597  * @package WordPress
598  * @subpackage Option
599  * @since 2.8.0
600  *
601  * @param string $name The name of the setting.
602  * @param string $value The value for the setting.
603  * @return bool true if set successfully/false if not.
604  */
605 function set_user_setting( $name, $value ) {
606
607         if ( headers_sent() )
608                 return false;
609
610         $all = get_all_user_settings();
611         $name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
612
613         if ( empty($name) )
614                 return false;
615
616         $all[$name] = $value;
617
618         return wp_set_all_user_settings($all);
619 }
620
621 /**
622  * Delete user interface settings.
623  *
624  * Deleting settings would reset them to the defaults.
625  * This function has to be used before any output has started as it calls setcookie().
626  *
627  * @package WordPress
628  * @subpackage Option
629  * @since 2.7.0
630  *
631  * @param mixed $names The name or array of names of the setting to be deleted.
632  * @return bool true if deleted successfully/false if not.
633  */
634 function delete_user_setting( $names ) {
635
636         if ( headers_sent() )
637                 return false;
638
639         $all = get_all_user_settings();
640         $names = (array) $names;
641
642         foreach ( $names as $name ) {
643                 if ( isset($all[$name]) ) {
644                         unset($all[$name]);
645                         $deleted = true;
646                 }
647         }
648
649         if ( isset($deleted) )
650                 return wp_set_all_user_settings($all);
651
652         return false;
653 }
654
655 /**
656  * Retrieve all user interface settings.
657  *
658  * @package WordPress
659  * @subpackage Option
660  * @since 2.7.0
661  *
662  * @return array the last saved user settings or empty array.
663  */
664 function get_all_user_settings() {
665         global $_updated_user_settings;
666
667         if ( ! $user = wp_get_current_user() )
668                 return array();
669
670         if ( isset($_updated_user_settings) && is_array($_updated_user_settings) )
671                 return $_updated_user_settings;
672
673         $all = array();
674         if ( isset($_COOKIE['wp-settings-' . $user->ID]) ) {
675                 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
676
677                 if ( $cookie && strpos($cookie, '=') ) // the '=' cannot be 1st char
678                         parse_str($cookie, $all);
679
680         } else {
681                 $option = get_user_option('user-settings', $user->ID);
682                 if ( $option && is_string($option) )
683                         parse_str( $option, $all );
684         }
685
686         return $all;
687 }
688
689 /**
690  * Private. Set all user interface settings.
691  *
692  * @package WordPress
693  * @subpackage Option
694  * @since 2.8.0
695  *
696  * @param unknown $all
697  * @return bool
698  */
699 function wp_set_all_user_settings($all) {
700         global $_updated_user_settings;
701
702         if ( ! $user = wp_get_current_user() )
703                 return false;
704
705         if ( is_super_admin( $user->ID ) &&
706                 ! in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user->ID ) ) )
707                 )
708                 return;
709
710         $_updated_user_settings = $all;
711         $settings = '';
712         foreach ( $all as $k => $v ) {
713                 $v = preg_replace( '/[^A-Za-z0-9_]+/', '', $v );
714                 $settings .= $k . '=' . $v . '&';
715         }
716
717         $settings = rtrim($settings, '&');
718
719         update_user_option( $user->ID, 'user-settings', $settings, false );
720         update_user_option( $user->ID, 'user-settings-time', time(), false );
721
722         return true;
723 }
724
725 /**
726  * Delete the user settings of the current user.
727  *
728  * @package WordPress
729  * @subpackage Option
730  * @since 2.7.0
731  */
732 function delete_all_user_settings() {
733         if ( ! $user = wp_get_current_user() )
734                 return;
735
736         update_user_option( $user->ID, 'user-settings', '', false );
737         setcookie('wp-settings-' . $user->ID, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH);
738 }
739
740 /**
741  * Retrieve site option value based on name of option.
742  *
743  * @see get_option()
744  * @package WordPress
745  * @subpackage Option
746  * @since 2.8.0
747  *
748  * @uses apply_filters() Calls 'pre_site_option_$option' before checking the option.
749  *      Any value other than false will "short-circuit" the retrieval of the option
750  *      and return the returned value.
751  * @uses apply_filters() Calls 'site_option_$option', after checking the  option, with
752  *      the option value.
753  *
754  * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
755  * @param mixed $default Optional value to return if option doesn't exist. Default false.
756  * @param bool $use_cache Whether to use cache. Multisite only. Default true.
757  * @return mixed Value set for the option.
758  */
759 function get_site_option( $option, $default = false, $use_cache = true ) {
760         global $wpdb;
761
762         // Allow plugins to short-circuit site options.
763         $pre = apply_filters( 'pre_site_option_' . $option, false );
764         if ( false !== $pre )
765                 return $pre;
766
767         if ( ! is_multisite() ) {
768                 $default = apply_filters( 'default_site_option_' . $option, $default );
769                 $value = get_option($option, $default);
770         } else {
771                 $cache_key = "{$wpdb->siteid}:$option";
772                 if ( $use_cache )
773                         $value = wp_cache_get($cache_key, 'site-options');
774
775                 if ( !isset($value) || (false === $value) ) {
776                         $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
777
778                         // Has to be get_row instead of get_var because of funkiness with 0, false, null values
779                         if ( is_object( $row ) ) {
780                                 $value = $row->meta_value;
781                                 $value = maybe_unserialize( $value );
782                                 wp_cache_set( $cache_key, $value, 'site-options' );
783                         } else {
784                                 $value = apply_filters( 'default_site_option_' . $option, $default );
785                         }
786                 }
787         }
788
789         return apply_filters( 'site_option_' . $option, $value );
790 }
791
792 /**
793  * Add a new site option.
794  *
795  * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
796  *
797  * @see add_option()
798  * @package WordPress
799  * @subpackage Option
800  * @since 2.8.0
801  *
802  * @uses apply_filters() Calls 'pre_add_site_option_$option' hook to allow overwriting the
803  *      option value to be stored.
804  * @uses do_action() Calls 'add_site_option_$option' and 'add_site_option' hooks on success.
805  *
806  * @param string $option Name of option to add. Expected to not be SQL-escaped.
807  * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
808  * @return bool False if option was not added and true if option was added.
809  */
810 function add_site_option( $option, $value ) {
811         global $wpdb;
812
813         $value = apply_filters( 'pre_add_site_option_' . $option, $value );
814
815         if ( !is_multisite() ) {
816                 $result = add_option( $option, $value );
817         } else {
818                 $cache_key = "{$wpdb->siteid}:$option";
819
820                 if ( false !== get_site_option( $option ) )
821                         return false;
822
823                 $value = sanitize_option( $option, $value );
824                 wp_cache_set( $cache_key, $value, 'site-options' );
825
826                 $_value = $value;
827                 $value = maybe_serialize( $value );
828                 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $value ) );
829                 $value = $_value;
830         }
831
832         if ( $result ) {
833                 do_action( "add_site_option_{$option}", $option, $value );
834                 do_action( "add_site_option", $option, $value );
835                 return true;
836         }
837         return false;
838 }
839
840 /**
841  * Removes site option by name.
842  *
843  * @see delete_option()
844  * @package WordPress
845  * @subpackage Option
846  * @since 2.8.0
847  *
848  * @uses do_action() Calls 'pre_delete_site_option_$option' hook before option is deleted.
849  * @uses do_action() Calls 'delete_site_option' and 'delete_site_option_$option'
850  *      hooks on success.
851  *
852  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
853  * @return bool True, if succeed. False, if failure.
854  */
855 function delete_site_option( $option ) {
856         global $wpdb;
857
858         // ms_protect_special_option( $option ); @todo
859
860         do_action( 'pre_delete_site_option_' . $option );
861
862         if ( !is_multisite() ) {
863                 $result = delete_option( $option );
864         } else {
865                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
866                 if ( is_null( $row ) || !$row->meta_id )
867                         return false;
868                 $cache_key = "{$wpdb->siteid}:$option";
869                 wp_cache_delete( $cache_key, 'site-options' );
870
871                 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
872         }
873
874         if ( $result ) {
875                 do_action( "delete_site_option_{$option}", $option );
876                 do_action( "delete_site_option", $option );
877                 return true;
878         }
879         return false;
880 }
881
882 /**
883  * Update the value of a site option that was already added.
884  *
885  * @see update_option()
886  * @since 2.8.0
887  * @package WordPress
888  * @subpackage Option
889  *
890  * @uses apply_filters() Calls 'pre_update_site_option_$option' hook to allow overwriting the
891  *      option value to be stored.
892  * @uses do_action() Calls 'update_site_option_$option' and 'update_site_option' hooks on success.
893  *
894  * @param string $option Name of option. Expected to not be SQL-escaped.
895  * @param mixed $value Option value. Expected to not be SQL-escaped.
896  * @return bool False if value was not updated and true if value was updated.
897  */
898 function update_site_option( $option, $value ) {
899         global $wpdb;
900
901         $oldvalue = get_site_option( $option );
902         $value = apply_filters( 'pre_update_site_option_' . $option, $value, $oldvalue );
903
904         if ( $value === $oldvalue )
905                 return false;
906
907         if ( false === $oldvalue )
908                 return add_site_option( $option, $value );
909
910         if ( !is_multisite() ) {
911                 $result = update_option( $option, $value );
912         } else {
913                 $value = sanitize_option( $option, $value );
914                 $cache_key = "{$wpdb->siteid}:$option";
915                 wp_cache_set( $cache_key, $value, 'site-options' );
916
917                 $_value = $value;
918                 $value = maybe_serialize( $value );
919                 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
920                 $value = $_value;
921         }
922
923         if ( $result ) {
924                 do_action( "update_site_option_{$option}", $option, $value, $oldvalue );
925                 do_action( "update_site_option", $option, $value, $oldvalue );
926                 return true;
927         }
928         return false;
929 }
930
931 /**
932  * Delete a site transient.
933  *
934  * @since 2.9.0
935  * @package WordPress
936  * @subpackage Transient
937  *
938  * @uses do_action() Calls 'delete_site_transient_$transient' hook before transient is deleted.
939  * @uses do_action() Calls 'deleted_site_transient' hook on success.
940  *
941  * @param string $transient Transient name. Expected to not be SQL-escaped.
942  * @return bool True if successful, false otherwise
943  */
944 function delete_site_transient( $transient ) {
945         global $_wp_using_ext_object_cache;
946
947         do_action( 'delete_site_transient_' . $transient, $transient );
948         if ( $_wp_using_ext_object_cache ) {
949                 $result = wp_cache_delete( $transient, 'site-transient' );
950         } else {
951                 $option_timeout = '_site_transient_timeout_' . $transient;
952                 $option = '_site_transient_' . $transient;
953                 $result = delete_site_option( $option );
954                 if ( $result )
955                         delete_site_option( $option_timeout );
956         }
957         if ( $result )
958                 do_action( 'deleted_site_transient', $transient );
959         return $result;
960 }
961
962 /**
963  * Get the value of a site transient.
964  *
965  * If the transient does not exist or does not have a value, then the return value
966  * will be false.
967  *
968  * @see get_transient()
969  * @since 2.9.0
970  * @package WordPress
971  * @subpackage Transient
972  *
973  * @uses apply_filters() Calls 'pre_site_transient_$transient' hook before checking the transient.
974  *      Any value other than false will "short-circuit" the retrieval of the transient
975  *      and return the returned value.
976  * @uses apply_filters() Calls 'site_transient_$option' hook, after checking the transient, with
977  *      the transient value.
978  *
979  * @param string $transient Transient name. Expected to not be SQL-escaped.
980  * @return mixed Value of transient
981  */
982 function get_site_transient( $transient ) {
983         global $_wp_using_ext_object_cache;
984
985         $pre = apply_filters( 'pre_site_transient_' . $transient, false );
986         if ( false !== $pre )
987                 return $pre;
988
989         if ( $_wp_using_ext_object_cache ) {
990                 $value = wp_cache_get( $transient, 'site-transient' );
991         } else {
992                 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
993                 $no_timeout = array('update_core', 'update_plugins', 'update_themes');
994                 $transient_option = '_site_transient_' . $transient;
995                 if ( ! in_array( $transient, $no_timeout ) ) {
996                         $transient_timeout = '_site_transient_timeout_' . $transient;
997                         $timeout = get_site_option( $transient_timeout );
998                         if ( false !== $timeout && $timeout < time() ) {
999                                 delete_site_option( $transient_option  );
1000                                 delete_site_option( $transient_timeout );
1001                                 return false;
1002                         }
1003                 }
1004
1005                 $value = get_site_option( $transient_option );
1006         }
1007
1008         return apply_filters( 'site_transient_' . $transient, $value );
1009 }
1010
1011 /**
1012  * Set/update the value of a site transient.
1013  *
1014  * You do not need to serialize values, if the value needs to be serialize, then
1015  * it will be serialized before it is set.
1016  *
1017  * @see set_transient()
1018  * @since 2.9.0
1019  * @package WordPress
1020  * @subpackage Transient
1021  *
1022  * @uses apply_filters() Calls 'pre_set_site_transient_$transient' hook to allow overwriting the
1023  *      transient value to be stored.
1024  * @uses do_action() Calls 'set_site_transient_$transient' and 'setted_site_transient' hooks on success.
1025  *
1026  * @param string $transient Transient name. Expected to not be SQL-escaped.
1027  * @param mixed $value Transient value. Expected to not be SQL-escaped.
1028  * @param int $expiration Time until expiration in seconds, default 0
1029  * @return bool False if value was not set and true if value was set.
1030  */
1031 function set_site_transient( $transient, $value, $expiration = 0 ) {
1032         global $_wp_using_ext_object_cache;
1033
1034         $value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
1035
1036         if ( $_wp_using_ext_object_cache ) {
1037                 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1038         } else {
1039                 $transient_timeout = '_site_transient_timeout_' . $transient;
1040                 $transient = '_site_transient_' . $transient;
1041                 if ( false === get_site_option( $transient ) ) {
1042                         if ( $expiration )
1043                                 add_site_option( $transient_timeout, time() + $expiration );
1044                         $result = add_site_option( $transient, $value );
1045                 } else {
1046                         if ( $expiration )
1047                                 update_site_option( $transient_timeout, time() + $expiration );
1048                         $result = update_site_option( $transient, $value );
1049                 }
1050         }
1051         if ( $result ) {
1052                 do_action( 'set_site_transient_' . $transient );
1053                 do_action( 'setted_site_transient', $transient );
1054         }
1055         return $result;
1056 }