Skip to content

Commit 19f9266

Browse files
committed
fix: exclude deleted/archived/spam sites from Site Query
1 parent 8db3d9f commit 19f9266

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

__tests__/unit-tests/test-cli-orchestrate-sites.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ public function setUp(): void {
1414
parent::setUp();
1515
}
1616

17-
public function test_list_sites_removes_inactive_subsites() {
18-
add_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );
19-
20-
// The archived/spam/deleted subsites should not be returned.
21-
$expected = wp_json_encode( [ [ 'url' => 'site1.com' ], [ 'url' => 'site2.com/two' ], [ 'url' => 'site3.com/three' ], [ 'url' => 'site7.com/seven' ] ] );
22-
$this->expectOutputString( $expected );
23-
( new CLI\Orchestrate_Sites() )->list();
24-
25-
remove_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );
26-
}
27-
2817
public function test_list_sites_2_hosts() {
2918
add_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 );
3019

@@ -64,16 +53,13 @@ public function mock_hosts_list( $number_of_hosts ) {
6453

6554
public function mock_get_sites( $site_data, $query_class ) {
6655
if ( $query_class->query_vars['count'] ) {
67-
return 7;
56+
return 4;
6857
}
6958

7059
return [
7160
new WP_Site( (object) [ 'domain' => 'site1.com', 'path' => '/' ] ),
7261
new WP_Site( (object) [ 'domain' => 'site2.com', 'path' => '/two' ] ),
7362
new WP_Site( (object) [ 'domain' => 'site3.com', 'path' => '/three' ] ),
74-
new WP_Site( (object) [ 'domain' => 'site4.com', 'path' => '/four', 'archived' => '1' ] ),
75-
new WP_Site( (object) [ 'domain' => 'site5.com', 'path' => '/five', 'spam' => '1' ] ),
76-
new WP_Site( (object) [ 'domain' => 'site6.com', 'path' => '/six', 'deleted' => '1' ] ),
7763
new WP_Site( (object) [ 'domain' => 'site7.com', 'path' => '/seven' ] ),
7864
];
7965
}

includes/wp-cli/class-orchestrate-sites.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,27 @@ public function list() {
6161
* @param int $group Group number.
6262
*/
6363
private function display_sites( $num_groups = 1, $group = 0 ) {
64-
$site_count = get_sites( [ 'count' => 1 ] );
64+
$site_query = array(
65+
'archived' => 0,
66+
'spam' => 0,
67+
'deleted' => 0,
68+
);
69+
70+
$site_count = get_sites( array_merge( array( 'count' => 1 ), $site_query ) );
71+
6572
if ( $site_count > self::MAX_SITES ) {
66-
trigger_error( 'Cron-Control: This multisite has more than ' . self::MAX_SITES . ' subsites, currently unsupported.', E_USER_WARNING );
73+
trigger_error( sprintf( 'Cron-Control: This multisite has more than %u active subsites, currently unsupported.', self::MAX_SITES ), E_USER_WARNING );
6774
}
6875

6976
// Keep the query simple, then process the results.
70-
$all_sites = get_sites( [ 'number' => self::MAX_SITES ] );
77+
$all_sites = get_sites( array_merge( array( 'number' => self::MAX_SITES ), $site_query ) );
7178
$sites_to_display = [];
7279
foreach ( $all_sites as $index => $site ) {
7380
if ( $index % $num_groups !== $group ) {
7481
// The site does not belong to this group.
7582
continue;
7683
}
7784

78-
if ( in_array( '1', array( $site->archived, $site->spam, $site->deleted ), true ) ) {
79-
// Deactivated subsites don't need cron run on them.
80-
continue;
81-
}
82-
8385
// We just need the url to display.
8486
$sites_to_display[] = [ 'url' => $this->get_raw_site_url( $site->path, $site->domain ) ];
8587
}

0 commit comments

Comments
 (0)