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