Make WordPress Core

Changeset 54262

Timestamp:
09/20/2022 04:24:56 PM (23 months ago)
Author:
SergeyBiryukov
Message:

Users: Make wp_list_authors() and wp_list_users() filterable.

This commit adds three filters to customize the wp_list_authors() and wp_list_users() output:

  • wp_list_authors_args: Filters the query arguments for the list of all authors of the site.
  • pre_wp_list_authors_post_counts_query: Filters whether to short-circuit performing the query for author post counts. This may be useful to account for custom post types or post statuses.
  • wp_list_users_args: Filters the query arguments for the list of all users of the site.

Follow-up to [979], [3848], [5135], [5727], [31653], [52064], [53486], [53501].

Props kevinB, wonderboymusic, DrewAPicture, Mte90, audrasjb, rafiahmedd, costdev, nacin, afercia, chetan200891, hellofromTonya, TimothyBlynJacobs, chaion07, SergeyBiryukov.
Fixes #17025.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/author-template.php

    r53501 r54262  
    451451    );
    452452
    453     $args = wp_parse_args( $args, $defaults );
     453    $args = wp_parse_args( $args, $defaults );
    454454
    455455    $return = '';
    456456
    457     $query_args           = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
     457    $query_args           = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
    458458    $query_args['fields'] = 'ids';
    459     $authors              = get_users( $query_args );
    460 
    461     $author_count = array();
    462     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 ) {
    463         $author_count[ $row->post_author ] = $row->count;
    464     }
     459
     460    /**
     461     * Filters the query arguments for the list of all authors of the site.
     462     *
     463     * @since 6.1.0
     464     *
     465     * @param array $query_args  The query arguments for get_users().
     466     * @param array $parsed_args The arguments passed to wp_list_authors() combined with the defaults.
     467     */
     468    $query_args = apply_filters( 'wp_list_authors_args', $query_args, $parsed_args );
     469
     470    $authors     = get_users( $query_args );
     471    $post_counts = array();
     472
     473    /**
     474     * Filters whether to short-circuit performing the query for author post counts.
     475     *
     476     * @since 6.1.0
     477     *
     478     * @param int[]|false $post_counts Array of post counts, keyed by author ID.
     479     * @param array       $parsed_args The arguments passed to wp_list_authors() combined with the defaults.
     480     */
     481    $post_counts = apply_filters( 'pre_wp_list_authors_post_counts_query', false, $parsed_args );
     482
     483    if ( ! is_array( $post_counts ) ) {
     484        $post_counts = $wpdb->get_results(
     485            "SELECT DISTINCT post_author, COUNT(ID) AS count
     486            FROM $wpdb->posts
     487            WHERE " . get_private_posts_cap_sql( 'post' ) . '
     488            GROUP BY post_author'
     489        );
     490
     491        foreach ( (array) $post_counts as $row ) {
     492            $post_counts[ $row->post_author ] = $row->count;
     493        }
     494    }
     495
    465496    foreach ( $authors as $author_id ) {
    466         $posts = isset( $author_count[ $author_id ] ) ? $author_count[ $author_id ] : 0;
    467 
    468         if ( ! $posts && $args['hide_empty'] ) {
     497        $posts = isset( $[ $author_id ] : 0;
     498
     499        if ( ! $posts && $args['hide_empty'] ) {
    469500            continue;
    470501        }
     
    472503        $author = get_userdata( $author_id );
    473504
    474         if ( $args['exclude_admin'] && 'admin' === $author->display_name ) {
     505        if ( $args['exclude_admin'] && 'admin' === $author->display_name ) {
    475506            continue;
    476507        }
    477508
    478         if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
     509        if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
    479510            $name = sprintf(
    480511                /* translators: 1: User's first name, 2: Last name. */
     
    487518        }
    488519
    489         if ( ! $args['html'] ) {
     520        if ( ! $args['html'] ) {
    490521            $return .= $name . ', ';
    491522
     
    493524        }
    494525
    495         if ( 'list' === $args['style'] ) {
     526        if ( 'list' === $args['style'] ) {
    496527            $return .= '<li>';
    497528        }
     
    505536        );
    506537
    507         if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
     538        if ( ! empty( $args['feed'] ) ) {
    508539            $link .= ' ';
    509             if ( empty( $args['feed_image'] ) ) {
     540            if ( empty( $args['feed_image'] ) ) {
    510541                $link .= '(';
    511542            }
    512543
    513             $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
     544            $link .= '<a href="' . get_author_feed_link( $author->ID, $args['feed_type'] ) . '"';
    514545
    515546            $alt = '';
    516             if ( ! empty( $args['feed'] ) ) {
    517                 $alt  = ' alt="' . esc_attr( $args['feed'] ) . '"';
    518                 $name = $args['feed'];
     547            if ( ! empty( $args['feed'] ) ) {
     548                $alt  = ' alt="' . esc_attr( $args['feed'] ) . '"';
     549                $name = $args['feed'];
    519550            }
    520551
    521552            $link .= '>';
    522553
    523             if ( ! empty( $args['feed_image'] ) ) {
    524                 $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
     554            if ( ! empty( $args['feed_image'] ) ) {
     555                $link .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
    525556            } else {
    526557                $link .= $name;
     
    529560            $link .= '</a>';
    530561
    531             if ( empty( $args['feed_image'] ) ) {
     562            if ( empty( $args['feed_image'] ) ) {
    532563                $link .= ')';
    533564            }
    534565        }
    535566
    536         if ( $args['optioncount'] ) {
     567        if ( $args['optioncount'] ) {
    537568            $link .= ' (' . $posts . ')';
    538569        }
    539570
    540571        $return .= $link;
    541         $return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
     572        $return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
    542573    }
    543574
    544575    $return = rtrim( $return, ', ' );
    545576
    546     if ( $args['echo'] ) {
     577    if ( $args['echo'] ) {
    547578        echo $return;
    548579    } else {
  • trunk/src/wp-includes/user.php

    r54182 r54262  
    812812    );
    813813
    814     $args = wp_parse_args( $args, $defaults );
     814    $args = wp_parse_args( $args, $defaults );
    815815
    816816    $return = '';
    817817
    818     $query_args           = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
     818    $query_args           = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
    819819    $query_args['fields'] = 'ids';
    820     $users                = get_users( $query_args );
     820
     821    /**
     822     * Filters the query arguments for the list of all users of the site.
     823     *
     824     * @since 6.1.0
     825     *
     826     * @param array $query_args  The query arguments for get_users().
     827     * @param array $parsed_args The arguments passed to wp_list_users() combined with the defaults.
     828     */
     829    $query_args = apply_filters( 'wp_list_users_args', $query_args, $parsed_args );
     830
     831    $users = get_users( $query_args );
    821832
    822833    foreach ( $users as $user_id ) {
    823834        $user = get_userdata( $user_id );
    824835
    825         if ( $args['exclude_admin'] && 'admin' === $user->display_name ) {
     836        if ( $args['exclude_admin'] && 'admin' === $user->display_name ) {
    826837            continue;
    827838        }
    828839
    829         if ( $args['show_fullname'] && '' !== $user->first_name && '' !== $user->last_name ) {
     840        if ( $args['show_fullname'] && '' !== $user->first_name && '' !== $user->last_name ) {
    830841            $name = sprintf(
    831842                /* translators: 1: User's first name, 2: Last name. */
     
    838849        }
    839850
    840         if ( ! $args['html'] ) {
     851        if ( ! $args['html'] ) {
    841852            $return .= $name . ', ';
    842853
     
    844855        }
    845856
    846         if ( 'list' === $args['style'] ) {
     857        if ( 'list' === $args['style'] ) {
    847858            $return .= '<li>';
    848859        }
     
    850861        $row = $name;
    851862
    852         if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
     863        if ( ! empty( $args['feed'] ) ) {
    853864            $row .= ' ';
    854             if ( empty( $args['feed_image'] ) ) {
     865            if ( empty( $args['feed_image'] ) ) {
    855866                $row .= '(';
    856867            }
    857868
    858             $row .= '<a href="' . get_author_feed_link( $user->ID, $args['feed_type'] ) . '"';
     869            $row .= '<a href="' . get_author_feed_link( $user->ID, $args['feed_type'] ) . '"';
    859870
    860871            $alt = '';
    861             if ( ! empty( $args['feed'] ) ) {
    862                 $alt  = ' alt="' . esc_attr( $args['feed'] ) . '"';
    863                 $name = $args['feed'];
     872            if ( ! empty( $args['feed'] ) ) {
     873                $alt  = ' alt="' . esc_attr( $args['feed'] ) . '"';
     874                $name = $args['feed'];
    864875            }
    865876
    866877            $row .= '>';
    867878
    868             if ( ! empty( $args['feed_image'] ) ) {
    869                 $row .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
     879            if ( ! empty( $args['feed_image'] ) ) {
     880                $row .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
    870881            } else {
    871882                $row .= $name;
     
    874885            $row .= '</a>';
    875886
    876             if ( empty( $args['feed_image'] ) ) {
     887            if ( empty( $args['feed_image'] ) ) {
    877888                $row .= ')';
    878889            }
     
    880891
    881892        $return .= $row;
    882         $return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
     893        $return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
    883894    }
    884895
    885896    $return = rtrim( $return, ', ' );
    886897
    887     if ( ! $args['echo'] ) {
     898    if ( ! $args['echo'] ) {
    888899        return $return;
    889900    }
Note: See TracChangeset for help on using the changeset viewer.