Make WordPress Core

Changeset 46251


Ignore:
Timestamp:
09/23/2019 05:34:20 PM (5 years ago)
Author:
johnjamesjacoby
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/network/sites.php

    r46211 r46251  
    370370<hr class="wp-header-end">
    371371
     372<?php $wp_list_table->views(); ?>
     373
    372374<?php echo $msg; ?>
    373375
  • trunk/src/wp-includes/ms-blogs.php

    r45794 r46251  
    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}
  • trunk/tests/phpunit/tests/multisite.php

    r45607 r46251  
    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
Note: See TracChangeset for help on using the changeset viewer.