Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templates: Search for old template names in the parent theme too #36910

Merged
merged 3 commits into from
Dec 13, 2021

Conversation

scruffian
Copy link
Contributor

Description

In #36647 we added a fallback for themes that don't have templates in the new directory structure. However the checks only looked in the child theme, not the parent. This adds the same check to the parent theme, so that templates in child themes continue to load as before

How has this been tested?

This is quite hard to test as most child themes specify their own index.html.
This PR is an example of theme that doesn't: Automattic/themes#5094

Screenshots

Before:
Screenshot 2021-11-26 at 12 16 40

After:
Screenshot 2021-11-26 at 12 15 45

Types of changes

Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

$parent_root_dir = get_theme_root( $parent_theme_name );
$parent_theme_dir = "$parent_root_dir/$parent_theme_name";

if ( is_readable( $theme_dir . '/block-templates/index.html' ) || is_readable( $parent_theme_dir . '/block-templates/index.html' ) ) {
Copy link
Contributor

@youknowriad youknowriad Nov 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function here is supposed to be called with a theme stylesheet, so there shouldn't be a need for this added check. Maybe there's an issue elsewhere where we call this and we're not calling it with the parent theme as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forget the first comment, I understand the issue better now and I have separate thoughts :)

What happens if the child theme has been migrated but not the parent, it seems this PR will break that behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the fix is to actually check the presence of the folder and not the index file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would work I think, although I guess its possible that a child theme could not have the folder at all and just supply a theme.json.

If the child has been migrated before the parent then I would expect things to break to be honest!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would work I think, although I guess its possible that a child theme could not have the folder at all and just supply a theme.json.

In that case, it doesn't matter since this function is about returning the folders.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point :)

$parent_root_dir = get_theme_root( $parent_theme_name );
$parent_theme_dir = "$parent_root_dir/$parent_theme_name";

if ( is_readable( $theme_dir . '/block-templates/' ) || is_readable( $parent_theme_dir . '/block-templates/' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just remove || is_readable( $parent_theme_dir . '/block-templates/' ), since we're calling the function for both themes (parent and child), this should work right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. If we did that then we'd be back to the changes that are already in trunk, since the other lines don't actually do anything except assign variables.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the issue was about themes that are child themes, override some templates but not "index.html" so it's different than trunk? Are there any other use-cases here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They might not override any templates at all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They might not override any templates at all.

In that case that function is useless and doesn't matter what it returns.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Child themes can simply provide a different theme.json file and no other changes.

In that case it doesn't matter what this function returns.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I create a child theme with no templates or template parts I see this message:

Screenshot 2021-12-13 at 11 08 48

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I think we had an issue with the template part block's code where the child theme was not handled properly and it should be fixed in 004cba5

The root issue is that get_theme_file_path function tries to fallback to the parent theme automatically but it doesn't account with the fact that the path could change slightly between parent and child themes (folder names)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to ship this if the fix in that commit is good enough for you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good thanks

@youknowriad youknowriad added [Type] Bug An existing feature does not function as intended Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Dec 13, 2021
@scruffian scruffian merged commit 7e121d9 into trunk Dec 13, 2021
@scruffian scruffian deleted the fix/templates branch December 13, 2021 13:54
@github-actions github-actions bot added this to the Gutenberg 12.2 milestone Dec 13, 2021
@noisysocks noisysocks removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Dec 14, 2021
noisysocks pushed a commit that referenced this pull request Dec 14, 2021
)

* Templates: Search for old template names in the parent theme too

* check for the directory rather than the file

* Fix template part file lookup

Co-authored-by: Riad Benguella <benguella@gmail.com>
noisysocks added a commit to noisysocks/wordpress-develop that referenced this pull request Dec 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants