Make WordPress Core

Ticket #15145: 15145.diff

File 15145.diff, 4.3 KB (added by rohan013, 9 years ago)

Adds a new function wp_list_users() which is very similar to wp_list_authors except the 'optioncount' and 'hide_empty' args since they are not valid here.

  • trunk/src/wp-includes/user.php

     
    940940}
    941941
    942942/**
     943 * List all the users of the blog, with several options available.
     944 *
     945 * @param string|array $args {
     946 *     Optional. Array or string of default arguments.
     947 *
     948 *     @type string $orderby       How to sort the users. Accepts 'nicename', 'email', 'url', 'registered',
     949 *                                 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
     950 *                                 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
     951 *     @type string $order         Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
     952 *     @type int    $number        Maximum users to return or display. Default empty (all users).
     953 *     @type bool   $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
     954 *     @type bool   $show_fullname Whether to show the user's full name. Default false.
     955 *     @type string $feed          If not empty, show a link to the user's feed and use this text as the alt
     956 *                                 parameter of the link. Default empty.
     957 *     @type string $feed_image    If not empty, show a link to the user's feed and use this image URL as
     958 *                                 clickable anchor. Default empty.
     959 *     @type string $feed_type     The feed type to link to, such as 'rss2'. Defaults to default feed type.
     960 *     @type bool   $echo          Whether to output the result or instead return it. Default true.
     961 *     @type string $style         If 'list', each user is wrapped in an `<li>` element, otherwise the users
     962 *                                 will be separated by commas.
     963 *     @type bool   $html          Whether to list the items in HTML form or plaintext. Default true.
     964 *     @type string $exclude       An array, comma-, or space-separated list of user IDs to exclude. Default empty.
     965 *     @type string $include       An array, comma-, or space-separated list of user IDs to include. Default empty.
     966 * }
     967 * @return null|string The output, if echo is set to false. Otherwise null.
     968 */
     969function wp_list_users( $args = array() ) {
     970
     971        $defaults = array(
     972                'orderby' => 'name',
     973                'order' => 'ASC',
     974                'number' => '',
     975                'exclude_admin' => true,
     976                'show_fullname' => false,
     977                'feed' => '',
     978                'feed_image' => '',
     979                'feed_type' => '',
     980                'echo' => true,
     981                'style' => 'list',
     982                'html' => true,
     983                'exclude' => '',
     984                'include' => ''
     985        );
     986
     987        $args = wp_parse_args( $args, $defaults );
     988
     989        $return = '';
     990
     991        $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
     992        $query_args['fields'] = 'ids';
     993        $users = get_users( $query_args );
     994
     995        foreach ( $users as $user_id ) {
     996                $user = get_userdata( $user_id );
     997
     998                if ( $args['exclude_admin'] && 'admin' == $user->display_name ) {
     999                        continue;
     1000                }
     1001
     1002                if ( $args['show_fullname'] && $user->first_name && $user->last_name ) {
     1003                        $name = "$user->first_name $user->last_name";
     1004                } else {
     1005                        $name = $user->display_name;
     1006                }
     1007
     1008                if ( ! $args['html'] ) {
     1009                        $return .= $name . ', ';
     1010
     1011                        continue; // No need to go further to process HTML.
     1012                }
     1013
     1014                if ( 'list' == $args['style'] ) {
     1015                        $return .= '<li>';
     1016                }
     1017
     1018                $row = $name;
     1019
     1020                if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
     1021                        $row .= ' ';
     1022                        if ( empty( $args['feed_image'] ) ) {
     1023                                $row .= '(';
     1024                        }
     1025
     1026                        $row .= '<a href="' . get_author_feed_link( $user->ID, $args['feed_type'] ) . '"';
     1027
     1028                        $alt = '';
     1029                        if ( ! empty( $args['feed'] ) ) {
     1030                                $alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
     1031                                $name = $args['feed'];
     1032                        }
     1033
     1034                        $row .= '>';
     1035
     1036                        if ( ! empty( $args['feed_image'] ) ) {
     1037                                $row .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
     1038                        } else {
     1039                                $row .= $name;
     1040                        }
     1041
     1042                        $row .= '</a>';
     1043
     1044                        if ( empty( $args['feed_image'] ) ) {
     1045                                $row .= ')';
     1046                        }
     1047                }
     1048
     1049                $return .= $row;
     1050                $return .= ( 'list' == $args['style'] ) ? '</li>' : ', ';
     1051        }
     1052
     1053        $return = rtrim( $return, ', ' );
     1054
     1055        if ( ! $args['echo'] ) {
     1056                return $return;
     1057        }
     1058        echo $return;
     1059}
     1060
     1061/**
    9431062 * Get the blogs a user belongs to.
    9441063 *
    9451064 * @since 3.0.0