Opened 11 months ago
Closed 11 months ago
#59677 closed defect (bug) (fixed)
Incorrect theme returned when switching to blog in multisite subsite
Reported by: | codex-m | Owned by: | flixos90 |
---|---|---|---|
Milestone: | 6.4 | Priority: | normal |
Severity: | major | Version: | 6.4 |
Component: | Networks and Sites | Keywords: | has-patch has-unit-tests dev-reviewed |
Focuses: | multisite, performance | Cc: |
Description
I think I have found issue with the coming WordPress 6.4. I tested with latest WordPress 6.4 RC1.
Environment
PHP Version: 8.0
Operating system: Ubuntu 20.04
Test code to reproduce the issue (e.g. save this as reproduce.php):
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); ini_set('error_reporting', E_ALL); require_once('wp-load.php'); switch_to_blog(2); $template_path = get_template_directory(); $stylesheet_path = get_stylesheet_directory(); restore_current_blog(); echo "PARENT THEME: {$template_path}"; echo "<br />"; echo "CHILD THEME: {$stylesheet_path}";
How to reproduce the issue:
1.) Setup a fresh WordPress multisite.
2.) Create a new subsite (BLOG ID: 2)
3.) Activate a non-default theme (with parent and child theme) subsite BLOG ID: 2. For example, in this testing I use Astra parent and child theme).
4.) Run the test script on the main site by calling it as:
https://yourmultisite.tld/reproduce.php
Test result:
1.) WordPress 6.3.2 OUTPUT:
PARENT THEME: /home/emerson/sourcecode/subdomaintest/wp-content/themes/astra
CHILD THEME: /home/emerson/sourcecode/subdomaintest/wp-content/themes/astra-child
2.) Latest WordPress 6.4 RC1 OUTPUT:
PARENT THEME: /home/emerson/sourcecode/subdomaintest/wp-content/themes/twentytwentythree
CHILD THEME: /home/emerson/sourcecode/subdomaintest/wp-content/themes/twentytwentythree
Test summary:
I expected to see the actual theme used in the subsite (blog ID: 2) instead I got the currently activated main site theme (incorrect).
Solution
I think the issue is that in WordPress 6.4, the result of these functions (get_template_directory()
& get_stylesheet_directory()
) are now stored into global variables for performance reasons. However the issue is that in multisite - these can be set first before the blog is switched thus preventing to return the correct value once the blog is switched.
I think we can fix this by checking if the call is made in multisite and if the blog is already switched. Once switched, we unset these global variables so it will return the correct values.
This is only my suggestion to fix this issue. Please attached patch. Thanks!
Attachments (3)
Change History (14)
#1
@
11 months ago
- Milestone changed from Awaiting Review to 6.4
Moving into 6.4 milestone for investigation.
@clarkeemily @flixos90 @joemcgill for awareness.
This ticket was mentioned in PR #5532 on WordPress/wordpress-develop by @jeremyfelt.
11 months ago
#3
- Keywords has-patch has-unit-tests added
This nulls the global wp_template_path
and wp_stylesheet_path
data whenever a site is switched or restored so that the next call to get_stylesheet_directory()
or get_template_directory()
will provide fresh data.
See https://core.trac.wordpress.org/ticket/59677
Trac ticket: https://core.trac.wordpress.org/ticket/59677
#4
@
11 months ago
It seems like @codex-m has the right idea in patch3.diff, but I think we can move the nulling of that data to switch_to_blog()
and restore_current_blog()
so that it's only cleared once instead of every time get_stylesheet_directory()
or get_template_directory()
is called.
I've added an attempt at this in PR#5532 with some tests that clarify what's happening.
#5
@
11 months ago
- Owner set to flixos90
- Status changed from new to reviewing
Thank you for the report @codex-m, great catch. And thanks for the PR @jeremyfelt. I think that solution looks good, unsetting these globals when switching the site should be the most efficient solution to that bug.
#7
@
11 months ago
- Keywords dev-feedback added
- Resolution fixed deleted
- Status changed from closed to reopened
Reopening for backporting [56974] to the 6.4 branch.
@costdev @joemcgill Could one of you maybe give this a second sign-off?
@flixos90 commented on PR #5532:
11 months ago
#8
Committed in https://core.trac.wordpress.org/changeset/56974
#9
@
11 months ago
Thank you guys! Looks good to me. I will do another quick check with our plugins compatibility for WP 6.4 once this is available https://core.svn.wordpress.org/branches/6.4/
Cheers.
Suggested fix