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