Last active
July 19, 2022 01:58
-
-
Save gamaup/20a992a42eb5234f77119c128a73da46 to your computer and use it in GitHub Desktop.
PSI Optimization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wp_dereg_script_comment_reply(){ | |
wp_deregister_script( 'comment-reply' ); | |
} | |
add_action('init','wp_dereg_script_comment_reply'); | |
add_action('wp_head', 'wp_reload_script_comment_reply'); | |
function wp_reload_script_comment_reply() { | |
?> | |
<script> | |
//Function checks if a given script is already loaded | |
function isScriptLoaded(src){ | |
return document.querySelector('script[src="' + src + '"]') ? true : false; | |
} | |
//When a reply link is clicked, check if reply-script is loaded. If not, load it and emulate the click | |
var repLinks = document.getElementsByClassName("comment-reply-link"); | |
for (var i=0; i < repLinks.length; i++) { | |
repLinks[i].onclick = function() { | |
if(!(isScriptLoaded("/wp-includes/js/comment-reply.min.js"))){ | |
var script = document.createElement('script'); | |
script.src = "/wp-includes/js/comment-reply.min.js"; | |
script.onload = emRepClick(event.target.getAttribute('data-commentid')); | |
document.head.appendChild(script); | |
} | |
} | |
}; | |
//Function waits 50 ms before it emulates a click on the relevant reply link now that the reply script is loaded | |
function emRepClick(comId) { | |
sleep(50).then(() => { | |
document.querySelectorAll('[data-commentid="'+comId+'"]')[0].dispatchEvent(new Event('click')); | |
}); | |
} | |
//Function does nothing, for a given amount of time | |
function sleep (time) { | |
return new Promise((resolve) => setTimeout(resolve, time)); | |
} | |
</script> | |
<?php | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment