Last active
January 16, 2025 13:23
-
-
Save jdevalk/2d4d4f6d055ceede40a68eeb21800ef4 to your computer and use it in GitHub Desktop.
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 | |
class Joost_Comment_Fixes { | |
public function __construct() { | |
// Unhook comment cookies stuff from core. | |
remove_action( 'set_comment_cookies', 'wp_set_comment_cookies' ); | |
add_action( 'wp_footer', [ $this, 'print_custom_comment_script' ] ); | |
add_filter( 'wp_get_current_commenter', [ $this, 'ignore_comment_cookies_serverside' ] ); | |
add_filter( 'comment_post_redirect', [ $this, 'comment_moderation_redirect' ], 10, 2 ); | |
} | |
/** | |
* Filter the comment moderation redirect, to prevent adding unneeded parameters. | |
*/ | |
public function comment_moderation_redirect( $location, $comment ) { | |
// Check if the comment is held for moderation. | |
if ( $comment->comment_approved == '0' ) { | |
// Redirect to a custom "held for moderation" page. | |
// return home_url( '/comment-moderation/' ); | |
return get_permalink( $comment->comment_post_ID ); | |
} | |
return $location; | |
} | |
/** | |
* To prevent this data from being cached, we don't want to show it server side. | |
* | |
* @param array $commenter Ignored. | |
* | |
* @return array With empty values for all three keys. | |
*/ | |
public function ignore_comment_cookies_serverside( $commenter ) { | |
return [ | |
'comment_author' => '', | |
'comment_author_email' => '', | |
'comment_author_url' => '', | |
]; | |
} | |
/** | |
* Prints a script that sets cookies for comment form field values on comment submission, and reads them from a cookie if it's there. | |
*/ | |
public function print_custom_comment_script() { | |
if ( ! is_singular() || ! comments_open() ) { | |
return; | |
} | |
?> | |
<script> | |
// Function to set comment cookies. | |
function setCommentCookies( name, email, url ) { | |
const oneYear = 365 * 24 * 60 * 60 * 1000; | |
const expiryDate = new Date( Date.now() + oneYear ).toUTCString(); | |
// Set cookies for comment author data. | |
document.cookie = `comment_author=${encodeURIComponent(name)}; expires=${expiryDate}; path=/`; | |
document.cookie = `comment_author_email=${encodeURIComponent(email)}; expires=${expiryDate}; path=/`; | |
document.cookie = `comment_author_url=${encodeURIComponent(url)}; expires=${expiryDate}; path=/`; | |
} | |
// Function to read cookies. | |
function getCommentCookies() { | |
const cookies = document.cookie.split( '; ' ).reduce(( acc, cookie ) => { | |
const [key, value] = cookie.split( '=' ); | |
acc[key] = decodeURIComponent(value); | |
return acc; | |
}, {} ); | |
return { | |
name: cookies['comment_author'] || '', | |
email: cookies['comment_author_email'] || '', | |
url: cookies['comment_author_url'] || '' | |
} | |
} | |
document.addEventListener( 'DOMContentLoaded', function () { | |
// Populate comment form fields with cookie data. | |
const cookies = getCommentCookies(); | |
const nameField = document.getElementById( 'author' ); | |
const emailField = document.getElementById( 'email' ); | |
const urlField = document.getElementById( 'url' ); | |
if (nameField) nameField.value = cookies.name; | |
if (emailField) emailField.value = cookies.email; | |
if (urlField) urlField.value = cookies.url; | |
// Set cookies on form submission. | |
const commentForm = document.getElementById( 'commentform' ); | |
if ( commentForm ) { | |
commentForm.addEventListener( 'submit', function() { | |
if ( nameField && emailField && urlField ) { | |
setCommentCookies( nameField.value, emailField.value, urlField.value ); | |
} | |
}); | |
} | |
}) | |
</script> | |
<?php | |
} | |
} | |
new Joost_Comment_Fixes(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment