WordPress.org

Make WordPress Core

Ticket #21431: 21431.diff

File 21431.diff, 5.2 KB (added by ryan, 21 months ago)

Core patch plus corresponding changes to a plugin to provide context

  • wp-includes/post.php

     
    42344234 * @param string $post_type Post type. 
    42354235 * @param bool $full Optional. Returns a full WHERE statement instead of just an 'andalso' term. 
    42364236 * @param int $post_author Optional. Query posts having a single author ID. 
     4237 * @param bool $public_only Optional. Only return public posts. Skips cap checks for $current_user.  Default is false. 
    42374238 * @return string SQL WHERE code that can be added to a query. 
    42384239 */ 
    4239 function get_posts_by_author_sql( $post_type, $full = true, $post_author = null ) { 
     4240function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) { 
    42404241        global $user_ID, $wpdb; 
    42414242 
    42424243        // Private posts 
     
    42604261 
    42614262        $sql .= "(post_status = 'publish'"; 
    42624263 
    4263         if ( current_user_can( $cap ) ) { 
    4264                 // Does the user have the capability to view private posts? Guess so. 
    4265                 $sql .= " OR post_status = 'private'"; 
    4266         } elseif ( is_user_logged_in() ) { 
    4267                 // Users can view their own private posts. 
    4268                 $id = (int) $user_ID; 
    4269                 if ( null === $post_author || ! $full ) { 
    4270                         $sql .= " OR post_status = 'private' AND post_author = $id"; 
    4271                 } elseif ( $id == (int) $post_author ) { 
     4264        // Only need to check the cap if $public_only is false 
     4265        if ( false === $public_only ) { 
     4266                if ( current_user_can( $cap ) ) { 
     4267                        // Does the user have the capability to view private posts? Guess so. 
    42724268                        $sql .= " OR post_status = 'private'"; 
     4269                } elseif ( is_user_logged_in() ) { 
     4270                        // Users can view their own private posts. 
     4271                        $id = (int) $user_ID; 
     4272                        if ( null === $post_author || ! $full ) { 
     4273                                $sql .= " OR post_status = 'private' AND post_author = $id"; 
     4274                        } elseif ( $id == (int) $post_author ) { 
     4275                                $sql .= " OR post_status = 'private'"; 
     4276                        } // else none 
    42734277                } // else none 
    4274         } // else none 
     4278        } 
    42754279 
    42764280        $sql .= ')'; 
    42774281 
  • wp-includes/user.php

     
    167167 * 
    168168 * @param array $users Array of user IDs. 
    169169 * @param string|array $post_type Optional. Post type to check. Defaults to post. 
     170 * @param bool $public_only Optional. Only return counts for public posts.  Defaults to false. 
    170171 * @return array Amount of posts each user has written. 
    171172 */ 
    172 function count_many_users_posts( $users, $post_type = 'post' ) { 
     173function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) { 
    173174        global $wpdb; 
    174175 
    175176        $count = array(); 
     
    177178                return $count; 
    178179 
    179180        $userlist = implode( ',', array_map( 'absint', $users ) ); 
    180         $where = get_posts_by_author_sql( $post_type ); 
     181        $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); 
    181182 
    182183        $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N ); 
    183184        foreach ( $result as $row ) { 
  • wp-content/mu-plugins/widgets/authors.php

     
    152152 * Widget to display a grid of author avatar images 
    153153 * Default size is 32px 
    154154 */ 
    155 function wp_widget_author_grid($args) { 
    156         global $wpdb; 
    157  
     155function wp_widget_author_grid( $args ) { 
     156        // This is lame and should go away but we need to do something with the output from get_author_posts_url() 
    158157        $cache_bucket = is_ssl() ? 'widget_author_grid_ssl' : 'widget_author_grid'; 
    159158 
    160159        if ( '%BEG_OF_TITLE%' != $args['before_title'] ) { 
     
    167166        $options = get_option('widget_author_grid'); 
    168167        $title = empty($options['title']) ? __('Authors') : $options['title']; 
    169168        $display_all_authors = $options['all'] ? true : false; 
     169        $users_of_blog = get_users_of_blog(); 
     170        foreach ( $users_of_blog as $user ) { 
     171                $user_ids[] = $user->ID; 
     172        } 
     173        $post_counts = count_many_users_posts( $user_ids, 'post', true ); 
    170174 
    171         $query_string = "SELECT user_id, display_name, user_login FROM $wpdb->users, $wpdb->usermeta WHERE $wpdb->users.ID = $wpdb->usermeta.user_id AND meta_key = '" . $wpdb->prefix . "capabilities'"; 
    172         $results = $wpdb->get_results($query_string); 
    173  
    174175        $size = $options[ 'avatar_size' ] ? $options[ 'avatar_size' ] : 32; 
    175176 
    176177        echo $before_widget; 
    177178        echo "\t" . $before_title . esc_html($title) . $after_title; 
    178179        echo "\t" . '<ul>' . "\n"; 
    179180 
    180         foreach ($results as $row) { 
    181                 $r = new WP_Query("author=$row->user_id&showposts=$query_number&what_to_show=posts&nopaging=0&post_status=publish"); 
     181        foreach ( $post_counts as $post_author => $post_count ) { 
     182                if ( 0 == $post_count && !$display_all_authors ) continue; 
    182183 
    183                 if ( !($r->have_posts() || $display_all_authors) ) continue; 
    184  
    185184                echo "\t\t" . '<li>' . "\n"; 
    186185 
    187186                //display avatar 
    188                 echo "\t\t\t" . '<a href="' . get_option('home') . '/author/' . esc_attr($row->user_login) . '/"> '; 
    189                 echo get_avatar($row->user_id, $size, '', true ); 
     187                echo "\t\t\t" . '<a href="' . esc_url( get_author_posts_url( $post_author ) ) . '"> '; 
     188                echo get_avatar( $post_author, $size, '', true ); 
    190189                echo '</a>' . "\n"; 
    191190 
    192191                echo "\t\t" . '</li>' . "\n";