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

Multisite: Add WP_Site_State class for efficient site state management #8542

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

Sukhendu2002
Copy link

@Sukhendu2002 Sukhendu2002 commented Mar 19, 2025

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

This PR adds a new WP_Site_State class and corresponding helper functions that provide methods for managing site state in WordPress Multisite.


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.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@Sukhendu2002 Sukhendu2002 marked this pull request as ready for review March 19, 2025 09:29
Copy link

github-actions bot commented Mar 19, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props sukhendu2002, tfrommen.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@tfrommen tfrommen 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 upgrading my 8-year-old patch to 2025, and turning it into a GitHub PR. 🙏

I left some inline comments, none blocking at all.

Has the wp_xmlrpc_server class not been updated deliberately? I know it's not something many people use (and is even recommended against in many contexts), but still.

* @return WP_Site_State A snapshot of the current site state.
*/
function get_site_state() {
require_once ABSPATH . WPINC . '/class-wp-site-state.php';
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't make much of a difference, but this could also be done in wp-includes/ms-settings.php, making this function pure.

Copy link
Author

Choose a reason for hiding this comment

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

That's a great suggestion. Moving the require to ms-settings.php would make the get_site_state() function more efficient. I'll implement this change.

* @param WP_Site_State $state The site state object to restore to.
* @return bool True on success, false on failure.
*/
function restore_site_state( $state ) {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we really need this function. Looking at the Locale API, there is no function to restore locales. It's always done using the relevant method on the (global) locale switcher class instance.

Personally, I think having the above factory function is fine, but doing $site_state->restore(); instead of restore_site_state( $site_state ); feels more natural, and also in line with several other classes that don't have API function wrappers around them.

Thoughts?

Copy link
Author

Choose a reason for hiding this comment

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

I see your point about directly using the class method instead of a wrapper. I initially created the wrapper function because it follows the pattern used for switch_to_blog() and restore_current_blog()

However, I'm open to removing it if you feel the direct method call is more appropriate. The Locale API example you mentioned makes sense.

@Sukhendu2002
Copy link
Author

Hey @tfrommen, Thank you for reviewing the PR! I really appreciate your detailed feedback.

You're right, I didn't update the XML-RPC class since I was focusing on the core multisite functionality first. I can certainly look at improving this class too if you think it would be valuable.

I'll update the PR with fixes for the identified issues!

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.

2 participants