diff --git src/wp-includes/theme.php src/wp-includes/theme.php
index 130b90e..3e72107 100644
|
|
|
function get_theme_mods() { |
| 832 | 832 | function get_theme_mod( $name, $default = false ) { |
| 833 | 833 | $mods = get_theme_mods(); |
| 834 | 834 | |
| 835 | | if ( isset( $mods[$name] ) ) { |
| | 835 | if ( isset( $mods[ $name ] ) && '' !== $mods[ $name ] ) { |
| 836 | 836 | /** |
| 837 | 837 | * Filter the theme modification, or 'theme_mod', value. |
| 838 | 838 | * |
| … |
… |
function get_theme_mod( $name, $default = false ) { |
| 845 | 845 | * |
| 846 | 846 | * @param string $current_mod The value of the current theme modification. |
| 847 | 847 | */ |
| 848 | | return apply_filters( "theme_mod_{$name}", $mods[$name] ); |
| | 848 | return apply_filters( "theme_mod_{$name}", $mods[ $name ] ); |
| 849 | 849 | } |
| 850 | 850 | |
| 851 | | if ( is_string( $default ) ) |
| | 851 | if ( is_string( $default ) ) { |
| 852 | 852 | $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ); |
| | 853 | } |
| 853 | 854 | |
| 854 | 855 | /** This filter is documented in wp-includes/theme.php */ |
| 855 | 856 | return apply_filters( "theme_mod_{$name}", $default ); |
| … |
… |
function set_theme_mod( $name, $value ) { |
| 881 | 882 | */ |
| 882 | 883 | $mods[ $name ] = apply_filters( "pre_set_theme_mod_$name", $value, $old_value ); |
| 883 | 884 | |
| | 885 | /* |
| | 886 | * If the filtered value is an empty string, we should remove it entirely before mods are saved to the database. |
| | 887 | * This will ensure that any requests for this theme modification name to get_theme_mod() will return false, |
| | 888 | * and subsequently the default value will be returned if one has been passed to the function. |
| | 889 | */ |
| | 890 | if ( '' === $mods[ $name ] ) { |
| | 891 | unset( $mods[ $name ] ); |
| | 892 | } |
| | 893 | |
| 884 | 894 | $theme = get_option( 'stylesheet' ); |
| 885 | 895 | update_option( "theme_mods_$theme", $mods ); |
| 886 | 896 | } |
diff --git tests/phpunit/tests/option/themeMods.php tests/phpunit/tests/option/themeMods.php
index 8bd8f5d..3927f46 100644
|
|
|
class Tests_Option_Theme_Mods extends WP_UnitTestCase { |
| 32 | 32 | $this->assertEquals( '', get_theme_mod( 'test_remove' ) ); |
| 33 | 33 | } |
| 34 | 34 | |
| | 35 | function test_theme_mod_returns_default_when_empty_string_was_previously_saved() { |
| | 36 | // This is to mimic what would happen when an old mod has already saved an empty string value. |
| | 37 | $name = 'test_empty_value'; |
| | 38 | $mods = get_theme_mods(); |
| | 39 | $mods[ $name ] = ''; |
| | 40 | $theme = get_option( 'stylesheet' ); |
| | 41 | update_option( "theme_mods_$theme", $mods ); |
| | 42 | |
| | 43 | $this->assertEquals( 'expected', get_theme_mod( 'test_empty_value', 'expected' ) ); |
| | 44 | } |
| | 45 | |
| | 46 | function test_theme_mod_returns_default_when_empty_string_is_not_saved() { |
| | 47 | remove_theme_mod( 'test_empty_value' ); |
| | 48 | set_theme_mod( 'test_empty_value', '' ); |
| | 49 | $this->assertEquals( 'expected', get_theme_mod( 'test_empty_value', 'expected' ) ); |
| | 50 | } |
| | 51 | |
| 35 | 52 | } |