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

When switching site, also switch network as well #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/test-networkoperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ public function test_move_site() {
$this->assertEquals( 1, $site->site_id, 'Site should be back in main network' );
}

public function test_switch_site() {
// Site first site and network
$network_id = $this->factory->network->create( array( 'domain' => 'wordpress.com', 'path' => '/', ) );
$site_id = $this->factory->blog->create( array( 'site_id' => $network_id ) );

// Site second site and network
$other_network_id = $this->factory->network->create( array( 'domain' => 'example.com', 'path' => '/', ) );
$site_id_diffent_network = $this->factory->blog->create( array( 'site_id' => $other_network_id ) );

// Assert default network is 1
$this->assertEquals( 1, get_current_network_id(), 'Network should start as 1' );

// Switch to first site
switch_to_blog( $site_id );
$this->assertEquals( $network_id, get_current_network_id(), 'Network should change to '. $network_id );

// Switch to second site
switch_to_blog( $site_id_diffent_network );
$this->assertEquals( $other_network_id, get_current_network_id(), 'Network should change to '. $other_network_id );
// Switch to first site
restore_current_blog();

$this->assertEquals( $network_id, get_current_network_id(), 'Network should change to '. $network_id );

// Restore default site / network
restore_current_blog();

$this->assertEquals( 1, get_current_network_id(), 'Network should end as 1' );

}

public function test_switch_to_network() {
global $current_site;

Expand Down
12 changes: 12 additions & 0 deletions wp-multi-network/includes/actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

function switch_blog_and_network ( $new_blog, $prev_blog_id ){
$site_object = get_site( $new_blog );
if ( !($site_object instanceof WP_Site ) ){
Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be simplified to just ! $site_object.

return;
}
if( get_current_network_id() != $site_object->site_id ) {
switch_to_network( $site_object->site_id );
}
}
add_action( 'switch_blog', 'switch_blog_and_network', 10, 2 );
1 change: 1 addition & 0 deletions wpmn-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ private function includes() {
// Functions & Core Compatibility
require $this->plugin_dir . 'includes/compat.php';
require $this->plugin_dir . 'includes/functions.php';
require $this->plugin_dir . 'includes/actions.php';

// WordPress Admin
if ( is_blog_admin() || is_network_admin() ) {
Expand Down