Skip to content

Commit 2ba4a48

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/trunk@51524 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ac47170 commit 2ba4a48

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/wp-includes/theme.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,23 +966,31 @@ function validate_theme_requirements( $stylesheet ) {
966966
* Retrieves all theme modifications.
967967
*
968968
* @since 3.1.0
969+
* @since 5.9.0 The return value is always an array.
969970
*
970-
* @return array|void Theme modifications.
971+
* @return array Theme modifications.
971972
*/
972973
function 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' );
977979
if ( false === $theme_name ) {
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 ) {
982985
update_option( "theme_mods_$theme_slug", $mods );
983986
delete_option( "mods_$theme_name" );
984987
}
985988
}
989+
990+
if ( ! is_array( $mods ) ) {
991+
$mods = array();
992+
}
993+
986994
return $mods;
987995
}
988996

tests/phpunit/tests/option/themeMods.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ function test_theme_mod_set() {
1919
$this->assertSame( $expected, get_theme_mod( 'test_name' ) );
2020
}
2121

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();
29+
}
30+
2231
function test_theme_mod_update() {
2332
set_theme_mod( 'test_update', 'first_value' );
2433
$expected = 'updated_value';

0 commit comments

Comments
 (0)