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