WordPress.org

Make WordPress Core

Changeset 51524


Ignore:
Timestamp:
08/01/2021 02:54:52 PM (6 months ago)
Author:
SergeyBiryukov
Message:

Themes: Make sure get_theme_mods() always returns an array.

This avoids a "Cannot access offset of type string on string" fatal error in set_theme_mod() on PHP 8 if the theme_mods_$theme_slug option has an incorrect value, e.g. an empty string instead of an array.

With this change, set_theme_mod() should be able to resolve the issue by saving a correct value.

Follow-up to [15736], [15739], [30672], [32629], [32632].

Props xknown.
See #51423.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/theme.php

    r51251 r51524  
    967967 *
    968968 * @since 3.1.0
    969  *
    970  * @return array|void Theme modifications.
     969 * @since 5.9.0 The return value is always an array.
     970 *
     971 * @return array Theme modifications.
    971972 */
    972973function get_theme_mods() {
    973974    $theme_slug = get_option( 'stylesheet' );
    974975    $mods       = get_option( "theme_mods_$theme_slug" );
     976
    975977    if ( false === $mods ) {
    976978        $theme_name = get_option( 'current_theme' );
     
    978980            $theme_name = wp_get_theme()->get( 'Name' );
    979981        }
     982
    980983        $mods = get_option( "mods_$theme_name" ); // Deprecated location.
    981984        if ( is_admin() && false !== $mods ) {
     
    984987        }
    985988    }
     989
     990    if ( ! is_array( $mods ) ) {
     991        $mods = array();
     992    }
     993
    986994    return $mods;
    987995}
  • trunk/tests/phpunit/tests/option/themeMods.php

    r48937 r51524  
    1818        set_theme_mod( 'test_name', $expected );
    1919        $this->assertSame( $expected, get_theme_mod( 'test_name' ) );
     20    }
     21
     22    /**
     23     * @ticket 51423
     24     */
     25    function test_theme_mod_set_with_invalid_theme_mods_option() {
     26        $theme_slug = get_option( 'stylesheet' );
     27        update_option( 'theme_mods_' . $theme_slug, '' );
     28        self::test_theme_mod_set();
    2029    }
    2130
Note: See TracChangeset for help on using the changeset viewer.