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

Prevent unneeded blog switching in multisite env. #2781

Merged
merged 7 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,30 @@ public function wp_object(): ?WP_Site
/**
* Switches to the blog requested in the request
*
* @param string|integer|null $site_name_or_id
* @param string|integer|null $blog_identifier The name or ID of the blog to switch to. If `null`, the current blog.
* @return integer with the ID of the new blog
*/
protected static function switch_to_blog($site_name_or_id)
protected static function switch_to_blog($blog_identifier = null): int
{
if ($site_name_or_id === null) {
$site_name_or_id = \get_current_blog_id();
$current_id = \get_current_blog_id();

if ($blog_identifier === null) {
$blog_identifier = $current_id;
}

$info = \get_blog_details($blog_identifier, false);

if (false === $info) {
return $current_id;
}
$info = \get_blog_details($site_name_or_id);
\switch_to_blog($info->blog_id);
return $info->blog_id;

$blog_identifier = $info->blog_id;

if ((int) $current_id !== (int) $blog_identifier) {
\switch_to_blog($blog_identifier);
}

return (int) $blog_identifier;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/test-timber-multisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function testPostGettingAcrossSites()
$this->factory->post->create([
'post_title' => array_pop($post_titles),
]);
restore_current_blog();
}

$timber_posts = [];
Expand Down Expand Up @@ -108,6 +109,7 @@ public function testPostGettingAcrossSitesNoArgs()
$this->factory->post->create([
'post_title' => 'Zebras are good on site ID = ' . $site_id,
]);
restore_current_blog();
}
$this->go_to('/');
$timber_posts = [];
Expand Down Expand Up @@ -144,8 +146,11 @@ public function testPostSearchAcrossSites()
return;
}
$site_ids[] = self::createSubDomainSite('foo.example.org', 'My Foo');
restore_current_blog();
$site_ids[] = self::createSubDomainSite('quack.example.org', "Ducks R Us");
restore_current_blog();
$site_ids[] = self::createSubDomainSite('duck.example.org', "More Ducks R Us");
restore_current_blog();

$post_titles = ["I don't like zebras", "Zebra and a half", "Have a zebra of a time"];
$others = $this->factory->post->create_many(8);
Expand All @@ -154,6 +159,7 @@ public function testPostSearchAcrossSites()
$this->factory->post->create([
'post_title' => array_pop($post_titles),
]);
restore_current_blog();
}

$timber_posts = [];
Expand Down