Skip to content

Commit f2b03e2

Browse files
committed
Multisite/Sites: Add links to filter websites by status.
This commit brings the Network-Admin Sites list page up-to-speed with other similar list-table powered pages, by adding links to filter the results by Site Status. Includes a single unit test for the newly introduced `wp_count_sites()` multisite function, named to match the `wp_count_` function pattern from other list tables. Fixes #37392. Props mnelson4, spacedmonkey, pbiron. git-svn-id: https://develop.svn.wordpress.org/trunk@46251 602fd350-edb4-49c9-b593-d223f7449a82
1 parent aba005b commit f2b03e2

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

src/wp-admin/network/sites.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@
369369

370370
<hr class="wp-header-end">
371371

372+
<?php $wp_list_table->views(); ?>
373+
372374
<?php echo $msg; ?>
373375

374376
<form method="get" id="ms-search" class="wp-clearfix">

src/wp-includes/ms-blogs.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,3 +853,42 @@ function _update_posts_count_on_transition_post_status( $new_status, $old_status
853853

854854
update_posts_count();
855855
}
856+
857+
/**
858+
* Count number of sites grouped by site status.
859+
*
860+
* @since 5.3.0
861+
*
862+
* @param int $network_id The network to get counts for. Default is the current network id.
863+
* @return array Includes a grand total 'all' and an array of counts indexed by
864+
* status strings: public, archived, mature, spam, deleted.
865+
*/
866+
function wp_count_sites( $network_id = null ) {
867+
if ( empty( $network_id ) ) {
868+
$network_id = get_current_network_id();
869+
}
870+
871+
$counts = array();
872+
$args = array(
873+
'network_id' => $network_id,
874+
'number' => 1,
875+
'fields' => 'ids',
876+
'no_found_rows' => false,
877+
);
878+
879+
$q = new WP_Site_Query( $args );
880+
$counts['all'] = $q->found_sites;
881+
882+
$_args = $args;
883+
$statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' );
884+
885+
foreach ( $statuses as $status ) {
886+
$_args = $args;
887+
$_args[ $status ] = 1;
888+
889+
$q = new WP_Site_Query( $_args );
890+
$counts[ $status ] = $q->found_sites;
891+
}
892+
893+
return $counts;
894+
}

tests/phpunit/tests/multisite.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,48 @@ function test_wpmu_log_new_registrations() {
3434
$reg_blog = $wpdb->get_col( $wpdb->prepare( "SELECT email FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.blog_id = 1 AND IP LIKE %s", $ip ) );
3535
$this->assertEquals( $user->user_email, $reg_blog[ count( $reg_blog ) - 1 ] );
3636
}
37+
38+
/**
39+
* @ticket 37392
40+
*/
41+
function test_wp_count_sites() {
42+
// create a random number of sites with each status.
43+
$site_ids = array(
44+
'public' => self::factory()->blog->create_many(
45+
random_int( 0, 5 ),
46+
array( 'meta' => array( 'public' => 1 ) )
47+
),
48+
'archived' => self::factory()->blog->create_many(
49+
random_int( 0, 5 ),
50+
array( 'meta' => array( 'archived' => 1 ) )
51+
),
52+
'mature' => self::factory()->blog->create_many(
53+
random_int( 0, 5 ),
54+
array( 'meta' => array( 'mature' => 1 ) )
55+
),
56+
'spam' => self::factory()->blog->create_many(
57+
random_int( 0, 5 ),
58+
array( 'meta' => array( 'spam' => 1 ) )
59+
),
60+
'deleted' => self::factory()->blog->create_many(
61+
random_int( 0, 5 ),
62+
array( 'meta' => array( 'deleted' => 1 ) )
63+
),
64+
);
65+
66+
$counts = wp_count_sites();
67+
68+
$counts_by_status = array_map( 'count', $site_ids );
69+
$expected = array_merge(
70+
array( 'all' => array_sum( $counts_by_status ) ),
71+
$counts_by_status
72+
);
73+
// add 1 to all & public for the main site.
74+
$expected['all'] += 1;
75+
$expected['public'] += 1;
76+
77+
$this->assertEquals( $expected, $counts );
78+
}
3779
}
3880

3981
endif;

0 commit comments

Comments
 (0)