WordPress.org

Make WordPress Core

Ticket #15530: garyc40.15530.diff

File garyc40.15530.diff, 8.6 KB (added by garyc40, 7 years ago)

return localized number of items for each delete-comment ajax request

  • wp-admin/admin-ajax.php

    diff --git wp-admin/admin-ajax.php wp-admin/admin-ajax.php
    index d5a0d1e..968b069 100644
    endif; 
    189189 * @param int $comment_id
    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'];
    195195        $page = (int) @$_POST['_page'];
    function _wp_ajax_delete_comment_response( $comment_id ) { 
    198198        if ( !$total || !$per_page || !$page || !$url )
    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);
    219         $time = time(); // The time since the last comment count
    220 
    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         ) );
    233         $x = new WP_Ajax_Response( array(
     205        $response = array(
    234206                'what' => 'comment',
    235207                'id' => $comment_id, // here for completeness - not used
    236208                'supplemental' => array(
    237                         'pageLinks' => $page_links,
     209                        'time' => time(),
     210                        'l10nTotal' => sprintf( _n( '1 item', '%s items', $total ), number_format_i18n( $total ) ),
    238211                        'total' => $total,
    239                         'time' => $time
    240                 )
    241         ) );
     212                ),
     213        );
     214       
     215        // TODO: determine if pagination links need to be updated in case total number of pages change
     216 
     217        $x = new WP_Ajax_Response( $response );
     218       
    242219        $x->send();
    243220}
    244221
    case 'delete-comment' : // On success, die with time() instead of 1 
    330307
    331308        check_ajax_referer( "delete-comment_$id" );
    332309        $status = wp_get_comment_status( $comment->comment_ID );
    333 
     310       
     311        $delta = -1;
    334312        if ( isset($_POST['trash']) && 1 == $_POST['trash'] ) {
    335313                if ( 'trash' == $status )
    336314                        die( (string) time() );
    case 'delete-comment' : // On success, die with time() instead of 1 
    339317                if ( 'trash' != $status )
    340318                        die( (string) time() );
    341319                $r = wp_untrash_comment( $comment->comment_ID );
     320                if (  $_POST['comment_status'] != 'trash' ) //undo trash
     321                        $delta = 1;
    342322        } elseif ( isset($_POST['spam']) && 1 == $_POST['spam'] ) {
    343323                if ( 'spam' == $status )
    344324                        die( (string) time() );
    case 'delete-comment' : // On success, die with time() instead of 1 
    347327                if ( 'spam' != $status )
    348328                        die( (string) time() );
    349329                $r = wp_unspam_comment( $comment->comment_ID );
     330                if ( $_POST['comment_status'] != 'spam' ) // undo spam
     331                        $delta = 1;
    350332        } elseif ( isset($_POST['delete']) && 1 == $_POST['delete'] ) {
    351333                $r = wp_delete_comment( $comment->comment_ID );
    352334        } else {
    case 'delete-comment' : // On success, die with time() instead of 1 
    354336        }
    355337
    356338        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 );
     339                _wp_ajax_delete_comment_response( $comment->comment_ID, $delta );
    358340        die( '0' );
    359341        break;
    360342case 'delete-tag' :
  • wp-admin/edit-comments.php

    diff --git wp-admin/edit-comments.php wp-admin/edit-comments.php
    index 269dbff..97b7013 100644
    if ( isset($_REQUEST['approved']) || isset($_REQUEST['deleted']) || isset($_REQU 
    228228<input type="hidden" name="comment_status" value="<?php echo esc_attr($comment_status); ?>" />
    229229<input type="hidden" name="pagegen_timestamp" value="<?php echo esc_attr(current_time('mysql', 1)); ?>" />
    230230
    231 <input type="hidden" name="_total" value="<?php echo esc_attr( $wp_list_table->get_pagination_arg('total_items') ); ?>" />
    232 <input type="hidden" name="_per_page" value="<?php echo esc_attr( $wp_list_table->get_pagination_arg('per_page') ); ?>" />
    233 <input type="hidden" name="_page" value="<?php echo esc_attr( $wp_list_table->get_pagination_arg('page') ); ?>" />
     231<input type="hidden" autocomplete="off" name="_total" value="<?php echo esc_attr( $wp_list_table->get_pagination_arg('total_items') ); ?>" />
     232<input type="hidden" autocomplete="off" name="_per_page" value="<?php echo esc_attr( $wp_list_table->get_pagination_arg('per_page') ); ?>" />
     233<input type="hidden" autocomplete="off" name="_page" value="<?php echo esc_attr( $wp_list_table->get_pagination_arg('page') ); ?>" />
    234234
    235235<?php if ( isset($_REQUEST['paged']) ) { ?>
    236236        <input type="hidden" name="paged" value="<?php echo esc_attr( absint( $_REQUEST['paged'] ) ); ?>" />
  • wp-admin/js/edit-comments.dev.js

    diff --git wp-admin/js/edit-comments.dev.js wp-admin/js/edit-comments.dev.js
    index 5ca2fb6..bf5728f 100644
    var theList, theExtraList, toggleWithKeyboard = false; 
    44setCommentsList = function() {
    55        var totalInput, perPageInput, pageInput, lastConfidentTime = 0, dimAfter, delBefore, updateTotalCount, delAfter;
    66
    7         totalInput = $('.tablenav input[name="_total"]', '#comments-form');
    8         perPageInput = $('.tablenav input[name="_per_page"]', '#comments-form');
    9         pageInput = $('.tablenav input[name="_page"]', '#comments-form');
     7        totalInput = $('input[name="_total"]', '#comments-form');
     8        perPageInput = $('input[name="_per_page"]', '#comments-form');
     9        pageInput = $('input[name="_page"]', '#comments-form');
    1010
    1111        dimAfter = function( r, settings ) {
    1212                var c = $('#' + settings.element);
    setCommentsList = function() { 
    3838                settings.data._per_page = perPageInput.val() || 0;
    3939                settings.data._page = pageInput.val() || 0;
    4040                settings.data._url = document.location.href;
     41                settings.data.comment_status = $('input[name=comment_status]', '#comments-form').val();
    4142
    4243                if ( cl.indexOf(':trash=1') != -1 )
    4344                        action = 'trash';
    setCommentsList = function() { 
    8586        };
    8687
    8788        // Updates the current total (as displayed visibly)
    88         updateTotalCount = function( total, time, setConfidentTime ) {
     89        updateTotalCount = function( total, time, setConfidentTime, l10nTotal ) {
    8990                if ( time < lastConfidentTime )
    9091                        return;
    9192
    setCommentsList = function() { 
    9394                        lastConfidentTime = time;
    9495
    9596                totalInput.val( total.toString() );
    96                 $('span.total-type-count').each( function() {
    97                         updateCount( $(this), total );
    98                 });
     97                $('span.displaying-num').text(l10nTotal);
    9998        };
    10099
    101100        function dashboardTotals(n) {
    setCommentsList = function() { 
    138137                el.html(n);
    139138        }
    140139
    141         // In admin-ajax.php, we send back the unix time stamp instead of 1 on success
     140        // In admin-ajax.php, we send back the unix time stamp as well as localized total items on success
    142141        delAfter = function( r, settings ) {
    143142                var total, pageLinks, N, untrash = $(settings.target).parent().is('span.untrash'), unspam = $(settings.target).parent().is('span.unspam'), spam, trash;
    144143
    setCommentsList = function() { 
    186185                        N = trash ? -1 * trash : 0;
    187186                        dashboardTotals(N);
    188187                } else {
    189                         total = totalInput.val() ? parseInt( totalInput.val(), 10 ) : 0;
    190                         total = total - spam - trash;
    191                         if ( total < 0 )
    192                                 total = 0;
    193 
    194                         if ( ( 'object' == typeof r ) && lastConfidentTime < settings.parsed.responses[0].supplemental.time ) {
    195                                 pageLinks = settings.parsed.responses[0].supplemental.pageLinks || '';
    196                                 if ( $.trim( pageLinks ) )
    197                                         $('.tablenav-pages').find( '.page-numbers' ).remove().end().append( $( pageLinks ) );
    198                                 else
    199                                         $('.tablenav-pages').find( '.page-numbers' ).remove();
    200 
    201                                 updateTotalCount( total, settings.parsed.responses[0].supplemental.time, true );
    202                         } else {
    203                                 updateTotalCount( total, r, false );
     188                        if      ( ( 'object' == typeof r ) && lastConfidentTime < settings.parsed.responses[0].supplemental.time ) {
     189                                parsedResponse = settings.parsed.responses[0];
     190                                updateTotalCount( parsedResponse.supplemental.total, parsedResponse.supplemental.time, true, parsedResponse.supplemental.l10nTotal );
    204191                        }
    205192                }
    206193
    207 
    208194                if ( ! theExtraList || theExtraList.size() == 0 || theExtraList.children().size() == 0 || untrash || unspam ) {
    209195                        return;
    210196                }