WordPress.org

Make WordPress Core


Ignore:
Timestamp:
12/14/2008 12:13:30 PM (9 years ago)
Author:
matt
Message:

Update pagination links on edit-comments when approving/deleting/etc. Hat tip: mdawaffe. Fixes #8584.

File:
1 edited

Legend:

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

    r10150 r10204  
    6464endif;
    6565
     66/**
     67 * Sends back current comment total and new page links if they need to be updated.
     68 *
     69 * Contrary to normal success AJAX response ("1"), die with time() on success.
     70 *
     71 * @since 2.7
     72 *
     73 * @param int $comment_id
     74 * @return die
     75 */
     76function _wp_ajax_delete_comment_response( $comment_id ) {
     77    $total = (int) @$_POST['_total'];
     78    $per_page = (int) @$_POST['_per_page'];
     79    $page = (int) @$_POST['_page'];
     80    $url = clean_url( @$_POST['_url'], null, 'url' );
     81    // JS didn't send us everything we need to know.  Just die with success message
     82    if ( !$total || !$per_page || !$page || !$url )
     83        die( (string) time() );
     84
     85    if ( --$total < 0 ) // Take the total from POST and decrement it (since we just deleted one)
     86        $total = 0;
     87
     88    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
     89        die( (string) time() );
     90
     91    $status = 'total_comments'; // What type of comment count are we looking for?
     92    $parsed = parse_url( $url );
     93    if ( isset( $parsed['query'] ) ) {
     94        parse_str( $parsed['query'], $query_vars );
     95        if ( !empty( $query_vars['comment_status'] ) )
     96            $status = $query_vars['comment_status'];
     97    }
     98
     99    $comment_count = wp_count_comments();
     100    $time = time(); // The time since the last comment count
     101
     102    if ( isset( $comment_count->$status ) ) // We're looking for a known type of comment count
     103        $total = $comment_count->$status;
     104    // else use the decremented value from above
     105
     106    $page_links = paginate_links( array(
     107        'base' => add_query_arg( 'apage', '%#%', $url ),
     108        'format' => '',
     109        'prev_text' => __('&laquo;'),
     110        'next_text' => __('&raquo;'),
     111        'total' => ceil($total / $per_page),
     112        'current' => $page
     113    ) );
     114    $x = new WP_Ajax_Response( array(
     115        'what' => 'comment',
     116        'id' => $comment_id, // here for completeness - not used
     117        'supplemental' => array(
     118            'pageLinks' => $page_links,
     119            'total' => $total,
     120            'time' => $time
     121        )
     122    ) );
     123    $x->send();
     124}
     125
    66126$id = isset($_POST['id'])? (int) $_POST['id'] : 0;
    67127switch ( $action = $_POST['action'] ) :
    68 case 'delete-comment' :
     128case 'delete-comment' : // On success, die with time() instead of 1
    69129    check_ajax_referer( "delete-comment_$id" );
    70130    if ( !$comment = get_comment( $id ) )
    71         die('1');
     131        die( (string) time() );
    72132    if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
    73133        die('-1');
     
    75135    if ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
    76136        if ( 'spam' == wp_get_comment_status( $comment->comment_ID ) )
    77             die('1');
     137            die( (string) time() );
    78138        $r = wp_set_comment_status( $comment->comment_ID, 'spam' );
    79139    } else {
    80140        $r = wp_delete_comment( $comment->comment_ID );
    81141    }
    82 
    83     die( $r ? '1' : '0' );
     142    if ( $r ) // Decide if we need to send back '1' or a more complicated response including page links and comment counts
     143        _wp_ajax_delete_comment_response( $comment->comment_ID );
     144    die( '0' );
    84145    break;
    85146case 'delete-cat' :
     
    196257        die('0');
    197258    break;
    198 case 'dim-comment' :
     259case 'dim-comment' : // On success, die with time() instead of 1
    199260    if ( !$comment = get_comment( $id ) )
    200261        die('0');
     
    207268    $current = wp_get_comment_status( $comment->comment_ID );
    208269    if ( $_POST['new'] == $current )
    209         die('1');
    210 
     270        die( (string) time() );
     271
     272    $r = 0;
    211273    if ( in_array( $current, array( 'unapproved', 'spam' ) ) ) {
    212274        check_ajax_referer( "approve-comment_$id" );
    213275        if ( wp_set_comment_status( $comment->comment_ID, 'approve' ) )
    214             die('1');
     276            $r = 1;
    215277    } else {
    216278        check_ajax_referer( "unapprove-comment_$id" );
    217279        if ( wp_set_comment_status( $comment->comment_ID, 'hold' ) )
    218             die('1');
    219     }
    220     die('0');
     280            $r = 1;
     281    }
     282    if ( $r ) // Decide if we need to send back '1' or a more complicated response including page links and comment counts
     283        _wp_ajax_delete_comment_response( $comment->comment_ID );
     284    die( '0' );
    221285    break;
    222286case 'add-category' : // On the Fly
Note: See TracChangeset for help on using the changeset viewer.