Comments: Introduce a method for commenters to opt-in to receiving an email notification when their moderated comment gets approved.

The opt-in form is shown after the comment is submitted and held for moderation.

Sorry this took five years.

Props jeffr0, swissspidy, mrahmadawais, wonderboymusic, jdgrimes, obenland, Monika, imath, garrett-eclipse, johnbillion

Fixes #33717

Built from https://develop.svn.wordpress.org/trunk@50109


git-svn-id: http://core.svn.wordpress.org/trunk@49788 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
John Blackbourn
2021-01-31 12:50:01 +00:00
parent f6191c07df
commit dad34fe877
5 changed files with 262 additions and 46 deletions

View File

@@ -2348,6 +2348,114 @@ function wp_new_comment_notify_postauthor( $comment_ID ) {
return wp_notify_postauthor( $comment_ID );
}
/**
* Notifies the comment author when their comment gets approved.
*
* This notification is only sent once when the comment status
* changes from unapproved to approved.
*
* @since 5.7.0
*
* @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
* @return bool Whether the email was sent.
*/
function wp_new_comment_notify_comment_author( $comment_id ) {
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return false;
}
$post = get_post( $comment->comment_post_ID );
if ( ! $post ) {
return false;
}
// Make sure the comment author can be notified by email.
if ( empty( $comment->comment_author_email ) ) {
return false;
}
if ( ! get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) {
return false;
}
/**
* The blogname option is escaped with esc_html when
* saved into the database, we need to reverse this for
* the plain text area of the email.
*/
$blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
$subject = sprintf(
/* translators: 1: blog name, 2: post title */
__( '[%1$s] Your comment on "%2$s" has been approved' ),
$blogname,
$post->post_title
);
if ( ! empty( $comment->comment_author ) ) {
$notify_message = sprintf(
/* translators: 1: comment author's name */
__( 'Howdy %s,' ),
$comment->comment_author
) . "\r\n\r\n";
} else {
$notify_message = __( 'Howdy,' ) . "\r\n\r\n";
}
$notify_message .= sprintf(
/* translators: 1: post title */
__( 'Your comment on "%s" has been approved.' ),
$post->post_title
) . "\r\n\r\n";
$notify_message .= sprintf(
/* translators: 1: comment permalink */
__( 'View comment: %s' ),
get_comment_link( $comment )
) . "\r\n";
$email = array(
'to' => $comment->comment_author_email,
'subject' => $subject,
'message' => $notify_message,
'headers' => '',
);
/**
* Filters the contents of the email sent to notify a comment author that their comment was approved.
*
* Content should be formatted for transmission via wp_mail().
*
* @since 5.7.0
*
* @param array $email {
* Used to build wp_mail().
*
* @type string $to The email address of the comment author.
* @type string $subject The subject of the email.
* @type string $message The content of the email.
* @type string $headers Headers.
* }
* @param WP_Comment $comment Comment object.
*/
$email = apply_filters( 'comment_approval_notification', $email, $comment );
$sent = wp_mail(
$email['to'],
wp_specialchars_decode( $email['subject'] ),
$email['message'],
$email['headers']
);
// Delete the opt-in now the notification has been sent.
delete_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin' );
return $sent;
}
/**
* Sets the status of a comment.
*