Changeset 38910
- Timestamp:
- 10/25/2016 05:07:20 PM (7 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/option.php
r38832 r38910 56 56 if ( defined( 'WP_SETUP_CONFIG' ) ) 57 57 return false; 58 59 // Distinguish between `false` as a default, and not passing one. 60 $passed_default = func_num_args() > 1; 58 61 59 62 if ( ! wp_installing() ) { … … 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 … … 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 } … … 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 } … … 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 */ … … 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; … … 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 } -
trunk/tests/phpunit/tests/option/registration.php
r38690 r38910 41 41 42 42 /** 43 * @ticket 38176 44 */ 45 public function test_register_with_default() { 46 register_setting( 'test_group', 'test_default', array( 47 'default' => 'Fuck Cancer' 48 )); 49 50 $this->assertEquals( 'Fuck Cancer', get_option( 'test_default' ) ); 51 } 52 53 /** 54 * @ticket 38176 55 */ 56 public function test_register_with_default_override() { 57 register_setting( 'test_group', 'test_default', array( 58 'default' => 'Fuck Cancer' 59 )); 60 61 $this->assertEquals( 'Fuck Leukemia', get_option( 'test_default', 'Fuck Leukemia' ) ); 62 } 63 64 /** 43 65 * @expectedDeprecated register_setting 44 66 */
Note: See TracChangeset
for help on using the changeset viewer.