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

Using wp_set_option_autoload() for theme options #5692

Closed
wants to merge 9 commits into from

Conversation

Rajinsharwar
Copy link

Using wp_set_option_autoload() to set no to the old theme's autoload option.

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


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.

@@ -866,6 +866,13 @@ function switch_theme( $stylesheet ) {

update_option( 'theme_switched', $old_theme->get_stylesheet() );

// Set autoload=no for the previous theme
$old_theme_stylesheet = $old_theme->get_stylesheet();
wp_set_option_autoload( "theme_mods_$old_theme_stylesheet", 'no' );
Copy link
Member

Choose a reason for hiding this comment

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

This adjustment alters the autoload option for the previous theme. If the user had activated multiple themes previously, it becomes essential to verify and modify the autoload value for those themes as well. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

@mukeshpanchal27 I think this is only an issue for existing sites that had multiple themes active before updating to WordPress 6.5. So I think this is better addressed separately via your new ticket https://core.trac.wordpress.org/ticket/59975 and #5711.

Any site that starts with WordPress 6.5 should not have this problem as every time the theme is switched the previous theme's theme mods are set to not autoload. So there should at no point be more than one theme with autoloaded theme mods.

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.

Thank you for the PR @Rajinsharwar, I left a few points of feedback.

Would you also be able to add a unit test for the new functionality in switch_theme()?

@@ -866,6 +866,13 @@ function switch_theme( $stylesheet ) {

update_option( 'theme_switched', $old_theme->get_stylesheet() );

// Set autoload=no for the previous theme
$old_theme_stylesheet = $old_theme->get_stylesheet();
wp_set_option_autoload( "theme_mods_$old_theme_stylesheet", 'no' );
Copy link
Member

Choose a reason for hiding this comment

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

@mukeshpanchal27 I think this is only an issue for existing sites that had multiple themes active before updating to WordPress 6.5. So I think this is better addressed separately via your new ticket https://core.trac.wordpress.org/ticket/59975 and #5711.

Any site that starts with WordPress 6.5 should not have this problem as every time the theme is switched the previous theme's theme mods are set to not autoload. So there should at no point be more than one theme with autoloaded theme mods.

Comment on lines +870 to +871
$old_theme_stylesheet = $old_theme->get_stylesheet();
wp_set_option_autoload( "theme_mods_$old_theme_stylesheet", 'no' );
Copy link
Member

Choose a reason for hiding this comment

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

We need to also cover a potential parent theme here (i.e. if the old template is different from the old stylesheet). We could then use wp_set_options_autoload() to reduce the database requests needed.

Suggested change
$old_theme_stylesheet = $old_theme->get_stylesheet();
wp_set_option_autoload( "theme_mods_$old_theme_stylesheet", 'no' );
$old_theme_mods = array_unique(
array(
"theme_mods_{$old_theme->get_stylesheet()}",
"theme_mods_{$old_theme->get_template()}"
)
);
wp_set_options_autoload( $old_theme_mods, 'no' );

@@ -866,6 +866,13 @@ function switch_theme( $stylesheet ) {

update_option( 'theme_switched', $old_theme->get_stylesheet() );

// Set autoload=no for the previous theme
Copy link
Member

Choose a reason for hiding this comment

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

Missing period after inline comment.

Suggested change
// Set autoload=no for the previous theme
// Set autoload=no for the previous theme.

Comment on lines +873 to +874
// Set autoload=yes for the switched theme if not already set
wp_set_option_autoload( "theme_mods_$stylesheet", 'yes' );
Copy link
Member

Choose a reason for hiding this comment

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

Same here:

Suggested change
// Set autoload=yes for the switched theme if not already set
wp_set_option_autoload( "theme_mods_$stylesheet", 'yes' );
// Set autoload=yes for the switched to theme if not already set.
$new_theme_mods = array_unique(
array(
"theme_mods_{$stylesheet}",
"theme_mods_{$template}"
)
);
wp_set_options_autoload( $new_theme_mods, 'yes' );

@felixarntz
Copy link
Member

Closing in favor of #5706. Thanks for working on this!

@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
3 participants