Make WordPress Core

Ticket #39787: 39787.diff

File 39787.diff, 2.9 KB (added by theskinnyghost, 7 years ago)

function changes + unit tests

  • src/wp-includes/author-template.php

    diff --git a/src/wp-includes/author-template.php b/src/wp-includes/author-template.php
    index 2646fb94c3..d7b649e769 100644
    a b function wp_list_authors( $args = '' ) {  
    428428                'html'          => true,
    429429                'exclude'       => '',
    430430                'include'       => '',
     431                'role'          => '',
     432                'role__in'      => array(),
     433                'role__not_in'  => array(),
    431434        );
    432435
    433436        $args = wp_parse_args( $args, $defaults );
    434437
    435438        $return = '';
    436439
    437         $query_args           = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
    438         $query_args['fields'] = 'ids';
     440        $query_args           = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include', 'role', 'role__in', 'role__not_in' ) );
    439441        $authors              = get_users( $query_args );
    440442
    441443        $author_count = array();
    442444        foreach ( (array) $wpdb->get_results( "SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE " . get_private_posts_cap_sql( 'post' ) . ' GROUP BY post_author' ) as $row ) {
    443445                $author_count[ $row->post_author ] = $row->count;
    444446        }
    445         foreach ( $authors as $author_id ) {
    446                 $posts = isset( $author_count[ $author_id ] ) ? $author_count[ $author_id ] : 0;
     447        foreach ( $authors as $author ) {
     448                $posts = isset( $author_count[ $author->ID ] ) ? $author_count[ $author->ID ] : 0;
    447449
    448450                if ( ! $posts && $args['hide_empty'] ) {
    449451                        continue;
    450452                }
    451453
    452                 $author = get_userdata( $author_id );
    453 
    454454                if ( $args['exclude_admin'] && 'admin' === $author->display_name ) {
    455455                        continue;
    456456                }
  • tests/phpunit/tests/user/listAuthors.php

    diff --git a/tests/phpunit/tests/user/listAuthors.php b/tests/phpunit/tests/user/listAuthors.php
    index ccaa733551..8869d66186 100644
    a b class Tests_User_ListAuthors extends WP_UnitTestCase {  
    304304                        )
    305305                );
    306306        }
     307
     308        function test_wp_list_authors_role() {
     309                $expected['role'] = '';
     310
     311                $this->assertEquals(
     312                        $expected['role'],
     313                        wp_list_authors(
     314                                array(
     315                                        'echo' => false,
     316                                        'role' => 'administrator',
     317                                )
     318                        )
     319                );
     320        }
     321
     322        function test_wp_list_authors_role__in() {
     323                $expected['role__in'] = '';
     324
     325                $this->assertEquals(
     326                        $expected['role__in'],
     327                        wp_list_authors(
     328                                array(
     329                                        'echo' => false,
     330                                        'role__in' => [ 'administrator', 'editor' ],
     331                                )
     332                        )
     333                );
     334        }
     335
     336        function test_wp_list_authors_role__not_in() {
     337                $expected['role__not_in'] =
     338                        '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a></li>' .
     339                        '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a></li>' .
     340                        '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a></li>';
     341
     342                $this->assertEquals(
     343                        $expected['role__not_in'],
     344                        wp_list_authors(
     345                                array(
     346                                        'echo' => false,
     347                                        'role__not_in' => [ 'administrator', 'editor' ],
     348                                )
     349                        )
     350                );
     351        }
    307352}