diff --git src/wp-admin/includes/class-wp-ms-sites-list-table.php src/wp-admin/includes/class-wp-ms-sites-list-table.php old mode 100644 new mode 100755 index 9ce3ffe..987813c --- src/wp-admin/includes/class-wp-ms-sites-list-table.php +++ src/wp-admin/includes/class-wp-ms-sites-list-table.php @@ -157,6 +157,11 @@ $args['no_found_rows'] = false; } + // Site status + if ( ! empty( $_REQUEST['site_status'] ) && in_array( $_REQUEST['site_status'], array_keys( $this->status_list ), true ) ) { + $args[ $_REQUEST['site_status'] ] = '1'; + } + /** * Filters the arguments for the site query in the sites list table. * @@ -193,6 +198,117 @@ } /** + * Determine if the current view is the "All" view. + * + * @since 4.7.0 + * + * @return bool Whether the current view is the "All" view. + */ + protected function is_base_request() { + $vars = $_GET; + unset( $vars['paged'] ); + + if ( empty( $vars ) ) { + return true; + } + + return 1 === count( $vars ) && ! empty( $vars['mode'] ); + } + + /** + * Helper to create links to sites.php with params. + * + * @since 4.7.0 + * @access protected + * + * @param array $args URL parameters for the link. + * @param string $label Link text. + * @param string $class Optional. Class attribute. Default empty string. + * @return string The formatted link string. + */ + protected function get_edit_link( $args, $label, $class = '' ) { + $url = add_query_arg( $args, 'sites.php' ); + + $class_html = ''; + if ( ! empty( $class ) ) { + $class_html = sprintf( + ' class="%s"', + esc_attr( $class ) + ); + } + + return sprintf( + '%s', + esc_url( $url ), + $class_html, + $label + ); + } + + /** + * Return array of site status views + * + * @since 4.7.0 + * + * @return array + */ + protected function get_views() { + + $total_sites = get_blog_count(); + $counts = wp_count_sites(); + $status_links = array(); + + // All sites + if ( empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_sites'] ) ) ) { + $class = 'current'; + } else { + $class = ''; + } + + $all_inner_html = sprintf( + _nx( + 'All (%s)', + 'All (%s)', + $total_sites, + 'sites' + ), + number_format_i18n( $total_sites ) + ); + + $status_links['all'] = $this->get_edit_link( array(), $all_inner_html, $class ); + + // Add public to available site statuses + $avail_site_stati = array_keys( $this->status_list ); + array_unshift( $avail_site_stati, 'public' ); + + // Loop through statuses looking for counts + foreach ( $this->status_list as $status_key => $status ) { + $class = ''; + + if ( ! in_array( $status_key, $avail_site_stati, true ) || empty( $counts->{$status_key} ) ) { + continue; + } + + if ( isset( $_REQUEST['site_status'] ) && ( $status_key === $_REQUEST['site_status'] ) ) { + $class = 'current'; + } + + $status_args = array( + 'site_status' => $status_key, + ); + + $status_label = sprintf( + sprintf( '%s (%s)', $status[1], '%s' ), + number_format_i18n( $counts->$status_key ) + ); + + $status_links[ $status_key ] = $this->get_edit_link( $status_args, $status_label, $class ); + } + + return $status_links; + } + + /** * * @return array */ diff --git src/wp-admin/network/sites.php src/wp-admin/network/sites.php index 38aa2ec..57fb011 100644 --- src/wp-admin/network/sites.php +++ src/wp-admin/network/sites.php @@ -282,30 +282,29 @@ ?>
- + diff --git src/wp-includes/ms-functions.php src/wp-includes/ms-functions.php index 0d97ea7..f889f1a 100644 --- src/wp-includes/ms-functions.php +++ src/wp-includes/ms-functions.php @@ -2267,6 +2267,60 @@ } /** + * Count number of sites of a site status. + * + * This function provides an efficient method of finding the amount of sites + * + * @since 2.5.0 + * + * @global wpdb $wpdb WordPress database abstraction object. + * + * @return object Number of sites for each status. + */ +function wp_count_sites() { + global $wpdb; + + // Get network ID + $network_id = get_current_network_id(); + + // Bail if counts already cached + $counts = wp_cache_get( "sites-{$network_id}", 'networks' ); + if ( false !== $counts ) { + return apply_filters( 'wp_count_sites', $counts ); + } + + // Query for site statuses + $query = "SELECT public, archived, mature, spam, deleted FROM {$wpdb->blogs} WHERE site_id = %d"; + $prepare = $wpdb->prepare( $query, $network_id ); + $results = $wpdb->get_results( $prepare, ARRAY_A ); + + // Populate counts + $statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' ); + $counts = array_fill_keys( $statuses, 0 ); + foreach ( $results as $row ) { + foreach ( $row as $status => $value ) { + if ( ! empty( $value ) ) { + ++$counts[ $status ]; + } + } + } + + // Objectify for caching + $counts = (object) $counts; + wp_cache_set( "sites-{$network_id}", $counts, 'networks' ); + + /** + * Modify returned site counts by status for the current network. + * + * @since 4.7.0 + * + * @param object $counts An object containing the current network's site + * counts by status. + */ + return apply_filters( 'wp_count_sites', $counts, $network_id ); +} + +/** * Returns the space used by the current blog. * * @since 3.5.0