Skip to content

Commit 6663deb

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/trunk@54262 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 79d65aa commit 6663deb

File tree

2 files changed

+84
-42
lines changed

2 files changed

+84
-42
lines changed

src/wp-includes/author-template.php

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -450,32 +450,63 @@ function wp_list_authors( $args = '' ) {
450450
'include' => '',
451451
);
452452

453-
$args = wp_parse_args( $args, $defaults );
453+
$parsed_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( $parsed_args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
458458
$query_args['fields'] = 'ids';
459-
$authors = get_users( $query_args );
460459

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;
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+
}
464494
}
495+
465496
foreach ( $authors as $author_id ) {
466-
$posts = isset( $author_count[ $author_id ] ) ? $author_count[ $author_id ] : 0;
497+
$posts = isset( $post_counts[ $author_id ] ) ? $post_counts[ $author_id ] : 0;
467498

468-
if ( ! $posts && $args['hide_empty'] ) {
499+
if ( ! $posts && $parsed_args['hide_empty'] ) {
469500
continue;
470501
}
471502

472503
$author = get_userdata( $author_id );
473504

474-
if ( $args['exclude_admin'] && 'admin' === $author->display_name ) {
505+
if ( $parsed_args['exclude_admin'] && 'admin' === $author->display_name ) {
475506
continue;
476507
}
477508

478-
if ( $args['show_fullname'] && $author->first_name && $author->last_name ) {
509+
if ( $parsed_args['show_fullname'] && $author->first_name && $author->last_name ) {
479510
$name = sprintf(
480511
/* translators: 1: User's first name, 2: Last name. */
481512
_x( '%1$s %2$s', 'Display name based on first name and last name' ),
@@ -486,13 +517,13 @@ function wp_list_authors( $args = '' ) {
486517
$name = $author->display_name;
487518
}
488519

489-
if ( ! $args['html'] ) {
520+
if ( ! $parsed_args['html'] ) {
490521
$return .= $name . ', ';
491522

492523
continue; // No need to go further to process HTML.
493524
}
494525

495-
if ( 'list' === $args['style'] ) {
526+
if ( 'list' === $parsed_args['style'] ) {
496527
$return .= '<li>';
497528
}
498529

@@ -504,46 +535,46 @@ function wp_list_authors( $args = '' ) {
504535
$name
505536
);
506537

507-
if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
538+
if ( ! empty( $parsed_args['feed_image'] ) || ! empty( $parsed_args['feed'] ) ) {
508539
$link .= ' ';
509-
if ( empty( $args['feed_image'] ) ) {
540+
if ( empty( $parsed_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, $parsed_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( $parsed_args['feed'] ) ) {
548+
$alt = ' alt="' . esc_attr( $parsed_args['feed'] ) . '"';
549+
$name = $parsed_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( $parsed_args['feed_image'] ) ) {
555+
$link .= '<img src="' . esc_url( $parsed_args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
525556
} else {
526557
$link .= $name;
527558
}
528559

529560
$link .= '</a>';
530561

531-
if ( empty( $args['feed_image'] ) ) {
562+
if ( empty( $parsed_args['feed_image'] ) ) {
532563
$link .= ')';
533564
}
534565
}
535566

536-
if ( $args['optioncount'] ) {
567+
if ( $parsed_args['optioncount'] ) {
537568
$link .= ' (' . $posts . ')';
538569
}
539570

540571
$return .= $link;
541-
$return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
572+
$return .= ( 'list' === $parsed_args['style'] ) ? '</li>' : ', ';
542573
}
543574

544575
$return = rtrim( $return, ', ' );
545576

546-
if ( $args['echo'] ) {
577+
if ( $parsed_args['echo'] ) {
547578
echo $return;
548579
} else {
549580
return $return;

src/wp-includes/user.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -811,22 +811,33 @@ function wp_list_users( $args = array() ) {
811811
'include' => '',
812812
);
813813

814-
$args = wp_parse_args( $args, $defaults );
814+
$parsed_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( $parsed_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 ( $parsed_args['exclude_admin'] && 'admin' === $user->display_name ) {
826837
continue;
827838
}
828839

829-
if ( $args['show_fullname'] && '' !== $user->first_name && '' !== $user->last_name ) {
840+
if ( $parsed_args['show_fullname'] && '' !== $user->first_name && '' !== $user->last_name ) {
830841
$name = sprintf(
831842
/* translators: 1: User's first name, 2: Last name. */
832843
_x( '%1$s %2$s', 'Display name based on first name and last name' ),
@@ -837,54 +848,54 @@ function wp_list_users( $args = array() ) {
837848
$name = $user->display_name;
838849
}
839850

840-
if ( ! $args['html'] ) {
851+
if ( ! $parsed_args['html'] ) {
841852
$return .= $name . ', ';
842853

843854
continue; // No need to go further to process HTML.
844855
}
845856

846-
if ( 'list' === $args['style'] ) {
857+
if ( 'list' === $parsed_args['style'] ) {
847858
$return .= '<li>';
848859
}
849860

850861
$row = $name;
851862

852-
if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
863+
if ( ! empty( $parsed_args['feed_image'] ) || ! empty( $parsed_args['feed'] ) ) {
853864
$row .= ' ';
854-
if ( empty( $args['feed_image'] ) ) {
865+
if ( empty( $parsed_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, $parsed_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( $parsed_args['feed'] ) ) {
873+
$alt = ' alt="' . esc_attr( $parsed_args['feed'] ) . '"';
874+
$name = $parsed_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( $parsed_args['feed_image'] ) ) {
880+
$row .= '<img src="' . esc_url( $parsed_args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
870881
} else {
871882
$row .= $name;
872883
}
873884

874885
$row .= '</a>';
875886

876-
if ( empty( $args['feed_image'] ) ) {
887+
if ( empty( $parsed_args['feed_image'] ) ) {
877888
$row .= ')';
878889
}
879890
}
880891

881892
$return .= $row;
882-
$return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
893+
$return .= ( 'list' === $parsed_args['style'] ) ? '</li>' : ', ';
883894
}
884895

885896
$return = rtrim( $return, ', ' );
886897

887-
if ( ! $args['echo'] ) {
898+
if ( ! $parsed_args['echo'] ) {
888899
return $return;
889900
}
890901
echo $return;

0 commit comments

Comments
 (0)