Make WordPress Core

Opened 2 years ago

Closed 18 months ago

#55195 closed defect (bug) (worksforme)

Calling `::parent()` on a child theme object where parent theme is missing `index.php` returns false in WP5.9

Reported by: robdxw's profile robdxw Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.9
Component: Themes Keywords: reporter-feedback
Focuses: Cc:

Description

Behaviour before v5.9:

<?php
$theme = wp_get_theme();
$theme->parent();

would return the parent theme object, even if the parent theme was missing its index.php file.

Since 5.9, this now returns false. This can result in fatal PHP errors being thrown if e.g. you call $theme->parent()->get('Version');, where previously the version would have been returned.

To recreate:

  1. Create a child theme of twentytwentyone with the following functions.php file:
<?php
<?php
$theme = wp_get_theme();
$theme->parent()->get('Version');

and the following styles.css:

/* 
Theme Name: twentytwentyone - Child
Description: 2021 - Child
Template: twentytwentyone
*/
  1. Remove (or rename) the index.php file from the parent theme.

In versions 5.8.3 and below, this would result in a "No matching template found" message being rendered on pages that should be using the parent index.php, and a warning on the admin themes page that the parent theme is missing its index.php template.

In version 5.9, this results in a fatal PHP error in both the frontend and the admin.

Investigation suggests the change was introduced in this commit: https://github.com/WordPress/WordPress/commit/f70fb51f99973274809caa80135d87f78b66b86a

I appreciate that themes shouldn't be missing their index.php file, but it's not clear from the version history that this change in behaviour was intentional, and I haven't found it documented anywhere.

Change History (12)

#1 @SergeyBiryukov
2 years ago

  • Milestone changed from Awaiting Review to 5.9.2

#2 @azouamauriac
2 years ago

  • Keywords needs-patch added

Hello thanks for your report. I'm able to reproduce the bug, unfortunately I don't find the cause for the moment. but it seems like $theme->parent() returns an objet in 5.8 while it returns false in 5.9...
Also the github link you've provided, it seems like it's just for a fonction renamed.

If you have more detail about the source of issue feel free to share it.
thanks.

#3 @robdxw
2 years ago

@azouamauriac There is one logic change in the commit I linked to, that seems to be the source of the issue. The function isn't just renamed, its implementation has been altered as well.

Before:

<?php
function wp_is_block_template_theme() {
        return is_readable( get_theme_file_path( '/block-templates/index.html' ) ) ||
                is_readable( get_theme_file_path( '/templates/index.html' ) );
}

After:

<?php
function wp_is_block_theme() {
        return wp_get_theme()->is_block_theme();
}

If I replace the contents of wp_is_block_theme() with the contents of wp_is_block_template_theme(), the previous behaviour is restored.

#4 @audrasjb
2 years ago

  • Milestone changed from 5.9.2 to 5.9.3

Moving to milestone 5.9.3 since we're about to release 5.9.2.

#5 @audrasjb
2 years ago

  • Milestone changed from 5.9.3 to 5.9.4

Given 5.9.3 is supposed to be released soon, and as this ticket is still waiting for a patch, let's move it to milestone 5.9.4.

#6 @kunalkataria
2 years ago

@robdxw I tried to reproduce the bug but I was getting the version number in branch 5.9. But it wasn't showing fatal error.

#7 @audrasjb
2 years ago

  • Milestone changed from 5.9.4 to 6.1

Moving this ticket to next major release since it wasn't addressed during this cycle. Anyone is welcome to move it back to 6.0.x minor releases cycle if a patch is ready to ship.

#8 @NomNom99
20 months ago

Hello everyone, this issue may have been resolved as it is not reproducible in:

  • WordPress 6.0
  • and WordPress (6.1-alpha-53848)

@audrasjb we may be able to close this one.

#9 @audrasjb
20 months ago

Good to know that the issue was resolved, though I can't see any change to this function since 5.9 using GIT blame: https://github.com/WordPress/wordpress-develop/blame/trunk/src/wp-includes/theme.php.

#10 @audrasjb
20 months ago

  • Keywords close added; needs-patch removed

#11 @desrosj
18 months ago

  • Keywords reporter-feedback added

@robdxw and @azouamauriac, as people who were able to reproduce this originally, could you please test again using 6.0 (or ideally 6.1 beta 3) to confirm that you're now unable to reproduce?

#12 @desrosj
18 months ago

  • Keywords close removed
  • Milestone 6.1 deleted
  • Resolution set to worksforme
  • Status changed from new to closed

Going to close this one out as worksforme. But if anyone is still able to reproduce, please reopen with more details.

Note: See TracTickets for help on using tickets.