Make WordPress Core

Ticket #28637: 28637.3.diff

File 28637.3.diff, 2.7 KB (added by valendesigns, 9 years ago)
  • src/wp-includes/theme.php

    diff --git src/wp-includes/theme.php src/wp-includes/theme.php
    index 130b90e..3e72107 100644
    function get_theme_mods() { 
    832832function get_theme_mod( $name, $default = false ) {
    833833        $mods = get_theme_mods();
    834834
    835         if ( isset( $mods[$name] ) ) {
     835        if ( isset( $mods[ $name ] ) && '' !== $mods[ $name ] ) {
    836836                /**
    837837                 * Filter the theme modification, or 'theme_mod', value.
    838838                 *
    function get_theme_mod( $name, $default = false ) { 
    845845                 *
    846846                 * @param string $current_mod The value of the current theme modification.
    847847                 */
    848                 return apply_filters( "theme_mod_{$name}", $mods[$name] );
     848                return apply_filters( "theme_mod_{$name}", $mods[ $name ] );
    849849        }
    850850
    851         if ( is_string( $default ) )
     851        if ( is_string( $default ) ) {
    852852                $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
     853        }
    853854
    854855        /** This filter is documented in wp-includes/theme.php */
    855856        return apply_filters( "theme_mod_{$name}", $default );
    function set_theme_mod( $name, $value ) { 
    881882         */
    882883        $mods[ $name ] = apply_filters( "pre_set_theme_mod_$name", $value, $old_value );
    883884
     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
    884894        $theme = get_option( 'stylesheet' );
    885895        update_option( "theme_mods_$theme", $mods );
    886896}
  • tests/phpunit/tests/option/themeMods.php

    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 { 
    3232                $this->assertEquals( '', get_theme_mod( 'test_remove' ) );
    3333        }
    3434
     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
    3552}