wp/wp-includes/option.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    26  *
    26  *
    27  * In most cases non-string scalar and null values will be converted and returned
    27  * In most cases non-string scalar and null values will be converted and returned
    28  * as string equivalents.
    28  * as string equivalents.
    29  *
    29  *
    30  * Exceptions:
    30  * Exceptions:
       
    31  *
    31  * 1. When the option has not been saved in the database, the `$default` value
    32  * 1. When the option has not been saved in the database, the `$default` value
    32  *    is returned if provided. If not, boolean `false` is returned.
    33  *    is returned if provided. If not, boolean `false` is returned.
    33  * 2. When one of the Options API filters is used: {@see 'pre_option_{$option}'},
    34  * 2. When one of the Options API filters is used: {@see 'pre_option_$option'},
    34  *    {@see 'default_option_{$option}'}, or {@see 'option_{$option}'}, the returned
    35  *    {@see 'default_option_$option'}, or {@see 'option_$option'}, the returned
    35  *    value may not match the expected type.
    36  *    value may not match the expected type.
    36  * 3. When the option has just been saved in the database, and get_option()
    37  * 3. When the option has just been saved in the database, and get_option()
    37  *    is used right after, non-string scalar and null values are not converted to
    38  *    is used right after, non-string scalar and null values are not converted to
    38  *    string equivalents and the original type is returned.
    39  *    string equivalents and the original type is returned.
    39  *
    40  *
    40  * Examples:
    41  * Examples:
    41  *
    42  *
    42  * When adding options like this: `add_option( 'my_option_name', 'value' );`
    43  * When adding options like this: `add_option( 'my_option_name', 'value' )`
    43  * and then retrieving them with `get_option( 'my_option_name' );`, the returned
    44  * and then retrieving them with `get_option( 'my_option_name' )`, the returned
    44  * values will be:
    45  * values will be:
    45  *
    46  *
    46  * `false` returns `string(0) ""`
    47  *   - `false` returns `string(0) ""`
    47  * `true`  returns `string(1) "1"`
    48  *   - `true`  returns `string(1) "1"`
    48  * `0`     returns `string(1) "0"`
    49  *   - `0`     returns `string(1) "0"`
    49  * `1`     returns `string(1) "1"`
    50  *   - `1`     returns `string(1) "1"`
    50  * `'0'`   returns `string(1) "0"`
    51  *   - `'0'`   returns `string(1) "0"`
    51  * `'1'`   returns `string(1) "1"`
    52  *   - `'1'`   returns `string(1) "1"`
    52  * `null`  returns `string(0) ""`
    53  *   - `null`  returns `string(0) ""`
    53  *
    54  *
    54  * When adding options with non-scalar values like
    55  * When adding options with non-scalar values like
    55  * `add_option( 'my_array', array( false, 'str', null ) );`, the returned value
    56  * `add_option( 'my_array', array( false, 'str', null ) )`, the returned value
    56  * will be identical to the original as it is serialized before saving
    57  * will be identical to the original as it is serialized before saving
    57  * it in the database:
    58  * it in the database:
    58  *
    59  *
    59  *    array(3) {
    60  *     array(3) {
    60  *        [0] => bool(false)
    61  *         [0] => bool(false)
    61  *        [1] => string(3) "str"
    62  *         [1] => string(3) "str"
    62  *        [2] => NULL
    63  *         [2] => NULL
    63  *    }
    64  *     }
    64  *
    65  *
    65  * @since 1.5.0
    66  * @since 1.5.0
    66  *
    67  *
    67  * @global wpdb $wpdb WordPress database abstraction object.
    68  * @global wpdb $wpdb WordPress database abstraction object.
    68  *
    69  *
    75  *               boolean `false` is returned.
    76  *               boolean `false` is returned.
    76  */
    77  */
    77 function get_option( $option, $default = false ) {
    78 function get_option( $option, $default = false ) {
    78 	global $wpdb;
    79 	global $wpdb;
    79 
    80 
    80 	$option = trim( $option );
    81 	if ( is_scalar( $option ) ) {
       
    82 		$option = trim( $option );
       
    83 	}
       
    84 
    81 	if ( empty( $option ) ) {
    85 	if ( empty( $option ) ) {
    82 		return false;
    86 		return false;
    83 	}
    87 	}
    84 
    88 
    85 	/*
    89 	/*
   339 	$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' );
   343 	$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' );
   340 
   344 
   341 	$core_options_in = "'" . implode( "', '", $core_options ) . "'";
   345 	$core_options_in = "'" . implode( "', '", $core_options ) . "'";
   342 	$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 ) );
   346 	$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 ) );
   343 
   347 
       
   348 	$data = array();
   344 	foreach ( $options as $option ) {
   349 	foreach ( $options as $option ) {
   345 		$key                = $option->meta_key;
   350 		$key                = $option->meta_key;
   346 		$cache_key          = "{$network_id}:$key";
   351 		$cache_key          = "{$network_id}:$key";
   347 		$option->meta_value = maybe_unserialize( $option->meta_value );
   352 		$option->meta_value = maybe_unserialize( $option->meta_value );
   348 
   353 
   349 		wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
   354 		$data[ $cache_key ] = $option->meta_value;
   350 	}
   355 	}
       
   356 	wp_cache_set_multiple( $data, 'site-options' );
   351 }
   357 }
   352 
   358 
   353 /**
   359 /**
   354  * Updates the value of an option that was already added.
   360  * Updates the value of an option that was already added.
   355  *
   361  *
   376  * @return bool True if the value was updated, false otherwise.
   382  * @return bool True if the value was updated, false otherwise.
   377  */
   383  */
   378 function update_option( $option, $value, $autoload = null ) {
   384 function update_option( $option, $value, $autoload = null ) {
   379 	global $wpdb;
   385 	global $wpdb;
   380 
   386 
   381 	$option = trim( $option );
   387 	if ( is_scalar( $option ) ) {
       
   388 		$option = trim( $option );
       
   389 	}
       
   390 
   382 	if ( empty( $option ) ) {
   391 	if ( empty( $option ) ) {
   383 		return false;
   392 		return false;
   384 	}
   393 	}
   385 
   394 
   386 	/*
   395 	/*
   563 
   572 
   564 	if ( ! empty( $deprecated ) ) {
   573 	if ( ! empty( $deprecated ) ) {
   565 		_deprecated_argument( __FUNCTION__, '2.3.0' );
   574 		_deprecated_argument( __FUNCTION__, '2.3.0' );
   566 	}
   575 	}
   567 
   576 
   568 	$option = trim( $option );
   577 	if ( is_scalar( $option ) ) {
       
   578 		$option = trim( $option );
       
   579 	}
       
   580 
   569 	if ( empty( $option ) ) {
   581 	if ( empty( $option ) ) {
   570 		return false;
   582 		return false;
   571 	}
   583 	}
   572 
   584 
   573 	/*
   585 	/*
   685  * @return bool True if the option was deleted, false otherwise.
   697  * @return bool True if the option was deleted, false otherwise.
   686  */
   698  */
   687 function delete_option( $option ) {
   699 function delete_option( $option ) {
   688 	global $wpdb;
   700 	global $wpdb;
   689 
   701 
   690 	$option = trim( $option );
   702 	if ( is_scalar( $option ) ) {
       
   703 		$option = trim( $option );
       
   704 	}
       
   705 
   691 	if ( empty( $option ) ) {
   706 	if ( empty( $option ) ) {
   692 		return false;
   707 		return false;
   693 	}
   708 	}
   694 
   709 
   695 	wp_protect_special_option( $option );
   710 	wp_protect_special_option( $option );
   770 	 *
   785 	 *
   771 	 * @param string $transient Transient name.
   786 	 * @param string $transient Transient name.
   772 	 */
   787 	 */
   773 	do_action( "delete_transient_{$transient}", $transient );
   788 	do_action( "delete_transient_{$transient}", $transient );
   774 
   789 
   775 	if ( wp_using_ext_object_cache() ) {
   790 	if ( wp_using_ext_object_cache() || wp_installing() ) {
   776 		$result = wp_cache_delete( $transient, 'transient' );
   791 		$result = wp_cache_delete( $transient, 'transient' );
   777 	} else {
   792 	} else {
   778 		$option_timeout = '_transient_timeout_' . $transient;
   793 		$option_timeout = '_transient_timeout_' . $transient;
   779 		$option         = '_transient_' . $transient;
   794 		$option         = '_transient_' . $transient;
   780 		$result         = delete_option( $option );
   795 		$result         = delete_option( $option );
   832 
   847 
   833 	if ( false !== $pre ) {
   848 	if ( false !== $pre ) {
   834 		return $pre;
   849 		return $pre;
   835 	}
   850 	}
   836 
   851 
   837 	if ( wp_using_ext_object_cache() ) {
   852 	if ( wp_using_ext_object_cache() || wp_installing() ) {
   838 		$value = wp_cache_get( $transient, 'transient' );
   853 		$value = wp_cache_get( $transient, 'transient' );
   839 	} else {
   854 	} else {
   840 		$transient_option = '_transient_' . $transient;
   855 		$transient_option = '_transient_' . $transient;
   841 		if ( ! wp_installing() ) {
   856 		if ( ! wp_installing() ) {
   842 			// If option is not in alloptions, it is not autoloaded and thus has a timeout.
   857 			// If option is not in alloptions, it is not autoloaded and thus has a timeout.
   916 	 * @param mixed  $value      New value of transient.
   931 	 * @param mixed  $value      New value of transient.
   917 	 * @param string $transient  Transient name.
   932 	 * @param string $transient  Transient name.
   918 	 */
   933 	 */
   919 	$expiration = apply_filters( "expiration_of_transient_{$transient}", $expiration, $value, $transient );
   934 	$expiration = apply_filters( "expiration_of_transient_{$transient}", $expiration, $value, $transient );
   920 
   935 
   921 	if ( wp_using_ext_object_cache() ) {
   936 	if ( wp_using_ext_object_cache() || wp_installing() ) {
   922 		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
   937 		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
   923 	} else {
   938 	} else {
   924 		$transient_timeout = '_transient_timeout_' . $transient;
   939 		$transient_timeout = '_transient_timeout_' . $transient;
   925 		$transient_option  = '_transient_' . $transient;
   940 		$transient_option  = '_transient_' . $transient;
   926 
   941 
  1844 	 *
  1859 	 *
  1845 	 * @param string $transient Transient name.
  1860 	 * @param string $transient Transient name.
  1846 	 */
  1861 	 */
  1847 	do_action( "delete_site_transient_{$transient}", $transient );
  1862 	do_action( "delete_site_transient_{$transient}", $transient );
  1848 
  1863 
  1849 	if ( wp_using_ext_object_cache() ) {
  1864 	if ( wp_using_ext_object_cache() || wp_installing() ) {
  1850 		$result = wp_cache_delete( $transient, 'site-transient' );
  1865 		$result = wp_cache_delete( $transient, 'site-transient' );
  1851 	} else {
  1866 	} else {
  1852 		$option_timeout = '_site_transient_timeout_' . $transient;
  1867 		$option_timeout = '_site_transient_timeout_' . $transient;
  1853 		$option         = '_site_transient_' . $transient;
  1868 		$option         = '_site_transient_' . $transient;
  1854 		$result         = delete_site_option( $option );
  1869 		$result         = delete_site_option( $option );
  1908 
  1923 
  1909 	if ( false !== $pre ) {
  1924 	if ( false !== $pre ) {
  1910 		return $pre;
  1925 		return $pre;
  1911 	}
  1926 	}
  1912 
  1927 
  1913 	if ( wp_using_ext_object_cache() ) {
  1928 	if ( wp_using_ext_object_cache() || wp_installing() ) {
  1914 		$value = wp_cache_get( $transient, 'site-transient' );
  1929 		$value = wp_cache_get( $transient, 'site-transient' );
  1915 	} else {
  1930 	} else {
  1916 		// Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
  1931 		// Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
  1917 		$no_timeout       = array( 'update_core', 'update_plugins', 'update_themes' );
  1932 		$no_timeout       = array( 'update_core', 'update_plugins', 'update_themes' );
  1918 		$transient_option = '_site_transient_' . $transient;
  1933 		$transient_option = '_site_transient_' . $transient;
  1989 	 * @param mixed  $value      New value of site transient.
  2004 	 * @param mixed  $value      New value of site transient.
  1990 	 * @param string $transient  Transient name.
  2005 	 * @param string $transient  Transient name.
  1991 	 */
  2006 	 */
  1992 	$expiration = apply_filters( "expiration_of_site_transient_{$transient}", $expiration, $value, $transient );
  2007 	$expiration = apply_filters( "expiration_of_site_transient_{$transient}", $expiration, $value, $transient );
  1993 
  2008 
  1994 	if ( wp_using_ext_object_cache() ) {
  2009 	if ( wp_using_ext_object_cache() || wp_installing() ) {
  1995 		$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
  2010 		$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
  1996 	} else {
  2011 	} else {
  1997 		$transient_timeout = '_site_transient_timeout_' . $transient;
  2012 		$transient_timeout = '_site_transient_timeout_' . $transient;
  1998 		$option            = '_site_transient_' . $transient;
  2013 		$option            = '_site_transient_' . $transient;
  1999 
  2014 
  2046  *
  2061  *
  2047  * The settings registered here are primarily useful for the REST API, so this
  2062  * The settings registered here are primarily useful for the REST API, so this
  2048  * does not encompass all settings available in WordPress.
  2063  * does not encompass all settings available in WordPress.
  2049  *
  2064  *
  2050  * @since 4.7.0
  2065  * @since 4.7.0
       
  2066  * @since 6.0.1 The `show_on_front`, `page_on_front`, and `page_for_posts` options were added.
  2051  */
  2067  */
  2052 function register_initial_settings() {
  2068 function register_initial_settings() {
  2053 	register_setting(
  2069 	register_setting(
  2054 		'general',
  2070 		'general',
  2055 		'blogname',
  2071 		'blogname',
  2204 			'default'      => 10,
  2220 			'default'      => 10,
  2205 		)
  2221 		)
  2206 	);
  2222 	);
  2207 
  2223 
  2208 	register_setting(
  2224 	register_setting(
       
  2225 		'reading',
       
  2226 		'show_on_front',
       
  2227 		array(
       
  2228 			'show_in_rest' => true,
       
  2229 			'type'         => 'string',
       
  2230 			'description'  => __( 'What to show on the front page' ),
       
  2231 		)
       
  2232 	);
       
  2233 
       
  2234 	register_setting(
       
  2235 		'reading',
       
  2236 		'page_on_front',
       
  2237 		array(
       
  2238 			'show_in_rest' => true,
       
  2239 			'type'         => 'integer',
       
  2240 			'description'  => __( 'The ID of the page that should be displayed on the front page' ),
       
  2241 		)
       
  2242 	);
       
  2243 
       
  2244 	register_setting(
       
  2245 		'reading',
       
  2246 		'page_for_posts',
       
  2247 		array(
       
  2248 			'show_in_rest' => true,
       
  2249 			'type'         => 'integer',
       
  2250 			'description'  => __( 'The ID of the page that should display the latest posts' ),
       
  2251 		)
       
  2252 	);
       
  2253 
       
  2254 	register_setting(
  2209 		'discussion',
  2255 		'discussion',
  2210 		'default_ping_status',
  2256 		'default_ping_status',
  2211 		array(
  2257 		array(
  2212 			'show_in_rest' => array(
  2258 			'show_in_rest' => array(
  2213 				'schema' => array(
  2259 				'schema' => array(
  2236 
  2282 
  2237 /**
  2283 /**
  2238  * Registers a setting and its data.
  2284  * Registers a setting and its data.
  2239  *
  2285  *
  2240  * @since 2.7.0
  2286  * @since 2.7.0
       
  2287  * @since 3.0.0 The `misc` option group was deprecated.
       
  2288  * @since 3.5.0 The `privacy` option group was deprecated.
  2241  * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
  2289  * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
  2242  * @since 5.5.0 `$new_whitelist_options` was renamed to `$new_allowed_options`.
  2290  * @since 5.5.0 `$new_whitelist_options` was renamed to `$new_allowed_options`.
  2243  *              Please consider writing more inclusive code.
  2291  *              Please consider writing more inclusive code.
  2244  *
  2292  *
  2245  * @global array $new_allowed_options
  2293  * @global array $new_allowed_options
  2246  * @global array $wp_registered_settings
  2294  * @global array $wp_registered_settings
  2247  *
  2295  *
  2248  * @param string $option_group A settings group name. Should correspond to an allowed option key name.
  2296  * @param string $option_group A settings group name. Should correspond to an allowed option key name.
  2249  *                             Default allowed option key names include 'general', 'discussion', 'media',
  2297  *                             Default allowed option key names include 'general', 'discussion', 'media',
  2250  *                             'reading', 'writing', 'misc', 'options', and 'privacy'.
  2298  *                             'reading', 'writing', and 'options'.
  2251  * @param string $option_name The name of an option to sanitize and save.
  2299  * @param string $option_name The name of an option to sanitize and save.
  2252  * @param array  $args {
  2300  * @param array  $args {
  2253  *     Data used to describe the setting when registered.
  2301  *     Data used to describe the setting when registered.
  2254  *
  2302  *
  2255  *     @type string     $type              The type of data associated with this setting.
  2303  *     @type string     $type              The type of data associated with this setting.
  2367  *              Please consider writing more inclusive code.
  2415  *              Please consider writing more inclusive code.
  2368  *
  2416  *
  2369  * @global array $new_allowed_options
  2417  * @global array $new_allowed_options
  2370  * @global array $wp_registered_settings
  2418  * @global array $wp_registered_settings
  2371  *
  2419  *
  2372  * @param string          $option_group The settings group name used during registration.
  2420  * @param string   $option_group The settings group name used during registration.
  2373  * @param string          $option_name  The name of the option to unregister.
  2421  * @param string   $option_name  The name of the option to unregister.
  2374  * @param callable|string $deprecated   Deprecated.
  2422  * @param callable $deprecated   Optional. Deprecated.
  2375  */
  2423  */
  2376 function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
  2424 function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
  2377 	global $new_allowed_options, $wp_registered_settings;
  2425 	global $new_allowed_options, $wp_registered_settings;
  2378 
  2426 
  2379 	/*
  2427 	/*