Opened 4 weeks ago
Last modified 4 weeks ago
#63128 new defect (bug)
Too Early Caching is breaking Block Child Themes
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | 6.9 | Priority: | normal |
Severity: | major | Version: | |
Component: | Themes | Keywords: | has-testing-info dev-feedback has-patch |
Focuses: | Cc: |
Description
This is a follow-up for this ticket: #63086
I'm going to copy/paste the most relevant aspects of my last research:
I've been able to narrow down to the point that I've found that the problem is exactly here:
<?php $parent_dir = dirname( $this->stylesheet ); $directories = search_theme_directories(); if ( '.' !== $parent_dir && file_exists( $this->theme_root . '/' . $parent_dir . '/' . $this->template . '/index.php' ) ) { $this->template = $parent_dir . '/' . $this->template; } elseif ( $directories && isset( $directories[ $this->template ] ) ) { /* * Look for the template in the search_theme_directories() results, in case it is in another theme root. * We don't look into directories of themes, just the theme root. */ $theme_root_template = $directories[ $this->template ]['theme_root']; } else { // Parent theme is missing. $this->errors = new WP_Error( 'theme_no_parent', sprintf( /* translators: %s: Theme directory name. */ __( 'The parent theme is missing. Please install the "%s" parent theme.' ), esc_html( $this->template ) ) ); $this->cache_add( 'theme', array( 'block_template_folders' => $this->get_block_template_folders(), 'block_theme' => $this->is_block_theme(), 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template, ) ); $this->parent = new WP_Theme( $this->template, $this->theme_root, $this ); return; }
The cache_add
is done too early.
By removing this fragment
<?php $this->cache_add( 'theme', array( 'block_template_folders' => $this->get_block_template_folders(), 'block_theme' => $this->is_block_theme(), 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template, ) );
It works well.
This code is historical, before the introduction of block themes, where having a index.php
in parent was a staple, useful for checks, but with block themes, this isn't the case anymore
For example, adding this check here:
<?php if ( ! $this->is_block_theme() ) { $this->cache_add( 'theme', array( 'block_template_folders' => $this->get_block_template_folders(), 'block_theme' => $this->is_block_theme(), 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template, ) ); }
Solves this report also, but cache should be populated after the parent call.
Maybe the solution is simply considering another check specific for block themes instead of asking devs as the proposed solution in #63086 for not using this function between 'muplugins_loaded' and 'plugins_loaded' (in a mu-plugin basically)
Change History (2)
This ticket was mentioned in PR #8545 on WordPress/wordpress-develop by @SirLouen.
4 weeks ago
#2
- Keywords has-patch added
I was going crazy with so many repetitions of the same code in the WP_Theme
class. Extremely difficult to understand the constructor and to track down the problems. But still, I think that comments are not very self-explanatory.
This patch partially refactors the constructor and sorts this issue (that is mostly caused because of the current convoluted state of such constructor). And still, I'm not 100% sure that this is the best solution.
Apart from this, here are the testing instructions for this patch:
- I have created an adhoc plugin that checks the result of wp_is_block_theme() function in each step
- Install this plugin as a mu-plugin
- Install this Child theme and activate it
- Make sure you have TwentyTwentyFive also installed
- The theme should be loading without issues.
- Activate a non-block theme ⇾ Go to customizer, check there are no notices there
- Add the short code [block_theme_detection] to a post and check if its passing in all the actions hooks
Trac ticket: https://core.trac.wordpress.org/ticket/63128
Thanks, @SirLouen! I'm marking this for 6.9 to keep visibility on this issue.