wp/wp-includes/option.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    21  *
    21  *
    22  * @since 1.5.0
    22  * @since 1.5.0
    23  *
    23  *
    24  * @global wpdb $wpdb WordPress database abstraction object.
    24  * @global wpdb $wpdb WordPress database abstraction object.
    25  *
    25  *
    26  * @param string $option  Name of option to retrieve. Expected to not be SQL-escaped.
    26  * @param string $option  Name of the option to retrieve. Expected to not be SQL-escaped.
    27  * @param mixed  $default Optional. Default value to return if the option does not exist.
    27  * @param mixed  $default Optional. Default value to return if the option does not exist.
    28  * @return mixed Value set for the option.
    28  * @return mixed Value set for the option.
    29  */
    29  */
    30 function get_option( $option, $default = false ) {
    30 function get_option( $option, $default = false ) {
    31 	global $wpdb;
    31 	global $wpdb;
    33 	$option = trim( $option );
    33 	$option = trim( $option );
    34 	if ( empty( $option ) ) {
    34 	if ( empty( $option ) ) {
    35 		return false;
    35 		return false;
    36 	}
    36 	}
    37 
    37 
       
    38 	/*
       
    39 	 * Until a proper _deprecated_option() function can be introduced,
       
    40 	 * redirect requests to deprecated keys to the new, correct ones.
       
    41 	 */
       
    42 	$deprecated_keys = array(
       
    43 		'blacklist_keys'    => 'disallowed_keys',
       
    44 		'comment_whitelist' => 'comment_previously_approved',
       
    45 	);
       
    46 
       
    47 	if ( ! wp_installing() && isset( $deprecated_keys[ $option ] ) ) {
       
    48 		_deprecated_argument(
       
    49 			__FUNCTION__,
       
    50 			'5.5.0',
       
    51 			sprintf(
       
    52 				/* translators: 1: Deprecated option key, 2: New option key. */
       
    53 				__( 'The "%1$s" option key has been renamed to "%2$s".' ),
       
    54 				$option,
       
    55 				$deprecated_keys[ $option ]
       
    56 			)
       
    57 		);
       
    58 		return get_option( $deprecated_keys[ $option ], $default );
       
    59 	}
       
    60 
    38 	/**
    61 	/**
    39 	 * Filters the value of an existing option before it is retrieved.
    62 	 * Filters the value of an existing option before it is retrieved.
    40 	 *
    63 	 *
    41 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
    64 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
    42 	 *
    65 	 *
    43 	 * Passing a truthy value to the filter will short-circuit retrieving
    66 	 * Returning a truthy value from the filter will effectively short-circuit retrieval
    44 	 * the option value, returning the passed value instead.
    67 	 * and return the passed value instead.
    45 	 *
    68 	 *
    46 	 * @since 1.5.0
    69 	 * @since 1.5.0
    47 	 * @since 4.4.0 The `$option` parameter was added.
    70 	 * @since 4.4.0 The `$option` parameter was added.
    48 	 * @since 4.9.0 The `$default` parameter was added.
    71 	 * @since 4.9.0 The `$default` parameter was added.
    49 	 *
    72 	 *
    50 	 * @param bool|mixed $pre_option The value to return instead of the option value. This differs from
    73 	 * @param mixed  $pre_option The value to return instead of the option value. This differs
    51 	 *                               `$default`, which is used as the fallback value in the event the option
    74 	 *                           from `$default`, which is used as the fallback value in the event
    52 	 *                               doesn't exist elsewhere in get_option(). Default false (to skip past the
    75 	 *                           the option doesn't exist elsewhere in get_option().
    53 	 *                               short-circuit).
    76 	 *                           Default false (to skip past the short-circuit).
    54 	 * @param string     $option     Option name.
    77 	 * @param string $option     Option name.
    55 	 * @param mixed      $default    The fallback value to return if the option does not exist.
    78 	 * @param mixed  $default    The fallback value to return if the option does not exist.
    56 	 *                               Default is false.
    79 	 *                           Default false.
    57 	 */
    80 	 */
    58 	$pre = apply_filters( "pre_option_{$option}", false, $option, $default );
    81 	$pre = apply_filters( "pre_option_{$option}", false, $option, $default );
    59 
    82 
    60 	if ( false !== $pre ) {
    83 	if ( false !== $pre ) {
    61 		return $pre;
    84 		return $pre;
    67 
    90 
    68 	// Distinguish between `false` as a default, and not passing one.
    91 	// Distinguish between `false` as a default, and not passing one.
    69 	$passed_default = func_num_args() > 1;
    92 	$passed_default = func_num_args() > 1;
    70 
    93 
    71 	if ( ! wp_installing() ) {
    94 	if ( ! wp_installing() ) {
    72 		// prevent non-existent options from triggering multiple queries
    95 		// Prevent non-existent options from triggering multiple queries.
    73 		$notoptions = wp_cache_get( 'notoptions', 'options' );
    96 		$notoptions = wp_cache_get( 'notoptions', 'options' );
       
    97 
    74 		if ( isset( $notoptions[ $option ] ) ) {
    98 		if ( isset( $notoptions[ $option ] ) ) {
    75 			/**
    99 			/**
    76 			 * Filters the default value for an option.
   100 			 * Filters the default value for an option.
    77 			 *
   101 			 *
    78 			 * The dynamic portion of the hook name, `$option`, refers to the option name.
   102 			 * The dynamic portion of the hook name, `$option`, refers to the option name.
    97 			$value = wp_cache_get( $option, 'options' );
   121 			$value = wp_cache_get( $option, 'options' );
    98 
   122 
    99 			if ( false === $value ) {
   123 			if ( false === $value ) {
   100 				$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
   124 				$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
   101 
   125 
   102 				// Has to be get_row instead of get_var because of funkiness with 0, false, null values
   126 				// Has to be get_row() instead of get_var() because of funkiness with 0, false, null values.
   103 				if ( is_object( $row ) ) {
   127 				if ( is_object( $row ) ) {
   104 					$value = $row->option_value;
   128 					$value = $row->option_value;
   105 					wp_cache_add( $option, $value, 'options' );
   129 					wp_cache_add( $option, $value, 'options' );
   106 				} else { // option does not exist, so we must cache its non-existence
   130 				} else { // Option does not exist, so we must cache its non-existence.
   107 					if ( ! is_array( $notoptions ) ) {
   131 					if ( ! is_array( $notoptions ) ) {
   108 						$notoptions = array();
   132 						$notoptions = array();
   109 					}
   133 					}
       
   134 
   110 					$notoptions[ $option ] = true;
   135 					$notoptions[ $option ] = true;
   111 					wp_cache_set( 'notoptions', $notoptions, 'options' );
   136 					wp_cache_set( 'notoptions', $notoptions, 'options' );
   112 
   137 
   113 					/** This filter is documented in wp-includes/option.php */
   138 					/** This filter is documented in wp-includes/option.php */
   114 					return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
   139 					return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
   117 		}
   142 		}
   118 	} else {
   143 	} else {
   119 		$suppress = $wpdb->suppress_errors();
   144 		$suppress = $wpdb->suppress_errors();
   120 		$row      = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
   145 		$row      = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
   121 		$wpdb->suppress_errors( $suppress );
   146 		$wpdb->suppress_errors( $suppress );
       
   147 
   122 		if ( is_object( $row ) ) {
   148 		if ( is_object( $row ) ) {
   123 			$value = $row->option_value;
   149 			$value = $row->option_value;
   124 		} else {
   150 		} else {
   125 			/** This filter is documented in wp-includes/option.php */
   151 			/** This filter is documented in wp-includes/option.php */
   126 			return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
   152 			return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
   127 		}
   153 		}
   128 	}
   154 	}
   129 
   155 
   130 	// If home is not set use siteurl.
   156 	// If home is not set, use siteurl.
   131 	if ( 'home' == $option && '' == $value ) {
   157 	if ( 'home' === $option && '' === $value ) {
   132 		return get_option( 'siteurl' );
   158 		return get_option( 'siteurl' );
   133 	}
   159 	}
   134 
   160 
   135 	if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) ) {
   161 	if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ), true ) ) {
   136 		$value = untrailingslashit( $value );
   162 		$value = untrailingslashit( $value );
   137 	}
   163 	}
   138 
   164 
   139 	/**
   165 	/**
   140 	 * Filters the value of an existing option.
   166 	 * Filters the value of an existing option.
   151 	 */
   177 	 */
   152 	return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
   178 	return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
   153 }
   179 }
   154 
   180 
   155 /**
   181 /**
   156  * Protect WordPress special option from being modified.
   182  * Protects WordPress special option from being modified.
   157  *
   183  *
   158  * Will die if $option is in protected list. Protected options are 'alloptions'
   184  * Will die if $option is in protected list. Protected options are 'alloptions'
   159  * and 'notoptions' options.
   185  * and 'notoptions' options.
   160  *
   186  *
   161  * @since 2.2.0
   187  * @since 2.2.0
   162  *
   188  *
   163  * @param string $option Option name.
   189  * @param string $option Option name.
   164  */
   190  */
   165 function wp_protect_special_option( $option ) {
   191 function wp_protect_special_option( $option ) {
   166 	if ( 'alloptions' === $option || 'notoptions' === $option ) {
   192 	if ( 'alloptions' === $option || 'notoptions' === $option ) {
   167 		wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
   193 		wp_die(
   168 	}
   194 			sprintf(
   169 }
   195 				/* translators: %s: Option name. */
   170 
   196 				__( '%s is a protected WP option and may not be modified' ),
   171 /**
   197 				esc_html( $option )
   172  * Print option value after sanitizing for forms.
   198 			)
       
   199 		);
       
   200 	}
       
   201 }
       
   202 
       
   203 /**
       
   204  * Prints option value after sanitizing for forms.
   173  *
   205  *
   174  * @since 1.5.0
   206  * @since 1.5.0
   175  *
   207  *
   176  * @param string $option Option name.
   208  * @param string $option Option name.
   177  */
   209  */
   181 
   213 
   182 /**
   214 /**
   183  * Loads and caches all autoloaded options, if available or all options.
   215  * Loads and caches all autoloaded options, if available or all options.
   184  *
   216  *
   185  * @since 2.2.0
   217  * @since 2.2.0
       
   218  * @since 5.3.1 The `$force_cache` parameter was added.
   186  *
   219  *
   187  * @global wpdb $wpdb WordPress database abstraction object.
   220  * @global wpdb $wpdb WordPress database abstraction object.
   188  *
   221  *
       
   222  * @param bool $force_cache Optional. Whether to force an update of the local cache
       
   223  *                          from the persistent cache. Default false.
   189  * @return array List of all options.
   224  * @return array List of all options.
   190  */
   225  */
   191 function wp_load_alloptions() {
   226 function wp_load_alloptions( $force_cache = false ) {
   192 	global $wpdb;
   227 	global $wpdb;
   193 
   228 
   194 	if ( ! wp_installing() || ! is_multisite() ) {
   229 	if ( ! wp_installing() || ! is_multisite() ) {
   195 		$alloptions = wp_cache_get( 'alloptions', 'options' );
   230 		$alloptions = wp_cache_get( 'alloptions', 'options', $force_cache );
   196 	} else {
   231 	} else {
   197 		$alloptions = false;
   232 		$alloptions = false;
   198 	}
   233 	}
   199 
   234 
   200 	if ( ! $alloptions ) {
   235 	if ( ! $alloptions ) {
   201 		$suppress = $wpdb->suppress_errors();
   236 		$suppress      = $wpdb->suppress_errors();
   202 		if ( ! $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) {
   237 		$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" );
       
   238 		if ( ! $alloptions_db ) {
   203 			$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
   239 			$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
   204 		}
   240 		}
   205 		$wpdb->suppress_errors( $suppress );
   241 		$wpdb->suppress_errors( $suppress );
   206 
   242 
   207 		$alloptions = array();
   243 		$alloptions = array();
   216 			 * @since 4.9.0
   252 			 * @since 4.9.0
   217 			 *
   253 			 *
   218 			 * @param array $alloptions Array with all options.
   254 			 * @param array $alloptions Array with all options.
   219 			 */
   255 			 */
   220 			$alloptions = apply_filters( 'pre_cache_alloptions', $alloptions );
   256 			$alloptions = apply_filters( 'pre_cache_alloptions', $alloptions );
       
   257 
   221 			wp_cache_add( 'alloptions', $alloptions, 'options' );
   258 			wp_cache_add( 'alloptions', $alloptions, 'options' );
   222 		}
   259 		}
   223 	}
   260 	}
   224 
   261 
   225 	/**
   262 	/**
   265 		wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
   302 		wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
   266 	}
   303 	}
   267 }
   304 }
   268 
   305 
   269 /**
   306 /**
   270  * Update the value of an option that was already added.
   307  * Updates the value of an option that was already added.
   271  *
   308  *
   272  * You do not need to serialize values. If the value needs to be serialized, then
   309  * You do not need to serialize values. If the value needs to be serialized,
   273  * it will be serialized before it is inserted into the database. Remember,
   310  * then it will be serialized before it is inserted into the database.
   274  * resources can not be serialized or added as an option.
   311  * Remember, resources cannot be serialized or added as an option.
   275  *
   312  *
   276  * If the option does not exist, then the option will be added with the option value,
   313  * If the option does not exist, it will be created.
   277  * with an `$autoload` value of 'yes'.
   314 
       
   315  * This function is designed to work with or without a logged-in user. In terms of security,
       
   316  * plugin developers should check the current user's capabilities before updating any options.
   278  *
   317  *
   279  * @since 1.0.0
   318  * @since 1.0.0
   280  * @since 4.2.0 The `$autoload` parameter was added.
   319  * @since 4.2.0 The `$autoload` parameter was added.
   281  *
   320  *
   282  * @global wpdb $wpdb WordPress database abstraction object.
   321  * @global wpdb $wpdb WordPress database abstraction object.
   283  *
   322  *
   284  * @param string      $option   Option name. Expected to not be SQL-escaped.
   323  * @param string      $option   Name of the option to update. Expected to not be SQL-escaped.
   285  * @param mixed       $value    Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
   324  * @param mixed       $value    Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
   286  * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
   325  * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
   287  *                              `$autoload` can only be updated using `update_option()` if `$value` is also changed.
   326  *                              `$autoload` can only be updated using `update_option()` if `$value` is also changed.
   288  *                              Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
   327  *                              Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
   289  *                              the default value is 'yes'. Default null.
   328  *                              the default value is 'yes'. Default null.
   290  * @return bool False if value was not updated and true if value was updated.
   329  * @return bool True if the value was updated, false otherwise.
   291  */
   330  */
   292 function update_option( $option, $value, $autoload = null ) {
   331 function update_option( $option, $value, $autoload = null ) {
   293 	global $wpdb;
   332 	global $wpdb;
   294 
   333 
   295 	$option = trim( $option );
   334 	$option = trim( $option );
   296 	if ( empty( $option ) ) {
   335 	if ( empty( $option ) ) {
   297 		return false;
   336 		return false;
       
   337 	}
       
   338 
       
   339 	/*
       
   340 	 * Until a proper _deprecated_option() function can be introduced,
       
   341 	 * redirect requests to deprecated keys to the new, correct ones.
       
   342 	 */
       
   343 	$deprecated_keys = array(
       
   344 		'blacklist_keys'    => 'disallowed_keys',
       
   345 		'comment_whitelist' => 'comment_previously_approved',
       
   346 	);
       
   347 
       
   348 	if ( ! wp_installing() && isset( $deprecated_keys[ $option ] ) ) {
       
   349 		_deprecated_argument(
       
   350 			__FUNCTION__,
       
   351 			'5.5.0',
       
   352 			sprintf(
       
   353 				/* translators: 1: Deprecated option key, 2: New option key. */
       
   354 				__( 'The "%1$s" option key has been renamed to "%2$s".' ),
       
   355 				$option,
       
   356 				$deprecated_keys[ $option ]
       
   357 			)
       
   358 		);
       
   359 		return update_option( $deprecated_keys[ $option ], $value, $autoload );
   298 	}
   360 	}
   299 
   361 
   300 	wp_protect_special_option( $option );
   362 	wp_protect_special_option( $option );
   301 
   363 
   302 	if ( is_object( $value ) ) {
   364 	if ( is_object( $value ) ) {
   379 	if ( ! $result ) {
   441 	if ( ! $result ) {
   380 		return false;
   442 		return false;
   381 	}
   443 	}
   382 
   444 
   383 	$notoptions = wp_cache_get( 'notoptions', 'options' );
   445 	$notoptions = wp_cache_get( 'notoptions', 'options' );
       
   446 
   384 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
   447 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
   385 		unset( $notoptions[ $option ] );
   448 		unset( $notoptions[ $option ] );
   386 		wp_cache_set( 'notoptions', $notoptions, 'options' );
   449 		wp_cache_set( 'notoptions', $notoptions, 'options' );
   387 	}
   450 	}
   388 
   451 
   389 	if ( ! wp_installing() ) {
   452 	if ( ! wp_installing() ) {
   390 		$alloptions = wp_load_alloptions();
   453 		$alloptions = wp_load_alloptions( true );
   391 		if ( isset( $alloptions[ $option ] ) ) {
   454 		if ( isset( $alloptions[ $option ] ) ) {
   392 			$alloptions[ $option ] = $serialized_value;
   455 			$alloptions[ $option ] = $serialized_value;
   393 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   456 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   394 		} else {
   457 		} else {
   395 			wp_cache_set( $option, $serialized_value, 'options' );
   458 			wp_cache_set( $option, $serialized_value, 'options' );
   418 	 * @param string $option    Name of the updated option.
   481 	 * @param string $option    Name of the updated option.
   419 	 * @param mixed  $old_value The old option value.
   482 	 * @param mixed  $old_value The old option value.
   420 	 * @param mixed  $value     The new option value.
   483 	 * @param mixed  $value     The new option value.
   421 	 */
   484 	 */
   422 	do_action( 'updated_option', $option, $old_value, $value );
   485 	do_action( 'updated_option', $option, $old_value, $value );
       
   486 
   423 	return true;
   487 	return true;
   424 }
   488 }
   425 
   489 
   426 /**
   490 /**
   427  * Add a new option.
   491  * Adds a new option.
   428  *
   492  *
   429  * You do not need to serialize values. If the value needs to be serialized, then
   493  * You do not need to serialize values. If the value needs to be serialized,
   430  * it will be serialized before it is inserted into the database. Remember,
   494  * then it will be serialized before it is inserted into the database.
   431  * resources can not be serialized or added as an option.
   495  * Remember, resources cannot be serialized or added as an option.
   432  *
   496  *
   433  * You can create options without values and then update the values later.
   497  * You can create options without values and then update the values later.
   434  * Existing options will not be updated and checks are performed to ensure that you
   498  * Existing options will not be updated and checks are performed to ensure that you
   435  * aren't adding a protected WordPress option. Care should be taken to not name
   499  * aren't adding a protected WordPress option. Care should be taken to not name
   436  * options the same as the ones which are protected.
   500  * options the same as the ones which are protected.
   437  *
   501  *
   438  * @since 1.0.0
   502  * @since 1.0.0
   439  *
   503  *
   440  * @global wpdb $wpdb WordPress database abstraction object.
   504  * @global wpdb $wpdb WordPress database abstraction object.
   441  *
   505  *
   442  * @param string         $option      Name of option to add. Expected to not be SQL-escaped.
   506  * @param string      $option     Name of the option to add. Expected to not be SQL-escaped.
   443  * @param mixed          $value       Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
   507  * @param mixed       $value      Optional. Option value. Must be serializable if non-scalar.
   444  * @param string         $deprecated  Optional. Description. Not used anymore.
   508  *                                Expected to not be SQL-escaped.
   445  * @param string|bool    $autoload    Optional. Whether to load the option when WordPress starts up.
   509  * @param string      $deprecated Optional. Description. Not used anymore.
   446  *                                    Default is enabled. Accepts 'no' to disable for legacy reasons.
   510  * @param string|bool $autoload   Optional. Whether to load the option when WordPress starts up.
   447  * @return bool False if option was not added and true if option was added.
   511  *                                Default is enabled. Accepts 'no' to disable for legacy reasons.
       
   512  * @return bool True if the option was added, false otherwise.
   448  */
   513  */
   449 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
   514 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
   450 	global $wpdb;
   515 	global $wpdb;
   451 
   516 
   452 	if ( ! empty( $deprecated ) ) {
   517 	if ( ! empty( $deprecated ) ) {
   456 	$option = trim( $option );
   521 	$option = trim( $option );
   457 	if ( empty( $option ) ) {
   522 	if ( empty( $option ) ) {
   458 		return false;
   523 		return false;
   459 	}
   524 	}
   460 
   525 
       
   526 	/*
       
   527 	 * Until a proper _deprecated_option() function can be introduced,
       
   528 	 * redirect requests to deprecated keys to the new, correct ones.
       
   529 	 */
       
   530 	$deprecated_keys = array(
       
   531 		'blacklist_keys'    => 'disallowed_keys',
       
   532 		'comment_whitelist' => 'comment_previously_approved',
       
   533 	);
       
   534 
       
   535 	if ( ! wp_installing() && isset( $deprecated_keys[ $option ] ) ) {
       
   536 		_deprecated_argument(
       
   537 			__FUNCTION__,
       
   538 			'5.5.0',
       
   539 			sprintf(
       
   540 				/* translators: 1: Deprecated option key, 2: New option key. */
       
   541 				__( 'The "%1$s" option key has been renamed to "%2$s".' ),
       
   542 				$option,
       
   543 				$deprecated_keys[ $option ]
       
   544 			)
       
   545 		);
       
   546 		return add_option( $deprecated_keys[ $option ], $value, $deprecated, $autoload );
       
   547 	}
       
   548 
   461 	wp_protect_special_option( $option );
   549 	wp_protect_special_option( $option );
   462 
   550 
   463 	if ( is_object( $value ) ) {
   551 	if ( is_object( $value ) ) {
   464 		$value = clone $value;
   552 		$value = clone $value;
   465 	}
   553 	}
   466 
   554 
   467 	$value = sanitize_option( $option, $value );
   555 	$value = sanitize_option( $option, $value );
   468 
   556 
   469 	// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
   557 	// Make sure the option doesn't already exist.
       
   558 	// We can check the 'notoptions' cache before we ask for a DB query.
   470 	$notoptions = wp_cache_get( 'notoptions', 'options' );
   559 	$notoptions = wp_cache_get( 'notoptions', 'options' );
       
   560 
   471 	if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
   561 	if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
   472 		/** This filter is documented in wp-includes/option.php */
   562 		/** This filter is documented in wp-includes/option.php */
   473 		if ( apply_filters( "default_option_{$option}", false, $option, false ) !== get_option( $option ) ) {
   563 		if ( apply_filters( "default_option_{$option}", false, $option, false ) !== get_option( $option ) ) {
   474 			return false;
   564 			return false;
   475 		}
   565 		}
   492 	if ( ! $result ) {
   582 	if ( ! $result ) {
   493 		return false;
   583 		return false;
   494 	}
   584 	}
   495 
   585 
   496 	if ( ! wp_installing() ) {
   586 	if ( ! wp_installing() ) {
   497 		if ( 'yes' == $autoload ) {
   587 		if ( 'yes' === $autoload ) {
   498 			$alloptions            = wp_load_alloptions();
   588 			$alloptions            = wp_load_alloptions( true );
   499 			$alloptions[ $option ] = $serialized_value;
   589 			$alloptions[ $option ] = $serialized_value;
   500 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   590 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   501 		} else {
   591 		} else {
   502 			wp_cache_set( $option, $serialized_value, 'options' );
   592 			wp_cache_set( $option, $serialized_value, 'options' );
   503 		}
   593 		}
   504 	}
   594 	}
   505 
   595 
   506 	// This option exists now
   596 	// This option exists now.
   507 	$notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
   597 	$notoptions = wp_cache_get( 'notoptions', 'options' ); // Yes, again... we need it to be fresh.
       
   598 
   508 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
   599 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
   509 		unset( $notoptions[ $option ] );
   600 		unset( $notoptions[ $option ] );
   510 		wp_cache_set( 'notoptions', $notoptions, 'options' );
   601 		wp_cache_set( 'notoptions', $notoptions, 'options' );
   511 	}
   602 	}
   512 
   603 
   530 	 *
   621 	 *
   531 	 * @param string $option Name of the added option.
   622 	 * @param string $option Name of the added option.
   532 	 * @param mixed  $value  Value of the option.
   623 	 * @param mixed  $value  Value of the option.
   533 	 */
   624 	 */
   534 	do_action( 'added_option', $option, $value );
   625 	do_action( 'added_option', $option, $value );
       
   626 
   535 	return true;
   627 	return true;
   536 }
   628 }
   537 
   629 
   538 /**
   630 /**
   539  * Removes option by name. Prevents removal of protected WordPress options.
   631  * Removes option by name. Prevents removal of protected WordPress options.
   540  *
   632  *
   541  * @since 1.2.0
   633  * @since 1.2.0
   542  *
   634  *
   543  * @global wpdb $wpdb WordPress database abstraction object.
   635  * @global wpdb $wpdb WordPress database abstraction object.
   544  *
   636  *
   545  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
   637  * @param string $option Name of the option to delete. Expected to not be SQL-escaped.
   546  * @return bool True, if option is successfully deleted. False on failure.
   638  * @return bool True if the option was deleted, false otherwise.
   547  */
   639  */
   548 function delete_option( $option ) {
   640 function delete_option( $option ) {
   549 	global $wpdb;
   641 	global $wpdb;
   550 
   642 
   551 	$option = trim( $option );
   643 	$option = trim( $option );
   553 		return false;
   645 		return false;
   554 	}
   646 	}
   555 
   647 
   556 	wp_protect_special_option( $option );
   648 	wp_protect_special_option( $option );
   557 
   649 
   558 	// Get the ID, if no ID then return
   650 	// Get the ID, if no ID then return.
   559 	$row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
   651 	$row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
   560 	if ( is_null( $row ) ) {
   652 	if ( is_null( $row ) ) {
   561 		return false;
   653 		return false;
   562 	}
   654 	}
   563 
   655 
   569 	 * @param string $option Name of the option to delete.
   661 	 * @param string $option Name of the option to delete.
   570 	 */
   662 	 */
   571 	do_action( 'delete_option', $option );
   663 	do_action( 'delete_option', $option );
   572 
   664 
   573 	$result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
   665 	$result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
       
   666 
   574 	if ( ! wp_installing() ) {
   667 	if ( ! wp_installing() ) {
   575 		if ( 'yes' == $row->autoload ) {
   668 		if ( 'yes' === $row->autoload ) {
   576 			$alloptions = wp_load_alloptions();
   669 			$alloptions = wp_load_alloptions( true );
   577 			if ( is_array( $alloptions ) && isset( $alloptions[ $option ] ) ) {
   670 			if ( is_array( $alloptions ) && isset( $alloptions[ $option ] ) ) {
   578 				unset( $alloptions[ $option ] );
   671 				unset( $alloptions[ $option ] );
   579 				wp_cache_set( 'alloptions', $alloptions, 'options' );
   672 				wp_cache_set( 'alloptions', $alloptions, 'options' );
   580 			}
   673 			}
   581 		} else {
   674 		} else {
   582 			wp_cache_delete( $option, 'options' );
   675 			wp_cache_delete( $option, 'options' );
   583 		}
   676 		}
   584 	}
   677 	}
       
   678 
   585 	if ( $result ) {
   679 	if ( $result ) {
   586 
   680 
   587 		/**
   681 		/**
   588 		 * Fires after a specific option has been deleted.
   682 		 * Fires after a specific option has been deleted.
   589 		 *
   683 		 *
   601 		 * @since 2.9.0
   695 		 * @since 2.9.0
   602 		 *
   696 		 *
   603 		 * @param string $option Name of the deleted option.
   697 		 * @param string $option Name of the deleted option.
   604 		 */
   698 		 */
   605 		do_action( 'deleted_option', $option );
   699 		do_action( 'deleted_option', $option );
       
   700 
   606 		return true;
   701 		return true;
   607 	}
   702 	}
       
   703 
   608 	return false;
   704 	return false;
   609 }
   705 }
   610 
   706 
   611 /**
   707 /**
   612  * Delete a transient.
   708  * Deletes a transient.
   613  *
   709  *
   614  * @since 2.8.0
   710  * @since 2.8.0
   615  *
   711  *
   616  * @param string $transient Transient name. Expected to not be SQL-escaped.
   712  * @param string $transient Transient name. Expected to not be SQL-escaped.
   617  * @return bool true if successful, false otherwise
   713  * @return bool True if the transient was deleted, false otherwise.
   618  */
   714  */
   619 function delete_transient( $transient ) {
   715 function delete_transient( $transient ) {
   620 
   716 
   621 	/**
   717 	/**
   622 	 * Fires immediately before a specific transient is deleted.
   718 	 * Fires immediately before a specific transient is deleted.
   633 		$result = wp_cache_delete( $transient, 'transient' );
   729 		$result = wp_cache_delete( $transient, 'transient' );
   634 	} else {
   730 	} else {
   635 		$option_timeout = '_transient_timeout_' . $transient;
   731 		$option_timeout = '_transient_timeout_' . $transient;
   636 		$option         = '_transient_' . $transient;
   732 		$option         = '_transient_' . $transient;
   637 		$result         = delete_option( $option );
   733 		$result         = delete_option( $option );
       
   734 
   638 		if ( $result ) {
   735 		if ( $result ) {
   639 			delete_option( $option_timeout );
   736 			delete_option( $option_timeout );
   640 		}
   737 		}
   641 	}
   738 	}
   642 
   739 
   654 
   751 
   655 	return $result;
   752 	return $result;
   656 }
   753 }
   657 
   754 
   658 /**
   755 /**
   659  * Get the value of a transient.
   756  * Retrieves the value of a transient.
   660  *
   757  *
   661  * If the transient does not exist, does not have a value, or has expired,
   758  * If the transient does not exist, does not have a value, or has expired,
   662  * then the return value will be false.
   759  * then the return value will be false.
   663  *
   760  *
   664  * @since 2.8.0
   761  * @since 2.8.0
   667  * @return mixed Value of transient.
   764  * @return mixed Value of transient.
   668  */
   765  */
   669 function get_transient( $transient ) {
   766 function get_transient( $transient ) {
   670 
   767 
   671 	/**
   768 	/**
   672 	 * Filters the value of an existing transient.
   769 	 * Filters the value of an existing transient before it is retrieved.
   673 	 *
   770 	 *
   674 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
   771 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
   675 	 *
   772 	 *
   676 	 * Passing a truthy value to the filter will effectively short-circuit retrieval
   773 	 * Returning a truthy value from the filter will effectively short-circuit retrieval
   677 	 * of the transient, returning the passed value instead.
   774 	 * and return the passed value instead.
   678 	 *
   775 	 *
   679 	 * @since 2.8.0
   776 	 * @since 2.8.0
   680 	 * @since 4.4.0 The `$transient` parameter was added
   777 	 * @since 4.4.0 The `$transient` parameter was added
   681 	 *
   778 	 *
   682 	 * @param mixed  $pre_transient The default value to return if the transient does not exist.
   779 	 * @param mixed  $pre_transient The default value to return if the transient does not exist.
   683 	 *                              Any value other than false will short-circuit the retrieval
   780 	 *                              Any value other than false will short-circuit the retrieval
   684 	 *                              of the transient, and return the returned value.
   781 	 *                              of the transient, and return that value.
   685 	 * @param string $transient     Transient name.
   782 	 * @param string $transient     Transient name.
   686 	 */
   783 	 */
   687 	$pre = apply_filters( "pre_transient_{$transient}", false, $transient );
   784 	$pre = apply_filters( "pre_transient_{$transient}", false, $transient );
       
   785 
   688 	if ( false !== $pre ) {
   786 	if ( false !== $pre ) {
   689 		return $pre;
   787 		return $pre;
   690 	}
   788 	}
   691 
   789 
   692 	if ( wp_using_ext_object_cache() ) {
   790 	if ( wp_using_ext_object_cache() ) {
   693 		$value = wp_cache_get( $transient, 'transient' );
   791 		$value = wp_cache_get( $transient, 'transient' );
   694 	} else {
   792 	} else {
   695 		$transient_option = '_transient_' . $transient;
   793 		$transient_option = '_transient_' . $transient;
   696 		if ( ! wp_installing() ) {
   794 		if ( ! wp_installing() ) {
   697 			// If option is not in alloptions, it is not autoloaded and thus has a timeout
   795 			// If option is not in alloptions, it is not autoloaded and thus has a timeout.
   698 			$alloptions = wp_load_alloptions();
   796 			$alloptions = wp_load_alloptions();
   699 			if ( ! isset( $alloptions[ $transient_option ] ) ) {
   797 			if ( ! isset( $alloptions[ $transient_option ] ) ) {
   700 				$transient_timeout = '_transient_timeout_' . $transient;
   798 				$transient_timeout = '_transient_timeout_' . $transient;
   701 				$timeout           = get_option( $transient_timeout );
   799 				$timeout           = get_option( $transient_timeout );
   702 				if ( false !== $timeout && $timeout < time() ) {
   800 				if ( false !== $timeout && $timeout < time() ) {
   725 	 */
   823 	 */
   726 	return apply_filters( "transient_{$transient}", $value, $transient );
   824 	return apply_filters( "transient_{$transient}", $value, $transient );
   727 }
   825 }
   728 
   826 
   729 /**
   827 /**
   730  * Set/update the value of a transient.
   828  * Sets/updates the value of a transient.
   731  *
   829  *
   732  * You do not need to serialize values. If the value needs to be serialized, then
   830  * You do not need to serialize values. If the value needs to be serialized,
   733  * it will be serialized before it is set.
   831  * then it will be serialized before it is set.
   734  *
   832  *
   735  * @since 2.8.0
   833  * @since 2.8.0
   736  *
   834  *
   737  * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
   835  * @param string $transient  Transient name. Expected to not be SQL-escaped.
   738  *                           172 characters or fewer in length.
   836  *                           Must be 172 characters or fewer in length.
   739  * @param mixed  $value      Transient value. Must be serializable if non-scalar.
   837  * @param mixed  $value      Transient value. Must be serializable if non-scalar.
   740  *                           Expected to not be SQL-escaped.
   838  *                           Expected to not be SQL-escaped.
   741  * @param int    $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
   839  * @param int    $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
   742  * @return bool False if value was not set and true if value was set.
   840  * @return bool True if the value was set, false otherwise.
   743  */
   841  */
   744 function set_transient( $transient, $value, $expiration = 0 ) {
   842 function set_transient( $transient, $value, $expiration = 0 ) {
   745 
   843 
   746 	$expiration = (int) $expiration;
   844 	$expiration = (int) $expiration;
   747 
   845 
   776 	if ( wp_using_ext_object_cache() ) {
   874 	if ( wp_using_ext_object_cache() ) {
   777 		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
   875 		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
   778 	} else {
   876 	} else {
   779 		$transient_timeout = '_transient_timeout_' . $transient;
   877 		$transient_timeout = '_transient_timeout_' . $transient;
   780 		$transient_option  = '_transient_' . $transient;
   878 		$transient_option  = '_transient_' . $transient;
       
   879 
   781 		if ( false === get_option( $transient_option ) ) {
   880 		if ( false === get_option( $transient_option ) ) {
   782 			$autoload = 'yes';
   881 			$autoload = 'yes';
   783 			if ( $expiration ) {
   882 			if ( $expiration ) {
   784 				$autoload = 'no';
   883 				$autoload = 'no';
   785 				add_option( $transient_timeout, time() + $expiration, '', 'no' );
   884 				add_option( $transient_timeout, time() + $expiration, '', 'no' );
   787 			$result = add_option( $transient_option, $value, '', $autoload );
   886 			$result = add_option( $transient_option, $value, '', $autoload );
   788 		} else {
   887 		} else {
   789 			// If expiration is requested, but the transient has no timeout option,
   888 			// If expiration is requested, but the transient has no timeout option,
   790 			// delete, then re-create transient rather than update.
   889 			// delete, then re-create transient rather than update.
   791 			$update = true;
   890 			$update = true;
       
   891 
   792 			if ( $expiration ) {
   892 			if ( $expiration ) {
   793 				if ( false === get_option( $transient_timeout ) ) {
   893 				if ( false === get_option( $transient_timeout ) ) {
   794 					delete_option( $transient_option );
   894 					delete_option( $transient_option );
   795 					add_option( $transient_timeout, time() + $expiration, '', 'no' );
   895 					add_option( $transient_timeout, time() + $expiration, '', 'no' );
   796 					$result = add_option( $transient_option, $value, '', 'no' );
   896 					$result = add_option( $transient_option, $value, '', 'no' );
   797 					$update = false;
   897 					$update = false;
   798 				} else {
   898 				} else {
   799 					update_option( $transient_timeout, time() + $expiration );
   899 					update_option( $transient_timeout, time() + $expiration );
   800 				}
   900 				}
   801 			}
   901 			}
       
   902 
   802 			if ( $update ) {
   903 			if ( $update ) {
   803 				$result = update_option( $transient_option, $value );
   904 				$result = update_option( $transient_option, $value );
   804 			}
   905 			}
   805 		}
   906 		}
   806 	}
   907 	}
   832 		 * @param mixed  $value      Transient value.
   933 		 * @param mixed  $value      Transient value.
   833 		 * @param int    $expiration Time until expiration in seconds.
   934 		 * @param int    $expiration Time until expiration in seconds.
   834 		 */
   935 		 */
   835 		do_action( 'setted_transient', $transient, $value, $expiration );
   936 		do_action( 'setted_transient', $transient, $value, $expiration );
   836 	}
   937 	}
       
   938 
   837 	return $result;
   939 	return $result;
   838 }
   940 }
   839 
   941 
   840 /**
   942 /**
   841  * Deletes all expired transients.
   943  * Deletes all expired transients.
   866 			time()
   968 			time()
   867 		)
   969 		)
   868 	);
   970 	);
   869 
   971 
   870 	if ( ! is_multisite() ) {
   972 	if ( ! is_multisite() ) {
   871 		// non-Multisite stores site transients in the options table.
   973 		// Single site stores site transients in the options table.
   872 		$wpdb->query(
   974 		$wpdb->query(
   873 			$wpdb->prepare(
   975 			$wpdb->prepare(
   874 				"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
   976 				"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
   875 				WHERE a.option_name LIKE %s
   977 				WHERE a.option_name LIKE %s
   876 				AND a.option_name NOT LIKE %s
   978 				AND a.option_name NOT LIKE %s
   911 
  1013 
   912 	if ( ! is_admin() || wp_doing_ajax() ) {
  1014 	if ( ! is_admin() || wp_doing_ajax() ) {
   913 		return;
  1015 		return;
   914 	}
  1016 	}
   915 
  1017 
   916 	if ( ! $user_id = get_current_user_id() ) {
  1018 	$user_id = get_current_user_id();
       
  1019 	if ( ! $user_id ) {
   917 		return;
  1020 		return;
   918 	}
  1021 	}
   919 
  1022 
   920 	if ( ! is_user_member_of_blog() ) {
  1023 	if ( ! is_user_member_of_blog() ) {
   921 		return;
  1024 		return;
   924 	$settings = (string) get_user_option( 'user-settings', $user_id );
  1027 	$settings = (string) get_user_option( 'user-settings', $user_id );
   925 
  1028 
   926 	if ( isset( $_COOKIE[ 'wp-settings-' . $user_id ] ) ) {
  1029 	if ( isset( $_COOKIE[ 'wp-settings-' . $user_id ] ) ) {
   927 		$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE[ 'wp-settings-' . $user_id ] );
  1030 		$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE[ 'wp-settings-' . $user_id ] );
   928 
  1031 
   929 		// No change or both empty
  1032 		// No change or both empty.
   930 		if ( $cookie == $settings ) {
  1033 		if ( $cookie == $settings ) {
   931 			return;
  1034 			return;
   932 		}
  1035 		}
   933 
  1036 
   934 		$last_saved = (int) get_user_option( 'user-settings-time', $user_id );
  1037 		$last_saved = (int) get_user_option( 'user-settings-time', $user_id );
   935 		$current    = isset( $_COOKIE[ 'wp-settings-time-' . $user_id ] ) ? preg_replace( '/[^0-9]/', '', $_COOKIE[ 'wp-settings-time-' . $user_id ] ) : 0;
  1038 		$current    = isset( $_COOKIE[ 'wp-settings-time-' . $user_id ] ) ? preg_replace( '/[^0-9]/', '', $_COOKIE[ 'wp-settings-time-' . $user_id ] ) : 0;
   936 
  1039 
   937 		// The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
  1040 		// The cookie is newer than the saved value. Update the user_option and leave the cookie as-is.
   938 		if ( $current > $last_saved ) {
  1041 		if ( $current > $last_saved ) {
   939 			update_user_option( $user_id, 'user-settings', $cookie, false );
  1042 			update_user_option( $user_id, 'user-settings', $cookie, false );
   940 			update_user_option( $user_id, 'user-settings-time', time() - 5, false );
  1043 			update_user_option( $user_id, 'user-settings-time', time() - 5, false );
   941 			return;
  1044 			return;
   942 		}
  1045 		}
   948 	setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
  1051 	setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
   949 	$_COOKIE[ 'wp-settings-' . $user_id ] = $settings;
  1052 	$_COOKIE[ 'wp-settings-' . $user_id ] = $settings;
   950 }
  1053 }
   951 
  1054 
   952 /**
  1055 /**
   953  * Retrieve user interface setting value based on setting name.
  1056  * Retrieves user interface setting value based on setting name.
   954  *
  1057  *
   955  * @since 2.7.0
  1058  * @since 2.7.0
   956  *
  1059  *
   957  * @param string $name    The name of the setting.
  1060  * @param string $name    The name of the setting.
   958  * @param string $default Optional default value to return when $name is not set.
  1061  * @param string $default Optional default value to return when $name is not set.
   959  * @return mixed the last saved user setting or the default value/false if it doesn't exist.
  1062  * @return mixed The last saved user setting or the default value/false if it doesn't exist.
   960  */
  1063  */
   961 function get_user_setting( $name, $default = false ) {
  1064 function get_user_setting( $name, $default = false ) {
   962 	$all_user_settings = get_all_user_settings();
  1065 	$all_user_settings = get_all_user_settings();
   963 
  1066 
   964 	return isset( $all_user_settings[ $name ] ) ? $all_user_settings[ $name ] : $default;
  1067 	return isset( $all_user_settings[ $name ] ) ? $all_user_settings[ $name ] : $default;
   965 }
  1068 }
   966 
  1069 
   967 /**
  1070 /**
   968  * Add or update user interface setting.
  1071  * Adds or updates user interface setting.
   969  *
  1072  *
   970  * Both $name and $value can contain only ASCII letters, numbers and underscores.
  1073  * Both $name and $value can contain only ASCII letters, numbers, hyphens, and underscores.
   971  *
  1074  *
   972  * This function has to be used before any output has started as it calls setcookie().
  1075  * This function has to be used before any output has started as it calls setcookie().
   973  *
  1076  *
   974  * @since 2.8.0
  1077  * @since 2.8.0
   975  *
  1078  *
   976  * @param string $name  The name of the setting.
  1079  * @param string $name  The name of the setting.
   977  * @param string $value The value for the setting.
  1080  * @param string $value The value for the setting.
   978  * @return bool|null True if set successfully, false if not. Null if the current user can't be established.
  1081  * @return bool|null True if set successfully, false otherwise.
       
  1082  *                   Null if the current user is not a member of the site.
   979  */
  1083  */
   980 function set_user_setting( $name, $value ) {
  1084 function set_user_setting( $name, $value ) {
   981 	if ( headers_sent() ) {
  1085 	if ( headers_sent() ) {
   982 		return false;
  1086 		return false;
   983 	}
  1087 	}
   987 
  1091 
   988 	return wp_set_all_user_settings( $all_user_settings );
  1092 	return wp_set_all_user_settings( $all_user_settings );
   989 }
  1093 }
   990 
  1094 
   991 /**
  1095 /**
   992  * Delete user interface settings.
  1096  * Deletes user interface settings.
   993  *
  1097  *
   994  * Deleting settings would reset them to the defaults.
  1098  * Deleting settings would reset them to the defaults.
   995  *
  1099  *
   996  * This function has to be used before any output has started as it calls setcookie().
  1100  * This function has to be used before any output has started as it calls setcookie().
   997  *
  1101  *
   998  * @since 2.7.0
  1102  * @since 2.7.0
   999  *
  1103  *
  1000  * @param string $names The name or array of names of the setting to be deleted.
  1104  * @param string $names The name or array of names of the setting to be deleted.
  1001  * @return bool|null True if deleted successfully, false if not. Null if the current user can't be established.
  1105  * @return bool|null True if deleted successfully, false otherwise.
       
  1106  *                   Null if the current user is not a member of the site.
  1002  */
  1107  */
  1003 function delete_user_setting( $names ) {
  1108 function delete_user_setting( $names ) {
  1004 	if ( headers_sent() ) {
  1109 	if ( headers_sent() ) {
  1005 		return false;
  1110 		return false;
  1006 	}
  1111 	}
  1022 
  1127 
  1023 	return false;
  1128 	return false;
  1024 }
  1129 }
  1025 
  1130 
  1026 /**
  1131 /**
  1027  * Retrieve all user interface settings.
  1132  * Retrieves all user interface settings.
  1028  *
  1133  *
  1029  * @since 2.7.0
  1134  * @since 2.7.0
  1030  *
  1135  *
  1031  * @global array $_updated_user_settings
  1136  * @global array $_updated_user_settings
  1032  *
  1137  *
  1033  * @return array the last saved user settings or empty array.
  1138  * @return array The last saved user settings or empty array.
  1034  */
  1139  */
  1035 function get_all_user_settings() {
  1140 function get_all_user_settings() {
  1036 	global $_updated_user_settings;
  1141 	global $_updated_user_settings;
  1037 
  1142 
  1038 	if ( ! $user_id = get_current_user_id() ) {
  1143 	$user_id = get_current_user_id();
       
  1144 	if ( ! $user_id ) {
  1039 		return array();
  1145 		return array();
  1040 	}
  1146 	}
  1041 
  1147 
  1042 	if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
  1148 	if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
  1043 		return $_updated_user_settings;
  1149 		return $_updated_user_settings;
  1046 	$user_settings = array();
  1152 	$user_settings = array();
  1047 
  1153 
  1048 	if ( isset( $_COOKIE[ 'wp-settings-' . $user_id ] ) ) {
  1154 	if ( isset( $_COOKIE[ 'wp-settings-' . $user_id ] ) ) {
  1049 		$cookie = preg_replace( '/[^A-Za-z0-9=&_-]/', '', $_COOKIE[ 'wp-settings-' . $user_id ] );
  1155 		$cookie = preg_replace( '/[^A-Za-z0-9=&_-]/', '', $_COOKIE[ 'wp-settings-' . $user_id ] );
  1050 
  1156 
  1051 		if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
  1157 		if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char.
  1052 			parse_str( $cookie, $user_settings );
  1158 			parse_str( $cookie, $user_settings );
  1053 		}
  1159 		}
  1054 	} else {
  1160 	} else {
  1055 		$option = get_user_option( 'user-settings', $user_id );
  1161 		$option = get_user_option( 'user-settings', $user_id );
  1056 
  1162 
  1062 	$_updated_user_settings = $user_settings;
  1168 	$_updated_user_settings = $user_settings;
  1063 	return $user_settings;
  1169 	return $user_settings;
  1064 }
  1170 }
  1065 
  1171 
  1066 /**
  1172 /**
  1067  * Private. Set all user interface settings.
  1173  * Private. Sets all user interface settings.
  1068  *
  1174  *
  1069  * @since 2.8.0
  1175  * @since 2.8.0
  1070  * @access private
  1176  * @access private
  1071  *
  1177  *
  1072  * @global array $_updated_user_settings
  1178  * @global array $_updated_user_settings
  1073  *
  1179  *
  1074  * @param array $user_settings User settings.
  1180  * @param array $user_settings User settings.
  1075  * @return bool|null False if the current user can't be found, null if the current
  1181  * @return bool|null True if set successfully, false if the current user could not be found.
  1076  *                   user is not a super admin or a member of the site, otherwise true.
  1182  *                   Null if the current user is not a member of the site.
  1077  */
  1183  */
  1078 function wp_set_all_user_settings( $user_settings ) {
  1184 function wp_set_all_user_settings( $user_settings ) {
  1079 	global $_updated_user_settings;
  1185 	global $_updated_user_settings;
  1080 
  1186 
  1081 	if ( ! $user_id = get_current_user_id() ) {
  1187 	$user_id = get_current_user_id();
       
  1188 	if ( ! $user_id ) {
  1082 		return false;
  1189 		return false;
  1083 	}
  1190 	}
  1084 
  1191 
  1085 	if ( ! is_user_member_of_blog() ) {
  1192 	if ( ! is_user_member_of_blog() ) {
  1086 		return;
  1193 		return;
  1104 
  1211 
  1105 	return true;
  1212 	return true;
  1106 }
  1213 }
  1107 
  1214 
  1108 /**
  1215 /**
  1109  * Delete the user settings of the current user.
  1216  * Deletes the user settings of the current user.
  1110  *
  1217  *
  1111  * @since 2.7.0
  1218  * @since 2.7.0
  1112  */
  1219  */
  1113 function delete_all_user_settings() {
  1220 function delete_all_user_settings() {
  1114 	if ( ! $user_id = get_current_user_id() ) {
  1221 	$user_id = get_current_user_id();
       
  1222 	if ( ! $user_id ) {
  1115 		return;
  1223 		return;
  1116 	}
  1224 	}
  1117 
  1225 
  1118 	update_user_option( $user_id, 'user-settings', '', false );
  1226 	update_user_option( $user_id, 'user-settings', '', false );
  1119 	setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
  1227 	setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
  1126  * @since 4.4.0 The `$use_cache` parameter was deprecated.
  1234  * @since 4.4.0 The `$use_cache` parameter was deprecated.
  1127  * @since 4.4.0 Modified into wrapper for get_network_option()
  1235  * @since 4.4.0 Modified into wrapper for get_network_option()
  1128  *
  1236  *
  1129  * @see get_network_option()
  1237  * @see get_network_option()
  1130  *
  1238  *
  1131  * @param string $option     Name of option to retrieve. Expected to not be SQL-escaped.
  1239  * @param string $option     Name of the option to retrieve. Expected to not be SQL-escaped.
  1132  * @param mixed  $default    Optional value to return if option doesn't exist. Default false.
  1240  * @param mixed  $default    Optional. Value to return if the option doesn't exist. Default false.
  1133  * @param bool   $deprecated Whether to use cache. Multisite only. Always set to true.
  1241  * @param bool   $deprecated Whether to use cache. Multisite only. Always set to true.
  1134  * @return mixed Value set for the option.
  1242  * @return mixed Value set for the option.
  1135  */
  1243  */
  1136 function get_site_option( $option, $default = false, $deprecated = true ) {
  1244 function get_site_option( $option, $default = false, $deprecated = true ) {
  1137 	return get_network_option( null, $option, $default );
  1245 	return get_network_option( null, $option, $default );
  1138 }
  1246 }
  1139 
  1247 
  1140 /**
  1248 /**
  1141  * Add a new option for the current network.
  1249  * Adds a new option for the current network.
  1142  *
  1250  *
  1143  * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
  1251  * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
  1144  *
  1252  *
  1145  * @since 2.8.0
  1253  * @since 2.8.0
  1146  * @since 4.4.0 Modified into wrapper for add_network_option()
  1254  * @since 4.4.0 Modified into wrapper for add_network_option()
  1147  *
  1255  *
  1148  * @see add_network_option()
  1256  * @see add_network_option()
  1149  *
  1257  *
  1150  * @param string $option Name of option to add. Expected to not be SQL-escaped.
  1258  * @param string $option Name of the option to add. Expected to not be SQL-escaped.
  1151  * @param mixed  $value  Option value, can be anything. Expected to not be SQL-escaped.
  1259  * @param mixed  $value  Option value, can be anything. Expected to not be SQL-escaped.
  1152  * @return bool False if the option was not added. True if the option was added.
  1260  * @return bool True if the option was added, false otherwise.
  1153  */
  1261  */
  1154 function add_site_option( $option, $value ) {
  1262 function add_site_option( $option, $value ) {
  1155 	return add_network_option( null, $option, $value );
  1263 	return add_network_option( null, $option, $value );
  1156 }
  1264 }
  1157 
  1265 
  1161  * @since 2.8.0
  1269  * @since 2.8.0
  1162  * @since 4.4.0 Modified into wrapper for delete_network_option()
  1270  * @since 4.4.0 Modified into wrapper for delete_network_option()
  1163  *
  1271  *
  1164  * @see delete_network_option()
  1272  * @see delete_network_option()
  1165  *
  1273  *
  1166  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
  1274  * @param string $option Name of the option to delete. Expected to not be SQL-escaped.
  1167  * @return bool True, if succeed. False, if failure.
  1275  * @return bool True if the option was deleted, false otherwise.
  1168  */
  1276  */
  1169 function delete_site_option( $option ) {
  1277 function delete_site_option( $option ) {
  1170 	return delete_network_option( null, $option );
  1278 	return delete_network_option( null, $option );
  1171 }
  1279 }
  1172 
  1280 
  1173 /**
  1281 /**
  1174  * Update the value of an option that was already added for the current network.
  1282  * Updates the value of an option that was already added for the current network.
  1175  *
  1283  *
  1176  * @since 2.8.0
  1284  * @since 2.8.0
  1177  * @since 4.4.0 Modified into wrapper for update_network_option()
  1285  * @since 4.4.0 Modified into wrapper for update_network_option()
  1178  *
  1286  *
  1179  * @see update_network_option()
  1287  * @see update_network_option()
  1180  *
  1288  *
  1181  * @param string $option Name of option. Expected to not be SQL-escaped.
  1289  * @param string $option Name of the option. Expected to not be SQL-escaped.
  1182  * @param mixed  $value  Option value. Expected to not be SQL-escaped.
  1290  * @param mixed  $value  Option value. Expected to not be SQL-escaped.
  1183  * @return bool False if value was not updated. True if value was updated.
  1291  * @return bool True if the value was updated, false otherwise.
  1184  */
  1292  */
  1185 function update_site_option( $option, $value ) {
  1293 function update_site_option( $option, $value ) {
  1186 	return update_network_option( null, $option, $value );
  1294 	return update_network_option( null, $option, $value );
  1187 }
  1295 }
  1188 
  1296 
  1189 /**
  1297 /**
  1190  * Retrieve a network's option value based on the option name.
  1298  * Retrieves a network's option value based on the option name.
  1191  *
  1299  *
  1192  * @since 4.4.0
  1300  * @since 4.4.0
  1193  *
  1301  *
  1194  * @see get_option()
  1302  * @see get_option()
  1195  *
  1303  *
  1196  * @global wpdb $wpdb WordPress database abstraction object.
  1304  * @global wpdb $wpdb WordPress database abstraction object.
  1197  *
  1305  *
  1198  * @param int      $network_id ID of the network. Can be null to default to the current network ID.
  1306  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
  1199  * @param string   $option     Name of option to retrieve. Expected to not be SQL-escaped.
  1307  * @param string $option     Name of the option to retrieve. Expected to not be SQL-escaped.
  1200  * @param mixed    $default    Optional. Value to return if the option doesn't exist. Default false.
  1308  * @param mixed  $default    Optional. Value to return if the option doesn't exist. Default false.
  1201  * @return mixed Value set for the option.
  1309  * @return mixed Value set for the option.
  1202  */
  1310  */
  1203 function get_network_option( $network_id, $option, $default = false ) {
  1311 function get_network_option( $network_id, $option, $default = false ) {
  1204 	global $wpdb;
  1312 	global $wpdb;
  1205 
  1313 
  1213 	if ( ! $network_id ) {
  1321 	if ( ! $network_id ) {
  1214 		$network_id = get_current_network_id();
  1322 		$network_id = get_current_network_id();
  1215 	}
  1323 	}
  1216 
  1324 
  1217 	/**
  1325 	/**
  1218 	 * Filters an existing network option before it is retrieved.
  1326 	 * Filters the value of an existing network option before it is retrieved.
  1219 	 *
  1327 	 *
  1220 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1328 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1221 	 *
  1329 	 *
  1222 	 * Passing a truthy value to the filter will effectively short-circuit retrieval,
  1330 	 * Returning a truthy value from the filter will effectively short-circuit retrieval
  1223 	 * returning the passed value instead.
  1331 	 * and return the passed value instead.
  1224 	 *
  1332 	 *
  1225 	 * @since 2.9.0 As 'pre_site_option_' . $key
  1333 	 * @since 2.9.0 As 'pre_site_option_' . $key
  1226 	 * @since 3.0.0
  1334 	 * @since 3.0.0
  1227 	 * @since 4.4.0 The `$option` parameter was added.
  1335 	 * @since 4.4.0 The `$option` parameter was added.
  1228 	 * @since 4.7.0 The `$network_id` parameter was added.
  1336 	 * @since 4.7.0 The `$network_id` parameter was added.
  1229 	 * @since 4.9.0 The `$default` parameter was added.
  1337 	 * @since 4.9.0 The `$default` parameter was added.
  1230 	 *
  1338 	 *
  1231 	 * @param mixed  $pre_option The value to return instead of the option value. This differs from
  1339 	 * @param mixed  $pre_option The value to return instead of the option value. This differs
  1232 	 *                           `$default`, which is used as the fallback value in the event the
  1340 	 *                           from `$default`, which is used as the fallback value in the event
  1233 	 *                           option doesn't exist elsewhere in get_network_option(). Default
  1341 	 *                           the option doesn't exist elsewhere in get_network_option().
  1234 	 *                           is false (to skip past the short-circuit).
  1342 	 *                           Default false (to skip past the short-circuit).
  1235 	 * @param string $option     Option name.
  1343 	 * @param string $option     Option name.
  1236 	 * @param int    $network_id ID of the network.
  1344 	 * @param int    $network_id ID of the network.
  1237 	 * @param mixed  $default    The fallback value to return if the option does not exist.
  1345 	 * @param mixed  $default    The fallback value to return if the option does not exist.
  1238 	 *                           Default is false.
  1346 	 *                           Default false.
  1239 	 */
  1347 	 */
  1240 	$pre = apply_filters( "pre_site_option_{$option}", false, $option, $network_id, $default );
  1348 	$pre = apply_filters( "pre_site_option_{$option}", false, $option, $network_id, $default );
  1241 
  1349 
  1242 	if ( false !== $pre ) {
  1350 	if ( false !== $pre ) {
  1243 		return $pre;
  1351 		return $pre;
  1244 	}
  1352 	}
  1245 
  1353 
  1246 	// prevent non-existent options from triggering multiple queries
  1354 	// Prevent non-existent options from triggering multiple queries.
  1247 	$notoptions_key = "$network_id:notoptions";
  1355 	$notoptions_key = "$network_id:notoptions";
  1248 	$notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
  1356 	$notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
  1249 
  1357 
  1250 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
  1358 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
  1251 
  1359 
  1275 		$value     = wp_cache_get( $cache_key, 'site-options' );
  1383 		$value     = wp_cache_get( $cache_key, 'site-options' );
  1276 
  1384 
  1277 		if ( ! isset( $value ) || false === $value ) {
  1385 		if ( ! isset( $value ) || false === $value ) {
  1278 			$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
  1386 			$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
  1279 
  1387 
  1280 			// Has to be get_row instead of get_var because of funkiness with 0, false, null values
  1388 			// Has to be get_row() instead of get_var() because of funkiness with 0, false, null values.
  1281 			if ( is_object( $row ) ) {
  1389 			if ( is_object( $row ) ) {
  1282 				$value = $row->meta_value;
  1390 				$value = $row->meta_value;
  1283 				$value = maybe_unserialize( $value );
  1391 				$value = maybe_unserialize( $value );
  1284 				wp_cache_set( $cache_key, $value, 'site-options' );
  1392 				wp_cache_set( $cache_key, $value, 'site-options' );
  1285 			} else {
  1393 			} else {
  1286 				if ( ! is_array( $notoptions ) ) {
  1394 				if ( ! is_array( $notoptions ) ) {
  1287 					$notoptions = array();
  1395 					$notoptions = array();
  1288 				}
  1396 				}
       
  1397 
  1289 				$notoptions[ $option ] = true;
  1398 				$notoptions[ $option ] = true;
  1290 				wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1399 				wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1291 
  1400 
  1292 				/** This filter is documented in wp-includes/option.php */
  1401 				/** This filter is documented in wp-includes/option.php */
  1293 				$value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
  1402 				$value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
  1316 	 */
  1425 	 */
  1317 	return apply_filters( "site_option_{$option}", $value, $option, $network_id );
  1426 	return apply_filters( "site_option_{$option}", $value, $option, $network_id );
  1318 }
  1427 }
  1319 
  1428 
  1320 /**
  1429 /**
  1321  * Add a new network option.
  1430  * Adds a new network option.
  1322  *
  1431  *
  1323  * Existing options will not be updated.
  1432  * Existing options will not be updated.
  1324  *
  1433  *
  1325  * @since 4.4.0
  1434  * @since 4.4.0
  1326  *
  1435  *
  1327  * @see add_option()
  1436  * @see add_option()
  1328  *
  1437  *
  1329  * @global wpdb $wpdb WordPress database abstraction object.
  1438  * @global wpdb $wpdb WordPress database abstraction object.
  1330  *
  1439  *
  1331  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
  1440  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
  1332  * @param string $option     Name of option to add. Expected to not be SQL-escaped.
  1441  * @param string $option     Name of the option to add. Expected to not be SQL-escaped.
  1333  * @param mixed  $value      Option value, can be anything. Expected to not be SQL-escaped.
  1442  * @param mixed  $value      Option value, can be anything. Expected to not be SQL-escaped.
  1334  * @return bool False if option was not added and true if option was added.
  1443  * @return bool True if the option was added, false otherwise.
  1335  */
  1444  */
  1336 function add_network_option( $network_id, $option, $value ) {
  1445 function add_network_option( $network_id, $option, $value ) {
  1337 	global $wpdb;
  1446 	global $wpdb;
  1338 
  1447 
  1339 	if ( $network_id && ! is_numeric( $network_id ) ) {
  1448 	if ( $network_id && ! is_numeric( $network_id ) ) {
  1370 	if ( ! is_multisite() ) {
  1479 	if ( ! is_multisite() ) {
  1371 		$result = add_option( $option, $value, '', 'no' );
  1480 		$result = add_option( $option, $value, '', 'no' );
  1372 	} else {
  1481 	} else {
  1373 		$cache_key = "$network_id:$option";
  1482 		$cache_key = "$network_id:$option";
  1374 
  1483 
  1375 		// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
  1484 		// Make sure the option doesn't already exist.
       
  1485 		// We can check the 'notoptions' cache before we ask for a DB query.
  1376 		$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
  1486 		$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
       
  1487 
  1377 		if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
  1488 		if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
  1378 			if ( false !== get_network_option( $network_id, $option, false ) ) {
  1489 			if ( false !== get_network_option( $network_id, $option, false ) ) {
  1379 				return false;
  1490 				return false;
  1380 			}
  1491 			}
  1381 		}
  1492 		}
  1396 			return false;
  1507 			return false;
  1397 		}
  1508 		}
  1398 
  1509 
  1399 		wp_cache_set( $cache_key, $value, 'site-options' );
  1510 		wp_cache_set( $cache_key, $value, 'site-options' );
  1400 
  1511 
  1401 		// This option exists now
  1512 		// This option exists now.
  1402 		$notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
  1513 		$notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // Yes, again... we need it to be fresh.
       
  1514 
  1403 		if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
  1515 		if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
  1404 			unset( $notoptions[ $option ] );
  1516 			unset( $notoptions[ $option ] );
  1405 			wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1517 			wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1406 		}
  1518 		}
  1407 	}
  1519 	}
  1449  * @see delete_option()
  1561  * @see delete_option()
  1450  *
  1562  *
  1451  * @global wpdb $wpdb WordPress database abstraction object.
  1563  * @global wpdb $wpdb WordPress database abstraction object.
  1452  *
  1564  *
  1453  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
  1565  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
  1454  * @param string $option     Name of option to remove. Expected to not be SQL-escaped.
  1566  * @param string $option     Name of the option to delete. Expected to not be SQL-escaped.
  1455  * @return bool True, if succeed. False, if failure.
  1567  * @return bool True if the option was deleted, false otherwise.
  1456  */
  1568  */
  1457 function delete_network_option( $network_id, $option ) {
  1569 function delete_network_option( $network_id, $option ) {
  1458 	global $wpdb;
  1570 	global $wpdb;
  1459 
  1571 
  1460 	if ( $network_id && ! is_numeric( $network_id ) ) {
  1572 	if ( $network_id && ! is_numeric( $network_id ) ) {
  1533 
  1645 
  1534 	return false;
  1646 	return false;
  1535 }
  1647 }
  1536 
  1648 
  1537 /**
  1649 /**
  1538  * Update the value of a network option that was already added.
  1650  * Updates the value of a network option that was already added.
  1539  *
  1651  *
  1540  * @since 4.4.0
  1652  * @since 4.4.0
  1541  *
  1653  *
  1542  * @see update_option()
  1654  * @see update_option()
  1543  *
  1655  *
  1544  * @global wpdb $wpdb WordPress database abstraction object.
  1656  * @global wpdb $wpdb WordPress database abstraction object.
  1545  *
  1657  *
  1546  * @param int      $network_id ID of the network. Can be null to default to the current network ID.
  1658  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
  1547  * @param string   $option     Name of option. Expected to not be SQL-escaped.
  1659  * @param string $option     Name of the option. Expected to not be SQL-escaped.
  1548  * @param mixed    $value      Option value. Expected to not be SQL-escaped.
  1660  * @param mixed  $value      Option value. Expected to not be SQL-escaped.
  1549  * @return bool False if value was not updated and true if value was updated.
  1661  * @return bool True if the value was updated, false otherwise.
  1550  */
  1662  */
  1551 function update_network_option( $network_id, $option, $value ) {
  1663 function update_network_option( $network_id, $option, $value ) {
  1552 	global $wpdb;
  1664 	global $wpdb;
  1553 
  1665 
  1554 	if ( $network_id && ! is_numeric( $network_id ) ) {
  1666 	if ( $network_id && ! is_numeric( $network_id ) ) {
  1600 		return add_network_option( $network_id, $option, $value );
  1712 		return add_network_option( $network_id, $option, $value );
  1601 	}
  1713 	}
  1602 
  1714 
  1603 	$notoptions_key = "$network_id:notoptions";
  1715 	$notoptions_key = "$network_id:notoptions";
  1604 	$notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
  1716 	$notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
       
  1717 
  1605 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
  1718 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
  1606 		unset( $notoptions[ $option ] );
  1719 		unset( $notoptions[ $option ] );
  1607 		wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1720 		wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1608 	}
  1721 	}
  1609 
  1722 
  1664 
  1777 
  1665 	return false;
  1778 	return false;
  1666 }
  1779 }
  1667 
  1780 
  1668 /**
  1781 /**
  1669  * Delete a site transient.
  1782  * Deletes a site transient.
  1670  *
  1783  *
  1671  * @since 2.9.0
  1784  * @since 2.9.0
  1672  *
  1785  *
  1673  * @param string $transient Transient name. Expected to not be SQL-escaped.
  1786  * @param string $transient Transient name. Expected to not be SQL-escaped.
  1674  * @return bool True if successful, false otherwise
  1787  * @return bool True if the transient was deleted, false otherwise.
  1675  */
  1788  */
  1676 function delete_site_transient( $transient ) {
  1789 function delete_site_transient( $transient ) {
  1677 
  1790 
  1678 	/**
  1791 	/**
  1679 	 * Fires immediately before a specific site transient is deleted.
  1792 	 * Fires immediately before a specific site transient is deleted.
  1690 		$result = wp_cache_delete( $transient, 'site-transient' );
  1803 		$result = wp_cache_delete( $transient, 'site-transient' );
  1691 	} else {
  1804 	} else {
  1692 		$option_timeout = '_site_transient_timeout_' . $transient;
  1805 		$option_timeout = '_site_transient_timeout_' . $transient;
  1693 		$option         = '_site_transient_' . $transient;
  1806 		$option         = '_site_transient_' . $transient;
  1694 		$result         = delete_site_option( $option );
  1807 		$result         = delete_site_option( $option );
       
  1808 
  1695 		if ( $result ) {
  1809 		if ( $result ) {
  1696 			delete_site_option( $option_timeout );
  1810 			delete_site_option( $option_timeout );
  1697 		}
  1811 		}
  1698 	}
  1812 	}
       
  1813 
  1699 	if ( $result ) {
  1814 	if ( $result ) {
  1700 
  1815 
  1701 		/**
  1816 		/**
  1702 		 * Fires after a transient is deleted.
  1817 		 * Fires after a transient is deleted.
  1703 		 *
  1818 		 *
  1710 
  1825 
  1711 	return $result;
  1826 	return $result;
  1712 }
  1827 }
  1713 
  1828 
  1714 /**
  1829 /**
  1715  * Get the value of a site transient.
  1830  * Retrieves the value of a site transient.
  1716  *
  1831  *
  1717  * If the transient does not exist, does not have a value, or has expired,
  1832  * If the transient does not exist, does not have a value, or has expired,
  1718  * then the return value will be false.
  1833  * then the return value will be false.
  1719  *
  1834  *
  1720  * @since 2.9.0
  1835  * @since 2.9.0
  1725  * @return mixed Value of transient.
  1840  * @return mixed Value of transient.
  1726  */
  1841  */
  1727 function get_site_transient( $transient ) {
  1842 function get_site_transient( $transient ) {
  1728 
  1843 
  1729 	/**
  1844 	/**
  1730 	 * Filters the value of an existing site transient.
  1845 	 * Filters the value of an existing site transient before it is retrieved.
  1731 	 *
  1846 	 *
  1732 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
  1847 	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
  1733 	 *
  1848 	 *
  1734 	 * Passing a truthy value to the filter will effectively short-circuit retrieval,
  1849 	 * Returning a truthy value from the filter will effectively short-circuit retrieval
  1735 	 * returning the passed value instead.
  1850 	 * and return the passed value instead.
  1736 	 *
  1851 	 *
  1737 	 * @since 2.9.0
  1852 	 * @since 2.9.0
  1738 	 * @since 4.4.0 The `$transient` parameter was added.
  1853 	 * @since 4.4.0 The `$transient` parameter was added.
  1739 	 *
  1854 	 *
  1740 	 * @param mixed  $pre_site_transient The default value to return if the site transient does not exist.
  1855 	 * @param mixed  $pre_site_transient The default value to return if the site transient does not exist.
  1741 	 *                                   Any value other than false will short-circuit the retrieval
  1856 	 *                                   Any value other than false will short-circuit the retrieval
  1742 	 *                                   of the transient, and return the returned value.
  1857 	 *                                   of the transient, and return that value.
  1743 	 * @param string $transient          Transient name.
  1858 	 * @param string $transient          Transient name.
  1744 	 */
  1859 	 */
  1745 	$pre = apply_filters( "pre_site_transient_{$transient}", false, $transient );
  1860 	$pre = apply_filters( "pre_site_transient_{$transient}", false, $transient );
  1746 
  1861 
  1747 	if ( false !== $pre ) {
  1862 	if ( false !== $pre ) {
  1752 		$value = wp_cache_get( $transient, 'site-transient' );
  1867 		$value = wp_cache_get( $transient, 'site-transient' );
  1753 	} else {
  1868 	} else {
  1754 		// Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
  1869 		// Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
  1755 		$no_timeout       = array( 'update_core', 'update_plugins', 'update_themes' );
  1870 		$no_timeout       = array( 'update_core', 'update_plugins', 'update_themes' );
  1756 		$transient_option = '_site_transient_' . $transient;
  1871 		$transient_option = '_site_transient_' . $transient;
  1757 		if ( ! in_array( $transient, $no_timeout ) ) {
  1872 		if ( ! in_array( $transient, $no_timeout, true ) ) {
  1758 			$transient_timeout = '_site_transient_timeout_' . $transient;
  1873 			$transient_timeout = '_site_transient_timeout_' . $transient;
  1759 			$timeout           = get_site_option( $transient_timeout );
  1874 			$timeout           = get_site_option( $transient_timeout );
  1760 			if ( false !== $timeout && $timeout < time() ) {
  1875 			if ( false !== $timeout && $timeout < time() ) {
  1761 				delete_site_option( $transient_option );
  1876 				delete_site_option( $transient_option );
  1762 				delete_site_option( $transient_timeout );
  1877 				delete_site_option( $transient_timeout );
  1782 	 */
  1897 	 */
  1783 	return apply_filters( "site_transient_{$transient}", $value, $transient );
  1898 	return apply_filters( "site_transient_{$transient}", $value, $transient );
  1784 }
  1899 }
  1785 
  1900 
  1786 /**
  1901 /**
  1787  * Set/update the value of a site transient.
  1902  * Sets/updates the value of a site transient.
  1788  *
  1903  *
  1789  * You do not need to serialize values, if the value needs to be serialize, then
  1904  * You do not need to serialize values. If the value needs to be serialized,
  1790  * it will be serialized before it is set.
  1905  * then it will be serialized before it is set.
  1791  *
  1906  *
  1792  * @since 2.9.0
  1907  * @since 2.9.0
  1793  *
  1908  *
  1794  * @see set_transient()
  1909  * @see set_transient()
  1795  *
  1910  *
  1796  * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
  1911  * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
  1797  *                           167 characters or fewer in length.
  1912  *                           167 characters or fewer in length.
  1798  * @param mixed  $value      Transient value. Expected to not be SQL-escaped.
  1913  * @param mixed  $value      Transient value. Expected to not be SQL-escaped.
  1799  * @param int    $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
  1914  * @param int    $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
  1800  * @return bool False if value was not set and true if value was set.
  1915  * @return bool True if the value was set, false otherwise.
  1801  */
  1916  */
  1802 function set_site_transient( $transient, $value, $expiration = 0 ) {
  1917 function set_site_transient( $transient, $value, $expiration = 0 ) {
  1803 
  1918 
  1804 	/**
  1919 	/**
  1805 	 * Filters the value of a specific site transient before it is set.
  1920 	 * Filters the value of a specific site transient before it is set.
  1832 	if ( wp_using_ext_object_cache() ) {
  1947 	if ( wp_using_ext_object_cache() ) {
  1833 		$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
  1948 		$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
  1834 	} else {
  1949 	} else {
  1835 		$transient_timeout = '_site_transient_timeout_' . $transient;
  1950 		$transient_timeout = '_site_transient_timeout_' . $transient;
  1836 		$option            = '_site_transient_' . $transient;
  1951 		$option            = '_site_transient_' . $transient;
       
  1952 
  1837 		if ( false === get_site_option( $option ) ) {
  1953 		if ( false === get_site_option( $option ) ) {
  1838 			if ( $expiration ) {
  1954 			if ( $expiration ) {
  1839 				add_site_option( $transient_timeout, time() + $expiration );
  1955 				add_site_option( $transient_timeout, time() + $expiration );
  1840 			}
  1956 			}
  1841 			$result = add_site_option( $option, $value );
  1957 			$result = add_site_option( $option, $value );
  1844 				update_site_option( $transient_timeout, time() + $expiration );
  1960 				update_site_option( $transient_timeout, time() + $expiration );
  1845 			}
  1961 			}
  1846 			$result = update_site_option( $option, $value );
  1962 			$result = update_site_option( $option, $value );
  1847 		}
  1963 		}
  1848 	}
  1964 	}
       
  1965 
  1849 	if ( $result ) {
  1966 	if ( $result ) {
  1850 
  1967 
  1851 		/**
  1968 		/**
  1852 		 * Fires after the value for a specific site transient has been set.
  1969 		 * Fires after the value for a specific site transient has been set.
  1853 		 *
  1970 		 *
  1871 		 * @param mixed  $value      Site transient value.
  1988 		 * @param mixed  $value      Site transient value.
  1872 		 * @param int    $expiration Time until expiration in seconds.
  1989 		 * @param int    $expiration Time until expiration in seconds.
  1873 		 */
  1990 		 */
  1874 		do_action( 'setted_site_transient', $transient, $value, $expiration );
  1991 		do_action( 'setted_site_transient', $transient, $value, $expiration );
  1875 	}
  1992 	}
       
  1993 
  1876 	return $result;
  1994 	return $result;
  1877 }
  1995 }
  1878 
  1996 
  1879 /**
  1997 /**
  1880  * Register default settings available in WordPress.
  1998  * Registers default settings available in WordPress.
  1881  *
  1999  *
  1882  * The settings registered here are primarily useful for the REST API, so this
  2000  * The settings registered here are primarily useful for the REST API, so this
  1883  * does not encompass all settings available in WordPress.
  2001  * does not encompass all settings available in WordPress.
  1884  *
  2002  *
  1885  * @since 4.7.0
  2003  * @since 4.7.0
  2062 				'schema' => array(
  2180 				'schema' => array(
  2063 					'enum' => array( 'open', 'closed' ),
  2181 					'enum' => array( 'open', 'closed' ),
  2064 				),
  2182 				),
  2065 			),
  2183 			),
  2066 			'type'         => 'string',
  2184 			'type'         => 'string',
  2067 			'description'  => __( 'Allow people to post comments on new articles.' ),
  2185 			'description'  => __( 'Allow people to submit comments on new posts.' ),
  2068 		)
  2186 		)
  2069 	);
  2187 	);
  2070 }
  2188 }
  2071 
  2189 
  2072 /**
  2190 /**
  2073  * Register a setting and its data.
  2191  * Registers a setting and its data.
  2074  *
  2192  *
  2075  * @since 2.7.0
  2193  * @since 2.7.0
  2076  * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
  2194  * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
  2077  *
  2195  * @since 5.5.0 `$new_whitelist_options` was renamed to `$new_allowed_options`.
  2078  * @global array $new_whitelist_options
  2196  *              Please consider writing more inclusive code.
       
  2197  *
       
  2198  * @global array $new_allowed_options
  2079  * @global array $wp_registered_settings
  2199  * @global array $wp_registered_settings
  2080  *
  2200  *
  2081  * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
  2201  * @param string $option_group A settings group name. Should correspond to an allowed option key name.
  2082  *  Default whitelisted option key names include "general," "discussion," and "reading," among others.
  2202  *                             Default allowed option key names include 'general', 'discussion', 'media',
       
  2203  *                             'reading', 'writing', 'misc', 'options', and 'privacy'.
  2083  * @param string $option_name The name of an option to sanitize and save.
  2204  * @param string $option_name The name of an option to sanitize and save.
  2084  * @param array  $args {
  2205  * @param array  $args {
  2085  *     Data used to describe the setting when registered.
  2206  *     Data used to describe the setting when registered.
  2086  *
  2207  *
  2087  *     @type string   $type              The type of data associated with this setting.
  2208  *     @type string     $type              The type of data associated with this setting.
  2088  *                                       Valid values are 'string', 'boolean', 'integer', and 'number'.
  2209  *                                         Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
  2089  *     @type string   $description       A description of the data attached to this setting.
  2210  *     @type string     $description       A description of the data attached to this setting.
  2090  *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
  2211  *     @type callable   $sanitize_callback A callback function that sanitizes the option's value.
  2091  *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
  2212  *     @type bool|array $show_in_rest      Whether data associated with this setting should be included in the REST API.
  2092  *     @type mixed    $default           Default value when calling `get_option()`.
  2213  *                                         When registering complex settings, this argument may optionally be an
       
  2214  *                                         array with a 'schema' key.
       
  2215  *     @type mixed      $default           Default value when calling `get_option()`.
  2093  * }
  2216  * }
  2094  */
  2217  */
  2095 function register_setting( $option_group, $option_name, $args = array() ) {
  2218 function register_setting( $option_group, $option_name, $args = array() ) {
  2096 	global $new_whitelist_options, $wp_registered_settings;
  2219 	global $new_allowed_options, $wp_registered_settings;
       
  2220 
       
  2221 	/*
       
  2222 	 * In 5.5.0, the `$new_whitelist_options` global variable was renamed to `$new_allowed_options`.
       
  2223 	 * Please consider writing more inclusive code.
       
  2224 	 */
       
  2225 	$GLOBALS['new_whitelist_options'] = &$new_allowed_options;
  2097 
  2226 
  2098 	$defaults = array(
  2227 	$defaults = array(
  2099 		'type'              => 'string',
  2228 		'type'              => 'string',
  2100 		'group'             => $option_group,
  2229 		'group'             => $option_group,
  2101 		'description'       => '',
  2230 		'description'       => '',
  2119 	 * @param array  $defaults     Array of default arguments.
  2248 	 * @param array  $defaults     Array of default arguments.
  2120 	 * @param string $option_group Setting group.
  2249 	 * @param string $option_group Setting group.
  2121 	 * @param string $option_name  Setting name.
  2250 	 * @param string $option_name  Setting name.
  2122 	 */
  2251 	 */
  2123 	$args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
  2252 	$args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
       
  2253 
  2124 	$args = wp_parse_args( $args, $defaults );
  2254 	$args = wp_parse_args( $args, $defaults );
       
  2255 
       
  2256 	// Require an item schema when registering settings with an array type.
       
  2257 	if ( false !== $args['show_in_rest'] && 'array' === $args['type'] && ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) ) {
       
  2258 		_doing_it_wrong( __FUNCTION__, __( 'When registering an "array" setting to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items".' ), '5.4.0' );
       
  2259 	}
  2125 
  2260 
  2126 	if ( ! is_array( $wp_registered_settings ) ) {
  2261 	if ( ! is_array( $wp_registered_settings ) ) {
  2127 		$wp_registered_settings = array();
  2262 		$wp_registered_settings = array();
  2128 	}
  2263 	}
  2129 
  2264 
  2130 	if ( 'misc' == $option_group ) {
  2265 	if ( 'misc' === $option_group ) {
  2131 		_deprecated_argument(
  2266 		_deprecated_argument(
  2132 			__FUNCTION__,
  2267 			__FUNCTION__,
  2133 			'3.0.0',
  2268 			'3.0.0',
  2134 			/* translators: %s: misc */
       
  2135 			sprintf(
  2269 			sprintf(
       
  2270 				/* translators: %s: misc */
  2136 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2271 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2137 				'misc'
  2272 				'misc'
  2138 			)
  2273 			)
  2139 		);
  2274 		);
  2140 		$option_group = 'general';
  2275 		$option_group = 'general';
  2141 	}
  2276 	}
  2142 
  2277 
  2143 	if ( 'privacy' == $option_group ) {
  2278 	if ( 'privacy' === $option_group ) {
  2144 		_deprecated_argument(
  2279 		_deprecated_argument(
  2145 			__FUNCTION__,
  2280 			__FUNCTION__,
  2146 			'3.5.0',
  2281 			'3.5.0',
  2147 			/* translators: %s: privacy */
       
  2148 			sprintf(
  2282 			sprintf(
       
  2283 				/* translators: %s: privacy */
  2149 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2284 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2150 				'privacy'
  2285 				'privacy'
  2151 			)
  2286 			)
  2152 		);
  2287 		);
  2153 		$option_group = 'reading';
  2288 		$option_group = 'reading';
  2154 	}
  2289 	}
  2155 
  2290 
  2156 	$new_whitelist_options[ $option_group ][] = $option_name;
  2291 	$new_allowed_options[ $option_group ][] = $option_name;
       
  2292 
  2157 	if ( ! empty( $args['sanitize_callback'] ) ) {
  2293 	if ( ! empty( $args['sanitize_callback'] ) ) {
  2158 		add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
  2294 		add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
  2159 	}
  2295 	}
  2160 	if ( array_key_exists( 'default', $args ) ) {
  2296 	if ( array_key_exists( 'default', $args ) ) {
  2161 		add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 );
  2297 		add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 );
  2162 	}
  2298 	}
  2163 
  2299 
       
  2300 	/**
       
  2301 	 * Fires immediately before the setting is registered but after its filters are in place.
       
  2302 	 *
       
  2303 	 * @since 5.5.0
       
  2304 	 *
       
  2305 	 * @param string $option_group Setting group.
       
  2306 	 * @param string $option_name  Setting name.
       
  2307 	 * @param array  $args         Array of setting registration arguments.
       
  2308 	 */
       
  2309 	do_action( 'register_setting', $option_group, $option_name, $args );
       
  2310 
  2164 	$wp_registered_settings[ $option_name ] = $args;
  2311 	$wp_registered_settings[ $option_name ] = $args;
  2165 }
  2312 }
  2166 
  2313 
  2167 /**
  2314 /**
  2168  * Unregister a setting.
  2315  * Unregisters a setting.
  2169  *
  2316  *
  2170  * @since 2.7.0
  2317  * @since 2.7.0
  2171  * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead.
  2318  * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead.
  2172  *
  2319  * @since 5.5.0 `$new_whitelist_options` was renamed to `$new_allowed_options`.
  2173  * @global array $new_whitelist_options
  2320  *              Please consider writing more inclusive code.
       
  2321  *
       
  2322  * @global array $new_allowed_options
  2174  * @global array $wp_registered_settings
  2323  * @global array $wp_registered_settings
  2175  *
  2324  *
  2176  * @param string   $option_group      The settings group name used during registration.
  2325  * @param string   $option_group The settings group name used during registration.
  2177  * @param string   $option_name       The name of the option to unregister.
  2326  * @param string   $option_name  The name of the option to unregister.
  2178  * @param callable $deprecated        Deprecated.
  2327  * @param callable $deprecated   Deprecated.
  2179  */
  2328  */
  2180 function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
  2329 function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
  2181 	global $new_whitelist_options, $wp_registered_settings;
  2330 	global $new_allowed_options, $wp_registered_settings;
  2182 
  2331 
  2183 	if ( 'misc' == $option_group ) {
  2332 	/*
       
  2333 	 * In 5.5.0, the `$new_whitelist_options` global variable was renamed to `$new_allowed_options`.
       
  2334 	 * Please consider writing more inclusive code.
       
  2335 	 */
       
  2336 	$GLOBALS['new_whitelist_options'] = &$new_allowed_options;
       
  2337 
       
  2338 	if ( 'misc' === $option_group ) {
  2184 		_deprecated_argument(
  2339 		_deprecated_argument(
  2185 			__FUNCTION__,
  2340 			__FUNCTION__,
  2186 			'3.0.0',
  2341 			'3.0.0',
  2187 			/* translators: %s: misc */
       
  2188 			sprintf(
  2342 			sprintf(
       
  2343 				/* translators: %s: misc */
  2189 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2344 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2190 				'misc'
  2345 				'misc'
  2191 			)
  2346 			)
  2192 		);
  2347 		);
  2193 		$option_group = 'general';
  2348 		$option_group = 'general';
  2194 	}
  2349 	}
  2195 
  2350 
  2196 	if ( 'privacy' == $option_group ) {
  2351 	if ( 'privacy' === $option_group ) {
  2197 		_deprecated_argument(
  2352 		_deprecated_argument(
  2198 			__FUNCTION__,
  2353 			__FUNCTION__,
  2199 			'3.5.0',
  2354 			'3.5.0',
  2200 			/* translators: %s: privacy */
       
  2201 			sprintf(
  2355 			sprintf(
       
  2356 				/* translators: %s: privacy */
  2202 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2357 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2203 				'privacy'
  2358 				'privacy'
  2204 			)
  2359 			)
  2205 		);
  2360 		);
  2206 		$option_group = 'reading';
  2361 		$option_group = 'reading';
  2207 	}
  2362 	}
  2208 
  2363 
  2209 	$pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ] );
  2364 	$pos = array_search( $option_name, (array) $new_allowed_options[ $option_group ], true );
  2210 	if ( $pos !== false ) {
  2365 
  2211 		unset( $new_whitelist_options[ $option_group ][ $pos ] );
  2366 	if ( false !== $pos ) {
  2212 	}
  2367 		unset( $new_allowed_options[ $option_group ][ $pos ] );
       
  2368 	}
       
  2369 
  2213 	if ( '' !== $deprecated ) {
  2370 	if ( '' !== $deprecated ) {
  2214 		_deprecated_argument(
  2371 		_deprecated_argument(
  2215 			__FUNCTION__,
  2372 			__FUNCTION__,
  2216 			'4.7.0',
  2373 			'4.7.0',
  2217 			/* translators: 1: $sanitize_callback, 2: register_setting() */
       
  2218 			sprintf(
  2374 			sprintf(
       
  2375 				/* translators: 1: $sanitize_callback, 2: register_setting() */
  2219 				__( '%1$s is deprecated. The callback from %2$s is used instead.' ),
  2376 				__( '%1$s is deprecated. The callback from %2$s is used instead.' ),
  2220 				'<code>$sanitize_callback</code>',
  2377 				'<code>$sanitize_callback</code>',
  2221 				'<code>register_setting()</code>'
  2378 				'<code>register_setting()</code>'
  2222 			)
  2379 			)
  2223 		);
  2380 		);
  2233 		// Remove the default filter if a default was provided during registration.
  2390 		// Remove the default filter if a default was provided during registration.
  2234 		if ( array_key_exists( 'default', $wp_registered_settings[ $option_name ] ) ) {
  2391 		if ( array_key_exists( 'default', $wp_registered_settings[ $option_name ] ) ) {
  2235 			remove_filter( "default_option_{$option_name}", 'filter_default_option', 10 );
  2392 			remove_filter( "default_option_{$option_name}", 'filter_default_option', 10 );
  2236 		}
  2393 		}
  2237 
  2394 
       
  2395 		/**
       
  2396 		 * Fires immediately before the setting is unregistered and after its filters have been removed.
       
  2397 		 *
       
  2398 		 * @since 5.5.0
       
  2399 		 *
       
  2400 		 * @param string $option_group Setting group.
       
  2401 		 * @param string $option_name  Setting name.
       
  2402 		 */
       
  2403 		do_action( 'unregister_setting', $option_group, $option_name );
       
  2404 
  2238 		unset( $wp_registered_settings[ $option_name ] );
  2405 		unset( $wp_registered_settings[ $option_name ] );
  2239 	}
  2406 	}
  2240 }
  2407 }
  2241 
  2408 
  2242 /**
  2409 /**
  2257 
  2424 
  2258 	return $wp_registered_settings;
  2425 	return $wp_registered_settings;
  2259 }
  2426 }
  2260 
  2427 
  2261 /**
  2428 /**
  2262  * Filter the default value for the option.
  2429  * Filters the default value for the option.
  2263  *
  2430  *
  2264  * For settings which register a default setting in `register_setting()`, this
  2431  * For settings which register a default setting in `register_setting()`, this
  2265  * function is added as a filter to `default_option_{$option}`.
  2432  * function is added as a filter to `default_option_{$option}`.
  2266  *
  2433  *
  2267  * @since 4.7.0
  2434  * @since 4.7.0
  2268  *
  2435  *
  2269  * @param mixed $default Existing default value to return.
  2436  * @param mixed  $default        Existing default value to return.
  2270  * @param string $option Option name.
  2437  * @param string $option         Option name.
  2271  * @param bool $passed_default Was `get_option()` passed a default value?
  2438  * @param bool   $passed_default Was `get_option()` passed a default value?
  2272  * @return mixed Filtered default value.
  2439  * @return mixed Filtered default value.
  2273  */
  2440  */
  2274 function filter_default_option( $default, $option, $passed_default ) {
  2441 function filter_default_option( $default, $option, $passed_default ) {
  2275 	if ( $passed_default ) {
  2442 	if ( $passed_default ) {
  2276 		return $default;
  2443 		return $default;