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.
Built from https://develop.svn.wordpress.org/trunk@51524


git-svn-id: http://core.svn.wordpress.org/trunk@51135 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2021-08-01 14:55:56 +00:00
parent 963dd5f06d
commit 9e33977bc9
2 changed files with 10 additions and 2 deletions

View File

@ -966,23 +966,31 @@ function validate_theme_requirements( $stylesheet ) {
* Retrieves all theme modifications. * Retrieves all theme modifications.
* *
* @since 3.1.0 * @since 3.1.0
* @since 5.9.0 The return value is always an array.
* *
* @return array|void Theme modifications. * @return array Theme modifications.
*/ */
function get_theme_mods() { function get_theme_mods() {
$theme_slug = get_option( 'stylesheet' ); $theme_slug = get_option( 'stylesheet' );
$mods = get_option( "theme_mods_$theme_slug" ); $mods = get_option( "theme_mods_$theme_slug" );
if ( false === $mods ) { if ( false === $mods ) {
$theme_name = get_option( 'current_theme' ); $theme_name = get_option( 'current_theme' );
if ( false === $theme_name ) { if ( false === $theme_name ) {
$theme_name = wp_get_theme()->get( 'Name' ); $theme_name = wp_get_theme()->get( 'Name' );
} }
$mods = get_option( "mods_$theme_name" ); // Deprecated location. $mods = get_option( "mods_$theme_name" ); // Deprecated location.
if ( is_admin() && false !== $mods ) { if ( is_admin() && false !== $mods ) {
update_option( "theme_mods_$theme_slug", $mods ); update_option( "theme_mods_$theme_slug", $mods );
delete_option( "mods_$theme_name" ); delete_option( "mods_$theme_name" );
} }
} }
if ( ! is_array( $mods ) ) {
$mods = array();
}
return $mods; return $mods;
} }

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.9-alpha-51523'; $wp_version = '5.9-alpha-51524';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.