From d8140ba6321b8b7b086026e6a24dcf625da17ee4 Mon Sep 17 00:00:00 2001 From: Spacedmonkey Date: Mon, 24 Apr 2017 20:14:52 +0100 Subject: [PATCH] When switching site, also switch network as well --- tests/test-networkoperations.php | 31 +++++++++++++++++++++++++++ wp-multi-network/includes/actions.php | 12 +++++++++++ wpmn-loader.php | 1 + 3 files changed, 44 insertions(+) create mode 100644 wp-multi-network/includes/actions.php diff --git a/tests/test-networkoperations.php b/tests/test-networkoperations.php index 37d9004..95a39b2 100644 --- a/tests/test-networkoperations.php +++ b/tests/test-networkoperations.php @@ -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; diff --git a/wp-multi-network/includes/actions.php b/wp-multi-network/includes/actions.php new file mode 100644 index 0000000..4cea445 --- /dev/null +++ b/wp-multi-network/includes/actions.php @@ -0,0 +1,12 @@ +site_id ) { + switch_to_network( $site_object->site_id ); + } +} +add_action( 'switch_blog', 'switch_blog_and_network', 10, 2 ); \ No newline at end of file diff --git a/wpmn-loader.php b/wpmn-loader.php index 9845f98..ec54eea 100644 --- a/wpmn-loader.php +++ b/wpmn-loader.php @@ -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() ) {