Make WordPress Core

Ticket #37392: 37392.3.diff

File 37392.3.diff, 6.6 KB (added by pbiron, 5 years ago)
  • src/wp-admin/includes/class-wp-ms-sites-list-table.php

    From e9a38db42d13a7f8a0499e51efda6e024432809d Mon Sep 17 00:00:00 2001
    From: Paul Biron <paul@sparrowhawkcomputing.com>
    Date: Mon, 23 Sep 2019 09:59:49 -0600
    Subject: [PATCH] Multisite "Sites" screen: Add links to filter websites by
     status.
    
    ---
     .../includes/class-wp-ms-sites-list-table.php | 50 +++++++++++++++++++
     src/wp-admin/network/sites.php                |  2 +
     src/wp-includes/ms-blogs.php                  | 39 +++++++++++++++
     tests/phpunit/tests/multisite.php             | 42 ++++++++++++++++
     4 files changed, 133 insertions(+)
    
    diff --git a/src/wp-admin/includes/class-wp-ms-sites-list-table.php b/src/wp-admin/includes/class-wp-ms-sites-list-table.php
    index 519a57d8c4..5147139835 100644
    a b class WP_MS_Sites_List_Table extends WP_List_Table { 
    160160                        $args['no_found_rows'] = false;
    161161                }
    162162
     163                // Take into account the role the user has selected.
     164                $status = isset( $_REQUEST['status'] ) ? wp_unslash( trim( $_REQUEST['status'] ) ) : '';
     165                if ( in_array( $status, array( 'public', 'archived', 'mature', 'spam', 'deleted' ), true ) ) {
     166                        $args[ $status ] = 1;
     167                }
     168
    163169                /**
    164170                 * Filters the arguments for the site query in the sites list table.
    165171                 *
    class WP_MS_Sites_List_Table extends WP_List_Table { 
    201207                _e( 'No sites found.' );
    202208        }
    203209
     210        /**
     211         * Gets links to filter sites by status.
     212         *
     213         * @since 5.3.0
     214         *
     215         * @return array
     216         *
     217         */
     218        protected function get_views() {
     219                $counts = wp_count_sites();
     220
     221                $statuses = array(
     222                        'all'      => _n_noop( 'All <span class="count">(%s)</span>', 'All <span class="count">(%1$s)</span>' ),
     223                        'public'   => _n_noop( 'Public <span class="count">(%s)</span>', 'Public <span class="count">(%1$s)</span>' ),
     224                        'archived' => _n_noop( 'Archived <span class="count">(%1$s)</span>', 'Archived <span class="count">(%1$s)</span>' ),
     225                        'mature'   => _n_noop( 'Mature <span class="count">(%1$s)</span>', 'Mature <span class="count">(%1$s)</span>' ),
     226                        'spam'     => _n_noop( 'Spam <span class="count">(%1$s)</span>', 'Spam <span class="count">(%1$s)</span>' ),
     227                        'deleted'  => _n_noop( 'Deleted <span class="count">(%1$s)</span>', 'Deleted <span class="count">(%1$s)</span>' ),
     228                );
     229
     230                $view_links       = array();
     231                $requested_status = isset( $_REQUEST['status'] ) ? wp_unslash( trim( $_REQUEST['status'] ) ) : '';
     232                $url              = 'sites.php';
     233
     234                foreach ( $statuses as $status => $label_count ) {
     235                        $current_link_attributes = $requested_status === $status || ( $requested_status === '' && 'all' === $status )
     236                                ? ' class="current" aria-current="page"'
     237                                : '';
     238                        if ( (int) $counts[ $status ] > 0 ) {
     239                                $label = sprintf( translate_nooped_plural( $label_count, $counts[ $status ] ), number_format_i18n( $counts[ $status ] ) );
     240                                $full_url = 'all' === $status ? $url : add_query_arg( 'status', $status, $url );
     241
     242                                $view_links[ $status ] = sprintf(
     243                                        '<a href="%1$s"%2$s>%3$s</a>',
     244                                        esc_url( $full_url ),
     245                                        $current_link_attributes,
     246                                        $label
     247                                );
     248                        }
     249                }
     250
     251                return $view_links;
     252        }
     253
    204254        /**
    205255         * @return array
    206256         */
  • src/wp-admin/network/sites.php

    diff --git a/src/wp-admin/network/sites.php b/src/wp-admin/network/sites.php
    index 227f37e289..52b4146c39 100644
    a b if ( isset( $_REQUEST['s'] ) && strlen( $_REQUEST['s'] ) ) { 
    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

    diff --git a/src/wp-includes/ms-blogs.php b/src/wp-includes/ms-blogs.php
    index 02a55692dc..2874f7e1f0 100644
    a b 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 */
     866function 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

    diff --git a/tests/phpunit/tests/multisite.php b/tests/phpunit/tests/multisite.php
    index 4d25ade892..1f50d3ab4a 100644
    a b if ( is_multisite() ) : 
    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
    3981endif;