mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Sukhendu2002
wants to merge
3
commits into
WordPress:trunk
Choose a base branch
from
Sukhendu2002:add/multisite-site-state-class
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
/** | ||
* Site State API: WP_Site_State class | ||
* | ||
* @package WordPress | ||
* @subpackage Multisite | ||
* @since 6.8.0 | ||
*/ | ||
|
||
/** | ||
* Core class used for efficiently switching and restoring site state. | ||
* | ||
* @since 6.8.0 | ||
*/ | ||
#[AllowDynamicProperties] | ||
class WP_Site_State { | ||
/** | ||
* Current site ID. | ||
* | ||
* @since 6.8.0 | ||
* @var int | ||
*/ | ||
private $site_id; | ||
|
||
/** | ||
* The switched stack. | ||
* | ||
* @since 6.8.0 | ||
* @var array | ||
*/ | ||
private $switched_stack = array(); | ||
|
||
/** | ||
* Whether or not we're currently switched. | ||
* | ||
* @since 6.8.0 | ||
* @var bool | ||
*/ | ||
private $switched = false; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* Stores the current site ID, the switched stack, and the switched state. | ||
* | ||
* @since 6.8.0 | ||
*/ | ||
public function __construct() { | ||
global $_wp_switched_stack, $switched; | ||
|
||
$this->site_id = get_current_blog_id(); | ||
|
||
if ( ! empty( $_wp_switched_stack ) ) { | ||
$this->switched_stack = $_wp_switched_stack; | ||
} | ||
|
||
$this->switched = ! empty( $switched ); | ||
} | ||
|
||
/** | ||
* Restores the stored site state. | ||
* | ||
* @since 6.8.0 | ||
* | ||
* @return bool True on success, false if no state change was needed. | ||
*/ | ||
public function restore() { | ||
global $_wp_switched_stack, $switched, $wpdb, $blog_id, $table_prefix; | ||
|
||
$current_blog_id = get_current_blog_id(); | ||
|
||
// If we're already on the target blog, just update the global state. | ||
if ( $current_blog_id === $this->site_id ) { | ||
$_wp_switched_stack = $this->switched_stack; | ||
$switched = $this->switched; | ||
return true; | ||
} | ||
|
||
$wpdb->set_blog_id( $this->site_id ); | ||
$table_prefix = $wpdb->get_blog_prefix(); | ||
$blog_id = $this->site_id; | ||
|
||
if ( function_exists( 'wp_cache_switch_to_blog' ) ) { | ||
wp_cache_switch_to_blog( $blog_id ); | ||
} | ||
|
||
// Restore the switched stack and state. | ||
$_wp_switched_stack = $this->switched_stack; | ||
$switched = $this->switched; | ||
|
||
/** | ||
* Fires when the blog is switched. | ||
* | ||
* @since MU (3.0.0) | ||
* @since 5.4.0 The `$context` parameter was added. | ||
* | ||
* @param int $new_blog_id New blog ID. | ||
* @param int $prev_blog_id Previous blog ID. | ||
* @param string $context Additional context. Accepts 'switch' when called from switch_to_blog() | ||
* or 'restore' when called from restore_current_blog(). | ||
*/ | ||
do_action( 'switch_blog', $blog_id, $current_blog_id, 'restore_state' ); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Gets the site ID stored in this state. | ||
* | ||
* @since 6.8.0 | ||
* | ||
* @return int The site ID. | ||
*/ | ||
public function get_site_id() { | ||
return $this->site_id; | ||
} | ||
|
||
/** | ||
* Gets the switched stack stored in this state. | ||
* | ||
* @since 6.8.0 | ||
* | ||
* @return array The switched stack. | ||
*/ | ||
public function get_switched_stack() { | ||
return $this->switched_stack; | ||
} | ||
|
||
/** | ||
* Gets the switched status stored in this state. | ||
* | ||
* @since 6.8.0 | ||
* | ||
* @return bool Whether the site was switched. | ||
*/ | ||
public function is_switched() { | ||
return $this->switched; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
if ( is_multisite() ) : | ||
|
||
/** | ||
* @group multisite | ||
* @group ms-site | ||
*/ | ||
class Tests_Multisite_WpSiteState extends WP_UnitTestCase { | ||
protected static $site_ids; | ||
protected static $network_id; | ||
|
||
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { | ||
self::$network_id = $factory->network->create(); | ||
self::$site_ids = array(); | ||
|
||
for ( $i = 0; $i < 3; $i++ ) { | ||
self::$site_ids[] = $factory->blog->create_object( | ||
array( | ||
'domain' => 'wordpress.org', | ||
'path' => '/sites/' . $i, | ||
'site_id' => self::$network_id, | ||
) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Tests that the site state can be saved and restored. | ||
* | ||
* @ticket 37958 | ||
*/ | ||
public function test_site_state_save_and_restore() { | ||
$original_blog_id = get_current_blog_id(); | ||
|
||
$state = get_site_state(); | ||
|
||
switch_to_blog( self::$site_ids[0] ); | ||
$this->assertEquals( self::$site_ids[0], get_current_blog_id() ); | ||
|
||
switch_to_blog( self::$site_ids[1] ); | ||
$this->assertEquals( self::$site_ids[1], get_current_blog_id() ); | ||
|
||
restore_site_state( $state ); | ||
$this->assertEquals( $original_blog_id, get_current_blog_id() ); | ||
|
||
$this->assertFalse( ms_is_switched() ); | ||
} | ||
|
||
/** | ||
* Tests that the site state can be saved and restored in bulk operations. | ||
* | ||
* @ticket 37958 | ||
*/ | ||
public function test_site_state_in_bulk_operations() { | ||
$original_blog_id = get_current_blog_id(); | ||
|
||
$state = get_site_state(); | ||
|
||
foreach ( self::$site_ids as $site_id ) { | ||
switch_to_blog( $site_id ); | ||
$this->assertEquals( $site_id, get_current_blog_id() ); | ||
} | ||
|
||
restore_site_state( $state ); | ||
$this->assertEquals( $original_blog_id, get_current_blog_id() ); | ||
|
||
$this->assertFalse( ms_is_switched() ); | ||
} | ||
|
||
/** | ||
* Tests that the site state object maintains its properties. | ||
* | ||
* @ticket 37958 | ||
*/ | ||
public function test_site_state_maintains_properties() { | ||
$state = get_site_state(); | ||
|
||
$this->assertEquals( get_current_blog_id(), $state->get_site_id() ); | ||
$this->assertEquals( ms_is_switched(), $state->is_switched() ); | ||
|
||
switch_to_blog( self::$site_ids[0] ); | ||
$switched_state = get_site_state(); | ||
$this->assertTrue( $switched_state->is_switched() ); | ||
|
||
restore_site_state( $switched_state ); | ||
$this->assertEquals( self::$site_ids[0], get_current_blog_id() ); | ||
|
||
restore_site_state( $state ); | ||
$this->assertFalse( ms_is_switched() ); | ||
} | ||
} | ||
|
||
endif; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofrestore_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?
There was a problem hiding this comment.
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.