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

Set autoload=no for previous theme after switching themes #5706

Closed
wants to merge 7 commits into from

Conversation

mukeshpanchal27
Copy link
Member

@mukeshpanchal27 mukeshpanchal27 commented Nov 27, 2023

Trac ticket: https://core.trac.wordpress.org/ticket/39537

This PR sets autoload=no for the old themes after switching between different themes, utilizing the wp_set_option_autoload_values function.

Within the switch_theme function, we retrieve two themes: the current theme and the previous theme. However, there isn't a direct method to obtain all previously activated themes. For instance, if a user has used 4-5 themes and all of these themes have theme_mods_* options autoloaded, this method involves querying to retrieve all the theme_mods_* and subsequently setting autoload=no for all themes except the current one.

This will be done using separate ticket: 59975

This PR only updates the autoload value for old and new themes.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@mukeshpanchal27 mukeshpanchal27 marked this pull request as ready for review November 27, 2023 10:09
@swissspidy
Copy link
Member

However, there isn't a direct method to obtain all previously activated themes. For instance, if a user has used 4-5 themes and all of these themes have theme_mods_* options autoloaded, this method involves querying to retrieve all the theme_mods_* and subsequently setting autoload=no for all themes except the current one.

This (multiple previous theme mods with autoload=yes) is basically just a one time scenario, right? After that, only updating the old theme's option would be enough.

@mukeshpanchal27
Copy link
Member Author

This (multiple previous theme mods with autoload=yes) is basically just a one time scenario, right? After that, only updating the old theme's option would be enough.

Yes. Could we add new function in src/wp-admin/includes/upgrade.php for 6.5 and set autoload=no for all stored theme options except current theme? and change the PR code to set autoload option for old theme only?

@swissspidy
Copy link
Member

Yeah we could. This would have the benefit that it will apply this change immediately upon upgrade, and not only upon the next theme change. And the change in switch_theme would become very straightforward.

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

I like the idea you and @swissspidy are discussing about making the initial change part of an upgrade routine and then only changing the autoload values for the old theme and new theme. I left a few nitpicks in the mean time for you to consider as part of the updated implementation, but neither blocking.

src/wp-includes/theme.php Outdated Show resolved Hide resolved
src/wp-includes/theme.php Outdated Show resolved Hide resolved
src/wp-includes/theme.php Outdated Show resolved Hide resolved
src/wp-includes/theme.php Outdated Show resolved Hide resolved
src/wp-includes/theme.php Outdated Show resolved Hide resolved
src/wp-includes/theme.php Outdated Show resolved Hide resolved
@mukeshpanchal27
Copy link
Member Author

Thanks @swissspidy @joemcgill and @westonruter for the review. I have updates PR so it will only updates the old and new theme autoload option not for other olds, for that i will open followup issue and raise PR for it.

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

This looks good to me, @mukeshpanchal27!

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@mukeshpanchal27 Thanks for the PR, it looks solid so far. However, two things are missing as far as I can tell.

Another question: I first saw there was #5692, how does this PR relate to that one?

src/wp-includes/theme.php Show resolved Hide resolved
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@mukeshpanchal27 Per #5706 (comment), most of my feedback can be ignored.

The only thing I'd agree we should change is https://github.com/WordPress/wordpress-develop/pull/5706/files#r1407978384.

@mukeshpanchal27
Copy link
Member Author

I have invested some time in examining the behavior of the cache that requires reloading or not. To address this issue, we can consider several options:

Option 1: Utilize the wp_set_option_autoload_values function to reset the autoload value. This function deletes all caches in this section of the code. Therefore, we should update the autoload option after deleting the cache within the same function.

Option 2: Remove the autoload option cache assertions from unit tests. Theoretically, these checks might not be necessary.

Option 3: Revert 041eaad to ensure that the autoload option cache reloads after a theme switch.

In my opinion, the second option seems preferable. When the cache is deleted, it seems logical to run a query to retrieve that option and then reload the cache.

@felixarntz @joemcgill @swissspidy @westonruter, I would greatly appreciate your thoughts on this matter. Thank you!

@westonruter
Copy link
Member

Option 4: Add wp_load_alloptions() to the test case.

		// Make sure that autoloaded options are cached properly.
+   		wp_load_alloptions();
  		$autoloaded_options = wp_cache_get( 'alloptions', 'options' );
  		$this->assertArrayNotHasKey( "theme_mods_$new_theme_stylesheet", $autoloaded_options, "Option theme_mods_$new_theme_stylesheet not deleted from alloptions cache" );
  		$this->assertArrayHasKey( "theme_mods_$current_theme_stylesheet", $autoloaded_options, "Option theme_mods_$current_theme_stylesheet unexpectedly deleted from alloptions cache" );

@felixarntz
Copy link
Member

@mukeshpanchal27 I think we should go with option 2. The assertions for whether the cache is refreshed IMO aren't suitable here, as they are related to the wp_set_option_autoload_values() function itself, which already has sufficient coverage.

For this change, the only thing we need to test is that the autoload values have been correctly updated, which is the main purpose of the function and this change.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

tests/phpunit/tests/theme/autoloadThemeMods.php Outdated Show resolved Hide resolved
tests/phpunit/tests/theme/autoloadThemeMods.php Outdated Show resolved Hide resolved
tests/phpunit/tests/theme/autoloadThemeMods.php Outdated Show resolved Hide resolved
@joemcgill
Copy link
Member

I agree with @mukeshpanchal27 here:

In my opinion, the second option seems preferable. When the cache is deleted, it seems logical to run a query to retrieve that option and then reload the cache.

The only reason the assertions testing the cache values are useful are if the behavior we want to confirm is that the cache is refreshed when the theme mod's autoload values are updated. I don't think that should be the responsibility of the switch_theme() function, and instead will naturally take place the next time that option is requested.

@mukeshpanchal27
Copy link
Member Author

Thanks for thoughts and feedback. The PR is ready for final review and commit.

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

The updates look good to me. The only thing I would consider changing is moving the new test method to the Test_Themes class in /tests/phpunit/tests/theme.php rather than creating a new class for just this one method, unless we think there will be more tests that need to be added here. Not a blocker, though.

@felixarntz
Copy link
Member

@felixarntz felixarntz closed this Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants