wp/wp-includes/option.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Option
     6  * @subpackage Option
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * Retrieve option value based on name of option.
    10  * Retrieves an option value based on an option name.
    11  *
    11  *
    12  * If the option does not exist or does not have a value, then the return value
    12  * If the option does not exist or does not have a value, then the return value
    13  * will be false. This is useful to check whether you need to install an option
    13  * will be false. This is useful to check whether you need to install an option
    14  * and is commonly used during installation of plugin options and to test
    14  * and is commonly used during installation of plugin options and to test
    15  * whether upgrading is required.
    15  * whether upgrading is required.
    16  *
    16  *
    17  * 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.
    18  *
    18  *
       
    19  * Any scalar values will be returned as strings. You may coerce the return type of
       
    20  * a given option by registering an {@see 'option_$option'} filter callback.
       
    21  *
    19  * @since 1.5.0
    22  * @since 1.5.0
    20  *
    23  *
    21  * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
    24  * @global wpdb $wpdb WordPress database abstraction object.
    22  * @param mixed $default Optional. Default value to return if the option does not exist.
    25  *
       
    26  * @param string $option  Name of option to retrieve. Expected to not be SQL-escaped.
       
    27  * @param mixed  $default Optional. Default value to return if the option does not exist.
    23  * @return mixed Value set for the option.
    28  * @return mixed Value set for the option.
    24  */
    29  */
    25 function get_option( $option, $default = false ) {
    30 function get_option( $option, $default = false ) {
    26 	global $wpdb;
    31 	global $wpdb;
    27 
    32 
    28 	$option = trim( $option );
    33 	$option = trim( $option );
    29 	if ( empty( $option ) )
    34 	if ( empty( $option ) )
    30 		return false;
    35 		return false;
    31 
    36 
    32 	/**
    37 	/**
    33 	 * Filter the value of an existing option before it is retrieved.
    38 	 * Filters the value of an existing option before it is retrieved.
    34 	 *
    39 	 *
    35 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
    40 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
    36 	 *
    41 	 *
    37 	 * Passing a truthy value to the filter will short-circuit retrieving
    42 	 * Passing a truthy value to the filter will short-circuit retrieving
    38 	 * the option value, returning the passed value instead.
    43 	 * the option value, returning the passed value instead.
    39 	 *
    44 	 *
    40 	 * @since 1.5.0
    45 	 * @since 1.5.0
    41 	 *
    46 	 * @since 4.4.0 The `$option` parameter was added.
    42 	 * @param bool|mixed $pre_option Value to return instead of the option value.
    47 	 * @since 4.9.0 The `$default` parameter was added.
    43 	 *                               Default false to skip it.
    48 	 *
    44 	 */
    49 	 *
    45 	$pre = apply_filters( 'pre_option_' . $option, false );
    50 	 * @param bool|mixed $pre_option The value to return instead of the option value. This differs from
       
    51 	 *                               `$default`, which is used as the fallback value in the event the option
       
    52 	 *                               doesn't exist elsewhere in get_option(). Default false (to skip past the
       
    53 	 *                               short-circuit).
       
    54 	 * @param string     $option     Option name.
       
    55 	 * @param mixed      $default    The fallback value to return if the option does not exist.
       
    56 	 *                               Default is false.
       
    57 	 */
       
    58 	$pre = apply_filters( "pre_option_{$option}", false, $option, $default );
       
    59 
    46 	if ( false !== $pre )
    60 	if ( false !== $pre )
    47 		return $pre;
    61 		return $pre;
    48 
    62 
    49 	if ( defined( 'WP_SETUP_CONFIG' ) )
    63 	if ( defined( 'WP_SETUP_CONFIG' ) )
    50 		return false;
    64 		return false;
    51 
    65 
    52 	if ( ! defined( 'WP_INSTALLING' ) ) {
    66 	// Distinguish between `false` as a default, and not passing one.
       
    67 	$passed_default = func_num_args() > 1;
       
    68 
       
    69 	if ( ! wp_installing() ) {
    53 		// prevent non-existent options from triggering multiple queries
    70 		// prevent non-existent options from triggering multiple queries
    54 		$notoptions = wp_cache_get( 'notoptions', 'options' );
    71 		$notoptions = wp_cache_get( 'notoptions', 'options' );
    55 		if ( isset( $notoptions[ $option ] ) ) {
    72 		if ( isset( $notoptions[ $option ] ) ) {
    56 			/**
    73 			/**
    57 			 * Filter the default value for an option.
    74 			 * Filters the default value for an option.
    58 			 *
    75 			 *
    59 			 * The dynamic portion of the hook name, `$option`, refers to the option name.
    76 			 * The dynamic portion of the hook name, `$option`, refers to the option name.
    60 			 *
    77 			 *
    61 			 * @since 3.4.0
    78 			 * @since 3.4.0
       
    79 			 * @since 4.4.0 The `$option` parameter was added.
       
    80 			 * @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value.
    62 			 *
    81 			 *
    63 			 * @param mixed $default The default value to return if the option does not exist
    82 			 * @param mixed  $default The default value to return if the option does not exist
    64 			 *                       in the database.
    83 			 *                        in the database.
       
    84 			 * @param string $option  Option name.
       
    85 			 * @param bool   $passed_default Was `get_option()` passed a default value?
    65 			 */
    86 			 */
    66 			return apply_filters( 'default_option_' . $option, $default );
    87 			return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
    67 		}
    88 		}
    68 
    89 
    69 		$alloptions = wp_load_alloptions();
    90 		$alloptions = wp_load_alloptions();
    70 
    91 
    71 		if ( isset( $alloptions[$option] ) ) {
    92 		if ( isset( $alloptions[$option] ) ) {
    79 				// Has to be get_row instead of get_var because of funkiness with 0, false, null values
   100 				// Has to be get_row instead of get_var because of funkiness with 0, false, null values
    80 				if ( is_object( $row ) ) {
   101 				if ( is_object( $row ) ) {
    81 					$value = $row->option_value;
   102 					$value = $row->option_value;
    82 					wp_cache_add( $option, $value, 'options' );
   103 					wp_cache_add( $option, $value, 'options' );
    83 				} else { // option does not exist, so we must cache its non-existence
   104 				} else { // option does not exist, so we must cache its non-existence
       
   105 					if ( ! is_array( $notoptions ) ) {
       
   106 						 $notoptions = array();
       
   107 					}
    84 					$notoptions[$option] = true;
   108 					$notoptions[$option] = true;
    85 					wp_cache_set( 'notoptions', $notoptions, 'options' );
   109 					wp_cache_set( 'notoptions', $notoptions, 'options' );
    86 
   110 
    87 					/** This filter is documented in wp-includes/option.php */
   111 					/** This filter is documented in wp-includes/option.php */
    88 					return apply_filters( 'default_option_' . $option, $default );
   112 					return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
    89 				}
   113 				}
    90 			}
   114 			}
    91 		}
   115 		}
    92 	} else {
   116 	} else {
    93 		$suppress = $wpdb->suppress_errors();
   117 		$suppress = $wpdb->suppress_errors();
    95 		$wpdb->suppress_errors( $suppress );
   119 		$wpdb->suppress_errors( $suppress );
    96 		if ( is_object( $row ) ) {
   120 		if ( is_object( $row ) ) {
    97 			$value = $row->option_value;
   121 			$value = $row->option_value;
    98 		} else {
   122 		} else {
    99 			/** This filter is documented in wp-includes/option.php */
   123 			/** This filter is documented in wp-includes/option.php */
   100 			return apply_filters( 'default_option_' . $option, $default );
   124 			return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
   101 		}
   125 		}
   102 	}
   126 	}
   103 
   127 
   104 	// If home is not set use siteurl.
   128 	// If home is not set use siteurl.
   105 	if ( 'home' == $option && '' == $value )
   129 	if ( 'home' == $option && '' == $value )
   107 
   131 
   108 	if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
   132 	if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
   109 		$value = untrailingslashit( $value );
   133 		$value = untrailingslashit( $value );
   110 
   134 
   111 	/**
   135 	/**
   112 	 * Filter the value of an existing option.
   136 	 * Filters the value of an existing option.
   113 	 *
   137 	 *
   114 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
   138 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
   115 	 *
   139 	 *
   116 	 * @since 1.5.0 As 'option_' . $setting
   140 	 * @since 1.5.0 As 'option_' . $setting
   117 	 * @since 3.0.0
   141 	 * @since 3.0.0
   118 	 *
   142 	 * @since 4.4.0 The `$option` parameter was added.
   119 	 * @param mixed $value Value of the option. If stored serialized, it will be
   143 	 *
   120 	 *                     unserialized prior to being returned.
   144 	 * @param mixed  $value  Value of the option. If stored serialized, it will be
   121 	 */
   145 	 *                       unserialized prior to being returned.
   122 	return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
   146 	 * @param string $option Option name.
       
   147 	 */
       
   148 	return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
   123 }
   149 }
   124 
   150 
   125 /**
   151 /**
   126  * Protect WordPress special option from being modified.
   152  * Protect WordPress special option from being modified.
   127  *
   153  *
   151 /**
   177 /**
   152  * Loads and caches all autoloaded options, if available or all options.
   178  * Loads and caches all autoloaded options, if available or all options.
   153  *
   179  *
   154  * @since 2.2.0
   180  * @since 2.2.0
   155  *
   181  *
       
   182  * @global wpdb $wpdb WordPress database abstraction object.
       
   183  *
   156  * @return array List of all options.
   184  * @return array List of all options.
   157  */
   185  */
   158 function wp_load_alloptions() {
   186 function wp_load_alloptions() {
   159 	global $wpdb;
   187 	global $wpdb;
   160 
   188 
   161 	if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
   189 	if ( ! wp_installing() || ! is_multisite() ) {
   162 		$alloptions = wp_cache_get( 'alloptions', 'options' );
   190 		$alloptions = wp_cache_get( 'alloptions', 'options' );
   163 	else
   191 	} else {
   164 		$alloptions = false;
   192 		$alloptions = false;
   165 
   193 	}
   166 	if ( !$alloptions ) {
   194 
       
   195 	if ( ! $alloptions ) {
   167 		$suppress = $wpdb->suppress_errors();
   196 		$suppress = $wpdb->suppress_errors();
   168 		if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
   197 		if ( ! $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) {
   169 			$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
   198 			$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
   170 		$wpdb->suppress_errors($suppress);
   199 		}
       
   200 		$wpdb->suppress_errors( $suppress );
       
   201 
   171 		$alloptions = array();
   202 		$alloptions = array();
   172 		foreach ( (array) $alloptions_db as $o ) {
   203 		foreach ( (array) $alloptions_db as $o ) {
   173 			$alloptions[$o->option_name] = $o->option_value;
   204 			$alloptions[$o->option_name] = $o->option_value;
   174 		}
   205 		}
   175 		if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
   206 
       
   207 		if ( ! wp_installing() || ! is_multisite() ) {
       
   208 			/**
       
   209 			 * Filters all options before caching them.
       
   210 			 *
       
   211 			 * @since 4.9.0
       
   212 			 *
       
   213 			 * @param array $alloptions Array with all options.
       
   214 			 */
       
   215 			$alloptions = apply_filters( 'pre_cache_alloptions', $alloptions );
   176 			wp_cache_add( 'alloptions', $alloptions, 'options' );
   216 			wp_cache_add( 'alloptions', $alloptions, 'options' );
   177 	}
   217 		}
   178 
   218 	}
   179 	return $alloptions;
   219 
       
   220 	/**
       
   221 	 * Filters all options after retrieving them.
       
   222 	 *
       
   223 	 * @since 4.9.0
       
   224 	 *
       
   225 	 * @param array $alloptions Array with all options.
       
   226 	 */
       
   227 	return apply_filters( 'alloptions', $alloptions );
   180 }
   228 }
   181 
   229 
   182 /**
   230 /**
   183  * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
   231  * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
   184  *
   232  *
   185  * @since 3.0.0
   233  * @since 3.0.0
   186  *
   234  *
   187  * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
   235  * @global wpdb $wpdb WordPress database abstraction object.
   188  */
   236  *
   189 function wp_load_core_site_options( $site_id = null ) {
   237  * @param int $network_id Optional site ID for which to query the options. Defaults to the current site.
       
   238  */
       
   239 function wp_load_core_site_options( $network_id = null ) {
   190 	global $wpdb;
   240 	global $wpdb;
   191 
   241 
   192 	if ( !is_multisite() || wp_using_ext_object_cache() || defined( 'WP_INSTALLING' ) )
   242 	if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() )
   193 		return;
   243 		return;
   194 
   244 
   195 	if ( empty($site_id) )
   245 	if ( empty($network_id) )
   196 		$site_id = $wpdb->siteid;
   246 		$network_id = get_current_network_id();
   197 
   247 
   198 	$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' );
   248 	$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' );
   199 
   249 
   200 	$core_options_in = "'" . implode("', '", $core_options) . "'";
   250 	$core_options_in = "'" . implode("', '", $core_options) . "'";
   201 	$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) );
   251 	$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", $network_id ) );
   202 
   252 
   203 	foreach ( $options as $option ) {
   253 	foreach ( $options as $option ) {
   204 		$key = $option->meta_key;
   254 		$key = $option->meta_key;
   205 		$cache_key = "{$site_id}:$key";
   255 		$cache_key = "{$network_id}:$key";
   206 		$option->meta_value = maybe_unserialize( $option->meta_value );
   256 		$option->meta_value = maybe_unserialize( $option->meta_value );
   207 
   257 
   208 		wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
   258 		wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
   209 	}
   259 	}
   210 }
   260 }
   214  *
   264  *
   215  * You do not need to serialize values. If the value needs to be serialized, then
   265  * You do not need to serialize values. If the value needs to be serialized, then
   216  * it will be serialized before it is inserted into the database. Remember,
   266  * it will be serialized before it is inserted into the database. Remember,
   217  * resources can not be serialized or added as an option.
   267  * resources can not be serialized or added as an option.
   218  *
   268  *
   219  * If the option does not exist, then the option will be added with the option
   269  * If the option does not exist, then the option will be added with the option value,
   220  * value, but you will not be able to set whether it is autoloaded. If you want
   270  * with an `$autoload` value of 'yes'.
   221  * to set whether an option is autoloaded, then you need to use the add_option().
       
   222  *
   271  *
   223  * @since 1.0.0
   272  * @since 1.0.0
   224  * @since 4.2.0 The `$autoload` parameter was added.
   273  * @since 4.2.0 The `$autoload` parameter was added.
       
   274  *
       
   275  * @global wpdb $wpdb WordPress database abstraction object.
   225  *
   276  *
   226  * @param string      $option   Option name. Expected to not be SQL-escaped.
   277  * @param string      $option   Option name. Expected to not be SQL-escaped.
   227  * @param mixed       $value    Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
   278  * @param mixed       $value    Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
   228  * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
   279  * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
   229  *                              `$autoload` can only be updated using `update_option()` if `$value` is also changed.
   280  *                              `$autoload` can only be updated using `update_option()` if `$value` is also changed.
   230  *                              Accepts 'yes' or true to enable, 'no' or false to disable. For non-existent options,
   281  *                              Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
   231  *                              the default value is 'yes'.
   282  *                              the default value is 'yes'. Default null.
   232  * @return bool False if value was not updated and true if value was updated.
   283  * @return bool False if value was not updated and true if value was updated.
   233  */
   284  */
   234 function update_option( $option, $value, $autoload = null ) {
   285 function update_option( $option, $value, $autoload = null ) {
   235 	global $wpdb;
   286 	global $wpdb;
   236 
   287 
   245 
   296 
   246 	$value = sanitize_option( $option, $value );
   297 	$value = sanitize_option( $option, $value );
   247 	$old_value = get_option( $option );
   298 	$old_value = get_option( $option );
   248 
   299 
   249 	/**
   300 	/**
   250 	 * Filter a specific option before its value is (maybe) serialized and updated.
   301 	 * Filters a specific option before its value is (maybe) serialized and updated.
   251 	 *
   302 	 *
   252 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
   303 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
   253 	 *
   304 	 *
   254 	 * @since 2.6.0
   305 	 * @since 2.6.0
   255 	 *
   306 	 * @since 4.4.0 The `$option` parameter was added.
   256 	 * @param mixed $value     The new, unserialized option value.
   307 	 *
   257 	 * @param mixed $old_value The old option value.
   308 	 * @param mixed  $value     The new, unserialized option value.
   258 	 */
   309 	 * @param mixed  $old_value The old option value.
   259 	$value = apply_filters( 'pre_update_option_' . $option, $value, $old_value );
   310 	 * @param string $option    Option name.
   260 
   311 	 */
   261 	/**
   312 	$value = apply_filters( "pre_update_option_{$option}", $value, $old_value, $option );
   262 	 * Filter an option before its value is (maybe) serialized and updated.
   313 
       
   314 	/**
       
   315 	 * Filters an option before its value is (maybe) serialized and updated.
   263 	 *
   316 	 *
   264 	 * @since 3.9.0
   317 	 * @since 3.9.0
   265 	 *
   318 	 *
   266 	 * @param mixed  $value     The new, unserialized option value.
   319 	 * @param mixed  $value     The new, unserialized option value.
   267 	 * @param string $option    Name of the option.
   320 	 * @param string $option    Name of the option.
   268 	 * @param mixed  $old_value The old option value.
   321 	 * @param mixed  $old_value The old option value.
   269 	 */
   322 	 */
   270 	$value = apply_filters( 'pre_update_option', $value, $option, $old_value );
   323 	$value = apply_filters( 'pre_update_option', $value, $option, $old_value );
   271 
   324 
   272 	// If the new and old values are the same, no need to update.
   325 	/*
   273 	if ( $value === $old_value )
   326 	 * If the new and old values are the same, no need to update.
       
   327 	 *
       
   328 	 * Unserialized values will be adequate in most cases. If the unserialized
       
   329 	 * data differs, the (maybe) serialized data is checked to avoid
       
   330 	 * unnecessary database calls for otherwise identical object instances.
       
   331 	 *
       
   332 	 * See https://core.trac.wordpress.org/ticket/38903
       
   333 	 */
       
   334 	if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
   274 		return false;
   335 		return false;
       
   336 	}
   275 
   337 
   276 	/** This filter is documented in wp-includes/option.php */
   338 	/** This filter is documented in wp-includes/option.php */
   277 	if ( apply_filters( 'default_option_' . $option, false ) === $old_value ) {
   339 	if ( apply_filters( "default_option_{$option}", false, $option, false ) === $old_value ) {
   278 		// Default setting for new options is 'yes'.
   340 		// Default setting for new options is 'yes'.
   279 		if ( null === $autoload ) {
   341 		if ( null === $autoload ) {
   280 			$autoload = 'yes';
   342 			$autoload = 'yes';
   281 		}
   343 		}
   282 
   344 
   312 	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
   374 	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
   313 		unset( $notoptions[$option] );
   375 		unset( $notoptions[$option] );
   314 		wp_cache_set( 'notoptions', $notoptions, 'options' );
   376 		wp_cache_set( 'notoptions', $notoptions, 'options' );
   315 	}
   377 	}
   316 
   378 
   317 	if ( ! defined( 'WP_INSTALLING' ) ) {
   379 	if ( ! wp_installing() ) {
   318 		$alloptions = wp_load_alloptions();
   380 		$alloptions = wp_load_alloptions();
   319 		if ( isset( $alloptions[$option] ) ) {
   381 		if ( isset( $alloptions[$option] ) ) {
   320 			$alloptions[ $option ] = $serialized_value;
   382 			$alloptions[ $option ] = $serialized_value;
   321 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   383 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   322 		} else {
   384 		} else {
   328 	 * Fires after the value of a specific option has been successfully updated.
   390 	 * Fires after the value of a specific option has been successfully updated.
   329 	 *
   391 	 *
   330 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
   392 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
   331 	 *
   393 	 *
   332 	 * @since 2.0.1
   394 	 * @since 2.0.1
   333 	 *
   395 	 * @since 4.4.0 The `$option` parameter was added.
   334 	 * @param mixed $old_value The old option value.
   396 	 *
   335 	 * @param mixed $value     The new option value.
   397 	 * @param mixed  $old_value The old option value.
   336 	 */
   398 	 * @param mixed  $value     The new option value.
   337 	do_action( "update_option_{$option}", $old_value, $value );
   399 	 * @param string $option    Option name.
       
   400 	 */
       
   401 	do_action( "update_option_{$option}", $old_value, $value, $option );
   338 
   402 
   339 	/**
   403 	/**
   340 	 * Fires after the value of an option has been successfully updated.
   404 	 * Fires after the value of an option has been successfully updated.
   341 	 *
   405 	 *
   342 	 * @since 2.9.0
   406 	 * @since 2.9.0
   360  * Existing options will not be updated and checks are performed to ensure that you
   424  * Existing options will not be updated and checks are performed to ensure that you
   361  * aren't adding a protected WordPress option. Care should be taken to not name
   425  * aren't adding a protected WordPress option. Care should be taken to not name
   362  * options the same as the ones which are protected.
   426  * options the same as the ones which are protected.
   363  *
   427  *
   364  * @since 1.0.0
   428  * @since 1.0.0
       
   429  *
       
   430  * @global wpdb $wpdb WordPress database abstraction object.
   365  *
   431  *
   366  * @param string         $option      Name of option to add. Expected to not be SQL-escaped.
   432  * @param string         $option      Name of option to add. Expected to not be SQL-escaped.
   367  * @param mixed          $value       Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
   433  * @param mixed          $value       Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
   368  * @param string         $deprecated  Optional. Description. Not used anymore.
   434  * @param string         $deprecated  Optional. Description. Not used anymore.
   369  * @param string|bool    $autoload    Optional. Whether to load the option when WordPress starts up.
   435  * @param string|bool    $autoload    Optional. Whether to load the option when WordPress starts up.
   372  */
   438  */
   373 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
   439 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
   374 	global $wpdb;
   440 	global $wpdb;
   375 
   441 
   376 	if ( !empty( $deprecated ) )
   442 	if ( !empty( $deprecated ) )
   377 		_deprecated_argument( __FUNCTION__, '2.3' );
   443 		_deprecated_argument( __FUNCTION__, '2.3.0' );
   378 
   444 
   379 	$option = trim($option);
   445 	$option = trim($option);
   380 	if ( empty($option) )
   446 	if ( empty($option) )
   381 		return false;
   447 		return false;
   382 
   448 
   389 
   455 
   390 	// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
   456 	// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
   391 	$notoptions = wp_cache_get( 'notoptions', 'options' );
   457 	$notoptions = wp_cache_get( 'notoptions', 'options' );
   392 	if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
   458 	if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
   393 		/** This filter is documented in wp-includes/option.php */
   459 		/** This filter is documented in wp-includes/option.php */
   394 		if ( apply_filters( 'default_option_' . $option, false ) !== get_option( $option ) )
   460 		if ( apply_filters( "default_option_{$option}", false, $option, false ) !== get_option( $option ) )
   395 			return false;
   461 			return false;
   396 
   462 
   397 	$serialized_value = maybe_serialize( $value );
   463 	$serialized_value = maybe_serialize( $value );
   398 	$autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
   464 	$autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
   399 
   465 
   409 
   475 
   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 ) );
   476 	$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 ) );
   411 	if ( ! $result )
   477 	if ( ! $result )
   412 		return false;
   478 		return false;
   413 
   479 
   414 	if ( ! defined( 'WP_INSTALLING' ) ) {
   480 	if ( ! wp_installing() ) {
   415 		if ( 'yes' == $autoload ) {
   481 		if ( 'yes' == $autoload ) {
   416 			$alloptions = wp_load_alloptions();
   482 			$alloptions = wp_load_alloptions();
   417 			$alloptions[ $option ] = $serialized_value;
   483 			$alloptions[ $option ] = $serialized_value;
   418 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   484 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   419 		} else {
   485 		} else {
   456 /**
   522 /**
   457  * Removes option by name. Prevents removal of protected WordPress options.
   523  * Removes option by name. Prevents removal of protected WordPress options.
   458  *
   524  *
   459  * @since 1.2.0
   525  * @since 1.2.0
   460  *
   526  *
       
   527  * @global wpdb $wpdb WordPress database abstraction object.
       
   528  *
   461  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
   529  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
   462  * @return bool True, if option is successfully deleted. False on failure.
   530  * @return bool True, if option is successfully deleted. False on failure.
   463  */
   531  */
   464 function delete_option( $option ) {
   532 function delete_option( $option ) {
   465 	global $wpdb;
   533 	global $wpdb;
   483 	 * @param string $option Name of the option to delete.
   551 	 * @param string $option Name of the option to delete.
   484 	 */
   552 	 */
   485 	do_action( 'delete_option', $option );
   553 	do_action( 'delete_option', $option );
   486 
   554 
   487 	$result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
   555 	$result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
   488 	if ( ! defined( 'WP_INSTALLING' ) ) {
   556 	if ( ! wp_installing() ) {
   489 		if ( 'yes' == $row->autoload ) {
   557 		if ( 'yes' == $row->autoload ) {
   490 			$alloptions = wp_load_alloptions();
   558 			$alloptions = wp_load_alloptions();
   491 			if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
   559 			if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
   492 				unset( $alloptions[$option] );
   560 				unset( $alloptions[$option] );
   493 				wp_cache_set( 'alloptions', $alloptions, 'options' );
   561 				wp_cache_set( 'alloptions', $alloptions, 'options' );
   505 		 *
   573 		 *
   506 		 * @since 3.0.0
   574 		 * @since 3.0.0
   507 		 *
   575 		 *
   508 		 * @param string $option Name of the deleted option.
   576 		 * @param string $option Name of the deleted option.
   509 		 */
   577 		 */
   510 		do_action( "delete_option_$option", $option );
   578 		do_action( "delete_option_{$option}", $option );
   511 
   579 
   512 		/**
   580 		/**
   513 		 * Fires after an option has been deleted.
   581 		 * Fires after an option has been deleted.
   514 		 *
   582 		 *
   515 		 * @since 2.9.0
   583 		 * @since 2.9.0
   539 	 *
   607 	 *
   540 	 * @since 3.0.0
   608 	 * @since 3.0.0
   541 	 *
   609 	 *
   542 	 * @param string $transient Transient name.
   610 	 * @param string $transient Transient name.
   543 	 */
   611 	 */
   544 	do_action( 'delete_transient_' . $transient, $transient );
   612 	do_action( "delete_transient_{$transient}", $transient );
   545 
   613 
   546 	if ( wp_using_ext_object_cache() ) {
   614 	if ( wp_using_ext_object_cache() ) {
   547 		$result = wp_cache_delete( $transient, 'transient' );
   615 		$result = wp_cache_delete( $transient, 'transient' );
   548 	} else {
   616 	} else {
   549 		$option_timeout = '_transient_timeout_' . $transient;
   617 		$option_timeout = '_transient_timeout_' . $transient;
   579  * @param string $transient Transient name. Expected to not be SQL-escaped.
   647  * @param string $transient Transient name. Expected to not be SQL-escaped.
   580  * @return mixed Value of transient.
   648  * @return mixed Value of transient.
   581  */
   649  */
   582 function get_transient( $transient ) {
   650 function get_transient( $transient ) {
   583 
   651 
   584  	/**
   652 	/**
   585 	 * Filter the value of an existing transient.
   653 	 * Filters the value of an existing transient.
   586 	 *
   654 	 *
   587 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
   655 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
   588 	 *
   656 	 *
   589 	 * Passing a truthy value to the filter will effectively short-circuit retrieval
   657 	 * Passing a truthy value to the filter will effectively short-circuit retrieval
   590 	 * of the transient, returning the passed value instead.
   658 	 * of the transient, returning the passed value instead.
   591 	 *
   659 	 *
   592 	 * @since 2.8.0
   660 	 * @since 2.8.0
   593 	 *
   661 	 * @since 4.4.0 The `$transient` parameter was added
   594 	 * @param mixed $pre_transient The default value to return if the transient does not exist.
   662 	 *
   595 	 *                             Any value other than false will short-circuit the retrieval
   663 	 * @param mixed  $pre_transient The default value to return if the transient does not exist.
   596 	 *                             of the transient, and return the returned value.
   664 	 *                              Any value other than false will short-circuit the retrieval
   597 	 */
   665 	 *                              of the transient, and return the returned value.
   598 	$pre = apply_filters( 'pre_transient_' . $transient, false );
   666 	 * @param string $transient     Transient name.
       
   667 	 */
       
   668 	$pre = apply_filters( "pre_transient_{$transient}", false, $transient );
   599 	if ( false !== $pre )
   669 	if ( false !== $pre )
   600 		return $pre;
   670 		return $pre;
   601 
   671 
   602 	if ( wp_using_ext_object_cache() ) {
   672 	if ( wp_using_ext_object_cache() ) {
   603 		$value = wp_cache_get( $transient, 'transient' );
   673 		$value = wp_cache_get( $transient, 'transient' );
   604 	} else {
   674 	} else {
   605 		$transient_option = '_transient_' . $transient;
   675 		$transient_option = '_transient_' . $transient;
   606 		if ( ! defined( 'WP_INSTALLING' ) ) {
   676 		if ( ! wp_installing() ) {
   607 			// If option is not in alloptions, it is not autoloaded and thus has a timeout
   677 			// If option is not in alloptions, it is not autoloaded and thus has a timeout
   608 			$alloptions = wp_load_alloptions();
   678 			$alloptions = wp_load_alloptions();
   609 			if ( !isset( $alloptions[$transient_option] ) ) {
   679 			if ( !isset( $alloptions[$transient_option] ) ) {
   610 				$transient_timeout = '_transient_timeout_' . $transient;
   680 				$transient_timeout = '_transient_timeout_' . $transient;
   611 				if ( get_option( $transient_timeout ) < time() ) {
   681 				$timeout = get_option( $transient_timeout );
       
   682 				if ( false !== $timeout && $timeout < time() ) {
   612 					delete_option( $transient_option  );
   683 					delete_option( $transient_option  );
   613 					delete_option( $transient_timeout );
   684 					delete_option( $transient_timeout );
   614 					$value = false;
   685 					$value = false;
   615 				}
   686 				}
   616 			}
   687 			}
   619 		if ( ! isset( $value ) )
   690 		if ( ! isset( $value ) )
   620 			$value = get_option( $transient_option );
   691 			$value = get_option( $transient_option );
   621 	}
   692 	}
   622 
   693 
   623 	/**
   694 	/**
   624 	 * Filter an existing transient's value.
   695 	 * Filters an existing transient's value.
   625 	 *
   696 	 *
   626 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
   697 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
   627 	 *
   698 	 *
   628 	 * @since 2.8.0
   699 	 * @since 2.8.0
   629 	 *
   700 	 * @since 4.4.0 The `$transient` parameter was added
   630 	 * @param mixed $value Value of transient.
   701 	 *
   631 	 */
   702 	 * @param mixed  $value     Value of transient.
   632 	return apply_filters( 'transient_' . $transient, $value );
   703 	 * @param string $transient Transient name.
       
   704 	 */
       
   705 	return apply_filters( "transient_{$transient}", $value, $transient );
   633 }
   706 }
   634 
   707 
   635 /**
   708 /**
   636  * Set/update the value of a transient.
   709  * Set/update the value of a transient.
   637  *
   710  *
   639  * it will be serialized before it is set.
   712  * it will be serialized before it is set.
   640  *
   713  *
   641  * @since 2.8.0
   714  * @since 2.8.0
   642  *
   715  *
   643  * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
   716  * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
   644  *                           45 characters or fewer in length.
   717  *                           172 characters or fewer in length.
   645  * @param mixed  $value      Transient value. Must be serializable if non-scalar.
   718  * @param mixed  $value      Transient value. Must be serializable if non-scalar.
   646  *                           Expected to not be SQL-escaped.
   719  *                           Expected to not be SQL-escaped.
   647  * @param int    $expiration Optional. Time until expiration in seconds. Default 0.
   720  * @param int    $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
   648  * @return bool False if value was not set and true if value was set.
   721  * @return bool False if value was not set and true if value was set.
   649  */
   722  */
   650 function set_transient( $transient, $value, $expiration = 0 ) {
   723 function set_transient( $transient, $value, $expiration = 0 ) {
   651 
   724 
   652 	$expiration = (int) $expiration;
   725 	$expiration = (int) $expiration;
   653 
   726 
   654 	/**
   727 	/**
   655 	 * Filter a specific transient before its value is set.
   728 	 * Filters a specific transient before its value is set.
   656 	 *
   729 	 *
   657 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
   730 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
   658 	 *
   731 	 *
   659 	 * @since 3.0.0
   732 	 * @since 3.0.0
   660 	 * @since 4.2.0 Added `$expiration` parameter.
   733 	 * @since 4.2.0 The `$expiration` parameter was added.
   661 	 *
   734 	 * @since 4.4.0 The `$transient` parameter was added.
   662 	 * @param mixed $value      New value of transient.
   735 	 *
   663 	 * @param int   $expiration Time until expiration in seconds.
   736 	 * @param mixed  $value      New value of transient.
   664 	 */
   737 	 * @param int    $expiration Time until expiration in seconds.
   665 	$value = apply_filters( 'pre_set_transient_' . $transient, $value, $expiration );
   738 	 * @param string $transient  Transient name.
       
   739 	 */
       
   740 	$value = apply_filters( "pre_set_transient_{$transient}", $value, $expiration, $transient );
       
   741 
       
   742 	/**
       
   743 	 * Filters the expiration for a transient before its value is set.
       
   744 	 *
       
   745 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
       
   746 	 *
       
   747 	 * @since 4.4.0
       
   748 	 *
       
   749 	 * @param int    $expiration Time until expiration in seconds. Use 0 for no expiration.
       
   750 	 * @param mixed  $value      New value of transient.
       
   751 	 * @param string $transient  Transient name.
       
   752 	 */
       
   753 	$expiration = apply_filters( "expiration_of_transient_{$transient}", $expiration, $value, $transient );
   666 
   754 
   667 	if ( wp_using_ext_object_cache() ) {
   755 	if ( wp_using_ext_object_cache() ) {
   668 		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
   756 		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
   669 	} else {
   757 	} else {
   670 		$transient_timeout = '_transient_timeout_' . $transient;
   758 		$transient_timeout = '_transient_timeout_' . $transient;
   671 		$transient = '_transient_' . $transient;
   759 		$transient_option = '_transient_' . $transient;
   672 		if ( false === get_option( $transient ) ) {
   760 		if ( false === get_option( $transient_option ) ) {
   673 			$autoload = 'yes';
   761 			$autoload = 'yes';
   674 			if ( $expiration ) {
   762 			if ( $expiration ) {
   675 				$autoload = 'no';
   763 				$autoload = 'no';
   676 				add_option( $transient_timeout, time() + $expiration, '', 'no' );
   764 				add_option( $transient_timeout, time() + $expiration, '', 'no' );
   677 			}
   765 			}
   678 			$result = add_option( $transient, $value, '', $autoload );
   766 			$result = add_option( $transient_option, $value, '', $autoload );
   679 		} else {
   767 		} else {
   680 			// If expiration is requested, but the transient has no timeout option,
   768 			// If expiration is requested, but the transient has no timeout option,
   681 			// delete, then re-create transient rather than update.
   769 			// delete, then re-create transient rather than update.
   682 			$update = true;
   770 			$update = true;
   683 			if ( $expiration ) {
   771 			if ( $expiration ) {
   684 				if ( false === get_option( $transient_timeout ) ) {
   772 				if ( false === get_option( $transient_timeout ) ) {
   685 					delete_option( $transient );
   773 					delete_option( $transient_option );
   686 					add_option( $transient_timeout, time() + $expiration, '', 'no' );
   774 					add_option( $transient_timeout, time() + $expiration, '', 'no' );
   687 					$result = add_option( $transient, $value, '', 'no' );
   775 					$result = add_option( $transient_option, $value, '', 'no' );
   688 					$update = false;
   776 					$update = false;
   689 				} else {
   777 				} else {
   690 					update_option( $transient_timeout, time() + $expiration );
   778 					update_option( $transient_timeout, time() + $expiration );
   691 				}
   779 				}
   692 			}
   780 			}
   693 			if ( $update ) {
   781 			if ( $update ) {
   694 				$result = update_option( $transient, $value );
   782 				$result = update_option( $transient_option, $value );
   695 			}
   783 			}
   696 		}
   784 		}
   697 	}
   785 	}
   698 
   786 
   699 	if ( $result ) {
   787 	if ( $result ) {
   702 		 * Fires after the value for a specific transient has been set.
   790 		 * Fires after the value for a specific transient has been set.
   703 		 *
   791 		 *
   704 		 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
   792 		 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
   705 		 *
   793 		 *
   706 		 * @since 3.0.0
   794 		 * @since 3.0.0
   707 		 *
   795 		 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
   708 		 * @param mixed $value      Transient value.
   796 		 * @since 4.4.0 The `$transient` parameter was added.
   709 		 * @param int   $expiration Time until expiration in seconds. Default 0.
   797 		 *
       
   798 		 * @param mixed  $value      Transient value.
       
   799 		 * @param int    $expiration Time until expiration in seconds.
       
   800 		 * @param string $transient  The name of the transient.
   710 		 */
   801 		 */
   711 		do_action( 'set_transient_' . $transient, $value, $expiration );
   802 		do_action( "set_transient_{$transient}", $value, $expiration, $transient );
   712 
   803 
   713 		/**
   804 		/**
   714 		 * Fires after the value for a transient has been set.
   805 		 * Fires after the value for a transient has been set.
   715 		 *
   806 		 *
   716 		 * @since 3.0.0
   807 		 * @since 3.0.0
       
   808 		 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
   717 		 *
   809 		 *
   718 		 * @param string $transient  The name of the transient.
   810 		 * @param string $transient  The name of the transient.
   719 		 * @param mixed  $value      Transient value.
   811 		 * @param mixed  $value      Transient value.
   720 		 * @param int    $expiration Time until expiration in seconds. Default 0.
   812 		 * @param int    $expiration Time until expiration in seconds.
   721 		 */
   813 		 */
   722 		do_action( 'setted_transient', $transient, $value, $expiration );
   814 		do_action( 'setted_transient', $transient, $value, $expiration );
   723 	}
   815 	}
   724 	return $result;
   816 	return $result;
       
   817 }
       
   818 
       
   819 /**
       
   820  * Deletes all expired transients.
       
   821  *
       
   822  * The multi-table delete syntax is used to delete the transient record
       
   823  * from table a, and the corresponding transient_timeout record from table b.
       
   824  *
       
   825  * @since 4.9.0
       
   826  *
       
   827  * @param bool $force_db Optional. Force cleanup to run against the database even when an external object cache is used.
       
   828  */
       
   829 function delete_expired_transients( $force_db = false ) {
       
   830 	global $wpdb;
       
   831 
       
   832 	if ( ! $force_db && wp_using_ext_object_cache() ) {
       
   833 		return;
       
   834 	}
       
   835 
       
   836 	$wpdb->query( $wpdb->prepare(
       
   837 		"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
       
   838 			WHERE a.option_name LIKE %s
       
   839 			AND a.option_name NOT LIKE %s
       
   840 			AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
       
   841 			AND b.option_value < %d",
       
   842 		$wpdb->esc_like( '_transient_' ) . '%',
       
   843 		$wpdb->esc_like( '_transient_timeout_' ) . '%',
       
   844 		time()
       
   845 	) );
       
   846 
       
   847 	if ( ! is_multisite() ) {
       
   848 		// non-Multisite stores site transients in the options table.
       
   849 		$wpdb->query( $wpdb->prepare(
       
   850 			"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
       
   851 				WHERE a.option_name LIKE %s
       
   852 				AND a.option_name NOT LIKE %s
       
   853 				AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
       
   854 				AND b.option_value < %d",
       
   855 			$wpdb->esc_like( '_site_transient_' ) . '%',
       
   856 			$wpdb->esc_like( '_site_transient_timeout_' ) . '%',
       
   857 			time()
       
   858 		) );
       
   859 	} elseif ( is_multisite() && is_main_site() && is_main_network() ) {
       
   860 		// Multisite stores site transients in the sitemeta table.
       
   861 		$wpdb->query( $wpdb->prepare(
       
   862 			"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
       
   863 				WHERE a.meta_key LIKE %s
       
   864 				AND a.meta_key NOT LIKE %s
       
   865 				AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
       
   866 				AND b.meta_value < %d",
       
   867 			$wpdb->esc_like( '_site_transient_' ) . '%',
       
   868 			$wpdb->esc_like( '_site_transient_timeout_' ) . '%',
       
   869 			time()
       
   870 		) );
       
   871 	}
   725 }
   872 }
   726 
   873 
   727 /**
   874 /**
   728  * Saves and restores user interface settings stored in a cookie.
   875  * Saves and restores user interface settings stored in a cookie.
   729  *
   876  *
   733  *
   880  *
   734  * @since 2.7.0
   881  * @since 2.7.0
   735  */
   882  */
   736 function wp_user_settings() {
   883 function wp_user_settings() {
   737 
   884 
   738 	if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
   885 	if ( ! is_admin() || wp_doing_ajax() ) {
   739 		return;
   886 		return;
   740 	}
   887 	}
   741 
   888 
   742 	if ( ! $user_id = get_current_user_id() ) {
   889 	if ( ! $user_id = get_current_user_id() ) {
   743 		return;
   890 		return;
   744 	}
   891 	}
   745 
   892 
   746 	if ( is_super_admin() && ! is_user_member_of_blog() ) {
   893 	if ( ! is_user_member_of_blog() ) {
   747 		return;
   894 		return;
   748 	}
   895 	}
   749 
   896 
   750 	$settings = (string) get_user_option( 'user-settings', $user_id );
   897 	$settings = (string) get_user_option( 'user-settings', $user_id );
   751 
   898 
   766 			return;
   913 			return;
   767 		}
   914 		}
   768 	}
   915 	}
   769 
   916 
   770 	// The cookie is not set in the current browser or the saved value is newer.
   917 	// The cookie is not set in the current browser or the saved value is newer.
   771 	$secure = ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) );
   918 	$secure = ( 'https' === parse_url( admin_url(), PHP_URL_SCHEME ) );
   772 	setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
   919 	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 );
   920 	setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
   774 	$_COOKIE['wp-settings-' . $user_id] = $settings;
   921 	$_COOKIE['wp-settings-' . $user_id] = $settings;
   775 }
   922 }
   776 
   923 
   777 /**
   924 /**
   778  * Retrieve user interface setting value based on setting name.
   925  * Retrieve user interface setting value based on setting name.
   779  *
   926  *
   780  * @since 2.7.0
   927  * @since 2.7.0
   781  *
   928  *
   782  * @param string $name The name of the setting.
   929  * @param string $name    The name of the setting.
   783  * @param string $default Optional default value to return when $name is not set.
   930  * @param string $default Optional default value to return when $name is not set.
   784  * @return mixed the last saved user setting or the default value/false if it doesn't exist.
   931  * @return mixed the last saved user setting or the default value/false if it doesn't exist.
   785  */
   932  */
   786 function get_user_setting( $name, $default = false ) {
   933 function get_user_setting( $name, $default = false ) {
   787 	$all_user_settings = get_all_user_settings();
   934 	$all_user_settings = get_all_user_settings();
   791 
   938 
   792 /**
   939 /**
   793  * Add or update user interface setting.
   940  * Add or update user interface setting.
   794  *
   941  *
   795  * Both $name and $value can contain only ASCII letters, numbers and underscores.
   942  * Both $name and $value can contain only ASCII letters, numbers and underscores.
       
   943  *
   796  * This function has to be used before any output has started as it calls setcookie().
   944  * This function has to be used before any output has started as it calls setcookie().
   797  *
   945  *
   798  * @since 2.8.0
   946  * @since 2.8.0
   799  *
   947  *
   800  * @param string $name The name of the setting.
   948  * @param string $name  The name of the setting.
   801  * @param string $value The value for the setting.
   949  * @param string $value The value for the setting.
   802  * @return null|bool true if set successfully/false if not.
   950  * @return bool|null True if set successfully, false if not. Null if the current user can't be established.
   803  */
   951  */
   804 function set_user_setting( $name, $value ) {
   952 function set_user_setting( $name, $value ) {
   805 
       
   806 	if ( headers_sent() ) {
   953 	if ( headers_sent() ) {
   807 		return false;
   954 		return false;
   808 	}
   955 	}
   809 
   956 
   810 	$all_user_settings = get_all_user_settings();
   957 	$all_user_settings = get_all_user_settings();
   815 
   962 
   816 /**
   963 /**
   817  * Delete user interface settings.
   964  * Delete user interface settings.
   818  *
   965  *
   819  * Deleting settings would reset them to the defaults.
   966  * Deleting settings would reset them to the defaults.
       
   967  *
   820  * This function has to be used before any output has started as it calls setcookie().
   968  * This function has to be used before any output has started as it calls setcookie().
   821  *
   969  *
   822  * @since 2.7.0
   970  * @since 2.7.0
   823  *
   971  *
   824  * @param string $names The name or array of names of the setting to be deleted.
   972  * @param string $names The name or array of names of the setting to be deleted.
   825  * @return null|bool true if deleted successfully/false if not.
   973  * @return bool|null True if deleted successfully, false if not. Null if the current user can't be established.
   826  */
   974  */
   827 function delete_user_setting( $names ) {
   975 function delete_user_setting( $names ) {
   828 
       
   829 	if ( headers_sent() ) {
   976 	if ( headers_sent() ) {
   830 		return false;
   977 		return false;
   831 	}
   978 	}
   832 
   979 
   833 	$all_user_settings = get_all_user_settings();
   980 	$all_user_settings = get_all_user_settings();
   851 /**
   998 /**
   852  * Retrieve all user interface settings.
   999  * Retrieve all user interface settings.
   853  *
  1000  *
   854  * @since 2.7.0
  1001  * @since 2.7.0
   855  *
  1002  *
       
  1003  * @global array $_updated_user_settings
       
  1004  *
   856  * @return array the last saved user settings or empty array.
  1005  * @return array the last saved user settings or empty array.
   857  */
  1006  */
   858 function get_all_user_settings() {
  1007 function get_all_user_settings() {
   859 	global $_updated_user_settings;
  1008 	global $_updated_user_settings;
   860 
  1009 
   867 	}
  1016 	}
   868 
  1017 
   869 	$user_settings = array();
  1018 	$user_settings = array();
   870 
  1019 
   871 	if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
  1020 	if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
   872 		$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
  1021 		$cookie = preg_replace( '/[^A-Za-z0-9=&_-]/', '', $_COOKIE['wp-settings-' . $user_id] );
   873 
  1022 
   874 		if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
  1023 		if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
   875 			parse_str( $cookie, $user_settings );
  1024 			parse_str( $cookie, $user_settings );
   876 		}
  1025 		}
   877 	} else {
  1026 	} else {
   888 
  1037 
   889 /**
  1038 /**
   890  * Private. Set all user interface settings.
  1039  * Private. Set all user interface settings.
   891  *
  1040  *
   892  * @since 2.8.0
  1041  * @since 2.8.0
   893  *
  1042  * @access private
   894  * @param array $user_settings
  1043  *
   895  * @return null|bool
  1044  * @global array $_updated_user_settings
       
  1045  *
       
  1046  * @param array $user_settings User settings.
       
  1047  * @return bool|null False if the current user can't be found, null if the current
       
  1048  *                   user is not a super admin or a member of the site, otherwise true.
   896  */
  1049  */
   897 function wp_set_all_user_settings( $user_settings ) {
  1050 function wp_set_all_user_settings( $user_settings ) {
   898 	global $_updated_user_settings;
  1051 	global $_updated_user_settings;
   899 
  1052 
   900 	if ( ! $user_id = get_current_user_id() ) {
  1053 	if ( ! $user_id = get_current_user_id() ) {
   901 		return false;
  1054 		return false;
   902 	}
  1055 	}
   903 
  1056 
   904 	if ( is_super_admin() && ! is_user_member_of_blog() ) {
  1057 	if ( ! is_user_member_of_blog() ) {
   905 		return;
  1058 		return;
   906 	}
  1059 	}
   907 
  1060 
   908 	$settings = '';
  1061 	$settings = '';
   909 	foreach ( $user_settings as $name => $value ) {
  1062 	foreach ( $user_settings as $name => $value ) {
   910 		$_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
  1063 		$_name = preg_replace( '/[^A-Za-z0-9_-]+/', '', $name );
   911 		$_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value );
  1064 		$_value = preg_replace( '/[^A-Za-z0-9_-]+/', '', $value );
   912 
  1065 
   913 		if ( ! empty( $_name ) ) {
  1066 		if ( ! empty( $_name ) ) {
   914 			$settings .= $_name . '=' . $_value . '&';
  1067 			$settings .= $_name . '=' . $_value . '&';
   915 		}
  1068 		}
   916 	}
  1069 	}
   937 	update_user_option( $user_id, 'user-settings', '', false );
  1090 	update_user_option( $user_id, 'user-settings', '', false );
   938 	setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
  1091 	setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
   939 }
  1092 }
   940 
  1093 
   941 /**
  1094 /**
   942  * Retrieve site option value based on name of option.
  1095  * Retrieve an option value for the current network based on name of option.
   943  *
  1096  *
   944  * @since 2.8.0
  1097  * @since 2.8.0
       
  1098  * @since 4.4.0 The `$use_cache` parameter was deprecated.
       
  1099  * @since 4.4.0 Modified into wrapper for get_network_option()
       
  1100  *
       
  1101  * @see get_network_option()
       
  1102  *
       
  1103  * @param string $option     Name of option to retrieve. Expected to not be SQL-escaped.
       
  1104  * @param mixed  $default    Optional value to return if option doesn't exist. Default false.
       
  1105  * @param bool   $deprecated Whether to use cache. Multisite only. Always set to true.
       
  1106  * @return mixed Value set for the option.
       
  1107  */
       
  1108 function get_site_option( $option, $default = false, $deprecated = true ) {
       
  1109 	return get_network_option( null, $option, $default );
       
  1110 }
       
  1111 
       
  1112 /**
       
  1113  * Add a new option for the current network.
       
  1114  *
       
  1115  * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
       
  1116  *
       
  1117  * @since 2.8.0
       
  1118  * @since 4.4.0 Modified into wrapper for add_network_option()
       
  1119  *
       
  1120  * @see add_network_option()
       
  1121  *
       
  1122  * @param string $option Name of option to add. Expected to not be SQL-escaped.
       
  1123  * @param mixed  $value  Option value, can be anything. Expected to not be SQL-escaped.
       
  1124  * @return bool False if the option was not added. True if the option was added.
       
  1125  */
       
  1126 function add_site_option( $option, $value ) {
       
  1127 	return add_network_option( null, $option, $value );
       
  1128 }
       
  1129 
       
  1130 /**
       
  1131  * Removes a option by name for the current network.
       
  1132  *
       
  1133  * @since 2.8.0
       
  1134  * @since 4.4.0 Modified into wrapper for delete_network_option()
       
  1135  *
       
  1136  * @see delete_network_option()
       
  1137  *
       
  1138  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
       
  1139  * @return bool True, if succeed. False, if failure.
       
  1140  */
       
  1141 function delete_site_option( $option ) {
       
  1142 	return delete_network_option( null, $option );
       
  1143 }
       
  1144 
       
  1145 /**
       
  1146  * Update the value of an option that was already added for the current network.
       
  1147  *
       
  1148  * @since 2.8.0
       
  1149  * @since 4.4.0 Modified into wrapper for update_network_option()
       
  1150  *
       
  1151  * @see update_network_option()
       
  1152  *
       
  1153  * @param string $option Name of option. Expected to not be SQL-escaped.
       
  1154  * @param mixed  $value  Option value. Expected to not be SQL-escaped.
       
  1155  * @return bool False if value was not updated. True if value was updated.
       
  1156  */
       
  1157 function update_site_option( $option, $value ) {
       
  1158 	return update_network_option( null, $option, $value );
       
  1159 }
       
  1160 
       
  1161 /**
       
  1162  * Retrieve a network's option value based on the option name.
       
  1163  *
       
  1164  * @since 4.4.0
   945  *
  1165  *
   946  * @see get_option()
  1166  * @see get_option()
   947  *
  1167  *
   948  * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
  1168  * @global wpdb $wpdb
   949  * @param mixed $default Optional value to return if option doesn't exist. Default false.
  1169  *
   950  * @param bool $use_cache Whether to use cache. Multisite only. Default true.
  1170  * @param int      $network_id ID of the network. Can be null to default to the current network ID.
       
  1171  * @param string   $option     Name of option to retrieve. Expected to not be SQL-escaped.
       
  1172  * @param mixed    $default    Optional. Value to return if the option doesn't exist. Default false.
   951  * @return mixed Value set for the option.
  1173  * @return mixed Value set for the option.
   952  */
  1174  */
   953 function get_site_option( $option, $default = false, $use_cache = true ) {
  1175 function get_network_option( $network_id, $option, $default = false ) {
   954 	global $wpdb;
  1176 	global $wpdb;
   955 
  1177 
   956 	/**
  1178 	if ( $network_id && ! is_numeric( $network_id ) ) {
   957 	 * Filter an existing site option before it is retrieved.
  1179 		return false;
       
  1180 	}
       
  1181 
       
  1182 	$network_id = (int) $network_id;
       
  1183 
       
  1184 	// Fallback to the current network if a network ID is not specified.
       
  1185 	if ( ! $network_id ) {
       
  1186 		$network_id = get_current_network_id();
       
  1187 	}
       
  1188 
       
  1189 	/**
       
  1190 	 * Filters an existing network option before it is retrieved.
   958 	 *
  1191 	 *
   959 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1192 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
   960 	 *
  1193 	 *
   961 	 * Passing a truthy value to the filter will effectively short-circuit retrieval,
  1194 	 * Passing a truthy value to the filter will effectively short-circuit retrieval,
   962 	 * returning the passed value instead.
  1195 	 * returning the passed value instead.
   963 	 *
  1196 	 *
   964 	 * @since 2.9.0 As 'pre_site_option_' . $key
  1197 	 * @since 2.9.0 As 'pre_site_option_' . $key
   965 	 * @since 3.0.0
  1198 	 * @since 3.0.0
   966 	 *
  1199 	 * @since 4.4.0 The `$option` parameter was added.
   967 	 * @param mixed $pre_option The default value to return if the option does not exist.
  1200 	 * @since 4.7.0 The `$network_id` parameter was added.
   968 	 */
  1201 	 * @since 4.9.0 The `$default` parameter was added.
   969  	$pre = apply_filters( 'pre_site_option_' . $option, false );
  1202 	 *
   970 
  1203 	 * @param mixed  $pre_option The value to return instead of the option value. This differs from
   971  	if ( false !== $pre )
  1204 	 *                           `$default`, which is used as the fallback value in the event the
   972  		return $pre;
  1205 	 *                           option doesn't exist elsewhere in get_network_option(). Default
       
  1206 	 *                           is false (to skip past the short-circuit).
       
  1207 	 * @param string $option     Option name.
       
  1208 	 * @param int    $network_id ID of the network.
       
  1209 	 * @param mixed  $default    The fallback value to return if the option does not exist.
       
  1210 	 *                           Default is false.
       
  1211 	 */
       
  1212 	$pre = apply_filters( "pre_site_option_{$option}", false, $option, $network_id, $default );
       
  1213 
       
  1214 	if ( false !== $pre ) {
       
  1215 		return $pre;
       
  1216 	}
   973 
  1217 
   974 	// prevent non-existent options from triggering multiple queries
  1218 	// prevent non-existent options from triggering multiple queries
   975 	$notoptions_key = "{$wpdb->siteid}:notoptions";
  1219 	$notoptions_key = "$network_id:notoptions";
   976 	$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
  1220 	$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
   977 
  1221 
   978 	if ( isset( $notoptions[$option] ) ) {
  1222 	if ( isset( $notoptions[ $option ] ) ) {
   979 
  1223 
   980 		/**
  1224 		/**
   981 		 * Filter a specific default site option.
  1225 		 * Filters a specific default network option.
   982 		 *
  1226 		 *
   983 		 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1227 		 * The dynamic portion of the hook name, `$option`, refers to the option name.
   984 		 *
  1228 		 *
   985 		 * @since 3.4.0
  1229 		 * @since 3.4.0
   986 		 *
  1230 		 * @since 4.4.0 The `$option` parameter was added.
   987 		 * @param mixed $default The value to return if the site option does not exist
  1231 		 * @since 4.7.0 The `$network_id` parameter was added.
   988 		 *                       in the database.
  1232 		 *
       
  1233 		 * @param mixed  $default    The value to return if the site option does not exist
       
  1234 		 *                           in the database.
       
  1235 		 * @param string $option     Option name.
       
  1236 		 * @param int    $network_id ID of the network.
   989 		 */
  1237 		 */
   990 		return apply_filters( 'default_site_option_' . $option, $default );
  1238 		return apply_filters( "default_site_option_{$option}", $default, $option, $network_id );
   991 	}
  1239 	}
   992 
  1240 
   993 	if ( ! is_multisite() ) {
  1241 	if ( ! is_multisite() ) {
   994 
       
   995 		/** This filter is documented in wp-includes/option.php */
  1242 		/** This filter is documented in wp-includes/option.php */
   996 		$default = apply_filters( 'default_site_option_' . $option, $default );
  1243 		$default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
   997 		$value = get_option($option, $default);
  1244 		$value = get_option( $option, $default );
   998 	} else {
  1245 	} else {
   999 		$cache_key = "{$wpdb->siteid}:$option";
  1246 		$cache_key = "$network_id:$option";
  1000 		if ( $use_cache )
  1247 		$value = wp_cache_get( $cache_key, 'site-options' );
  1001 			$value = wp_cache_get($cache_key, 'site-options');
  1248 
  1002 
  1249 		if ( ! isset( $value ) || false === $value ) {
  1003 		if ( !isset($value) || (false === $value) ) {
  1250 			$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
  1004 			$row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
       
  1005 
  1251 
  1006 			// Has to be get_row instead of get_var because of funkiness with 0, false, null values
  1252 			// Has to be get_row instead of get_var because of funkiness with 0, false, null values
  1007 			if ( is_object( $row ) ) {
  1253 			if ( is_object( $row ) ) {
  1008 				$value = $row->meta_value;
  1254 				$value = $row->meta_value;
  1009 				$value = maybe_unserialize( $value );
  1255 				$value = maybe_unserialize( $value );
  1010 				wp_cache_set( $cache_key, $value, 'site-options' );
  1256 				wp_cache_set( $cache_key, $value, 'site-options' );
  1011 			} else {
  1257 			} else {
  1012 				$notoptions[$option] = true;
  1258 				if ( ! is_array( $notoptions ) ) {
       
  1259 					$notoptions = array();
       
  1260 				}
       
  1261 				$notoptions[ $option ] = true;
  1013 				wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1262 				wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1014 
  1263 
  1015 				/** This filter is documented in wp-includes/option.php */
  1264 				/** This filter is documented in wp-includes/option.php */
  1016 				$value = apply_filters( 'default_site_option_' . $option, $default );
  1265 				$value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
  1017 			}
  1266 			}
  1018 		}
  1267 		}
  1019 	}
  1268 	}
  1020 
  1269 
  1021 	/**
  1270 	/**
  1022 	 * Filter the value of an existing site option.
  1271 	 * Filters the value of an existing network option.
  1023 	 *
  1272 	 *
  1024 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1273 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1025 	 *
  1274 	 *
  1026 	 * @since 2.9.0 As 'site_option_' . $key
  1275 	 * @since 2.9.0 As 'site_option_' . $key
  1027 	 * @since 3.0.0
  1276 	 * @since 3.0.0
  1028 	 *
  1277 	 * @since 4.4.0 The `$option` parameter was added.
  1029 	 * @param mixed $value Value of site option.
  1278 	 * @since 4.7.0 The `$network_id` parameter was added.
  1030 	 */
  1279 	 *
  1031  	return apply_filters( 'site_option_' . $option, $value );
  1280 	 * @param mixed  $value      Value of network option.
  1032 }
  1281 	 * @param string $option     Option name.
  1033 
  1282 	 * @param int    $network_id ID of the network.
  1034 /**
  1283 	 */
  1035  * Add a new site option.
  1284 	return apply_filters( "site_option_{$option}", $value, $option, $network_id );
  1036  *
  1285 }
  1037  * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
  1286 
  1038  *
  1287 /**
  1039  * @since 2.8.0
  1288  * Add a new network option.
       
  1289  *
       
  1290  * Existing options will not be updated.
       
  1291  *
       
  1292  * @since 4.4.0
  1040  *
  1293  *
  1041  * @see add_option()
  1294  * @see add_option()
  1042  *
  1295  *
  1043  * @param string $option Name of option to add. Expected to not be SQL-escaped.
  1296  * @global wpdb $wpdb
  1044  * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
  1297  *
       
  1298  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
       
  1299  * @param string $option     Name of option to add. Expected to not be SQL-escaped.
       
  1300  * @param mixed  $value      Option value, can be anything. Expected to not be SQL-escaped.
  1045  * @return bool False if option was not added and true if option was added.
  1301  * @return bool False if option was not added and true if option was added.
  1046  */
  1302  */
  1047 function add_site_option( $option, $value ) {
  1303 function add_network_option( $network_id, $option, $value ) {
  1048 	global $wpdb;
  1304 	global $wpdb;
  1049 
  1305 
       
  1306 	if ( $network_id && ! is_numeric( $network_id ) ) {
       
  1307 		return false;
       
  1308 	}
       
  1309 
       
  1310 	$network_id = (int) $network_id;
       
  1311 
       
  1312 	// Fallback to the current network if a network ID is not specified.
       
  1313 	if ( ! $network_id ) {
       
  1314 		$network_id = get_current_network_id();
       
  1315 	}
       
  1316 
  1050 	wp_protect_special_option( $option );
  1317 	wp_protect_special_option( $option );
  1051 
  1318 
  1052 	/**
  1319 	/**
  1053 	 * Filter the value of a specific site option before it is added.
  1320 	 * Filters the value of a specific network option before it is added.
  1054 	 *
  1321 	 *
  1055 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1322 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1056 	 *
  1323 	 *
  1057 	 * @since 2.9.0 As 'pre_add_site_option_' . $key
  1324 	 * @since 2.9.0 As 'pre_add_site_option_' . $key
  1058 	 * @since 3.0.0
  1325 	 * @since 3.0.0
  1059 	 *
  1326 	 * @since 4.4.0 The `$option` parameter was added.
  1060 	 * @param mixed $value Value of site option.
  1327 	 * @since 4.7.0 The `$network_id` parameter was added.
  1061 	 */
  1328 	 *
  1062 	$value = apply_filters( 'pre_add_site_option_' . $option, $value );
  1329 	 * @param mixed  $value      Value of network option.
  1063 
  1330 	 * @param string $option     Option name.
  1064 	$notoptions_key = "{$wpdb->siteid}:notoptions";
  1331 	 * @param int    $network_id ID of the network.
  1065 
  1332 	 */
  1066 	if ( !is_multisite() ) {
  1333 	$value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id );
  1067 		$result = add_option( $option, $value );
  1334 
       
  1335 	$notoptions_key = "$network_id:notoptions";
       
  1336 
       
  1337 	if ( ! is_multisite() ) {
       
  1338 		$result = add_option( $option, $value, '', 'no' );
  1068 	} else {
  1339 	} else {
  1069 		$cache_key = "{$wpdb->siteid}:$option";
  1340 		$cache_key = "$network_id:$option";
  1070 
  1341 
  1071 		// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
  1342 		// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
  1072 		$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
  1343 		$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
  1073 		if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) )
  1344 		if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
  1074 			if ( false !== get_site_option( $option ) )
  1345 			if ( false !== get_network_option( $network_id, $option, false ) ) {
  1075 				return false;
  1346 				return false;
       
  1347 			}
       
  1348 		}
  1076 
  1349 
  1077 		$value = sanitize_option( $option, $value );
  1350 		$value = sanitize_option( $option, $value );
  1078 
  1351 
  1079 		$serialized_value = maybe_serialize( $value );
  1352 		$serialized_value = maybe_serialize( $value );
  1080 		$result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
  1353 		$result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id'    => $network_id, 'meta_key'   => $option, 'meta_value' => $serialized_value ) );
  1081 
  1354 
  1082 		if ( ! $result )
  1355 		if ( ! $result ) {
  1083 			return false;
  1356 			return false;
       
  1357 		}
  1084 
  1358 
  1085 		wp_cache_set( $cache_key, $value, 'site-options' );
  1359 		wp_cache_set( $cache_key, $value, 'site-options' );
  1086 
  1360 
  1087 		// This option exists now
  1361 		// This option exists now
  1088 		$notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
  1362 		$notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
  1089 		if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
  1363 		if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
  1090 			unset( $notoptions[$option] );
  1364 			unset( $notoptions[ $option ] );
  1091 			wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1365 			wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1092 		}
  1366 		}
  1093 	}
  1367 	}
  1094 
  1368 
  1095 	if ( $result ) {
  1369 	if ( $result ) {
  1096 
  1370 
  1097 		/**
  1371 		/**
  1098 		 * Fires after a specific site option has been successfully added.
  1372 		 * Fires after a specific network option has been successfully added.
  1099 		 *
  1373 		 *
  1100 		 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1374 		 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1101 		 *
  1375 		 *
  1102 		 * @since 2.9.0 As "add_site_option_{$key}"
  1376 		 * @since 2.9.0 As "add_site_option_{$key}"
  1103 		 * @since 3.0.0
  1377 		 * @since 3.0.0
  1104 		 *
  1378 		 * @since 4.7.0 The `$network_id` parameter was added.
  1105 		 * @param string $option Name of site option.
  1379 		 *
  1106 		 * @param mixed  $value  Value of site option.
  1380 		 * @param string $option     Name of the network option.
       
  1381 		 * @param mixed  $value      Value of the network option.
       
  1382 		 * @param int    $network_id ID of the network.
  1107 		 */
  1383 		 */
  1108 		do_action( "add_site_option_{$option}", $option, $value );
  1384 		do_action( "add_site_option_{$option}", $option, $value, $network_id );
  1109 
  1385 
  1110 		/**
  1386 		/**
  1111 		 * Fires after a site option has been successfully added.
  1387 		 * Fires after a network option has been successfully added.
  1112 		 *
  1388 		 *
  1113 		 * @since 3.0.0
  1389 		 * @since 3.0.0
  1114 		 *
  1390 		 * @since 4.7.0 The `$network_id` parameter was added.
  1115 		 * @param string $option Name of site option.
  1391 		 *
  1116 		 * @param mixed  $value  Value of site option.
  1392 		 * @param string $option     Name of the network option.
       
  1393 		 * @param mixed  $value      Value of the network option.
       
  1394 		 * @param int    $network_id ID of the network.
  1117 		 */
  1395 		 */
  1118 		do_action( "add_site_option", $option, $value );
  1396 		do_action( 'add_site_option', $option, $value, $network_id );
  1119 
  1397 
  1120 		return true;
  1398 		return true;
  1121 	}
  1399 	}
       
  1400 
  1122 	return false;
  1401 	return false;
  1123 }
  1402 }
  1124 
  1403 
  1125 /**
  1404 /**
  1126  * Removes site option by name.
  1405  * Removes a network option by name.
  1127  *
  1406  *
  1128  * @since 2.8.0
  1407  * @since 4.4.0
  1129  *
  1408  *
  1130  * @see delete_option()
  1409  * @see delete_option()
  1131  *
  1410  *
  1132  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
  1411  * @global wpdb $wpdb
       
  1412  *
       
  1413  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
       
  1414  * @param string $option     Name of option to remove. Expected to not be SQL-escaped.
  1133  * @return bool True, if succeed. False, if failure.
  1415  * @return bool True, if succeed. False, if failure.
  1134  */
  1416  */
  1135 function delete_site_option( $option ) {
  1417 function delete_network_option( $network_id, $option ) {
  1136 	global $wpdb;
  1418 	global $wpdb;
  1137 
  1419 
  1138 	// ms_protect_special_option( $option ); @todo
  1420 	if ( $network_id && ! is_numeric( $network_id ) ) {
  1139 
  1421 		return false;
  1140 	/**
  1422 	}
  1141 	 * Fires immediately before a specific site option is deleted.
  1423 
       
  1424 	$network_id = (int) $network_id;
       
  1425 
       
  1426 	// Fallback to the current network if a network ID is not specified.
       
  1427 	if ( ! $network_id ) {
       
  1428 		$network_id = get_current_network_id();
       
  1429 	}
       
  1430 
       
  1431 	/**
       
  1432 	 * Fires immediately before a specific network option is deleted.
  1142 	 *
  1433 	 *
  1143 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1434 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1144 	 *
  1435 	 *
  1145 	 * @since 3.0.0
  1436 	 * @since 3.0.0
  1146 	 */
  1437 	 * @since 4.4.0 The `$option` parameter was added.
  1147 	do_action( 'pre_delete_site_option_' . $option );
  1438 	 * @since 4.7.0 The `$network_id` parameter was added.
  1148 
  1439 	 *
  1149 	if ( !is_multisite() ) {
  1440 	 * @param string $option     Option name.
       
  1441 	 * @param int    $network_id ID of the network.
       
  1442 	 */
       
  1443 	do_action( "pre_delete_site_option_{$option}", $option, $network_id );
       
  1444 
       
  1445 	if ( ! is_multisite() ) {
  1150 		$result = delete_option( $option );
  1446 		$result = delete_option( $option );
  1151 	} else {
  1447 	} else {
  1152 		$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
  1448 		$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
  1153 		if ( is_null( $row ) || !$row->meta_id )
  1449 		if ( is_null( $row ) || ! $row->meta_id ) {
  1154 			return false;
  1450 			return false;
  1155 		$cache_key = "{$wpdb->siteid}:$option";
  1451 		}
       
  1452 		$cache_key = "$network_id:$option";
  1156 		wp_cache_delete( $cache_key, 'site-options' );
  1453 		wp_cache_delete( $cache_key, 'site-options' );
  1157 
  1454 
  1158 		$result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
  1455 		$result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $network_id ) );
  1159 	}
  1456 	}
  1160 
  1457 
  1161 	if ( $result ) {
  1458 	if ( $result ) {
  1162 
  1459 
  1163 		/**
  1460 		/**
  1164 		 * Fires after a specific site option has been deleted.
  1461 		 * Fires after a specific network option has been deleted.
  1165 		 *
  1462 		 *
  1166 		 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1463 		 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1167 		 *
  1464 		 *
  1168 		 * @since 2.9.0 As "delete_site_option_{$key}"
  1465 		 * @since 2.9.0 As "delete_site_option_{$key}"
  1169 		 * @since 3.0.0
  1466 		 * @since 3.0.0
  1170 		 *
  1467 		 * @since 4.7.0 The `$network_id` parameter was added.
  1171 		 * @param string $option Name of the site option.
  1468 		 *
       
  1469 		 * @param string $option     Name of the network option.
       
  1470 		 * @param int    $network_id ID of the network.
  1172 		 */
  1471 		 */
  1173 		do_action( "delete_site_option_{$option}", $option );
  1472 		do_action( "delete_site_option_{$option}", $option, $network_id );
  1174 
  1473 
  1175 		/**
  1474 		/**
  1176 		 * Fires after a site option has been deleted.
  1475 		 * Fires after a network option has been deleted.
  1177 		 *
  1476 		 *
  1178 		 * @since 3.0.0
  1477 		 * @since 3.0.0
  1179 		 *
  1478 		 * @since 4.7.0 The `$network_id` parameter was added.
  1180 		 * @param string $option Name of the site option.
  1479 		 *
       
  1480 		 * @param string $option     Name of the network option.
       
  1481 		 * @param int    $network_id ID of the network.
  1181 		 */
  1482 		 */
  1182 		do_action( "delete_site_option", $option );
  1483 		do_action( 'delete_site_option', $option, $network_id );
  1183 
  1484 
  1184 		return true;
  1485 		return true;
  1185 	}
  1486 	}
       
  1487 
  1186 	return false;
  1488 	return false;
  1187 }
  1489 }
  1188 
  1490 
  1189 /**
  1491 /**
  1190  * Update the value of a site option that was already added.
  1492  * Update the value of a network option that was already added.
  1191  *
  1493  *
  1192  * @since 2.8.0
  1494  * @since 4.4.0
  1193  *
  1495  *
  1194  * @see update_option()
  1496  * @see update_option()
  1195  *
  1497  *
  1196  * @param string $option Name of option. Expected to not be SQL-escaped.
  1498  * @global wpdb $wpdb
  1197  * @param mixed $value Option value. Expected to not be SQL-escaped.
  1499  *
       
  1500  * @param int      $network_id ID of the network. Can be null to default to the current network ID.
       
  1501  * @param string   $option     Name of option. Expected to not be SQL-escaped.
       
  1502  * @param mixed    $value      Option value. Expected to not be SQL-escaped.
  1198  * @return bool False if value was not updated and true if value was updated.
  1503  * @return bool False if value was not updated and true if value was updated.
  1199  */
  1504  */
  1200 function update_site_option( $option, $value ) {
  1505 function update_network_option( $network_id, $option, $value ) {
  1201 	global $wpdb;
  1506 	global $wpdb;
  1202 
  1507 
       
  1508 	if ( $network_id && ! is_numeric( $network_id ) ) {
       
  1509 		return false;
       
  1510 	}
       
  1511 
       
  1512 	$network_id = (int) $network_id;
       
  1513 
       
  1514 	// Fallback to the current network if a network ID is not specified.
       
  1515 	if ( ! $network_id ) {
       
  1516 		$network_id = get_current_network_id();
       
  1517 	}
       
  1518 
  1203 	wp_protect_special_option( $option );
  1519 	wp_protect_special_option( $option );
  1204 
  1520 
  1205 	$old_value = get_site_option( $option );
  1521 	$old_value = get_network_option( $network_id, $option, false );
  1206 
  1522 
  1207 	/**
  1523 	/**
  1208 	 * Filter a specific site option before its value is updated.
  1524 	 * Filters a specific network option before its value is updated.
  1209 	 *
  1525 	 *
  1210 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1526 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1211 	 *
  1527 	 *
  1212 	 * @since 2.9.0 As 'pre_update_site_option_' . $key
  1528 	 * @since 2.9.0 As 'pre_update_site_option_' . $key
  1213 	 * @since 3.0.0
  1529 	 * @since 3.0.0
  1214 	 *
  1530 	 * @since 4.4.0 The `$option` parameter was added.
  1215 	 * @param mixed $value     New value of site option.
  1531 	 * @since 4.7.0 The `$network_id` parameter was added.
  1216 	 * @param mixed $old_value Old value of site option.
  1532 	 *
  1217 	 */
  1533 	 * @param mixed  $value      New value of the network option.
  1218 	$value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value );
  1534 	 * @param mixed  $old_value  Old value of the network option.
  1219 
  1535 	 * @param string $option     Option name.
  1220 	if ( $value === $old_value )
  1536 	 * @param int    $network_id ID of the network.
       
  1537 	 */
       
  1538 	$value = apply_filters( "pre_update_site_option_{$option}", $value, $old_value, $option, $network_id );
       
  1539 
       
  1540 	if ( $value === $old_value ) {
  1221 		return false;
  1541 		return false;
  1222 
  1542 	}
  1223 	if ( false === $old_value )
  1543 
  1224 		return add_site_option( $option, $value );
  1544 	if ( false === $old_value ) {
  1225 
  1545 		return add_network_option( $network_id, $option, $value );
  1226 	$notoptions_key = "{$wpdb->siteid}:notoptions";
  1546 	}
       
  1547 
       
  1548 	$notoptions_key = "$network_id:notoptions";
  1227 	$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
  1549 	$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
  1228 	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
  1550 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
  1229 		unset( $notoptions[$option] );
  1551 		unset( $notoptions[ $option ] );
  1230 		wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1552 		wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1231 	}
  1553 	}
  1232 
  1554 
  1233 	if ( !is_multisite() ) {
  1555 	if ( ! is_multisite() ) {
  1234 		$result = update_option( $option, $value );
  1556 		$result = update_option( $option, $value, 'no' );
  1235 	} else {
  1557 	} else {
  1236 		$value = sanitize_option( $option, $value );
  1558 		$value = sanitize_option( $option, $value );
  1237 
  1559 
  1238 		$serialized_value = maybe_serialize( $value );
  1560 		$serialized_value = maybe_serialize( $value );
  1239 		$result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
  1561 		$result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $network_id, 'meta_key' => $option ) );
  1240 
  1562 
  1241 		if ( $result ) {
  1563 		if ( $result ) {
  1242 			$cache_key = "{$wpdb->siteid}:$option";
  1564 			$cache_key = "$network_id:$option";
  1243 			wp_cache_set( $cache_key, $value, 'site-options' );
  1565 			wp_cache_set( $cache_key, $value, 'site-options' );
  1244 		}
  1566 		}
  1245 	}
  1567 	}
  1246 
  1568 
  1247 	if ( $result ) {
  1569 	if ( $result ) {
  1248 
  1570 
  1249 		/**
  1571 		/**
  1250 		 * Fires after the value of a specific site option has been successfully updated.
  1572 		 * Fires after the value of a specific network option has been successfully updated.
  1251 		 *
  1573 		 *
  1252 		 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1574 		 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1253 		 *
  1575 		 *
  1254 		 * @since 2.9.0 As "update_site_option_{$key}"
  1576 		 * @since 2.9.0 As "update_site_option_{$key}"
  1255 		 * @since 3.0.0
  1577 		 * @since 3.0.0
  1256 		 *
  1578 		 * @since 4.7.0 The `$network_id` parameter was added.
  1257 		 * @param string $option    Name of site option.
  1579 		 *
  1258 		 * @param mixed  $value     Current value of site option.
  1580 		 * @param string $option     Name of the network option.
  1259 		 * @param mixed  $old_value Old value of site option.
  1581 		 * @param mixed  $value      Current value of the network option.
       
  1582 		 * @param mixed  $old_value  Old value of the network option.
       
  1583 		 * @param int    $network_id ID of the network.
  1260 		 */
  1584 		 */
  1261 		do_action( "update_site_option_{$option}", $option, $value, $old_value );
  1585 		do_action( "update_site_option_{$option}", $option, $value, $old_value, $network_id );
  1262 
  1586 
  1263 		/**
  1587 		/**
  1264 		 * Fires after the value of a site option has been successfully updated.
  1588 		 * Fires after the value of a network option has been successfully updated.
  1265 		 *
  1589 		 *
  1266 		 * @since 3.0.0
  1590 		 * @since 3.0.0
  1267 		 *
  1591 		 * @since 4.7.0 The `$network_id` parameter was added.
  1268 		 * @param string $option    Name of site option.
  1592 		 *
  1269 		 * @param mixed  $value     Current value of site option.
  1593 		 * @param string $option     Name of the network option.
  1270 		 * @param mixed  $old_value Old value of site option.
  1594 		 * @param mixed  $value      Current value of the network option.
       
  1595 		 * @param mixed  $old_value  Old value of the network option.
       
  1596 		 * @param int    $network_id ID of the network.
  1271 		 */
  1597 		 */
  1272 		do_action( "update_site_option", $option, $value, $old_value );
  1598 		do_action( 'update_site_option', $option, $value, $old_value, $network_id );
  1273 
  1599 
  1274 		return true;
  1600 		return true;
  1275 	}
  1601 	}
       
  1602 
  1276 	return false;
  1603 	return false;
  1277 }
  1604 }
  1278 
  1605 
  1279 /**
  1606 /**
  1280  * Delete a site transient.
  1607  * Delete a site transient.
  1293 	 *
  1620 	 *
  1294 	 * @since 3.0.0
  1621 	 * @since 3.0.0
  1295 	 *
  1622 	 *
  1296 	 * @param string $transient Transient name.
  1623 	 * @param string $transient Transient name.
  1297 	 */
  1624 	 */
  1298 	do_action( 'delete_site_transient_' . $transient, $transient );
  1625 	do_action( "delete_site_transient_{$transient}", $transient );
  1299 
  1626 
  1300 	if ( wp_using_ext_object_cache() ) {
  1627 	if ( wp_using_ext_object_cache() ) {
  1301 		$result = wp_cache_delete( $transient, 'site-transient' );
  1628 		$result = wp_cache_delete( $transient, 'site-transient' );
  1302 	} else {
  1629 	} else {
  1303 		$option_timeout = '_site_transient_timeout_' . $transient;
  1630 		$option_timeout = '_site_transient_timeout_' . $transient;
  1335  * @return mixed Value of transient.
  1662  * @return mixed Value of transient.
  1336  */
  1663  */
  1337 function get_site_transient( $transient ) {
  1664 function get_site_transient( $transient ) {
  1338 
  1665 
  1339 	/**
  1666 	/**
  1340 	 * Filter the value of an existing site transient.
  1667 	 * Filters the value of an existing site transient.
  1341 	 *
  1668 	 *
  1342 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
  1669 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
  1343 	 *
  1670 	 *
  1344 	 * Passing a truthy value to the filter will effectively short-circuit retrieval,
  1671 	 * Passing a truthy value to the filter will effectively short-circuit retrieval,
  1345 	 * returning the passed value instead.
  1672 	 * returning the passed value instead.
  1346 	 *
  1673 	 *
  1347 	 * @since 2.9.0
  1674 	 * @since 2.9.0
  1348 	 *
  1675 	 * @since 4.4.0 The `$transient` parameter was added.
  1349 	 * @param mixed $pre_site_transient The default value to return if the site transient does not exist.
  1676 	 *
  1350 	 *                                  Any value other than false will short-circuit the retrieval
  1677 	 * @param mixed  $pre_site_transient The default value to return if the site transient does not exist.
  1351 	 *                                  of the transient, and return the returned value.
  1678 	 *                                   Any value other than false will short-circuit the retrieval
  1352 	 */
  1679 	 *                                   of the transient, and return the returned value.
  1353 	$pre = apply_filters( 'pre_site_transient_' . $transient, false );
  1680 	 * @param string $transient          Transient name.
       
  1681 	 */
       
  1682 	$pre = apply_filters( "pre_site_transient_{$transient}", false, $transient );
  1354 
  1683 
  1355 	if ( false !== $pre )
  1684 	if ( false !== $pre )
  1356 		return $pre;
  1685 		return $pre;
  1357 
  1686 
  1358 	if ( wp_using_ext_object_cache() ) {
  1687 	if ( wp_using_ext_object_cache() ) {
  1374 		if ( ! isset( $value ) )
  1703 		if ( ! isset( $value ) )
  1375 			$value = get_site_option( $transient_option );
  1704 			$value = get_site_option( $transient_option );
  1376 	}
  1705 	}
  1377 
  1706 
  1378 	/**
  1707 	/**
  1379 	 * Filter the value of an existing site transient.
  1708 	 * Filters the value of an existing site transient.
  1380 	 *
  1709 	 *
  1381 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
  1710 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
  1382 	 *
  1711 	 *
  1383 	 * @since 2.9.0
  1712 	 * @since 2.9.0
  1384 	 *
  1713 	 * @since 4.4.0 The `$transient` parameter was added.
  1385 	 * @param mixed $value Value of site transient.
  1714 	 *
  1386 	 */
  1715 	 * @param mixed  $value     Value of site transient.
  1387 	return apply_filters( 'site_transient_' . $transient, $value );
  1716 	 * @param string $transient Transient name.
       
  1717 	 */
       
  1718 	return apply_filters( "site_transient_{$transient}", $value, $transient );
  1388 }
  1719 }
  1389 
  1720 
  1390 /**
  1721 /**
  1391  * Set/update the value of a site transient.
  1722  * Set/update the value of a site transient.
  1392  *
  1723  *
  1396  * @since 2.9.0
  1727  * @since 2.9.0
  1397  *
  1728  *
  1398  * @see set_transient()
  1729  * @see set_transient()
  1399  *
  1730  *
  1400  * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
  1731  * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
  1401  *                           40 characters or fewer in length.
  1732  *                           167 characters or fewer in length.
  1402  * @param mixed  $value      Transient value. Expected to not be SQL-escaped.
  1733  * @param mixed  $value      Transient value. Expected to not be SQL-escaped.
  1403  * @param int    $expiration Optional. Time until expiration in seconds. Default 0.
  1734  * @param int    $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
  1404  * @return bool False if value was not set and true if value was set.
  1735  * @return bool False if value was not set and true if value was set.
  1405  */
  1736  */
  1406 function set_site_transient( $transient, $value, $expiration = 0 ) {
  1737 function set_site_transient( $transient, $value, $expiration = 0 ) {
  1407 
  1738 
  1408 	/**
  1739 	/**
  1409 	 * Filter the value of a specific site transient before it is set.
  1740 	 * Filters the value of a specific site transient before it is set.
  1410 	 *
  1741 	 *
  1411 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
  1742 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
  1412 	 *
  1743 	 *
  1413 	 * @since 3.0.0
  1744 	 * @since 3.0.0
  1414 	 *
  1745 	 * @since 4.4.0 The `$transient` parameter was added.
  1415 	 * @param mixed $value Value of site transient.
  1746 	 *
  1416 	 */
  1747 	 * @param mixed  $value     New value of site transient.
  1417 	$value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
  1748 	 * @param string $transient Transient name.
       
  1749 	 */
       
  1750 	$value = apply_filters( "pre_set_site_transient_{$transient}", $value, $transient );
  1418 
  1751 
  1419 	$expiration = (int) $expiration;
  1752 	$expiration = (int) $expiration;
       
  1753 
       
  1754 	/**
       
  1755 	 * Filters the expiration for a site transient before its value is set.
       
  1756 	 *
       
  1757 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
       
  1758 	 *
       
  1759 	 * @since 4.4.0
       
  1760 	 *
       
  1761 	 * @param int    $expiration Time until expiration in seconds. Use 0 for no expiration.
       
  1762 	 * @param mixed  $value      New value of site transient.
       
  1763 	 * @param string $transient  Transient name.
       
  1764 	 */
       
  1765 	$expiration = apply_filters( "expiration_of_site_transient_{$transient}", $expiration, $value, $transient );
  1420 
  1766 
  1421 	if ( wp_using_ext_object_cache() ) {
  1767 	if ( wp_using_ext_object_cache() ) {
  1422 		$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
  1768 		$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
  1423 	} else {
  1769 	} else {
  1424 		$transient_timeout = '_site_transient_timeout_' . $transient;
  1770 		$transient_timeout = '_site_transient_timeout_' . $transient;
  1439 		 * Fires after the value for a specific site transient has been set.
  1785 		 * Fires after the value for a specific site transient has been set.
  1440 		 *
  1786 		 *
  1441 		 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
  1787 		 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
  1442 		 *
  1788 		 *
  1443 		 * @since 3.0.0
  1789 		 * @since 3.0.0
  1444 		 *
  1790 		 * @since 4.4.0 The `$transient` parameter was added
  1445 		 * @param mixed $value      Site transient value.
  1791 		 *
  1446 		 * @param int   $expiration Time until expiration in seconds. Default 0.
  1792 		 * @param mixed  $value      Site transient value.
       
  1793 		 * @param int    $expiration Time until expiration in seconds.
       
  1794 		 * @param string $transient  Transient name.
  1447 		 */
  1795 		 */
  1448 		do_action( 'set_site_transient_' . $transient, $value, $expiration );
  1796 		do_action( "set_site_transient_{$transient}", $value, $expiration, $transient );
  1449 
  1797 
  1450 		/**
  1798 		/**
  1451 		 * Fires after the value for a site transient has been set.
  1799 		 * Fires after the value for a site transient has been set.
  1452 		 *
  1800 		 *
  1453 		 * @since 3.0.0
  1801 		 * @since 3.0.0
  1454 		 *
  1802 		 *
  1455 		 * @param string $transient  The name of the site transient.
  1803 		 * @param string $transient  The name of the site transient.
  1456 		 * @param mixed  $value      Site transient value.
  1804 		 * @param mixed  $value      Site transient value.
  1457 		 * @param int    $expiration Time until expiration in seconds. Default 0.
  1805 		 * @param int    $expiration Time until expiration in seconds.
  1458 		 */
  1806 		 */
  1459 		do_action( 'setted_site_transient', $transient, $value, $expiration );
  1807 		do_action( 'setted_site_transient', $transient, $value, $expiration );
  1460 	}
  1808 	}
  1461 	return $result;
  1809 	return $result;
  1462 }
  1810 }
       
  1811 
       
  1812 /**
       
  1813  * Register default settings available in WordPress.
       
  1814  *
       
  1815  * The settings registered here are primarily useful for the REST API, so this
       
  1816  * does not encompass all settings available in WordPress.
       
  1817  *
       
  1818  * @since 4.7.0
       
  1819  */
       
  1820 function register_initial_settings() {
       
  1821 	register_setting( 'general', 'blogname', array(
       
  1822 		'show_in_rest' => array(
       
  1823 			'name' => 'title',
       
  1824 		),
       
  1825 		'type'         => 'string',
       
  1826 		'description'  => __( 'Site title.' ),
       
  1827 	) );
       
  1828 
       
  1829 	register_setting( 'general', 'blogdescription', array(
       
  1830 		'show_in_rest' => array(
       
  1831 			'name' => 'description',
       
  1832 		),
       
  1833 		'type'         => 'string',
       
  1834 		'description'  => __( 'Site tagline.' ),
       
  1835 	) );
       
  1836 
       
  1837 	if ( ! is_multisite() ) {
       
  1838 		register_setting( 'general', 'siteurl', array(
       
  1839 			'show_in_rest' => array(
       
  1840 				'name'    => 'url',
       
  1841 				'schema'  => array(
       
  1842 					'format' => 'uri',
       
  1843 				),
       
  1844 			),
       
  1845 			'type'         => 'string',
       
  1846 			'description'  => __( 'Site URL.' ),
       
  1847 		) );
       
  1848 	}
       
  1849 
       
  1850 	if ( ! is_multisite() ) {
       
  1851 		register_setting( 'general', 'admin_email', array(
       
  1852 			'show_in_rest' => array(
       
  1853 				'name'    => 'email',
       
  1854 				'schema'  => array(
       
  1855 					'format' => 'email',
       
  1856 				),
       
  1857 			),
       
  1858 			'type'         => 'string',
       
  1859 			'description'  => __( 'This address is used for admin purposes, like new user notification.' ),
       
  1860 		) );
       
  1861 	}
       
  1862 
       
  1863 	register_setting( 'general', 'timezone_string', array(
       
  1864 		'show_in_rest' => array(
       
  1865 			'name' => 'timezone',
       
  1866 		),
       
  1867 		'type'         => 'string',
       
  1868 		'description'  => __( 'A city in the same timezone as you.' ),
       
  1869 	) );
       
  1870 
       
  1871 	register_setting( 'general', 'date_format', array(
       
  1872 		'show_in_rest' => true,
       
  1873 		'type'         => 'string',
       
  1874 		'description'  => __( 'A date format for all date strings.' ),
       
  1875 	) );
       
  1876 
       
  1877 	register_setting( 'general', 'time_format', array(
       
  1878 		'show_in_rest' => true,
       
  1879 		'type'         => 'string',
       
  1880 		'description'  => __( 'A time format for all time strings.' ),
       
  1881 	) );
       
  1882 
       
  1883 	register_setting( 'general', 'start_of_week', array(
       
  1884 		'show_in_rest' => true,
       
  1885 		'type'         => 'integer',
       
  1886 		'description'  => __( 'A day number of the week that the week should start on.' ),
       
  1887 	) );
       
  1888 
       
  1889 	register_setting( 'general', 'WPLANG', array(
       
  1890 		'show_in_rest' => array(
       
  1891 			'name' => 'language',
       
  1892 		),
       
  1893 		'type'         => 'string',
       
  1894 		'description'  => __( 'WordPress locale code.' ),
       
  1895 		'default'      => 'en_US',
       
  1896 	) );
       
  1897 
       
  1898 	register_setting( 'writing', 'use_smilies', array(
       
  1899 		'show_in_rest' => true,
       
  1900 		'type'         => 'boolean',
       
  1901 		'description'  => __( 'Convert emoticons like :-) and :-P to graphics on display.' ),
       
  1902 		'default'      => true,
       
  1903 	) );
       
  1904 
       
  1905 	register_setting( 'writing', 'default_category', array(
       
  1906 		'show_in_rest' => true,
       
  1907 		'type'         => 'integer',
       
  1908 		'description'  => __( 'Default post category.' ),
       
  1909 	) );
       
  1910 
       
  1911 	register_setting( 'writing', 'default_post_format', array(
       
  1912 		'show_in_rest' => true,
       
  1913 		'type'         => 'string',
       
  1914 		'description'  => __( 'Default post format.' ),
       
  1915 	) );
       
  1916 
       
  1917 	register_setting( 'reading', 'posts_per_page', array(
       
  1918 		'show_in_rest' => true,
       
  1919 		'type'         => 'integer',
       
  1920 		'description'  => __( 'Blog pages show at most.' ),
       
  1921 		'default'      => 10,
       
  1922 	) );
       
  1923 
       
  1924 	register_setting( 'discussion', 'default_ping_status', array(
       
  1925 		'show_in_rest' => array(
       
  1926 			'schema'   => array(
       
  1927 				'enum' => array( 'open', 'closed' ),
       
  1928 			),
       
  1929 		),
       
  1930 		'type'         => 'string',
       
  1931 		'description'  => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles.' ),
       
  1932 	) );
       
  1933 
       
  1934 	register_setting( 'discussion', 'default_comment_status', array(
       
  1935 		'show_in_rest' => array(
       
  1936 			'schema'   => array(
       
  1937 				'enum' => array( 'open', 'closed' ),
       
  1938 			),
       
  1939 		),
       
  1940 		'type'         => 'string',
       
  1941 		'description'  => __( 'Allow people to post comments on new articles.' ),
       
  1942 	) );
       
  1943 
       
  1944 }
       
  1945 
       
  1946 /**
       
  1947  * Register a setting and its data.
       
  1948  *
       
  1949  * @since 2.7.0
       
  1950  * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
       
  1951  *
       
  1952  * @global array $new_whitelist_options
       
  1953  * @global array $wp_registered_settings
       
  1954  *
       
  1955  * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
       
  1956  * 	Default whitelisted option key names include "general," "discussion," and "reading," among others.
       
  1957  * @param string $option_name The name of an option to sanitize and save.
       
  1958  * @param array  $args {
       
  1959  *     Data used to describe the setting when registered.
       
  1960  *
       
  1961  *     @type string   $type              The type of data associated with this setting.
       
  1962  *                                       Valid values are 'string', 'boolean', 'integer', and 'number'.
       
  1963  *     @type string   $description       A description of the data attached to this setting.
       
  1964  *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
       
  1965  *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
       
  1966  *     @type mixed    $default           Default value when calling `get_option()`.
       
  1967  * }
       
  1968  */
       
  1969 function register_setting( $option_group, $option_name, $args = array() ) {
       
  1970 	global $new_whitelist_options, $wp_registered_settings;
       
  1971 
       
  1972 	$defaults = array(
       
  1973 		'type'              => 'string',
       
  1974 		'group'             => $option_group,
       
  1975 		'description'       => '',
       
  1976 		'sanitize_callback' => null,
       
  1977 		'show_in_rest'      => false,
       
  1978 	);
       
  1979 
       
  1980 	// Back-compat: old sanitize callback is added.
       
  1981 	if ( is_callable( $args ) ) {
       
  1982 		$args = array(
       
  1983 			'sanitize_callback' => $args,
       
  1984 		);
       
  1985 	}
       
  1986 
       
  1987 	/**
       
  1988 	 * Filters the registration arguments when registering a setting.
       
  1989 	 *
       
  1990 	 * @since 4.7.0
       
  1991 	 *
       
  1992 	 * @param array  $args         Array of setting registration arguments.
       
  1993 	 * @param array  $defaults     Array of default arguments.
       
  1994 	 * @param string $option_group Setting group.
       
  1995 	 * @param string $option_name  Setting name.
       
  1996 	 */
       
  1997 	$args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
       
  1998 	$args = wp_parse_args( $args, $defaults );
       
  1999 
       
  2000 	if ( ! is_array( $wp_registered_settings ) ) {
       
  2001 		$wp_registered_settings = array();
       
  2002 	}
       
  2003 
       
  2004 	if ( 'misc' == $option_group ) {
       
  2005 		_deprecated_argument( __FUNCTION__, '3.0.0',
       
  2006 			/* translators: %s: misc */
       
  2007 			sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ),
       
  2008 				'misc'
       
  2009 			)
       
  2010 		);
       
  2011 		$option_group = 'general';
       
  2012 	}
       
  2013 
       
  2014 	if ( 'privacy' == $option_group ) {
       
  2015 		_deprecated_argument( __FUNCTION__, '3.5.0',
       
  2016 			/* translators: %s: privacy */
       
  2017 			sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ),
       
  2018 				'privacy'
       
  2019 			)
       
  2020 		);
       
  2021 		$option_group = 'reading';
       
  2022 	}
       
  2023 
       
  2024 	$new_whitelist_options[ $option_group ][] = $option_name;
       
  2025 	if ( ! empty( $args['sanitize_callback'] ) ) {
       
  2026 		add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
       
  2027 	}
       
  2028 	if ( array_key_exists( 'default', $args ) ) {
       
  2029 		add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 );
       
  2030 	}
       
  2031 
       
  2032 	$wp_registered_settings[ $option_name ] = $args;
       
  2033 }
       
  2034 
       
  2035 /**
       
  2036  * Unregister a setting.
       
  2037  *
       
  2038  * @since 2.7.0
       
  2039  * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead.
       
  2040  *
       
  2041  * @global array $new_whitelist_options
       
  2042  *
       
  2043  * @param string   $option_group      The settings group name used during registration.
       
  2044  * @param string   $option_name       The name of the option to unregister.
       
  2045  * @param callable $deprecated        Deprecated.
       
  2046  */
       
  2047 function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
       
  2048 	global $new_whitelist_options, $wp_registered_settings;
       
  2049 
       
  2050 	if ( 'misc' == $option_group ) {
       
  2051 		_deprecated_argument( __FUNCTION__, '3.0.0',
       
  2052 			/* translators: %s: misc */
       
  2053 			sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ),
       
  2054 				'misc'
       
  2055 			)
       
  2056 		);
       
  2057 		$option_group = 'general';
       
  2058 	}
       
  2059 
       
  2060 	if ( 'privacy' == $option_group ) {
       
  2061 		_deprecated_argument( __FUNCTION__, '3.5.0',
       
  2062 			/* translators: %s: privacy */
       
  2063 			sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ),
       
  2064 				'privacy'
       
  2065 			)
       
  2066 		);
       
  2067 		$option_group = 'reading';
       
  2068 	}
       
  2069 
       
  2070 	$pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ] );
       
  2071 	if ( $pos !== false ) {
       
  2072 		unset( $new_whitelist_options[ $option_group ][ $pos ] );
       
  2073 	}
       
  2074 	if ( '' !== $deprecated ) {
       
  2075 		_deprecated_argument( __FUNCTION__, '4.7.0',
       
  2076 			/* translators: 1: $sanitize_callback, 2: register_setting() */
       
  2077 			sprintf( __( '%1$s is deprecated. The callback from %2$s is used instead.' ),
       
  2078 				'<code>$sanitize_callback</code>',
       
  2079 				'<code>register_setting()</code>'
       
  2080 			)
       
  2081 		);
       
  2082 		remove_filter( "sanitize_option_{$option_name}", $deprecated );
       
  2083 	}
       
  2084 
       
  2085 	if ( isset( $wp_registered_settings[ $option_name ] ) ) {
       
  2086 		// Remove the sanitize callback if one was set during registration.
       
  2087 		if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) {
       
  2088 			remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );
       
  2089 		}
       
  2090 
       
  2091 		unset( $wp_registered_settings[ $option_name ] );
       
  2092 	}
       
  2093 }
       
  2094 
       
  2095 /**
       
  2096  * Retrieves an array of registered settings.
       
  2097  *
       
  2098  * @since 4.7.0
       
  2099  *
       
  2100  * @return array List of registered settings, keyed by option name.
       
  2101  */
       
  2102 function get_registered_settings() {
       
  2103 	global $wp_registered_settings;
       
  2104 
       
  2105 	if ( ! is_array( $wp_registered_settings ) ) {
       
  2106 		return array();
       
  2107 	}
       
  2108 
       
  2109 	return $wp_registered_settings;
       
  2110 }
       
  2111 
       
  2112 /**
       
  2113  * Filter the default value for the option.
       
  2114  *
       
  2115  * For settings which register a default setting in `register_setting()`, this
       
  2116  * function is added as a filter to `default_option_{$option}`.
       
  2117  *
       
  2118  * @since 4.7.0
       
  2119  *
       
  2120  * @param mixed $default Existing default value to return.
       
  2121  * @param string $option Option name.
       
  2122  * @param bool $passed_default Was `get_option()` passed a default value?
       
  2123  * @return mixed Filtered default value.
       
  2124  */
       
  2125 function filter_default_option( $default, $option, $passed_default ) {
       
  2126 	if ( $passed_default ) {
       
  2127 		return $default;
       
  2128 	}
       
  2129 
       
  2130 	$registered = get_registered_settings();
       
  2131 	if ( empty( $registered[ $option ] ) ) {
       
  2132 		return $default;
       
  2133 	}
       
  2134 
       
  2135 	return $registered[ $option ]['default'];
       
  2136 }