示例#1
0
/**
 * Create dropdown HTML content of users.
 *
 * The content can either be displayed, which it is by default or retrieved by
 * setting the 'echo' argument. The 'include' and 'exclude' arguments do not
 * need to be used; all users will be displayed in that case. Only one can be
 * used, either 'include' or 'exclude', but not both.
 *
 * The available arguments are as follows:
 * <ol>
 * <li>show_option_all - Text to show all and whether HTML option exists.</li>
 * <li>show_option_none - Text for show none and whether HTML option exists.</li>
 * <li>hide_if_only_one_author - Don't create the dropdown if there is only one user.</li>
 * <li>orderby - SQL order by clause for what order the users appear. Default is 'display_name'.</li>
 * <li>order - Default is 'ASC'. Can also be 'DESC'.</li>
 * <li>include - User IDs to include.</li>
 * <li>exclude - User IDs to exclude.</li>
 * <li>multi - Default is 'false'. Whether to skip the ID attribute on the 'select' element. A 'true' value is overridden when id argument is set.</li>
 * <li>show - Default is 'display_name'. User table column to display. If the selected item is empty then the user_login will be displayed in parentheses</li>
 * <li>echo - Default is '1'. Whether to display or retrieve content.</li>
 * <li>selected - Which User ID is selected.</li>
 * <li>include_selected - Always include the selected user ID in the dropdown. Default is false.</li>
 * <li>name - Default is 'user'. Name attribute of select element.</li>
 * <li>id - Default is the value of the 'name' parameter. ID attribute of select element.</li>
 * <li>class - Class attribute of select element.</li>
 * <li>blog_id - ID of blog (Multisite only). Defaults to ID of current blog.</li>
 * <li>who - Which users to query.  Currently only 'authors' is supported. Default is all users.</li>
 * </ol>
 *
 * @since 2.3.0
 * @uses $nxtdb NXTClass database object for queries
 *
 * @param string|array $args Optional. Override defaults.
 * @return string|null Null on display. String of HTML content on retrieve.
 */
function nxt_dropdown_users($args = '')
{
    $defaults = array('show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '', 'orderby' => 'display_name', 'order' => 'ASC', 'include' => '', 'exclude' => '', 'multi' => 0, 'show' => 'display_name', 'echo' => 1, 'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '', 'blog_id' => $GLOBALS['blog_id'], 'who' => '', 'include_selected' => false);
    $defaults['selected'] = is_author() ? get_query_var('author') : 0;
    $r = nxt_parse_args($args, $defaults);
    extract($r, EXTR_SKIP);
    $query_args = nxt_array_slice_assoc($r, array('blog_id', 'include', 'exclude', 'orderby', 'order', 'who'));
    $query_args['fields'] = array('ID', $show);
    $users = get_users($query_args);
    $output = '';
    if (!empty($users) && (empty($hide_if_only_one_author) || count($users) > 1)) {
        $name = esc_attr($name);
        if ($multi && !$id) {
            $id = '';
        } else {
            $id = $id ? " id='" . esc_attr($id) . "'" : " id='{$name}'";
        }
        $output = "<select name='{$name}'{$id} class='{$class}'>\n";
        if ($show_option_all) {
            $output .= "\t<option value='0'>{$show_option_all}</option>\n";
        }
        if ($show_option_none) {
            $_selected = selected(-1, $selected, false);
            $output .= "\t<option value='-1'{$_selected}>{$show_option_none}</option>\n";
        }
        $found_selected = false;
        foreach ((array) $users as $user) {
            $user->ID = (int) $user->ID;
            $_selected = selected($user->ID, $selected, false);
            if ($_selected) {
                $found_selected = true;
            }
            $display = !empty($user->{$show}) ? $user->{$show} : '(' . $user->user_login . ')';
            $output .= "\t<option value='{$user->ID}'{$_selected}>" . esc_html($display) . "</option>\n";
        }
        if ($include_selected && !$found_selected && $selected > 0) {
            $user = get_userdata($selected);
            $_selected = selected($user->ID, $selected, false);
            $display = !empty($user->{$show}) ? $user->{$show} : '(' . $user->user_login . ')';
            $output .= "\t<option value='{$user->ID}'{$_selected}>" . esc_html($display) . "</option>\n";
        }
        $output .= "</select>";
    }
    $output = apply_filters('nxt_dropdown_users', $output);
    if ($echo) {
        echo $output;
    }
    return $output;
}
示例#2
0
/**
 * List all the authors of the blog, with several options available.
 *
 * <ul>
 * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
 * author's name.</li>
 * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
 * installed bydefault.</li>
 * <li>show_fullname (boolean) (false): Show their full names.</li>
 * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
 * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
 * <li>feed_image (string) (''): If isn't empty, use this image to link to
 * feeds.</li>
 * <li>echo (boolean) (true): Set to false to return the output, instead of
 * echoing.</li>
 * <li>style (string) ('list'): Whether to display list of authors in list form
 * or as a string.</li>
 * <li>html (bool) (true): Whether to list the items in html form or plaintext.
 * </li>
 * </ul>
 *
 * @link http://codex.nxtclass.org/Template_Tags/nxt_list_authors
 * @since 1.2.0
 * @param array $args The argument array.
 * @return null|string The output, if echo is set to false.
 */
