Add 'who' arg to wp_dropdown_users() and get_users(). Add' hide_if_only_one_author' argument to get_users(). Query only authors (user level greater than 0) when who => author is passed. Query only authors for author meta box and quick edit dropdowns. Props scribu. fixes #15871

git-svn-id: https://develop.svn.wordpress.org/trunk@17088 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2010-12-20 17:25:39 +00:00
parent dd449b12cc
commit aca2b86eaf
4 changed files with 31 additions and 14 deletions

View File

@ -757,7 +757,6 @@ class WP_Posts_List_Table extends WP_List_Table {
<?php touch_time( 1, 1, 4, 1 ); ?> <?php touch_time( 1, 1, 4, 1 ); ?>
</div> </div>
<br class="clear" /> <br class="clear" />
<?php endif; // $bulk <?php endif; // $bulk
if ( post_type_supports( $screen->post_type, 'author' ) ) : if ( post_type_supports( $screen->post_type, 'author' ) ) :
@ -765,6 +764,8 @@ class WP_Posts_List_Table extends WP_List_Table {
if ( is_super_admin() || current_user_can( $post_type_object->cap->edit_others_posts ) ) : if ( is_super_admin() || current_user_can( $post_type_object->cap->edit_others_posts ) ) :
$users_opt = array( $users_opt = array(
'hide_if_only_one_author' => true,
'who' => 'authors',
'name' => 'post_author', 'name' => 'post_author',
'class'=> 'authors', 'class'=> 'authors',
'multi' => 1, 'multi' => 1,
@ -772,10 +773,13 @@ class WP_Posts_List_Table extends WP_List_Table {
); );
if ( $bulk ) if ( $bulk )
$users_opt['show_option_none'] = __( '&mdash; No Change &mdash;' ); $users_opt['show_option_none'] = __( '&mdash; No Change &mdash;' );
if ( $authors = wp_dropdown_users( $users_opt ) ) :
$authors_dropdown = '<label>'; $authors_dropdown = '<label>';
$authors_dropdown .= '<span class="title">' . __( 'Author' ) . '</span>'; $authors_dropdown .= '<span class="title">' . __( 'Author' ) . '</span>';
$authors_dropdown .= wp_dropdown_users( $users_opt ); $authors_dropdown .= $authors;
$authors_dropdown .= '</label>'; $authors_dropdown .= '</label>';
endif;
endif; // authors endif; // authors
?> ?>

View File

@ -530,11 +530,11 @@ function post_slug_meta_box($post) {
*/ */
function post_author_meta_box($post) { function post_author_meta_box($post) {
global $user_ID; global $user_ID;
?> ?>
<label class="screen-reader-text" for="post_author_override"><?php _e('Author'); ?></label> <label class="screen-reader-text" for="post_author_override"><?php _e('Author'); ?></label>
<?php <?php
wp_dropdown_users( array( wp_dropdown_users( array(
'who' => 'authors',
'name' => 'post_author_override', 'name' => 'post_author_override',
'selected' => empty($post->ID) ? $user_ID : $post->post_author 'selected' => empty($post->ID) ? $user_ID : $post->post_author
) ); ) );

View File

@ -433,6 +433,11 @@ function get_meta_sql( $meta_query, $meta_type, $primary_table, $primary_id_colu
} else { } else {
$meta_compare_string = '%s'; $meta_compare_string = '%s';
} }
// @todo Temporary hack to support empty values. Do not use outside of core.
if ( '_wp_zero_value' == $meta_value )
$meta_value = 0;
$where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value ); $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
} }

View File

@ -481,10 +481,18 @@ class WP_User_Query {
$this->query_where .= $this->get_search_sql( $search, $search_columns, $wild ); $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild );
} }
$blog_id = absint( $qv['blog_id'] );
if ( 'authors' == $qv['who'] && $blog_id ) {
$qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level';
$qv['meta_value'] = '_wp_zero_value'; // Hack to pass '0'
$qv['meta_compare'] = '!=';
$qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
}
_parse_meta_query( $qv ); _parse_meta_query( $qv );
$role = trim( $qv['role'] ); $role = trim( $qv['role'] );
$blog_id = absint( $qv['blog_id'] );
if ( $blog_id && ( $role || is_multisite() ) ) { if ( $blog_id && ( $role || is_multisite() ) ) {
$cap_meta_query = array(); $cap_meta_query = array();
@ -929,6 +937,7 @@ function setup_userdata($for_user_id = '') {
* <ol> * <ol>
* <li>show_option_all - Text to show all and whether HTML option exists.</li> * <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>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>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>order - Default is 'ASC'. Can also be 'DESC'.</li>
* <li>include - User IDs to include.</li> * <li>include - User IDs to include.</li>
@ -941,6 +950,7 @@ function setup_userdata($for_user_id = '') {
* <li>id - Default is the value of the 'name' parameter. ID 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>class - Class attribute of select element.</li>
* <li>blog_id - ID of blog (Multisite only). Defaults to ID of current blog.</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> * </ol>
* *
* @since 2.3.0 * @since 2.3.0
@ -950,14 +960,13 @@ function setup_userdata($for_user_id = '') {
* @return string|null Null on display. String of HTML content on retrieve. * @return string|null Null on display. String of HTML content on retrieve.
*/ */
function wp_dropdown_users( $args = '' ) { function wp_dropdown_users( $args = '' ) {
global $wpdb;
$defaults = array( $defaults = array(
'show_option_all' => '', 'show_option_none' => '', 'show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '',
'orderby' => 'display_name', 'order' => 'ASC', 'orderby' => 'display_name', 'order' => 'ASC',
'include' => '', 'exclude' => '', 'multi' => 0, 'include' => '', 'exclude' => '', 'multi' => 0,
'show' => 'display_name', 'echo' => 1, 'show' => 'display_name', 'echo' => 1,
'selected' => 0, 'name' => 'user', 'class' => '', 'blog_id' => $GLOBALS['blog_id'], 'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '',
'id' => '', 'blog_id' => $GLOBALS['blog_id'], 'who' => ''
); );
$defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0; $defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0;
@ -965,13 +974,12 @@ function wp_dropdown_users( $args = '' ) {
$r = wp_parse_args( $args, $defaults ); $r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP ); extract( $r, EXTR_SKIP );
$query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order' ) ); $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
$query_args['fields'] = array( 'ID', $show ); $query_args['fields'] = array( 'ID', $show );
$users = get_users( $query_args ); $users = get_users( $query_args );
$output = ''; $output = '';
if ( !empty($users) ) { if ( !empty($users) && ( empty($hide_if_only_one_author) || count($users) > 1 ) ) {
$name = esc_attr( $name ); $name = esc_attr( $name );
if ( $multi && ! $id ) if ( $multi && ! $id )
$id = ''; $id = '';