From da4f8d0ffbab9699d940a4bde6839def27956021 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 5 Mar 2019 01:51:31 +0000 Subject: [PATCH] Comments: Allow for `comment-reply.js` to be loaded in the HTML header. Allows for themes or plugins setting the comment-reply JavaScript as a dependency of an HTML header script. This in turn causes `comment-reply.js` to be loaded early, requiring execution to be delayed. Props pento, peterwilsoncc, jorbin for feedback. Fixes #46280. git-svn-id: https://develop.svn.wordpress.org/trunk@44794 602fd350-edb4-49c9-b593-d223f7449a82 --- src/js/_enqueues/lib/comment-reply.js | 31 ++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/js/_enqueues/lib/comment-reply.js b/src/js/_enqueues/lib/comment-reply.js index fae92ee237..75fc183e42 100644 --- a/src/js/_enqueues/lib/comment-reply.js +++ b/src/js/_enqueues/lib/comment-reply.js @@ -32,7 +32,7 @@ window.addComment = ( function( window ) { * Check browser supports dataset. * !! sets the variable to true if the property exists. */ - var supportsDataset = !! document.body.dataset; + var supportsDataset = !! document.documentElement.dataset; // For holding the cancel element. var cancelElement; @@ -46,11 +46,24 @@ window.addComment = ( function( window ) { // The mutation observer. var observer; - // Initialise the events. - init(); + if ( cutsTheMustard && document.readyState !== 'loading' ) { + ready(); + } else if ( cutsTheMustard ) { + window.addEventListener( 'DOMContentLoaded', ready, false ); + } - // Set up a MutationObserver to check for comments loaded late. - observeChanges(); + /** + * Sets up object variables after the DOM is ready. + * + * @since 5.1.1 + */ + function ready() { + // Initialise the events. + init(); + + // Set up a MutationObserver to check for comments loaded late. + observeChanges(); + } /** * Add events to links classed .comment-reply-link. @@ -163,6 +176,14 @@ window.addComment = ( function( window ) { postId = getDataAttribute( replyLink, 'postid'), follow; + if ( ! commId || ! parentId || ! respondId || ! postId ) { + /* + * Theme or plugin defines own link via custom `wp_list_comments()` callback + * and calls `moveForm()` either directly or via a custom event hook. + */ + return; + } + /* * Third party comments systems can hook into this function via the global scope, * therefore the click event needs to reference the global scope.