From eb9fa276024a286abc71e11598ac5f56ce0d6f5e Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 20 Dec 2010 17:25:39 +0000 Subject: [PATCH] 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: http://svn.automattic.com/wordpress/trunk@17088 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../includes/class-wp-posts-list-table.php | 14 +++++++---- wp-admin/includes/meta-boxes.php | 2 +- wp-includes/meta.php | 5 ++++ wp-includes/user.php | 24 ++++++++++++------- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/wp-admin/includes/class-wp-posts-list-table.php b/wp-admin/includes/class-wp-posts-list-table.php index 3782ba61b1..ca59392b6a 100644 --- a/wp-admin/includes/class-wp-posts-list-table.php +++ b/wp-admin/includes/class-wp-posts-list-table.php @@ -757,7 +757,6 @@ class WP_Posts_List_Table extends WP_List_Table {
- 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 ) ) : $users_opt = array( + 'hide_if_only_one_author' => true, + 'who' => 'authors', 'name' => 'post_author', 'class'=> 'authors', 'multi' => 1, @@ -772,10 +773,13 @@ class WP_Posts_List_Table extends WP_List_Table { ); if ( $bulk ) $users_opt['show_option_none'] = __( '— No Change —' ); - $authors_dropdown = ''; + + if ( $authors = wp_dropdown_users( $users_opt ) ) : + $authors_dropdown = ''; + endif; endif; // authors ?> diff --git a/wp-admin/includes/meta-boxes.php b/wp-admin/includes/meta-boxes.php index 96466b5743..f7fcc9604b 100644 --- a/wp-admin/includes/meta-boxes.php +++ b/wp-admin/includes/meta-boxes.php @@ -530,11 +530,11 @@ function post_slug_meta_box($post) { */ function post_author_meta_box($post) { global $user_ID; - ?> 'authors', 'name' => 'post_author_override', 'selected' => empty($post->ID) ? $user_ID : $post->post_author ) ); diff --git a/wp-includes/meta.php b/wp-includes/meta.php index ca243da11a..cfbaf20d71 100644 --- a/wp-includes/meta.php +++ b/wp-includes/meta.php @@ -433,6 +433,11 @@ function get_meta_sql( $meta_query, $meta_type, $primary_table, $primary_id_colu } else { $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 ); } diff --git a/wp-includes/user.php b/wp-includes/user.php index aab7a51a81..4d7ea4a2cb 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -481,10 +481,18 @@ class WP_User_Query { $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 ); $role = trim( $qv['role'] ); - $blog_id = absint( $qv['blog_id'] ); if ( $blog_id && ( $role || is_multisite() ) ) { $cap_meta_query = array(); @@ -929,6 +937,7 @@ function setup_userdata($for_user_id = '') { *
    *
  1. show_option_all - Text to show all and whether HTML option exists.
  2. *
  3. show_option_none - Text for show none and whether HTML option exists.
  4. + *
  5. hide_if_only_one_author - Don't create the dropdown if there is only one user.
  6. *
  7. orderby - SQL order by clause for what order the users appear. Default is 'display_name'.
  8. *
  9. order - Default is 'ASC'. Can also be 'DESC'.
  10. *
  11. include - User IDs to include.
  12. @@ -941,6 +950,7 @@ function setup_userdata($for_user_id = '') { *
  13. id - Default is the value of the 'name' parameter. ID attribute of select element.
  14. *
  15. class - Class attribute of select element.
  16. *
  17. blog_id - ID of blog (Multisite only). Defaults to ID of current blog.
  18. + *
  19. who - Which users to query. Currently only 'authors' is supported. Default is all users.
  20. *
* * @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. */ function wp_dropdown_users( $args = '' ) { - global $wpdb; $defaults = array( - 'show_option_all' => '', 'show_option_none' => '', + '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' => '', 'blog_id' => $GLOBALS['blog_id'], - 'id' => '', + 'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '', + 'blog_id' => $GLOBALS['blog_id'], 'who' => '' ); $defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0; @@ -965,13 +974,12 @@ function wp_dropdown_users( $args = '' ) { $r = wp_parse_args( $args, $defaults ); 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 ); - $users = get_users( $query_args ); $output = ''; - if ( !empty($users) ) { + if ( !empty($users) && ( empty($hide_if_only_one_author) || count($users) > 1 ) ) { $name = esc_attr( $name ); if ( $multi && ! $id ) $id = '';