Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ public function get_items( $request ) {
}
}

// Without an explicit network filter, limit the results to the current network.
if ( empty( $prepared_args['network__in'] ) ) {
$prepared_args['network__in'] = array( get_current_network_id() );
}

// WP_Site_Query tests the status columns with is_numeric(), and a boolean is not numeric.
foreach ( array( 'public', 'archived', 'mature', 'spam', 'deleted' ) as $status_param ) {
if ( isset( $prepared_args[ $status_param ] ) ) {
Expand Down
44 changes: 44 additions & 0 deletions tests/phpunit/tests/rest-api/wpRestSitesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,50 @@ public function test_get_items() {
$this->assertCount( 7, $sites );
}

/**
* Without an explicit network filter, only sites on the current network are returned.
*
* @ticket 40365
* @covers ::get_items
* @group ms-required
*/
public function test_get_items_defaults_to_the_current_network() {
wp_set_current_user( self::$superadmin_id );

$current_network_site = self::factory()->blog->create( array( 'path' => '/current/' ) );

$other_network_id = self::factory()->network->create(
array(
'domain' => 'other-network.example.org',
'path' => '/',
)
);

$other_network_site = self::factory()->blog->create(
array(
'domain' => 'other-network.example.org',
'path' => '/',
'network_id' => $other_network_id,
)
);

$request = new WP_REST_Request( 'GET', '/wp/v2/sites' );
$response = rest_get_server()->dispatch( $request );

$this->assertEquals( 200, $response->get_status() );

$data = $response->get_data();
$site_ids = wp_list_pluck( $data, 'id' );

$this->assertContains( $current_network_site, $site_ids, 'Sites on the current network should be returned.' );
$this->assertNotContains( $other_network_site, $site_ids, 'Sites on another network should not be returned.' );

$current_network_id = get_current_network_id();
foreach ( $data as $site ) {
$this->assertSame( $current_network_id, $site['network'], 'Every returned site should belong to the current network.' );
}
}

/**
* @ticket 40365
* @covers ::get_item
Expand Down