Skip to content

Commit 472701e

Browse files
authored
Merge pull request #10 from spacedmonkey/feature/40365-rest-sites-endpoint-current-network
Site REST API: Default network id
2 parents 5973182 + 7ecdb29 commit 472701e

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎src/wp-includes/rest-api/endpoints/class-wp-rest-sites-controller.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ public function get_items( $request ) {
230230
}
231231
}
232232

233+
// Without an explicit network filter, limit the results to the current network.
234+
if ( empty( $prepared_args['network__in'] ) ) {
235+
$prepared_args['network__in'] = array( get_current_network_id() );
236+
}
237+
233238
// WP_Site_Query tests the status columns with is_numeric(), and a boolean is not numeric.
234239
foreach ( array( 'public', 'archived', 'mature', 'spam', 'deleted' ) as $status_param ) {
235240
if ( isset( $prepared_args[ $status_param ] ) ) {

‎tests/phpunit/tests/rest-api/wpRestSitesController.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,50 @@ public function test_get_items() {
106106
$this->assertCount( 7, $sites );
107107
}
108108

109+
/**
110+
* Without an explicit network filter, only sites on the current network are returned.
111+
*
112+
* @ticket 40365
113+
* @covers ::get_items
114+
* @group ms-required
115+
*/
116+
public function test_get_items_defaults_to_the_current_network() {
117+
wp_set_current_user( self::$superadmin_id );
118+
119+
$current_network_site = self::factory()->blog->create( array( 'path' => '/current/' ) );
120+
121+
$other_network_id = self::factory()->network->create(
122+
array(
123+
'domain' => 'other-network.example.org',
124+
'path' => '/',
125+
)
126+
);
127+
128+
$other_network_site = self::factory()->blog->create(
129+
array(
130+
'domain' => 'other-network.example.org',
131+
'path' => '/',
132+
'network_id' => $other_network_id,
133+
)
134+
);
135+
136+
$request = new WP_REST_Request( 'GET', '/wp/v2/sites' );
137+
$response = rest_get_server()->dispatch( $request );
138+
139+
$this->assertEquals( 200, $response->get_status() );
140+
141+
$data = $response->get_data();
142+
$site_ids = wp_list_pluck( $data, 'id' );
143+
144+
$this->assertContains( $current_network_site, $site_ids, 'Sites on the current network should be returned.' );
145+
$this->assertNotContains( $other_network_site, $site_ids, 'Sites on another network should not be returned.' );
146+
147+
$current_network_id = get_current_network_id();
148+
foreach ( $data as $site ) {
149+
$this->assertSame( $current_network_id, $site['network'], 'Every returned site should belong to the current network.' );
150+
}
151+
}
152+
109153
/**
110154
* @ticket 40365
111155
* @covers ::get_item

0 commit comments

Comments
 (0)