function nxt_list_authors($args = '')
{
    global $nxtdb;
    $defaults = array('orderby' => 'name', 'order' => 'ASC', 'number' => '', 'optioncount' => false, 'exclude_admin' => true, 'show_fullname' => false, 'hide_empty' => true, 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true, 'style' => 'list', 'html' => true);
    $args = nxt_parse_args($args, $defaults);
    extract($args, EXTR_SKIP);
    $return = '';
    $query_args = nxt_array_slice_assoc($args, array('orderby', 'order', 'number'));
    $query_args['fields'] = 'ids';
    $authors = get_users($query_args);
    $author_count = array();
    foreach ((array) $nxtdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM {$nxtdb->posts} WHERE post_type = 'post' AND " . get_private_posts_cap_sql('post') . " GROUP BY post_author") as $row) {
        $author_count[$row->post_author] = $row->count;
    }
    foreach ($authors as $author_id) {
        $author = get_userdata($author_id);
        if ($exclude_admin && 'admin' == $author->display_name) {
            continue;
        }
        $posts = isset($author_count[$author->ID]) ? $author_count[$author->ID] : 0;
        if (!$posts && $hide_empty) {
            continue;
        }
        $link = '';
        if ($show_fullname && $author->first_name && $author->last_name) {
            $name = "{$author->first_name} {$author->last_name}";
        } else {
            $name = $author->display_name;
        }
        if (!$html) {
            $return .= $name . ', ';
            continue;
            // No need to go further to process HTML.
        }
        if ('list' == $style) {
            $return .= '<li>';
        }
        $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr(sprintf(__("Posts by %s"), $author->display_name)) . '">' . $name . '</a>';
        if (!empty($feed_image) || !empty($feed)) {
            $link .= ' ';
            if (empty($feed_image)) {
                $link .= '(';
            }
            $link .= '<a href="' . get_author_feed_link($author->ID) . '"';
            $alt = $title = '';
            if (!empty($feed)) {
                $title = ' title="' . esc_attr($feed) . '"';
                $alt = ' alt="' . esc_attr($feed) . '"';
                $name = $feed;
                $link .= $title;
            }
            $link .= '>';
            if (!empty($feed_image)) {
                $link .= '<img src="' . esc_url($feed_image) . '" style="border: none;"' . $alt . $title . ' />';
            } else {
                $link .= $name;
            }
            $link .= '</a>';
            if (empty($feed_image)) {
                $link .= ')';
            }
        }
        if ($optioncount) {
            $link .= ' (' . $posts . ')';
        }
        $return .= $link;
        $return .= 'list' == $style ? '</li>' : ', ';
    }
    $return = rtrim($return, ', ');
    if (!$echo) {
        return $return;
    }
    echo $return;
}