From af101dec19cb781dcd5228ad33b6a77eafa92fb5 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 31 Aug 2015 17:57:32 +0000 Subject: [PATCH] Comments: dynamically update the document title text of the Comments List Table page when dynamically updating the number of comments awaiting moderation. Fixes #33414. git-svn-id: https://develop.svn.wordpress.org/trunk@33821 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/edit-comments.php | 27 +++++++++++++++++++---- src/wp-admin/js/edit-comments.js | 37 +++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/wp-admin/edit-comments.php b/src/wp-admin/edit-comments.php index ea5bb7624a..05e0b2fda0 100644 --- a/src/wp-admin/edit-comments.php +++ b/src/wp-admin/edit-comments.php @@ -104,10 +104,29 @@ $wp_list_table->prepare_items(); wp_enqueue_script('admin-comments'); enqueue_comment_hotkeys_js(); -if ( $post_id ) - $title = sprintf( __( 'Comments on “%s”' ), wp_html_excerpt( _draft_or_post_title( $post_id ), 50, '…' ) ); -else - $title = __('Comments'); +if ( $post_id ) { + $comments_count = wp_count_comments( $post_id ); + $draft_or_post_title = wp_html_excerpt( _draft_or_post_title( $post_id ), 50, '…' ); + if ( $comments_count->moderated > 0 ) { + $title = sprintf( + __( 'Comments (%s) on “%s”' ), + number_format_i18n( $comments_count->moderated ), + $draft_or_post_title + ); + } else { + $title = sprintf( __( 'Comments on “%s”' ), $draft_or_post_title ); + } +} else { + $comments_count = wp_count_comments(); + if ( $comments_count->moderated > 0 ) { + $title = sprintf( + __( 'Comments (%s)' ), + number_format_i18n( $comments_count->moderated ) + ); + } else { + $title = __( 'Comments' ); + } +} add_screen_option( 'per_page' ); diff --git a/src/wp-admin/js/edit-comments.js b/src/wp-admin/js/edit-comments.js index 8288016976..92f8d367e1 100644 --- a/src/wp-admin/js/edit-comments.js +++ b/src/wp-admin/js/edit-comments.js @@ -2,11 +2,13 @@ var setCommentsList, theList, theExtraList, commentReply; (function($) { -var getCount, updateCount, updateCountText, updatePending, updateApproved; +var getCount, updateCount, updateCountText, updatePending, updateApproved, + updateHtmlTitle, adminTitle = document.title; setCommentsList = function() { var totalInput, perPageInput, pageInput, dimAfter, delBefore, updateTotalCount, delAfter, refillTheExtraList, diff, - lastConfidentTime = 0; + lastConfidentTime = 0, titleDiv, titleRegEx, + isDashboard = $('#dashboard_right_now').length; totalInput = $('input[name="_total"]', '#comments-form'); perPageInput = $('input[name="_per_page"]', '#comments-form'); @@ -135,6 +137,31 @@ setCommentsList = function() { el.html(n); }; + updateHtmlTitle = function ( diff ) { + var newTitle, regExMatch, titleCount; + + titleRegEx = titleRegEx || new RegExp( 'Comments (\\([0-9' + thousandsSeparator + ']+\\))?' ); + // count funcs operate on a $'d element + titleDiv = titleDiv || $( '
' ); + newTitle = adminTitle; + + titleDiv.html( document.title ); + titleCount = getCount( titleDiv ) + diff; + if ( titleCount >= 1 ) { + updateCount( titleDiv, titleCount ); + regExMatch = titleRegEx.exec( document.title ); + if ( regExMatch ) { + newTitle = document.title.replace( regExMatch[0], 'Comments (' + titleDiv.text() + ') ' ); + } + } else { + regExMatch = titleRegEx.exec( newTitle ); + if ( regExMatch ) { + newTitle = newTitle.replace( regExMatch[0], 'Comments' ); + } + } + document.title = newTitle; + }; + updatePending = function( diff, commentPostId ) { var postSelector = '.post-com-count-' + commentPostId, noClass = 'comment-count-no-pending', @@ -143,6 +170,10 @@ setCommentsList = function() { pending, noPending; + if ( ! isDashboard ) { + updateHtmlTitle( diff ); + } + $( 'span.pending-count' ).each(function() { var a = $(this), n = getCount(a) + diff; if ( n < 1 ) @@ -390,7 +421,7 @@ setCommentsList = function() { updateCountText( 'span.trash-count', trashDiff ); } - if ( ! $('#dashboard_right_now').length ) { + if ( ! isDashboard ) { total = totalInput.val() ? parseInt( totalInput.val(), 10 ) : 0; if ( $(settings.target).parent().is('span.undo') ) total++;