WordPress.org

Make WordPress Core

Ticket #6770: get_comments_pending_num.diff

File get_comments_pending_num.diff, 2.1 KB (added by ryan, 7 years ago)

Consolidate get_pending_comments_num() queries

  • wp-admin/includes/comment.php

     
    6666 
    6767function get_pending_comments_num( $post_id ) { 
    6868        global $wpdb; 
    69         $post_id = (int) $post_id; 
    70         $pending = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '0'", $post_id) ); 
    71         return $pending; 
     69 
     70        $single = false; 
     71        if ( !is_array($post_id) ) { 
     72                $post_id = (array) $post_id; 
     73                $single = true; 
     74        } 
     75        $post_id = array_map('intval', $post_id); 
     76        $post_id = "'" . implode("', '", $post_id) . "'"; 
     77 
     78        $pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id ) AND comment_approved = '0' GROUP BY comment_post_ID", ARRAY_N ); 
     79 
     80        if ( empty($pending) ) 
     81                return 0; 
     82 
     83        if ( $single ) 
     84                return $pending[0][1]; 
     85 
     86        $pending_keyed = array(); 
     87        foreach ( $pending as $pend ) 
     88                $pending_keyed[$pend[0]] = $pend[1]; 
     89 
     90        return $pending_keyed; 
    7291} 
    7392 
    7493// Add avatars to relevant places in admin, or try to 
  • wp-admin/edit-post-rows.php

     
    2222if ( have_posts() ) { 
    2323$bgcolor = ''; 
    2424add_filter('the_title','wp_specialchars'); 
     25 
     26// Create array of post IDs. 
     27$post_ids = array(); 
     28foreach ( $wp_query->posts as $a_post ) 
     29        $post_ids[] = $a_post->ID; 
     30 
     31$comment_pending_count = get_pending_comments_num($post_ids); 
     32 
    2533while (have_posts()) : the_post(); 
    2634$class = 'alternate' == $class ? '' : 'alternate'; 
    2735global $current_user; 
     
    113121                ?> 
    114122                <td class="num"><div class="post-com-count-wrapper"> 
    115123                <?php 
    116                 $left = get_pending_comments_num( $post->ID ); 
     124                $left = isset($comment_pending_count) ? $comment_pending_count[$post->ID] : 0; 
    117125                $pending_phrase = sprintf( __('%s pending'), number_format( $left ) ); 
    118126                if ( $left ) 
    119127                        echo '<strong>';