From c0ebd3b5e2e510b1ceae1a383b340b551f95c83f Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 24 Sep 2015 15:52:41 +0000 Subject: [PATCH] Comments: update Comment counts dynamically in the Right Now widget based on moderation actions in the Activity widget. Fixes #10422. git-svn-id: https://develop.svn.wordpress.org/trunk@34500 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/ajax-actions.php | 32 +++++++++++++++++++++--- src/wp-admin/includes/dashboard.php | 18 ++++++++------ src/wp-admin/js/edit-comments.js | 34 ++++++++++++++++++++++---- 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 289d959224..c380583207 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -351,6 +351,8 @@ function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) { $time = time(); $comment = get_comment( $comment_id ); + $counts = wp_count_comments(); + $x = new WP_Ajax_Response( array( 'what' => 'comment', // Here for completeness - not used. @@ -358,7 +360,16 @@ function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) { 'supplemental' => array( 'status' => $comment ? $comment->comment_approved : '', 'postId' => $comment ? $comment->comment_post_ID : '', - 'time' => $time + 'time' => $time, + 'in_moderation' => $counts->moderated, + 'i18n_comments_text' => sprintf( + _n( '%s Comment', '%s Comments', $counts->approved ), + number_format_i18n( $counts->approved ) + ), + 'i18n_moderation_text' => sprintf( + _nx( '%s in moderation', '%s in moderation', $counts->moderated, 'comments' ), + number_format_i18n( $counts->moderated ) + ) ) ) ); $x->send(); @@ -1049,8 +1060,23 @@ function wp_ajax_replyto_comment( $action ) { 'position' => $position ); - if ( $comment_auto_approved ) - $response['supplemental'] = array( 'parent_approved' => $parent->comment_ID, 'parent_post_id' => $parent->comment_post_ID ); + $counts = wp_count_comments(); + $response['supplemental'] = array( + 'in_moderation' => $counts->moderated, + 'i18n_comments_text' => sprintf( + _n( '%s Comment', '%s Comments', $counts->approved ), + number_format_i18n( $counts->approved ) + ), + 'i18n_moderation_text' => sprintf( + _nx( '%s in moderation', '%s in moderation', $counts->moderated, 'comments' ), + number_format_i18n( $counts->moderated ) + ) + ); + + if ( $comment_auto_approved ) { + $response['supplemental']['parent_approved'] = $parent->comment_ID; + $response['supplemental']['parent_post_id'] = $parent->comment_post_ID; + } $x = new WP_Ajax_Response(); $x->add( $response ); diff --git a/src/wp-admin/includes/dashboard.php b/src/wp-admin/includes/dashboard.php index 806f6d938b..f09c7796b5 100644 --- a/src/wp-admin/includes/dashboard.php +++ b/src/wp-admin/includes/dashboard.php @@ -259,13 +259,15 @@ function wp_dashboard_right_now() { ?>
  • moderated ) { - /* translators: Number of comments in moderation */ - $text = sprintf( _nx( '%s in moderation', '%s in moderation', $num_comm->moderated, 'comments' ), number_format_i18n( $num_comm->moderated ) ); - ?> -
  • - moderated, 'comments' ), number_format_i18n( $num_comm->moderated ) ); + ?> +
  • + comment_post_ID > 0 && current_user_can( 'edit_post', $comment->comment_post_ID ) ) { $comment_post_title = _draft_or_post_title( $comment->comment_post_ID ); diff --git a/src/wp-admin/js/edit-comments.js b/src/wp-admin/js/edit-comments.js index e06f93a9d6..fe2a38b8e1 100644 --- a/src/wp-admin/js/edit-comments.js +++ b/src/wp-admin/js/edit-comments.js @@ -3,7 +3,7 @@ var setCommentsList, theList, theExtraList, commentReply; (function($) { var getCount, updateCount, updateCountText, updatePending, updateApproved, - updateHtmlTitle, adminTitle = document.title; + updateHtmlTitle, updateDashboardText, adminTitle = document.title; setCommentsList = function() { var totalInput, perPageInput, pageInput, dimAfter, delBefore, updateTotalCount, delAfter, refillTheExtraList, diff, @@ -19,6 +19,10 @@ setCommentsList = function() { var editRow, replyID, replyButton, response, c = $( '#' + settings.element ); + if ( true !== settings.parsed ) { + response = settings.parsed.responses[0]; + } + editRow = $('#replyrow'); replyID = $('#comment_ID', editRow).val(); replyButton = $('#replybtn', editRow); @@ -36,10 +40,10 @@ setCommentsList = function() { } diff = $('#' + settings.element).is('.' + settings.dimClass) ? 1 : -1; - if ( true !== settings.parsed && settings.parsed.responses.length ) { - response = settings.parsed.responses[0].supplemental; - updatePending( diff, response.postId ); - updateApproved( -1 * diff, response.postId ); + if ( response ) { + updateDashboardText( response.supplemental ); + updatePending( diff, response.supplemental.postId ); + updateApproved( -1 * diff, response.supplemental.postId ); } else { updatePending( diff ); updateApproved( -1 * diff ); @@ -277,12 +281,26 @@ setCommentsList = function() { }); }; + updateDashboardText = function ( response ) { + if ( ! isDashboard || ! response || ! response.i18n_comments_text ) { + return; + } + + var rightNow = $( '#dashboard_right_now' ); + + $( '.comment-count a', rightNow ).text( response.i18n_comments_text ); + $( '.comment-mod-count a', rightNow ).text( response.i18n_moderation_text ) + .parent() + [ response.in_moderation > 0 ? 'removeClass' : 'addClass' ]( 'hidden' ); + }; + // In admin-ajax.php, we send back the unix time stamp instead of 1 on success delAfter = function( r, settings ) { var total_items_i18n, total, animated, animatedCallback, response = true === settings.parsed ? {} : settings.parsed.responses[0], commentStatus = true === settings.parsed ? '' : response.supplemental.status, commentPostId = true === settings.parsed ? '' : response.supplemental.postId, + newTotal = true === settings.parsed ? '' : response.supplemental, targetParent = $( settings.target ).parent(), commentRow = $('#' + settings.element), @@ -294,6 +312,8 @@ setCommentsList = function() { spammed = commentRow.hasClass( 'spam' ), trashed = commentRow.hasClass( 'trash' ); + updateDashboardText( newTotal ); + // the order of these checks is important // .unspam can also have .approve or .unapprove // .untrash can also have .approve or .unapprove @@ -771,6 +791,10 @@ commentReply = { } } + if ( r.supplemental.i18n_comments_text ) { + updateDashboardText( r.supplemental ); + } + c = $.trim(r.data); // Trim leading whitespaces $(c).hide(); $('#replyrow').after(c);