Make WordPress Core


Ignore:
Timestamp:
08/01/2021 02:54:52 PM (5 years 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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.