Make WordPress Core


Ignore:
Timestamp:
01/12/2010 10:38:26 PM (16 years ago)
Author:
westi
Message:

Improve get_pending_comments_num() to be a little more predictable and revert the erroneous change in [12596]. See #11882.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/comment.php

    r12595 r12715  
    110110    $single = false;
    111111    if ( !is_array($post_id) ) {
    112         $post_id = (array) $post_id;
     112        $post_id_array = (array) $post_id;
    113113        $single = true;
     114    } else {
     115        $post_id_array = $post_id;
    114116    }
    115     $post_id = array_map('intval', $post_id);
    116     $post_id = "'" . implode("', '", $post_id) . "'";
     117    $post_id_array = array_map('intval', $post_id_array);
     118    $post_id_in = "'" . implode("', '", $post_id_array) . "'";
    117119
    118     $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 );
     120    $pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' GROUP BY comment_post_ID", ARRAY_A );
    119121
    120     if ( empty($pending) )
    121         return 0;
     122    if ( $single ) {
     123        if ( empty($pending) )
     124            return 0;
     125        else
     126            return absint($pending[0]['num_comments']);
     127    }
     128   
     129    $pending_keyed = array();
     130   
     131    // Default to zero pending for all posts in request
     132    foreach ( $post_id_array as $id )
     133        $pending_keyed[$id] = 0;
    122134
    123     if ( $single )
    124         return $pending[0][1];
    125 
    126     $pending_keyed = array();
    127     foreach ( $pending as $pend )
    128         $pending_keyed[$pend[0]] = $pend[1];
     135    if ( !empty($pending) )
     136        foreach ( $pending as $pend )
     137            $pending_keyed[$pend['comment_post_ID']] = absint($pend['num_comments']);
    129138
    130139    return $pending_keyed;
Note: See TracChangeset for help on using the changeset viewer.