Make WordPress Core


Ignore:
Timestamp:
01/22/2011 06:47:42 PM (16 years ago)
Author:
ryan
Message:

Update counts and pagination when trashing and moderating comments. Props garyc40, koopersmith, mdawaffe, nacin. fixes #15530

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-ajax.php

    r17325 r17354  
    190190 * @return die
    191191 */
    192 function _wp_ajax_delete_comment_response( $comment_id ) {
     192function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) {
    193193        $total = (int) @$_POST['_total'];
    194194        $per_page = (int) @$_POST['_per_page'];
     
    199199                die( (string) time() );
    200200
    201         if ( --$total < 0 ) // Take the total from POST and decrement it (since we just deleted one)
     201        $total += $delta;
     202        if ( $total < 0 )
    202203                $total = 0;
    203204
    204         if ( 0 != $total % $per_page && 1 != mt_rand( 1, $per_page ) ) // Only do the expensive stuff on a page-break, and about 1 other time per page
    205                 die( (string) time() );
    206 
    207         $post_id = 0;
    208         $status = 'total_comments'; // What type of comment count are we looking for?
    209         $parsed = parse_url( $url );
    210         if ( isset( $parsed['query'] ) ) {
    211                 parse_str( $parsed['query'], $query_vars );
    212                 if ( !empty( $query_vars['comment_status'] ) )
    213                         $status = $query_vars['comment_status'];
    214                 if ( !empty( $query_vars['p'] ) )
    215                         $post_id = (int) $query_vars['p'];
    216         }
    217 
    218         $comment_count = wp_count_comments($post_id);
     205        // Only do the expensive stuff on a page-break, and about 1 other time per page
     206        if ( 0 == $total % $per_page || 1 == mt_rand( 1, $per_page ) ) {
     207                $post_id = 0;
     208                $status = 'total_comments'; // What type of comment count are we looking for?
     209                $parsed = parse_url( $url );
     210                if ( isset( $parsed['query'] ) ) {
     211                        parse_str( $parsed['query'], $query_vars );
     212                        if ( !empty( $query_vars['comment_status'] ) )
     213                                $status = $query_vars['comment_status'];
     214                        if ( !empty( $query_vars['p'] ) )
     215                                $post_id = (int) $query_vars['p'];
     216                }
     217
     218                $comment_count = wp_count_comments($post_id);
     219
     220                if ( isset( $comment_count->$status ) ) // We're looking for a known type of comment count
     221                        $total = $comment_count->$status;
     222                        // else use the decremented value from above
     223        }
     224
    219225        $time = time(); // The time since the last comment count
    220226
    221         if ( isset( $comment_count->$status ) ) // We're looking for a known type of comment count
    222                 $total = $comment_count->$status;
    223         // else use the decremented value from above
    224 
    225         $page_links = paginate_links( array(
    226                 'base' => add_query_arg( 'apage', '%#%', $url ),
    227                 'format' => '',
    228                 'prev_text' => __('&laquo;'),
    229                 'next_text' => __('&raquo;'),
    230                 'total' => ceil($total / $per_page),
    231                 'current' => $page
    232         ) );
    233227        $x = new WP_Ajax_Response( array(
    234228                'what' => 'comment',
    235229                'id' => $comment_id, // here for completeness - not used
    236230                'supplemental' => array(
    237                         'pageLinks' => $page_links,
     231                        'total_items_i18n' => sprintf( _n( '1 item', '%s items', $total ), number_format_i18n( $total ) ),
     232                        'total_pages' => ceil( $total / $per_page ),
     233                        'total_pages_i18n' => number_format_i18n( ceil( $total / $per_page ) ),
    238234                        'total' => $total,
    239235                        'time' => $time
     
    332328        $status = wp_get_comment_status( $comment->comment_ID );
    333329
     330        $delta = -1;
    334331        if ( isset($_POST['trash']) && 1 == $_POST['trash'] ) {
    335332                if ( 'trash' == $status )
     
    340337                        die( (string) time() );
    341338                $r = wp_untrash_comment( $comment->comment_ID );
     339                if ( ! isset( $_POST['comment_status'] ) || $_POST['comment_status'] != 'trash' ) // undo trash, not in trash
     340                        $delta = 1;
    342341        } elseif ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
    343342                if ( 'spam' == $status )
     
    348347                        die( (string) time() );
    349348                $r = wp_unspam_comment( $comment->comment_ID );
     349                if ( ! isset( $_POST['comment_status'] ) || $_POST['comment_status'] != 'spam' ) // undo spam, not in spam
     350                        $delta = 1;
    350351        } elseif ( isset($_POST['delete']) && 1 == $_POST['delete'] ) {
    351352                $r = wp_delete_comment( $comment->comment_ID );
     
    355356
    356357        if ( $r ) // Decide if we need to send back '1' or a more complicated response including page links and comment counts
    357                 _wp_ajax_delete_comment_response( $comment->comment_ID );
     358                _wp_ajax_delete_comment_response( $comment->comment_ID, $delta );
    358359        die( '0' );
    359360        break;
Note: See TracChangeset for help on using the changeset viewer.