Ticket #38176: 38176.3.diff
File 38176.3.diff, 3.5 KB (added by , 8 years ago) |
---|
-
src/wp-includes/option.php
56 56 if ( defined( 'WP_SETUP_CONFIG' ) ) 57 57 return false; 58 58 59 // Distinguish between `false` as a default, and not passing one. 60 $passed_default = func_num_args() > 1; 61 59 62 if ( ! wp_installing() ) { 60 63 // prevent non-existent options from triggering multiple queries 61 64 $notoptions = wp_cache_get( 'notoptions', 'options' ); … … 67 70 * 68 71 * @since 3.4.0 69 72 * @since 4.4.0 The `$option` parameter was added. 73 * @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value. 70 74 * 71 75 * @param mixed $default The default value to return if the option does not exist 72 76 * in the database. 73 77 * @param string $option Option name. 78 * @param bool $passed_default Was `get_option()` passed a default value? 74 79 */ 75 return apply_filters( "default_option_{$option}", $default, $option );80 return apply_filters( "default_option_{$option}", $default, $option, $passed_default ); 76 81 } 77 82 78 83 $alloptions = wp_load_alloptions(); … … 97 102 wp_cache_set( 'notoptions', $notoptions, 'options' ); 98 103 99 104 /** This filter is documented in wp-includes/option.php */ 100 return apply_filters( 'default_option_' . $option, $default, $option );105 return apply_filters( 'default_option_' . $option, $default, $option, $passed_default ); 101 106 } 102 107 } 103 108 } … … 109 114 $value = $row->option_value; 110 115 } else { 111 116 /** This filter is documented in wp-includes/option.php */ 112 return apply_filters( 'default_option_' . $option, $default, $option );117 return apply_filters( 'default_option_' . $option, $default, $option, $passed_default ); 113 118 } 114 119 } 115 120 … … 1835 1840 * @type string $description A description of the data attached to this setting. 1836 1841 * @type callable $sanitize_callback A callback function that sanitizes the option's value. 1837 1842 * @type bool $show_in_rest Whether data associated with this setting should be included in the REST API. 1843 * @type mixed $default Default value when calling `get_option()`. 1838 1844 * } 1839 1845 */ 1840 1846 function register_setting( $option_group, $option_name, $args = array() ) { … … 1886 1892 if ( ! empty( $args['sanitize_callback'] ) ) { 1887 1893 add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] ); 1888 1894 } 1895 if ( array_key_exists( 'default', $args ) ) { 1896 add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 ); 1897 } 1889 1898 1890 1899 $wp_registered_settings[ $option_name ] = $args; 1891 1900 } … … 1950 1959 1951 1960 return $wp_registered_settings; 1952 1961 } 1962 1963 /** 1964 * Filter the default value for the option. 1965 * 1966 * For settings which register a default setting in `register_setting()`, this 1967 * function is added as a filter to `default_option_{$option}`. 1968 * 1969 * @since 4.7.0 1970 * 1971 * @param mixed $default Existing default value to return. 1972 * @param string $option Option name. 1973 * @param bool $passed_default Was `get_option()` passed a default value? 1974 * @return mixed Filtered default value. 1975 */ 1976 function filter_default_option( $default, $option, $passed_default ) { 1977 if ( $passed_default ) { 1978 return $default; 1979 } 1980 1981 $registered = get_registered_settings(); 1982 if ( empty( $registered[ $option ] ) ) { 1983 return $default; 1984 } 1985 1986 return $registered[ $option ]['default']; 1987 }