wp/wp-includes/option.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    29  */
    29  */
    30 function get_option( $option, $default = false ) {
    30 function get_option( $option, $default = false ) {
    31 	global $wpdb;
    31 	global $wpdb;
    32 
    32 
    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 	/**
    38 	 * Filters the value of an existing option before it is retrieved.
    39 	 * Filters the value of an existing option before it is retrieved.
    39 	 *
    40 	 *
    40 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
    41 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
    43 	 * the option value, returning the passed value instead.
    44 	 * the option value, returning the passed value instead.
    44 	 *
    45 	 *
    45 	 * @since 1.5.0
    46 	 * @since 1.5.0
    46 	 * @since 4.4.0 The `$option` parameter was added.
    47 	 * @since 4.4.0 The `$option` parameter was added.
    47 	 * @since 4.9.0 The `$default` parameter was added.
    48 	 * @since 4.9.0 The `$default` parameter was added.
    48 	 *
       
    49 	 *
    49 	 *
    50 	 * @param bool|mixed $pre_option The value to return instead of the option value. This differs from
    50 	 * @param bool|mixed $pre_option The value to return instead of the option value. This differs from
    51 	 *                               `$default`, which is used as the fallback value in the event the option
    51 	 *                               `$default`, which is used as the fallback value in the event the option
    52 	 *                               doesn't exist elsewhere in get_option(). Default false (to skip past the
    52 	 *                               doesn't exist elsewhere in get_option(). Default false (to skip past the
    53 	 *                               short-circuit).
    53 	 *                               short-circuit).
    55 	 * @param mixed      $default    The fallback value to return if the option does not exist.
    55 	 * @param mixed      $default    The fallback value to return if the option does not exist.
    56 	 *                               Default is false.
    56 	 *                               Default is false.
    57 	 */
    57 	 */
    58 	$pre = apply_filters( "pre_option_{$option}", false, $option, $default );
    58 	$pre = apply_filters( "pre_option_{$option}", false, $option, $default );
    59 
    59 
    60 	if ( false !== $pre )
    60 	if ( false !== $pre ) {
    61 		return $pre;
    61 		return $pre;
    62 
    62 	}
    63 	if ( defined( 'WP_SETUP_CONFIG' ) )
    63 
       
    64 	if ( defined( 'WP_SETUP_CONFIG' ) ) {
    64 		return false;
    65 		return false;
       
    66 	}
    65 
    67 
    66 	// Distinguish between `false` as a default, and not passing one.
    68 	// Distinguish between `false` as a default, and not passing one.
    67 	$passed_default = func_num_args() > 1;
    69 	$passed_default = func_num_args() > 1;
    68 
    70 
    69 	if ( ! wp_installing() ) {
    71 	if ( ! wp_installing() ) {
    87 			return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
    89 			return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
    88 		}
    90 		}
    89 
    91 
    90 		$alloptions = wp_load_alloptions();
    92 		$alloptions = wp_load_alloptions();
    91 
    93 
    92 		if ( isset( $alloptions[$option] ) ) {
    94 		if ( isset( $alloptions[ $option ] ) ) {
    93 			$value = $alloptions[$option];
    95 			$value = $alloptions[ $option ];
    94 		} else {
    96 		} else {
    95 			$value = wp_cache_get( $option, 'options' );
    97 			$value = wp_cache_get( $option, 'options' );
    96 
    98 
    97 			if ( false === $value ) {
    99 			if ( false === $value ) {
    98 				$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
   100 				$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
   101 				if ( is_object( $row ) ) {
   103 				if ( is_object( $row ) ) {
   102 					$value = $row->option_value;
   104 					$value = $row->option_value;
   103 					wp_cache_add( $option, $value, 'options' );
   105 					wp_cache_add( $option, $value, 'options' );
   104 				} else { // option does not exist, so we must cache its non-existence
   106 				} else { // option does not exist, so we must cache its non-existence
   105 					if ( ! is_array( $notoptions ) ) {
   107 					if ( ! is_array( $notoptions ) ) {
   106 						 $notoptions = array();
   108 						$notoptions = array();
   107 					}
   109 					}
   108 					$notoptions[$option] = true;
   110 					$notoptions[ $option ] = true;
   109 					wp_cache_set( 'notoptions', $notoptions, 'options' );
   111 					wp_cache_set( 'notoptions', $notoptions, 'options' );
   110 
   112 
   111 					/** This filter is documented in wp-includes/option.php */
   113 					/** This filter is documented in wp-includes/option.php */
   112 					return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
   114 					return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
   113 				}
   115 				}
   114 			}
   116 			}
   115 		}
   117 		}
   116 	} else {
   118 	} else {
   117 		$suppress = $wpdb->suppress_errors();
   119 		$suppress = $wpdb->suppress_errors();
   118 		$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
   120 		$row      = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
   119 		$wpdb->suppress_errors( $suppress );
   121 		$wpdb->suppress_errors( $suppress );
   120 		if ( is_object( $row ) ) {
   122 		if ( is_object( $row ) ) {
   121 			$value = $row->option_value;
   123 			$value = $row->option_value;
   122 		} else {
   124 		} else {
   123 			/** This filter is documented in wp-includes/option.php */
   125 			/** This filter is documented in wp-includes/option.php */
   124 			return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
   126 			return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
   125 		}
   127 		}
   126 	}
   128 	}
   127 
   129 
   128 	// If home is not set use siteurl.
   130 	// If home is not set use siteurl.
   129 	if ( 'home' == $option && '' == $value )
   131 	if ( 'home' == $option && '' == $value ) {
   130 		return get_option( 'siteurl' );
   132 		return get_option( 'siteurl' );
   131 
   133 	}
   132 	if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
   134 
       
   135 	if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) ) {
   133 		$value = untrailingslashit( $value );
   136 		$value = untrailingslashit( $value );
       
   137 	}
   134 
   138 
   135 	/**
   139 	/**
   136 	 * Filters the value of an existing option.
   140 	 * Filters the value of an existing option.
   137 	 *
   141 	 *
   138 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
   142 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
   157  * @since 2.2.0
   161  * @since 2.2.0
   158  *
   162  *
   159  * @param string $option Option name.
   163  * @param string $option Option name.
   160  */
   164  */
   161 function wp_protect_special_option( $option ) {
   165 function wp_protect_special_option( $option ) {
   162 	if ( 'alloptions' === $option || 'notoptions' === $option )
   166 	if ( 'alloptions' === $option || 'notoptions' === $option ) {
   163 		wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
   167 		wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
       
   168 	}
   164 }
   169 }
   165 
   170 
   166 /**
   171 /**
   167  * Print option value after sanitizing for forms.
   172  * Print option value after sanitizing for forms.
   168  *
   173  *
   199 		}
   204 		}
   200 		$wpdb->suppress_errors( $suppress );
   205 		$wpdb->suppress_errors( $suppress );
   201 
   206 
   202 		$alloptions = array();
   207 		$alloptions = array();
   203 		foreach ( (array) $alloptions_db as $o ) {
   208 		foreach ( (array) $alloptions_db as $o ) {
   204 			$alloptions[$o->option_name] = $o->option_value;
   209 			$alloptions[ $o->option_name ] = $o->option_value;
   205 		}
   210 		}
   206 
   211 
   207 		if ( ! wp_installing() || ! is_multisite() ) {
   212 		if ( ! wp_installing() || ! is_multisite() ) {
   208 			/**
   213 			/**
   209 			 * Filters all options before caching them.
   214 			 * Filters all options before caching them.
   237  * @param int $network_id Optional site ID for which to query the options. Defaults to the current site.
   242  * @param int $network_id Optional site ID for which to query the options. Defaults to the current site.
   238  */
   243  */
   239 function wp_load_core_site_options( $network_id = null ) {
   244 function wp_load_core_site_options( $network_id = null ) {
   240 	global $wpdb;
   245 	global $wpdb;
   241 
   246 
   242 	if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() )
   247 	if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() ) {
   243 		return;
   248 		return;
   244 
   249 	}
   245 	if ( empty($network_id) )
   250 
       
   251 	if ( empty( $network_id ) ) {
   246 		$network_id = get_current_network_id();
   252 		$network_id = get_current_network_id();
   247 
   253 	}
   248 	$core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled', 'ms_files_rewriting' );
   254 
   249 
   255 	$core_options = array( 'site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled', 'ms_files_rewriting' );
   250 	$core_options_in = "'" . implode("', '", $core_options) . "'";
   256 
   251 	$options = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $network_id ) );
   257 	$core_options_in = "'" . implode( "', '", $core_options ) . "'";
       
   258 	$options         = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $network_id ) );
   252 
   259 
   253 	foreach ( $options as $option ) {
   260 	foreach ( $options as $option ) {
   254 		$key = $option->meta_key;
   261 		$key                = $option->meta_key;
   255 		$cache_key = "{$network_id}:$key";
   262 		$cache_key          = "{$network_id}:$key";
   256 		$option->meta_value = maybe_unserialize( $option->meta_value );
   263 		$option->meta_value = maybe_unserialize( $option->meta_value );
   257 
   264 
   258 		wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
   265 		wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
   259 	}
   266 	}
   260 }
   267 }
   283  * @return bool False if value was not updated and true if value was updated.
   290  * @return bool False if value was not updated and true if value was updated.
   284  */
   291  */
   285 function update_option( $option, $value, $autoload = null ) {
   292 function update_option( $option, $value, $autoload = null ) {
   286 	global $wpdb;
   293 	global $wpdb;
   287 
   294 
   288 	$option = trim($option);
   295 	$option = trim( $option );
   289 	if ( empty($option) )
   296 	if ( empty( $option ) ) {
   290 		return false;
   297 		return false;
       
   298 	}
   291 
   299 
   292 	wp_protect_special_option( $option );
   300 	wp_protect_special_option( $option );
   293 
   301 
   294 	if ( is_object( $value ) )
   302 	if ( is_object( $value ) ) {
   295 		$value = clone $value;
   303 		$value = clone $value;
   296 
   304 	}
   297 	$value = sanitize_option( $option, $value );
   305 
       
   306 	$value     = sanitize_option( $option, $value );
   298 	$old_value = get_option( $option );
   307 	$old_value = get_option( $option );
   299 
   308 
   300 	/**
   309 	/**
   301 	 * Filters a specific option before its value is (maybe) serialized and updated.
   310 	 * Filters a specific option before its value is (maybe) serialized and updated.
   302 	 *
   311 	 *
   365 	if ( null !== $autoload ) {
   374 	if ( null !== $autoload ) {
   366 		$update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
   375 		$update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
   367 	}
   376 	}
   368 
   377 
   369 	$result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
   378 	$result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
   370 	if ( ! $result )
   379 	if ( ! $result ) {
   371 		return false;
   380 		return false;
       
   381 	}
   372 
   382 
   373 	$notoptions = wp_cache_get( 'notoptions', 'options' );
   383 	$notoptions = wp_cache_get( 'notoptions', 'options' );
   374 	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
   384 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
   375 		unset( $notoptions[$option] );
   385 		unset( $notoptions[ $option ] );
   376 		wp_cache_set( 'notoptions', $notoptions, 'options' );
   386 		wp_cache_set( 'notoptions', $notoptions, 'options' );
   377 	}
   387 	}
   378 
   388 
   379 	if ( ! wp_installing() ) {
   389 	if ( ! wp_installing() ) {
   380 		$alloptions = wp_load_alloptions();
   390 		$alloptions = wp_load_alloptions();
   381 		if ( isset( $alloptions[$option] ) ) {
   391 		if ( isset( $alloptions[ $option ] ) ) {
   382 			$alloptions[ $option ] = $serialized_value;
   392 			$alloptions[ $option ] = $serialized_value;
   383 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   393 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   384 		} else {
   394 		} else {
   385 			wp_cache_set( $option, $serialized_value, 'options' );
   395 			wp_cache_set( $option, $serialized_value, 'options' );
   386 		}
   396 		}
   437  * @return bool False if option was not added and true if option was added.
   447  * @return bool False if option was not added and true if option was added.
   438  */
   448  */
   439 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
   449 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
   440 	global $wpdb;
   450 	global $wpdb;
   441 
   451 
   442 	if ( !empty( $deprecated ) )
   452 	if ( ! empty( $deprecated ) ) {
   443 		_deprecated_argument( __FUNCTION__, '2.3.0' );
   453 		_deprecated_argument( __FUNCTION__, '2.3.0' );
   444 
   454 	}
   445 	$option = trim($option);
   455 
   446 	if ( empty($option) )
   456 	$option = trim( $option );
       
   457 	if ( empty( $option ) ) {
   447 		return false;
   458 		return false;
       
   459 	}
   448 
   460 
   449 	wp_protect_special_option( $option );
   461 	wp_protect_special_option( $option );
   450 
   462 
   451 	if ( is_object($value) )
   463 	if ( is_object( $value ) ) {
   452 		$value = clone $value;
   464 		$value = clone $value;
       
   465 	}
   453 
   466 
   454 	$value = sanitize_option( $option, $value );
   467 	$value = sanitize_option( $option, $value );
   455 
   468 
   456 	// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
   469 	// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
   457 	$notoptions = wp_cache_get( 'notoptions', 'options' );
   470 	$notoptions = wp_cache_get( 'notoptions', 'options' );
   458 	if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
   471 	if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
   459 		/** This filter is documented in wp-includes/option.php */
   472 		/** This filter is documented in wp-includes/option.php */
   460 		if ( apply_filters( "default_option_{$option}", false, $option, false ) !== get_option( $option ) )
   473 		if ( apply_filters( "default_option_{$option}", false, $option, false ) !== get_option( $option ) ) {
   461 			return false;
   474 			return false;
       
   475 		}
       
   476 	}
   462 
   477 
   463 	$serialized_value = maybe_serialize( $value );
   478 	$serialized_value = maybe_serialize( $value );
   464 	$autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
   479 	$autoload         = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
   465 
   480 
   466 	/**
   481 	/**
   467 	 * Fires before an option is added.
   482 	 * Fires before an option is added.
   468 	 *
   483 	 *
   469 	 * @since 2.9.0
   484 	 * @since 2.9.0
   472 	 * @param mixed  $value  Value of the option.
   487 	 * @param mixed  $value  Value of the option.
   473 	 */
   488 	 */
   474 	do_action( 'add_option', $option, $value );
   489 	do_action( 'add_option', $option, $value );
   475 
   490 
   476 	$result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $serialized_value, $autoload ) );
   491 	$result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $serialized_value, $autoload ) );
   477 	if ( ! $result )
   492 	if ( ! $result ) {
   478 		return false;
   493 		return false;
       
   494 	}
   479 
   495 
   480 	if ( ! wp_installing() ) {
   496 	if ( ! wp_installing() ) {
   481 		if ( 'yes' == $autoload ) {
   497 		if ( 'yes' == $autoload ) {
   482 			$alloptions = wp_load_alloptions();
   498 			$alloptions            = wp_load_alloptions();
   483 			$alloptions[ $option ] = $serialized_value;
   499 			$alloptions[ $option ] = $serialized_value;
   484 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   500 			wp_cache_set( 'alloptions', $alloptions, 'options' );
   485 		} else {
   501 		} else {
   486 			wp_cache_set( $option, $serialized_value, 'options' );
   502 			wp_cache_set( $option, $serialized_value, 'options' );
   487 		}
   503 		}
   488 	}
   504 	}
   489 
   505 
   490 	// This option exists now
   506 	// This option exists now
   491 	$notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
   507 	$notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
   492 	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
   508 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
   493 		unset( $notoptions[$option] );
   509 		unset( $notoptions[ $option ] );
   494 		wp_cache_set( 'notoptions', $notoptions, 'options' );
   510 		wp_cache_set( 'notoptions', $notoptions, 'options' );
   495 	}
   511 	}
   496 
   512 
   497 	/**
   513 	/**
   498 	 * Fires after a specific option has been added.
   514 	 * Fires after a specific option has been added.
   531  */
   547  */
   532 function delete_option( $option ) {
   548 function delete_option( $option ) {
   533 	global $wpdb;
   549 	global $wpdb;
   534 
   550 
   535 	$option = trim( $option );
   551 	$option = trim( $option );
   536 	if ( empty( $option ) )
   552 	if ( empty( $option ) ) {
   537 		return false;
   553 		return false;
       
   554 	}
   538 
   555 
   539 	wp_protect_special_option( $option );
   556 	wp_protect_special_option( $option );
   540 
   557 
   541 	// Get the ID, if no ID then return
   558 	// Get the ID, if no ID then return
   542 	$row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
   559 	$row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
   543 	if ( is_null( $row ) )
   560 	if ( is_null( $row ) ) {
   544 		return false;
   561 		return false;
       
   562 	}
   545 
   563 
   546 	/**
   564 	/**
   547 	 * Fires immediately before an option is deleted.
   565 	 * Fires immediately before an option is deleted.
   548 	 *
   566 	 *
   549 	 * @since 2.9.0
   567 	 * @since 2.9.0
   554 
   572 
   555 	$result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
   573 	$result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
   556 	if ( ! wp_installing() ) {
   574 	if ( ! wp_installing() ) {
   557 		if ( 'yes' == $row->autoload ) {
   575 		if ( 'yes' == $row->autoload ) {
   558 			$alloptions = wp_load_alloptions();
   576 			$alloptions = wp_load_alloptions();
   559 			if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
   577 			if ( is_array( $alloptions ) && isset( $alloptions[ $option ] ) ) {
   560 				unset( $alloptions[$option] );
   578 				unset( $alloptions[ $option ] );
   561 				wp_cache_set( 'alloptions', $alloptions, 'options' );
   579 				wp_cache_set( 'alloptions', $alloptions, 'options' );
   562 			}
   580 			}
   563 		} else {
   581 		} else {
   564 			wp_cache_delete( $option, 'options' );
   582 			wp_cache_delete( $option, 'options' );
   565 		}
   583 		}
   613 
   631 
   614 	if ( wp_using_ext_object_cache() ) {
   632 	if ( wp_using_ext_object_cache() ) {
   615 		$result = wp_cache_delete( $transient, 'transient' );
   633 		$result = wp_cache_delete( $transient, 'transient' );
   616 	} else {
   634 	} else {
   617 		$option_timeout = '_transient_timeout_' . $transient;
   635 		$option_timeout = '_transient_timeout_' . $transient;
   618 		$option = '_transient_' . $transient;
   636 		$option         = '_transient_' . $transient;
   619 		$result = delete_option( $option );
   637 		$result         = delete_option( $option );
   620 		if ( $result )
   638 		if ( $result ) {
   621 			delete_option( $option_timeout );
   639 			delete_option( $option_timeout );
       
   640 		}
   622 	}
   641 	}
   623 
   642 
   624 	if ( $result ) {
   643 	if ( $result ) {
   625 
   644 
   626 		/**
   645 		/**
   664 	 *                              Any value other than false will short-circuit the retrieval
   683 	 *                              Any value other than false will short-circuit the retrieval
   665 	 *                              of the transient, and return the returned value.
   684 	 *                              of the transient, and return the returned value.
   666 	 * @param string $transient     Transient name.
   685 	 * @param string $transient     Transient name.
   667 	 */
   686 	 */
   668 	$pre = apply_filters( "pre_transient_{$transient}", false, $transient );
   687 	$pre = apply_filters( "pre_transient_{$transient}", false, $transient );
   669 	if ( false !== $pre )
   688 	if ( false !== $pre ) {
   670 		return $pre;
   689 		return $pre;
       
   690 	}
   671 
   691 
   672 	if ( wp_using_ext_object_cache() ) {
   692 	if ( wp_using_ext_object_cache() ) {
   673 		$value = wp_cache_get( $transient, 'transient' );
   693 		$value = wp_cache_get( $transient, 'transient' );
   674 	} else {
   694 	} else {
   675 		$transient_option = '_transient_' . $transient;
   695 		$transient_option = '_transient_' . $transient;
   676 		if ( ! wp_installing() ) {
   696 		if ( ! wp_installing() ) {
   677 			// If option is not in alloptions, it is not autoloaded and thus has a timeout
   697 			// If option is not in alloptions, it is not autoloaded and thus has a timeout
   678 			$alloptions = wp_load_alloptions();
   698 			$alloptions = wp_load_alloptions();
   679 			if ( !isset( $alloptions[$transient_option] ) ) {
   699 			if ( ! isset( $alloptions[ $transient_option ] ) ) {
   680 				$transient_timeout = '_transient_timeout_' . $transient;
   700 				$transient_timeout = '_transient_timeout_' . $transient;
   681 				$timeout = get_option( $transient_timeout );
   701 				$timeout           = get_option( $transient_timeout );
   682 				if ( false !== $timeout && $timeout < time() ) {
   702 				if ( false !== $timeout && $timeout < time() ) {
   683 					delete_option( $transient_option  );
   703 					delete_option( $transient_option );
   684 					delete_option( $transient_timeout );
   704 					delete_option( $transient_timeout );
   685 					$value = false;
   705 					$value = false;
   686 				}
   706 				}
   687 			}
   707 			}
   688 		}
   708 		}
   689 
   709 
   690 		if ( ! isset( $value ) )
   710 		if ( ! isset( $value ) ) {
   691 			$value = get_option( $transient_option );
   711 			$value = get_option( $transient_option );
       
   712 		}
   692 	}
   713 	}
   693 
   714 
   694 	/**
   715 	/**
   695 	 * Filters an existing transient's value.
   716 	 * Filters an existing transient's value.
   696 	 *
   717 	 *
   754 
   775 
   755 	if ( wp_using_ext_object_cache() ) {
   776 	if ( wp_using_ext_object_cache() ) {
   756 		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
   777 		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
   757 	} else {
   778 	} else {
   758 		$transient_timeout = '_transient_timeout_' . $transient;
   779 		$transient_timeout = '_transient_timeout_' . $transient;
   759 		$transient_option = '_transient_' . $transient;
   780 		$transient_option  = '_transient_' . $transient;
   760 		if ( false === get_option( $transient_option ) ) {
   781 		if ( false === get_option( $transient_option ) ) {
   761 			$autoload = 'yes';
   782 			$autoload = 'yes';
   762 			if ( $expiration ) {
   783 			if ( $expiration ) {
   763 				$autoload = 'no';
   784 				$autoload = 'no';
   764 				add_option( $transient_timeout, time() + $expiration, '', 'no' );
   785 				add_option( $transient_timeout, time() + $expiration, '', 'no' );
   831 
   852 
   832 	if ( ! $force_db && wp_using_ext_object_cache() ) {
   853 	if ( ! $force_db && wp_using_ext_object_cache() ) {
   833 		return;
   854 		return;
   834 	}
   855 	}
   835 
   856 
   836 	$wpdb->query( $wpdb->prepare(
   857 	$wpdb->query(
   837 		"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
   858 		$wpdb->prepare(
       
   859 			"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
   838 			WHERE a.option_name LIKE %s
   860 			WHERE a.option_name LIKE %s
   839 			AND a.option_name NOT LIKE %s
   861 			AND a.option_name NOT LIKE %s
   840 			AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
   862 			AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
   841 			AND b.option_value < %d",
   863 			AND b.option_value < %d",
   842 		$wpdb->esc_like( '_transient_' ) . '%',
   864 			$wpdb->esc_like( '_transient_' ) . '%',
   843 		$wpdb->esc_like( '_transient_timeout_' ) . '%',
   865 			$wpdb->esc_like( '_transient_timeout_' ) . '%',
   844 		time()
   866 			time()
   845 	) );
   867 		)
       
   868 	);
   846 
   869 
   847 	if ( ! is_multisite() ) {
   870 	if ( ! is_multisite() ) {
   848 		// non-Multisite stores site transients in the options table.
   871 		// non-Multisite stores site transients in the options table.
   849 		$wpdb->query( $wpdb->prepare(
   872 		$wpdb->query(
   850 			"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
   873 			$wpdb->prepare(
       
   874 				"DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
   851 				WHERE a.option_name LIKE %s
   875 				WHERE a.option_name LIKE %s
   852 				AND a.option_name NOT LIKE %s
   876 				AND a.option_name NOT LIKE %s
   853 				AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
   877 				AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
   854 				AND b.option_value < %d",
   878 				AND b.option_value < %d",
   855 			$wpdb->esc_like( '_site_transient_' ) . '%',
   879 				$wpdb->esc_like( '_site_transient_' ) . '%',
   856 			$wpdb->esc_like( '_site_transient_timeout_' ) . '%',
   880 				$wpdb->esc_like( '_site_transient_timeout_' ) . '%',
   857 			time()
   881 				time()
   858 		) );
   882 			)
       
   883 		);
   859 	} elseif ( is_multisite() && is_main_site() && is_main_network() ) {
   884 	} elseif ( is_multisite() && is_main_site() && is_main_network() ) {
   860 		// Multisite stores site transients in the sitemeta table.
   885 		// Multisite stores site transients in the sitemeta table.
   861 		$wpdb->query( $wpdb->prepare(
   886 		$wpdb->query(
   862 			"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
   887 			$wpdb->prepare(
       
   888 				"DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
   863 				WHERE a.meta_key LIKE %s
   889 				WHERE a.meta_key LIKE %s
   864 				AND a.meta_key NOT LIKE %s
   890 				AND a.meta_key NOT LIKE %s
   865 				AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
   891 				AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
   866 				AND b.meta_value < %d",
   892 				AND b.meta_value < %d",
   867 			$wpdb->esc_like( '_site_transient_' ) . '%',
   893 				$wpdb->esc_like( '_site_transient_' ) . '%',
   868 			$wpdb->esc_like( '_site_transient_timeout_' ) . '%',
   894 				$wpdb->esc_like( '_site_transient_timeout_' ) . '%',
   869 			time()
   895 				time()
   870 		) );
   896 			)
       
   897 		);
   871 	}
   898 	}
   872 }
   899 }
   873 
   900 
   874 /**
   901 /**
   875  * Saves and restores user interface settings stored in a cookie.
   902  * Saves and restores user interface settings stored in a cookie.
   894 		return;
   921 		return;
   895 	}
   922 	}
   896 
   923 
   897 	$settings = (string) get_user_option( 'user-settings', $user_id );
   924 	$settings = (string) get_user_option( 'user-settings', $user_id );
   898 
   925 
   899 	if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
   926 	if ( isset( $_COOKIE[ 'wp-settings-' . $user_id ] ) ) {
   900 		$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
   927 		$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE[ 'wp-settings-' . $user_id ] );
   901 
   928 
   902 		// No change or both empty
   929 		// No change or both empty
   903 		if ( $cookie == $settings )
   930 		if ( $cookie == $settings ) {
   904 			return;
   931 			return;
       
   932 		}
   905 
   933 
   906 		$last_saved = (int) get_user_option( 'user-settings-time', $user_id );
   934 		$last_saved = (int) get_user_option( 'user-settings-time', $user_id );
   907 		$current = isset( $_COOKIE['wp-settings-time-' . $user_id]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user_id] ) : 0;
   935 		$current    = isset( $_COOKIE[ 'wp-settings-time-' . $user_id ] ) ? preg_replace( '/[^0-9]/', '', $_COOKIE[ 'wp-settings-time-' . $user_id ] ) : 0;
   908 
   936 
   909 		// The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
   937 		// The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
   910 		if ( $current > $last_saved ) {
   938 		if ( $current > $last_saved ) {
   911 			update_user_option( $user_id, 'user-settings', $cookie, false );
   939 			update_user_option( $user_id, 'user-settings', $cookie, false );
   912 			update_user_option( $user_id, 'user-settings-time', time() - 5, false );
   940 			update_user_option( $user_id, 'user-settings-time', time() - 5, false );
   916 
   944 
   917 	// The cookie is not set in the current browser or the saved value is newer.
   945 	// The cookie is not set in the current browser or the saved value is newer.
   918 	$secure = ( 'https' === parse_url( admin_url(), PHP_URL_SCHEME ) );
   946 	$secure = ( 'https' === parse_url( admin_url(), PHP_URL_SCHEME ) );
   919 	setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
   947 	setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
   920 	setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
   948 	setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
   921 	$_COOKIE['wp-settings-' . $user_id] = $settings;
   949 	$_COOKIE[ 'wp-settings-' . $user_id ] = $settings;
   922 }
   950 }
   923 
   951 
   924 /**
   952 /**
   925  * Retrieve user interface setting value based on setting name.
   953  * Retrieve user interface setting value based on setting name.
   926  *
   954  *
   931  * @return mixed the last saved user setting or the default value/false if it doesn't exist.
   959  * @return mixed the last saved user setting or the default value/false if it doesn't exist.
   932  */
   960  */
   933 function get_user_setting( $name, $default = false ) {
   961 function get_user_setting( $name, $default = false ) {
   934 	$all_user_settings = get_all_user_settings();
   962 	$all_user_settings = get_all_user_settings();
   935 
   963 
   936 	return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
   964 	return isset( $all_user_settings[ $name ] ) ? $all_user_settings[ $name ] : $default;
   937 }
   965 }
   938 
   966 
   939 /**
   967 /**
   940  * Add or update user interface setting.
   968  * Add or update user interface setting.
   941  *
   969  *
   952 function set_user_setting( $name, $value ) {
   980 function set_user_setting( $name, $value ) {
   953 	if ( headers_sent() ) {
   981 	if ( headers_sent() ) {
   954 		return false;
   982 		return false;
   955 	}
   983 	}
   956 
   984 
   957 	$all_user_settings = get_all_user_settings();
   985 	$all_user_settings          = get_all_user_settings();
   958 	$all_user_settings[$name] = $value;
   986 	$all_user_settings[ $name ] = $value;
   959 
   987 
   960 	return wp_set_all_user_settings( $all_user_settings );
   988 	return wp_set_all_user_settings( $all_user_settings );
   961 }
   989 }
   962 
   990 
   963 /**
   991 /**
   976 	if ( headers_sent() ) {
  1004 	if ( headers_sent() ) {
   977 		return false;
  1005 		return false;
   978 	}
  1006 	}
   979 
  1007 
   980 	$all_user_settings = get_all_user_settings();
  1008 	$all_user_settings = get_all_user_settings();
   981 	$names = (array) $names;
  1009 	$names             = (array) $names;
   982 	$deleted = false;
  1010 	$deleted           = false;
   983 
  1011 
   984 	foreach ( $names as $name ) {
  1012 	foreach ( $names as $name ) {
   985 		if ( isset( $all_user_settings[$name] ) ) {
  1013 		if ( isset( $all_user_settings[ $name ] ) ) {
   986 			unset( $all_user_settings[$name] );
  1014 			unset( $all_user_settings[ $name ] );
   987 			$deleted = true;
  1015 			$deleted = true;
   988 		}
  1016 		}
   989 	}
  1017 	}
   990 
  1018 
   991 	if ( $deleted ) {
  1019 	if ( $deleted ) {
  1015 		return $_updated_user_settings;
  1043 		return $_updated_user_settings;
  1016 	}
  1044 	}
  1017 
  1045 
  1018 	$user_settings = array();
  1046 	$user_settings = array();
  1019 
  1047 
  1020 	if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
  1048 	if ( isset( $_COOKIE[ 'wp-settings-' . $user_id ] ) ) {
  1021 		$cookie = preg_replace( '/[^A-Za-z0-9=&_-]/', '', $_COOKIE['wp-settings-' . $user_id] );
  1049 		$cookie = preg_replace( '/[^A-Za-z0-9=&_-]/', '', $_COOKIE[ 'wp-settings-' . $user_id ] );
  1022 
  1050 
  1023 		if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
  1051 		if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
  1024 			parse_str( $cookie, $user_settings );
  1052 			parse_str( $cookie, $user_settings );
  1025 		}
  1053 		}
  1026 	} else {
  1054 	} else {
  1058 		return;
  1086 		return;
  1059 	}
  1087 	}
  1060 
  1088 
  1061 	$settings = '';
  1089 	$settings = '';
  1062 	foreach ( $user_settings as $name => $value ) {
  1090 	foreach ( $user_settings as $name => $value ) {
  1063 		$_name = preg_replace( '/[^A-Za-z0-9_-]+/', '', $name );
  1091 		$_name  = preg_replace( '/[^A-Za-z0-9_-]+/', '', $name );
  1064 		$_value = preg_replace( '/[^A-Za-z0-9_-]+/', '', $value );
  1092 		$_value = preg_replace( '/[^A-Za-z0-9_-]+/', '', $value );
  1065 
  1093 
  1066 		if ( ! empty( $_name ) ) {
  1094 		if ( ! empty( $_name ) ) {
  1067 			$settings .= $_name . '=' . $_value . '&';
  1095 			$settings .= $_name . '=' . $_value . '&';
  1068 		}
  1096 		}
  1163  *
  1191  *
  1164  * @since 4.4.0
  1192  * @since 4.4.0
  1165  *
  1193  *
  1166  * @see get_option()
  1194  * @see get_option()
  1167  *
  1195  *
  1168  * @global wpdb $wpdb
  1196  * @global wpdb $wpdb WordPress database abstraction object.
  1169  *
  1197  *
  1170  * @param int      $network_id ID of the network. Can be null to default to the current network ID.
  1198  * @param int      $network_id ID of the network. Can be null to default to the current network ID.
  1171  * @param string   $option     Name of option to retrieve. Expected to not be SQL-escaped.
  1199  * @param string   $option     Name of option to retrieve. Expected to not be SQL-escaped.
  1172  * @param mixed    $default    Optional. Value to return if the option doesn't exist. Default false.
  1200  * @param mixed    $default    Optional. Value to return if the option doesn't exist. Default false.
  1173  * @return mixed Value set for the option.
  1201  * @return mixed Value set for the option.
  1215 		return $pre;
  1243 		return $pre;
  1216 	}
  1244 	}
  1217 
  1245 
  1218 	// prevent non-existent options from triggering multiple queries
  1246 	// prevent non-existent options from triggering multiple queries
  1219 	$notoptions_key = "$network_id:notoptions";
  1247 	$notoptions_key = "$network_id:notoptions";
  1220 	$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
  1248 	$notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
  1221 
  1249 
  1222 	if ( isset( $notoptions[ $option ] ) ) {
  1250 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
  1223 
  1251 
  1224 		/**
  1252 		/**
  1225 		 * Filters a specific default network option.
  1253 		 * Filters a specific default network option.
  1226 		 *
  1254 		 *
  1227 		 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1255 		 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1239 	}
  1267 	}
  1240 
  1268 
  1241 	if ( ! is_multisite() ) {
  1269 	if ( ! is_multisite() ) {
  1242 		/** This filter is documented in wp-includes/option.php */
  1270 		/** This filter is documented in wp-includes/option.php */
  1243 		$default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
  1271 		$default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
  1244 		$value = get_option( $option, $default );
  1272 		$value   = get_option( $option, $default );
  1245 	} else {
  1273 	} else {
  1246 		$cache_key = "$network_id:$option";
  1274 		$cache_key = "$network_id:$option";
  1247 		$value = wp_cache_get( $cache_key, 'site-options' );
  1275 		$value     = wp_cache_get( $cache_key, 'site-options' );
  1248 
  1276 
  1249 		if ( ! isset( $value ) || false === $value ) {
  1277 		if ( ! isset( $value ) || false === $value ) {
  1250 			$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
  1278 			$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
  1251 
  1279 
  1252 			// Has to be get_row instead of get_var because of funkiness with 0, false, null values
  1280 			// Has to be get_row instead of get_var because of funkiness with 0, false, null values
  1265 				$value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
  1293 				$value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
  1266 			}
  1294 			}
  1267 		}
  1295 		}
  1268 	}
  1296 	}
  1269 
  1297 
       
  1298 	if ( ! is_array( $notoptions ) ) {
       
  1299 		$notoptions = array();
       
  1300 		wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
       
  1301 	}
       
  1302 
  1270 	/**
  1303 	/**
  1271 	 * Filters the value of an existing network option.
  1304 	 * Filters the value of an existing network option.
  1272 	 *
  1305 	 *
  1273 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1306 	 * The dynamic portion of the hook name, `$option`, refers to the option name.
  1274 	 *
  1307 	 *
  1291  *
  1324  *
  1292  * @since 4.4.0
  1325  * @since 4.4.0
  1293  *
  1326  *
  1294  * @see add_option()
  1327  * @see add_option()
  1295  *
  1328  *
  1296  * @global wpdb $wpdb
  1329  * @global wpdb $wpdb WordPress database abstraction object.
  1297  *
  1330  *
  1298  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
  1331  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
  1299  * @param string $option     Name of option to add. Expected to not be SQL-escaped.
  1332  * @param string $option     Name of option to add. Expected to not be SQL-escaped.
  1300  * @param mixed  $value      Option value, can be anything. Expected to not be SQL-escaped.
  1333  * @param mixed  $value      Option value, can be anything. Expected to not be SQL-escaped.
  1301  * @return bool False if option was not added and true if option was added.
  1334  * @return bool False if option was not added and true if option was added.
  1348 		}
  1381 		}
  1349 
  1382 
  1350 		$value = sanitize_option( $option, $value );
  1383 		$value = sanitize_option( $option, $value );
  1351 
  1384 
  1352 		$serialized_value = maybe_serialize( $value );
  1385 		$serialized_value = maybe_serialize( $value );
  1353 		$result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id'    => $network_id, 'meta_key'   => $option, 'meta_value' => $serialized_value ) );
  1386 		$result           = $wpdb->insert(
       
  1387 			$wpdb->sitemeta,
       
  1388 			array(
       
  1389 				'site_id'    => $network_id,
       
  1390 				'meta_key'   => $option,
       
  1391 				'meta_value' => $serialized_value,
       
  1392 			)
       
  1393 		);
  1354 
  1394 
  1355 		if ( ! $result ) {
  1395 		if ( ! $result ) {
  1356 			return false;
  1396 			return false;
  1357 		}
  1397 		}
  1358 
  1398 
  1406  *
  1446  *
  1407  * @since 4.4.0
  1447  * @since 4.4.0
  1408  *
  1448  *
  1409  * @see delete_option()
  1449  * @see delete_option()
  1410  *
  1450  *
  1411  * @global wpdb $wpdb
  1451  * @global wpdb $wpdb WordPress database abstraction object.
  1412  *
  1452  *
  1413  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
  1453  * @param int    $network_id ID of the network. Can be null to default to the current network ID.
  1414  * @param string $option     Name of option to remove. Expected to not be SQL-escaped.
  1454  * @param string $option     Name of option to remove. Expected to not be SQL-escaped.
  1415  * @return bool True, if succeed. False, if failure.
  1455  * @return bool True, if succeed. False, if failure.
  1416  */
  1456  */
  1450 			return false;
  1490 			return false;
  1451 		}
  1491 		}
  1452 		$cache_key = "$network_id:$option";
  1492 		$cache_key = "$network_id:$option";
  1453 		wp_cache_delete( $cache_key, 'site-options' );
  1493 		wp_cache_delete( $cache_key, 'site-options' );
  1454 
  1494 
  1455 		$result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $network_id ) );
  1495 		$result = $wpdb->delete(
       
  1496 			$wpdb->sitemeta,
       
  1497 			array(
       
  1498 				'meta_key' => $option,
       
  1499 				'site_id'  => $network_id,
       
  1500 			)
       
  1501 		);
  1456 	}
  1502 	}
  1457 
  1503 
  1458 	if ( $result ) {
  1504 	if ( $result ) {
  1459 
  1505 
  1460 		/**
  1506 		/**
  1493  *
  1539  *
  1494  * @since 4.4.0
  1540  * @since 4.4.0
  1495  *
  1541  *
  1496  * @see update_option()
  1542  * @see update_option()
  1497  *
  1543  *
  1498  * @global wpdb $wpdb
  1544  * @global wpdb $wpdb WordPress database abstraction object.
  1499  *
  1545  *
  1500  * @param int      $network_id ID of the network. Can be null to default to the current network ID.
  1546  * @param int      $network_id ID of the network. Can be null to default to the current network ID.
  1501  * @param string   $option     Name of option. Expected to not be SQL-escaped.
  1547  * @param string   $option     Name of option. Expected to not be SQL-escaped.
  1502  * @param mixed    $value      Option value. Expected to not be SQL-escaped.
  1548  * @param mixed    $value      Option value. Expected to not be SQL-escaped.
  1503  * @return bool False if value was not updated and true if value was updated.
  1549  * @return bool False if value was not updated and true if value was updated.
  1535 	 * @param string $option     Option name.
  1581 	 * @param string $option     Option name.
  1536 	 * @param int    $network_id ID of the network.
  1582 	 * @param int    $network_id ID of the network.
  1537 	 */
  1583 	 */
  1538 	$value = apply_filters( "pre_update_site_option_{$option}", $value, $old_value, $option, $network_id );
  1584 	$value = apply_filters( "pre_update_site_option_{$option}", $value, $old_value, $option, $network_id );
  1539 
  1585 
  1540 	if ( $value === $old_value ) {
  1586 	/*
       
  1587 	 * If the new and old values are the same, no need to update.
       
  1588 	 *
       
  1589 	 * Unserialized values will be adequate in most cases. If the unserialized
       
  1590 	 * data differs, the (maybe) serialized data is checked to avoid
       
  1591 	 * unnecessary database calls for otherwise identical object instances.
       
  1592 	 *
       
  1593 	 * See https://core.trac.wordpress.org/ticket/44956
       
  1594 	 */
       
  1595 	if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
  1541 		return false;
  1596 		return false;
  1542 	}
  1597 	}
  1543 
  1598 
  1544 	if ( false === $old_value ) {
  1599 	if ( false === $old_value ) {
  1545 		return add_network_option( $network_id, $option, $value );
  1600 		return add_network_option( $network_id, $option, $value );
  1546 	}
  1601 	}
  1547 
  1602 
  1548 	$notoptions_key = "$network_id:notoptions";
  1603 	$notoptions_key = "$network_id:notoptions";
  1549 	$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
  1604 	$notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
  1550 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
  1605 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
  1551 		unset( $notoptions[ $option ] );
  1606 		unset( $notoptions[ $option ] );
  1552 		wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1607 		wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
  1553 	}
  1608 	}
  1554 
  1609 
  1556 		$result = update_option( $option, $value, 'no' );
  1611 		$result = update_option( $option, $value, 'no' );
  1557 	} else {
  1612 	} else {
  1558 		$value = sanitize_option( $option, $value );
  1613 		$value = sanitize_option( $option, $value );
  1559 
  1614 
  1560 		$serialized_value = maybe_serialize( $value );
  1615 		$serialized_value = maybe_serialize( $value );
  1561 		$result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $network_id, 'meta_key' => $option ) );
  1616 		$result           = $wpdb->update(
       
  1617 			$wpdb->sitemeta,
       
  1618 			array( 'meta_value' => $serialized_value ),
       
  1619 			array(
       
  1620 				'site_id'  => $network_id,
       
  1621 				'meta_key' => $option,
       
  1622 			)
       
  1623 		);
  1562 
  1624 
  1563 		if ( $result ) {
  1625 		if ( $result ) {
  1564 			$cache_key = "$network_id:$option";
  1626 			$cache_key = "$network_id:$option";
  1565 			wp_cache_set( $cache_key, $value, 'site-options' );
  1627 			wp_cache_set( $cache_key, $value, 'site-options' );
  1566 		}
  1628 		}
  1626 
  1688 
  1627 	if ( wp_using_ext_object_cache() ) {
  1689 	if ( wp_using_ext_object_cache() ) {
  1628 		$result = wp_cache_delete( $transient, 'site-transient' );
  1690 		$result = wp_cache_delete( $transient, 'site-transient' );
  1629 	} else {
  1691 	} else {
  1630 		$option_timeout = '_site_transient_timeout_' . $transient;
  1692 		$option_timeout = '_site_transient_timeout_' . $transient;
  1631 		$option = '_site_transient_' . $transient;
  1693 		$option         = '_site_transient_' . $transient;
  1632 		$result = delete_site_option( $option );
  1694 		$result         = delete_site_option( $option );
  1633 		if ( $result )
  1695 		if ( $result ) {
  1634 			delete_site_option( $option_timeout );
  1696 			delete_site_option( $option_timeout );
       
  1697 		}
  1635 	}
  1698 	}
  1636 	if ( $result ) {
  1699 	if ( $result ) {
  1637 
  1700 
  1638 		/**
  1701 		/**
  1639 		 * Fires after a transient is deleted.
  1702 		 * Fires after a transient is deleted.
  1679 	 *                                   of the transient, and return the returned value.
  1742 	 *                                   of the transient, and return the returned value.
  1680 	 * @param string $transient          Transient name.
  1743 	 * @param string $transient          Transient name.
  1681 	 */
  1744 	 */
  1682 	$pre = apply_filters( "pre_site_transient_{$transient}", false, $transient );
  1745 	$pre = apply_filters( "pre_site_transient_{$transient}", false, $transient );
  1683 
  1746 
  1684 	if ( false !== $pre )
  1747 	if ( false !== $pre ) {
  1685 		return $pre;
  1748 		return $pre;
       
  1749 	}
  1686 
  1750 
  1687 	if ( wp_using_ext_object_cache() ) {
  1751 	if ( wp_using_ext_object_cache() ) {
  1688 		$value = wp_cache_get( $transient, 'site-transient' );
  1752 		$value = wp_cache_get( $transient, 'site-transient' );
  1689 	} else {
  1753 	} else {
  1690 		// Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
  1754 		// Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
  1691 		$no_timeout = array('update_core', 'update_plugins', 'update_themes');
  1755 		$no_timeout       = array( 'update_core', 'update_plugins', 'update_themes' );
  1692 		$transient_option = '_site_transient_' . $transient;
  1756 		$transient_option = '_site_transient_' . $transient;
  1693 		if ( ! in_array( $transient, $no_timeout ) ) {
  1757 		if ( ! in_array( $transient, $no_timeout ) ) {
  1694 			$transient_timeout = '_site_transient_timeout_' . $transient;
  1758 			$transient_timeout = '_site_transient_timeout_' . $transient;
  1695 			$timeout = get_site_option( $transient_timeout );
  1759 			$timeout           = get_site_option( $transient_timeout );
  1696 			if ( false !== $timeout && $timeout < time() ) {
  1760 			if ( false !== $timeout && $timeout < time() ) {
  1697 				delete_site_option( $transient_option  );
  1761 				delete_site_option( $transient_option );
  1698 				delete_site_option( $transient_timeout );
  1762 				delete_site_option( $transient_timeout );
  1699 				$value = false;
  1763 				$value = false;
  1700 			}
  1764 			}
  1701 		}
  1765 		}
  1702 
  1766 
  1703 		if ( ! isset( $value ) )
  1767 		if ( ! isset( $value ) ) {
  1704 			$value = get_site_option( $transient_option );
  1768 			$value = get_site_option( $transient_option );
       
  1769 		}
  1705 	}
  1770 	}
  1706 
  1771 
  1707 	/**
  1772 	/**
  1708 	 * Filters the value of an existing site transient.
  1773 	 * Filters the value of an existing site transient.
  1709 	 *
  1774 	 *
  1766 
  1831 
  1767 	if ( wp_using_ext_object_cache() ) {
  1832 	if ( wp_using_ext_object_cache() ) {
  1768 		$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
  1833 		$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
  1769 	} else {
  1834 	} else {
  1770 		$transient_timeout = '_site_transient_timeout_' . $transient;
  1835 		$transient_timeout = '_site_transient_timeout_' . $transient;
  1771 		$option = '_site_transient_' . $transient;
  1836 		$option            = '_site_transient_' . $transient;
  1772 		if ( false === get_site_option( $option ) ) {
  1837 		if ( false === get_site_option( $option ) ) {
  1773 			if ( $expiration )
  1838 			if ( $expiration ) {
  1774 				add_site_option( $transient_timeout, time() + $expiration );
  1839 				add_site_option( $transient_timeout, time() + $expiration );
       
  1840 			}
  1775 			$result = add_site_option( $option, $value );
  1841 			$result = add_site_option( $option, $value );
  1776 		} else {
  1842 		} else {
  1777 			if ( $expiration )
  1843 			if ( $expiration ) {
  1778 				update_site_option( $transient_timeout, time() + $expiration );
  1844 				update_site_option( $transient_timeout, time() + $expiration );
       
  1845 			}
  1779 			$result = update_site_option( $option, $value );
  1846 			$result = update_site_option( $option, $value );
  1780 		}
  1847 		}
  1781 	}
  1848 	}
  1782 	if ( $result ) {
  1849 	if ( $result ) {
  1783 
  1850 
  1816  * does not encompass all settings available in WordPress.
  1883  * does not encompass all settings available in WordPress.
  1817  *
  1884  *
  1818  * @since 4.7.0
  1885  * @since 4.7.0
  1819  */
  1886  */
  1820 function register_initial_settings() {
  1887 function register_initial_settings() {
  1821 	register_setting( 'general', 'blogname', array(
  1888 	register_setting(
  1822 		'show_in_rest' => array(
  1889 		'general',
  1823 			'name' => 'title',
  1890 		'blogname',
  1824 		),
  1891 		array(
  1825 		'type'         => 'string',
  1892 			'show_in_rest' => array(
  1826 		'description'  => __( 'Site title.' ),
  1893 				'name' => 'title',
  1827 	) );
  1894 			),
  1828 
  1895 			'type'         => 'string',
  1829 	register_setting( 'general', 'blogdescription', array(
  1896 			'description'  => __( 'Site title.' ),
  1830 		'show_in_rest' => array(
  1897 		)
  1831 			'name' => 'description',
  1898 	);
  1832 		),
  1899 
  1833 		'type'         => 'string',
  1900 	register_setting(
  1834 		'description'  => __( 'Site tagline.' ),
  1901 		'general',
  1835 	) );
  1902 		'blogdescription',
       
  1903 		array(
       
  1904 			'show_in_rest' => array(
       
  1905 				'name' => 'description',
       
  1906 			),
       
  1907 			'type'         => 'string',
       
  1908 			'description'  => __( 'Site tagline.' ),
       
  1909 		)
       
  1910 	);
  1836 
  1911 
  1837 	if ( ! is_multisite() ) {
  1912 	if ( ! is_multisite() ) {
  1838 		register_setting( 'general', 'siteurl', array(
  1913 		register_setting(
       
  1914 			'general',
       
  1915 			'siteurl',
       
  1916 			array(
       
  1917 				'show_in_rest' => array(
       
  1918 					'name'   => 'url',
       
  1919 					'schema' => array(
       
  1920 						'format' => 'uri',
       
  1921 					),
       
  1922 				),
       
  1923 				'type'         => 'string',
       
  1924 				'description'  => __( 'Site URL.' ),
       
  1925 			)
       
  1926 		);
       
  1927 	}
       
  1928 
       
  1929 	if ( ! is_multisite() ) {
       
  1930 		register_setting(
       
  1931 			'general',
       
  1932 			'admin_email',
       
  1933 			array(
       
  1934 				'show_in_rest' => array(
       
  1935 					'name'   => 'email',
       
  1936 					'schema' => array(
       
  1937 						'format' => 'email',
       
  1938 					),
       
  1939 				),
       
  1940 				'type'         => 'string',
       
  1941 				'description'  => __( 'This address is used for admin purposes, like new user notification.' ),
       
  1942 			)
       
  1943 		);
       
  1944 	}
       
  1945 
       
  1946 	register_setting(
       
  1947 		'general',
       
  1948 		'timezone_string',
       
  1949 		array(
  1839 			'show_in_rest' => array(
  1950 			'show_in_rest' => array(
  1840 				'name'    => 'url',
  1951 				'name' => 'timezone',
  1841 				'schema'  => array(
  1952 			),
  1842 					'format' => 'uri',
  1953 			'type'         => 'string',
       
  1954 			'description'  => __( 'A city in the same timezone as you.' ),
       
  1955 		)
       
  1956 	);
       
  1957 
       
  1958 	register_setting(
       
  1959 		'general',
       
  1960 		'date_format',
       
  1961 		array(
       
  1962 			'show_in_rest' => true,
       
  1963 			'type'         => 'string',
       
  1964 			'description'  => __( 'A date format for all date strings.' ),
       
  1965 		)
       
  1966 	);
       
  1967 
       
  1968 	register_setting(
       
  1969 		'general',
       
  1970 		'time_format',
       
  1971 		array(
       
  1972 			'show_in_rest' => true,
       
  1973 			'type'         => 'string',
       
  1974 			'description'  => __( 'A time format for all time strings.' ),
       
  1975 		)
       
  1976 	);
       
  1977 
       
  1978 	register_setting(
       
  1979 		'general',
       
  1980 		'start_of_week',
       
  1981 		array(
       
  1982 			'show_in_rest' => true,
       
  1983 			'type'         => 'integer',
       
  1984 			'description'  => __( 'A day number of the week that the week should start on.' ),
       
  1985 		)
       
  1986 	);
       
  1987 
       
  1988 	register_setting(
       
  1989 		'general',
       
  1990 		'WPLANG',
       
  1991 		array(
       
  1992 			'show_in_rest' => array(
       
  1993 				'name' => 'language',
       
  1994 			),
       
  1995 			'type'         => 'string',
       
  1996 			'description'  => __( 'WordPress locale code.' ),
       
  1997 			'default'      => 'en_US',
       
  1998 		)
       
  1999 	);
       
  2000 
       
  2001 	register_setting(
       
  2002 		'writing',
       
  2003 		'use_smilies',
       
  2004 		array(
       
  2005 			'show_in_rest' => true,
       
  2006 			'type'         => 'boolean',
       
  2007 			'description'  => __( 'Convert emoticons like :-) and :-P to graphics on display.' ),
       
  2008 			'default'      => true,
       
  2009 		)
       
  2010 	);
       
  2011 
       
  2012 	register_setting(
       
  2013 		'writing',
       
  2014 		'default_category',
       
  2015 		array(
       
  2016 			'show_in_rest' => true,
       
  2017 			'type'         => 'integer',
       
  2018 			'description'  => __( 'Default post category.' ),
       
  2019 		)
       
  2020 	);
       
  2021 
       
  2022 	register_setting(
       
  2023 		'writing',
       
  2024 		'default_post_format',
       
  2025 		array(
       
  2026 			'show_in_rest' => true,
       
  2027 			'type'         => 'string',
       
  2028 			'description'  => __( 'Default post format.' ),
       
  2029 		)
       
  2030 	);
       
  2031 
       
  2032 	register_setting(
       
  2033 		'reading',
       
  2034 		'posts_per_page',
       
  2035 		array(
       
  2036 			'show_in_rest' => true,
       
  2037 			'type'         => 'integer',
       
  2038 			'description'  => __( 'Blog pages show at most.' ),
       
  2039 			'default'      => 10,
       
  2040 		)
       
  2041 	);
       
  2042 
       
  2043 	register_setting(
       
  2044 		'discussion',
       
  2045 		'default_ping_status',
       
  2046 		array(
       
  2047 			'show_in_rest' => array(
       
  2048 				'schema' => array(
       
  2049 					'enum' => array( 'open', 'closed' ),
  1843 				),
  2050 				),
  1844 			),
  2051 			),
  1845 			'type'         => 'string',
  2052 			'type'         => 'string',
  1846 			'description'  => __( 'Site URL.' ),
  2053 			'description'  => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles.' ),
  1847 		) );
  2054 		)
  1848 	}
  2055 	);
  1849 
  2056 
  1850 	if ( ! is_multisite() ) {
  2057 	register_setting(
  1851 		register_setting( 'general', 'admin_email', array(
  2058 		'discussion',
       
  2059 		'default_comment_status',
       
  2060 		array(
  1852 			'show_in_rest' => array(
  2061 			'show_in_rest' => array(
  1853 				'name'    => 'email',
  2062 				'schema' => array(
  1854 				'schema'  => array(
  2063 					'enum' => array( 'open', 'closed' ),
  1855 					'format' => 'email',
       
  1856 				),
  2064 				),
  1857 			),
  2065 			),
  1858 			'type'         => 'string',
  2066 			'type'         => 'string',
  1859 			'description'  => __( 'This address is used for admin purposes, like new user notification.' ),
  2067 			'description'  => __( 'Allow people to post comments on new articles.' ),
  1860 		) );
  2068 		)
  1861 	}
  2069 	);
  1862 
       
  1863 	register_setting( 'general', 'timezone_string', array(
       
  1864 		'show_in_rest' => array(
       
  1865 			'name' => 'timezone',
       
  1866 		),
       
  1867 		'type'         => 'string',
       
  1868 		'description'  => __( 'A city in the same timezone as you.' ),
       
  1869 	) );
       
  1870 
       
  1871 	register_setting( 'general', 'date_format', array(
       
  1872 		'show_in_rest' => true,
       
  1873 		'type'         => 'string',
       
  1874 		'description'  => __( 'A date format for all date strings.' ),
       
  1875 	) );
       
  1876 
       
  1877 	register_setting( 'general', 'time_format', array(
       
  1878 		'show_in_rest' => true,
       
  1879 		'type'         => 'string',
       
  1880 		'description'  => __( 'A time format for all time strings.' ),
       
  1881 	) );
       
  1882 
       
  1883 	register_setting( 'general', 'start_of_week', array(
       
  1884 		'show_in_rest' => true,
       
  1885 		'type'         => 'integer',
       
  1886 		'description'  => __( 'A day number of the week that the week should start on.' ),
       
  1887 	) );
       
  1888 
       
  1889 	register_setting( 'general', 'WPLANG', array(
       
  1890 		'show_in_rest' => array(
       
  1891 			'name' => 'language',
       
  1892 		),
       
  1893 		'type'         => 'string',
       
  1894 		'description'  => __( 'WordPress locale code.' ),
       
  1895 		'default'      => 'en_US',
       
  1896 	) );
       
  1897 
       
  1898 	register_setting( 'writing', 'use_smilies', array(
       
  1899 		'show_in_rest' => true,
       
  1900 		'type'         => 'boolean',
       
  1901 		'description'  => __( 'Convert emoticons like :-) and :-P to graphics on display.' ),
       
  1902 		'default'      => true,
       
  1903 	) );
       
  1904 
       
  1905 	register_setting( 'writing', 'default_category', array(
       
  1906 		'show_in_rest' => true,
       
  1907 		'type'         => 'integer',
       
  1908 		'description'  => __( 'Default post category.' ),
       
  1909 	) );
       
  1910 
       
  1911 	register_setting( 'writing', 'default_post_format', array(
       
  1912 		'show_in_rest' => true,
       
  1913 		'type'         => 'string',
       
  1914 		'description'  => __( 'Default post format.' ),
       
  1915 	) );
       
  1916 
       
  1917 	register_setting( 'reading', 'posts_per_page', array(
       
  1918 		'show_in_rest' => true,
       
  1919 		'type'         => 'integer',
       
  1920 		'description'  => __( 'Blog pages show at most.' ),
       
  1921 		'default'      => 10,
       
  1922 	) );
       
  1923 
       
  1924 	register_setting( 'discussion', 'default_ping_status', array(
       
  1925 		'show_in_rest' => array(
       
  1926 			'schema'   => array(
       
  1927 				'enum' => array( 'open', 'closed' ),
       
  1928 			),
       
  1929 		),
       
  1930 		'type'         => 'string',
       
  1931 		'description'  => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles.' ),
       
  1932 	) );
       
  1933 
       
  1934 	register_setting( 'discussion', 'default_comment_status', array(
       
  1935 		'show_in_rest' => array(
       
  1936 			'schema'   => array(
       
  1937 				'enum' => array( 'open', 'closed' ),
       
  1938 			),
       
  1939 		),
       
  1940 		'type'         => 'string',
       
  1941 		'description'  => __( 'Allow people to post comments on new articles.' ),
       
  1942 	) );
       
  1943 
       
  1944 }
  2070 }
  1945 
  2071 
  1946 /**
  2072 /**
  1947  * Register a setting and its data.
  2073  * Register a setting and its data.
  1948  *
  2074  *
  1951  *
  2077  *
  1952  * @global array $new_whitelist_options
  2078  * @global array $new_whitelist_options
  1953  * @global array $wp_registered_settings
  2079  * @global array $wp_registered_settings
  1954  *
  2080  *
  1955  * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
  2081  * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
  1956  * 	Default whitelisted option key names include "general," "discussion," and "reading," among others.
  2082  *  Default whitelisted option key names include "general," "discussion," and "reading," among others.
  1957  * @param string $option_name The name of an option to sanitize and save.
  2083  * @param string $option_name The name of an option to sanitize and save.
  1958  * @param array  $args {
  2084  * @param array  $args {
  1959  *     Data used to describe the setting when registered.
  2085  *     Data used to describe the setting when registered.
  1960  *
  2086  *
  1961  *     @type string   $type              The type of data associated with this setting.
  2087  *     @type string   $type              The type of data associated with this setting.
  2000 	if ( ! is_array( $wp_registered_settings ) ) {
  2126 	if ( ! is_array( $wp_registered_settings ) ) {
  2001 		$wp_registered_settings = array();
  2127 		$wp_registered_settings = array();
  2002 	}
  2128 	}
  2003 
  2129 
  2004 	if ( 'misc' == $option_group ) {
  2130 	if ( 'misc' == $option_group ) {
  2005 		_deprecated_argument( __FUNCTION__, '3.0.0',
  2131 		_deprecated_argument(
       
  2132 			__FUNCTION__,
       
  2133 			'3.0.0',
  2006 			/* translators: %s: misc */
  2134 			/* translators: %s: misc */
  2007 			sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ),
  2135 			sprintf(
       
  2136 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2008 				'misc'
  2137 				'misc'
  2009 			)
  2138 			)
  2010 		);
  2139 		);
  2011 		$option_group = 'general';
  2140 		$option_group = 'general';
  2012 	}
  2141 	}
  2013 
  2142 
  2014 	if ( 'privacy' == $option_group ) {
  2143 	if ( 'privacy' == $option_group ) {
  2015 		_deprecated_argument( __FUNCTION__, '3.5.0',
  2144 		_deprecated_argument(
       
  2145 			__FUNCTION__,
       
  2146 			'3.5.0',
  2016 			/* translators: %s: privacy */
  2147 			/* translators: %s: privacy */
  2017 			sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ),
  2148 			sprintf(
       
  2149 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2018 				'privacy'
  2150 				'privacy'
  2019 			)
  2151 			)
  2020 		);
  2152 		);
  2021 		$option_group = 'reading';
  2153 		$option_group = 'reading';
  2022 	}
  2154 	}
  2037  *
  2169  *
  2038  * @since 2.7.0
  2170  * @since 2.7.0
  2039  * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead.
  2171  * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead.
  2040  *
  2172  *
  2041  * @global array $new_whitelist_options
  2173  * @global array $new_whitelist_options
       
  2174  * @global array $wp_registered_settings
  2042  *
  2175  *
  2043  * @param string   $option_group      The settings group name used during registration.
  2176  * @param string   $option_group      The settings group name used during registration.
  2044  * @param string   $option_name       The name of the option to unregister.
  2177  * @param string   $option_name       The name of the option to unregister.
  2045  * @param callable $deprecated        Deprecated.
  2178  * @param callable $deprecated        Deprecated.
  2046  */
  2179  */
  2047 function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
  2180 function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
  2048 	global $new_whitelist_options, $wp_registered_settings;
  2181 	global $new_whitelist_options, $wp_registered_settings;
  2049 
  2182 
  2050 	if ( 'misc' == $option_group ) {
  2183 	if ( 'misc' == $option_group ) {
  2051 		_deprecated_argument( __FUNCTION__, '3.0.0',
  2184 		_deprecated_argument(
       
  2185 			__FUNCTION__,
       
  2186 			'3.0.0',
  2052 			/* translators: %s: misc */
  2187 			/* translators: %s: misc */
  2053 			sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ),
  2188 			sprintf(
       
  2189 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2054 				'misc'
  2190 				'misc'
  2055 			)
  2191 			)
  2056 		);
  2192 		);
  2057 		$option_group = 'general';
  2193 		$option_group = 'general';
  2058 	}
  2194 	}
  2059 
  2195 
  2060 	if ( 'privacy' == $option_group ) {
  2196 	if ( 'privacy' == $option_group ) {
  2061 		_deprecated_argument( __FUNCTION__, '3.5.0',
  2197 		_deprecated_argument(
       
  2198 			__FUNCTION__,
       
  2199 			'3.5.0',
  2062 			/* translators: %s: privacy */
  2200 			/* translators: %s: privacy */
  2063 			sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ),
  2201 			sprintf(
       
  2202 				__( 'The "%s" options group has been removed. Use another settings group.' ),
  2064 				'privacy'
  2203 				'privacy'
  2065 			)
  2204 			)
  2066 		);
  2205 		);
  2067 		$option_group = 'reading';
  2206 		$option_group = 'reading';
  2068 	}
  2207 	}
  2070 	$pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ] );
  2209 	$pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ] );
  2071 	if ( $pos !== false ) {
  2210 	if ( $pos !== false ) {
  2072 		unset( $new_whitelist_options[ $option_group ][ $pos ] );
  2211 		unset( $new_whitelist_options[ $option_group ][ $pos ] );
  2073 	}
  2212 	}
  2074 	if ( '' !== $deprecated ) {
  2213 	if ( '' !== $deprecated ) {
  2075 		_deprecated_argument( __FUNCTION__, '4.7.0',
  2214 		_deprecated_argument(
       
  2215 			__FUNCTION__,
       
  2216 			'4.7.0',
  2076 			/* translators: 1: $sanitize_callback, 2: register_setting() */
  2217 			/* translators: 1: $sanitize_callback, 2: register_setting() */
  2077 			sprintf( __( '%1$s is deprecated. The callback from %2$s is used instead.' ),
  2218 			sprintf(
       
  2219 				__( '%1$s is deprecated. The callback from %2$s is used instead.' ),
  2078 				'<code>$sanitize_callback</code>',
  2220 				'<code>$sanitize_callback</code>',
  2079 				'<code>register_setting()</code>'
  2221 				'<code>register_setting()</code>'
  2080 			)
  2222 			)
  2081 		);
  2223 		);
  2082 		remove_filter( "sanitize_option_{$option_name}", $deprecated );
  2224 		remove_filter( "sanitize_option_{$option_name}", $deprecated );
  2086 		// Remove the sanitize callback if one was set during registration.
  2228 		// Remove the sanitize callback if one was set during registration.
  2087 		if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) {
  2229 		if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) {
  2088 			remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );
  2230 			remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );
  2089 		}
  2231 		}
  2090 
  2232 
       
  2233 		// Remove the default filter if a default was provided during registration.
       
  2234 		if ( array_key_exists( 'default', $wp_registered_settings[ $option_name ] ) ) {
       
  2235 			remove_filter( "default_option_{$option_name}", 'filter_default_option', 10 );
       
  2236 		}
       
  2237 
  2091 		unset( $wp_registered_settings[ $option_name ] );
  2238 		unset( $wp_registered_settings[ $option_name ] );
  2092 	}
  2239 	}
  2093 }
  2240 }
  2094 
  2241 
  2095 /**
  2242 /**
  2096  * Retrieves an array of registered settings.
  2243  * Retrieves an array of registered settings.
  2097  *
  2244  *
  2098  * @since 4.7.0
  2245  * @since 4.7.0
       
  2246  *
       
  2247  * @global array $wp_registered_settings
  2099  *
  2248  *
  2100  * @return array List of registered settings, keyed by option name.
  2249  * @return array List of registered settings, keyed by option name.
  2101  */
  2250  */
  2102 function get_registered_settings() {
  2251 function get_registered_settings() {
  2103 	global $wp_registered_settings;
  2252 	global $wp_registered_settings;