<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[50109] trunk: Comments: Introduce a method for commenters to opt-in to receiving an email notification when their moderated comment gets approved.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/50109">50109</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/50109","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>johnbillion</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-01-31 12:48:24 +0000 (Sun, 31 Jan 2021)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/33717">#33717</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpcommentspostphp">trunk/src/wp-comments-post.php</a></li>
<li><a href="#trunksrcwpincludesclasswalkercommentphp">trunk/src/wp-includes/class-walker-comment.php</a></li>
<li><a href="#trunksrcwpincludescommentphp">trunk/src/wp-includes/comment.php</a></li>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunktestsphpunittestscommentphp">trunk/tests/phpunit/tests/comment.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpcommentspostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-comments-post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-comments-post.php 2021-01-30 21:29:39 UTC (rev 50108)
+++ trunk/src/wp-comments-post.php 2021-01-31 12:48:24 UTC (rev 50109)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,37 +22,54 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> nocache_headers();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-$comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
-if ( is_wp_error( $comment ) ) {
- $data = (int) $comment->get_error_data();
- if ( ! empty( $data ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( isset( $_POST['wp-comment-approved-notification-optin'], $_POST['comment_ID'], $_POST['moderation-hash'] ) ) {
+ $comment = get_comment( $_POST['comment_ID'] );
+
+ if ( $comment && hash_equals( $_POST['moderation-hash'], wp_hash( $comment->comment_date_gmt ) ) ) {
+ update_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true );
+ } else {
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_die(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- '<p>' . $comment->get_error_message() . '</p>',
- __( 'Comment Submission Failure' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '<p>' . __( 'Invalid comment ID.' ) . '</p>',
+ __( 'Comment Notification Opt-in Failure' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'response' => $data,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'response' => 404,
</ins><span class="cx" style="display: block; padding: 0 10px"> 'back_link' => true,
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } else {
- exit;
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-}
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+} else {
+ $comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
+ if ( is_wp_error( $comment ) ) {
+ $data = (int) $comment->get_error_data();
+ if ( ! empty( $data ) ) {
+ wp_die(
+ '<p>' . $comment->get_error_message() . '</p>',
+ __( 'Comment Submission Failure' ),
+ array(
+ 'response' => $data,
+ 'back_link' => true,
+ )
+ );
+ } else {
+ exit;
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-$user = wp_get_current_user();
-$cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user = wp_get_current_user();
+ $cookies_consent = ( isset( $_POST['wp-comment-cookies-consent'] ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/**
- * Perform other actions when comment cookies are set.
- *
- * @since 3.4.0
- * @since 4.9.6 The `$cookies_consent` parameter was added.
- *
- * @param WP_Comment $comment Comment object.
- * @param WP_User $user Comment author's user object. The user may not exist.
- * @param bool $cookies_consent Comment author's consent to store cookies.
- */
-do_action( 'set_comment_cookies', $comment, $user, $cookies_consent );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Perform other actions when comment cookies are set.
+ *
+ * @since 3.4.0
+ * @since 4.9.6 The `$cookies_consent` parameter was added.
+ *
+ * @param WP_Comment $comment Comment object.
+ * @param WP_User $user Comment author's user object. The user may not exist.
+ * @param bool $cookies_consent Comment author's consent to store cookies.
+ */
+ do_action( 'set_comment_cookies', $comment, $user, $cookies_consent );
+}
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $location = empty( $_POST['redirect_to'] ) ? get_comment_link( $comment ) : $_POST['redirect_to'] . '#comment-' . $comment->comment_ID;
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesclasswalkercommentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-walker-comment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-walker-comment.php 2021-01-30 21:29:39 UTC (rev 50108)
+++ trunk/src/wp-includes/class-walker-comment.php 2021-01-31 12:48:24 UTC (rev 50109)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -41,6 +41,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Whether the comment approval notification opt-in form or message needs to be
+ * output automatically.
+ *
+ * @since 5.7.0
+ *
+ * @var bool
+ */
+ protected $needs_comment_approval_notification_output = true;
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Starts the list before the elements are added.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -255,10 +265,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the comment text.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Removes links from the pending comment's text if the commenter did not consent
- * to the comment cookies.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * - Removes links from the pending comment's text if the commenter did not consent
+ * to the comment cookies
+ * - Prepends the approval notification opt-in form or message to pending comments
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.4.2
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.7.0 Comment approval notification opt-in form is now automatically
+ * appended if necessary.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $comment_text Text of the current comment.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Comment|null $comment The comment object. Null if not found.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -272,10 +285,91 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $comment_text = wp_kses( $comment_text, array() );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * Checks if we need to output the comment approval notification opt-in form.
+ */
+ if ( $this->needs_comment_approval_notification_output ) {
+ $comment_text = $this->comment_approval_notification_form( $comment ) . "\n" . $comment_text;
+ }
+
+ $this->needs_comment_approval_notification_output = true;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $comment_text;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Outputs the awaiting moderation text.
+ *
+ * @since 5.7.0
+ *
+ * @param WP_Comment $comment Comment to display.
+ */
+ protected function awaiting_moderation_text( $comment ) {
+ if ( '0' !== $comment->comment_approved ) {
+ return;
+ }
+
+ $commenter = wp_get_current_commenter();
+
+ if ( $commenter['comment_author_email'] ) {
+ $moderation_note = __( 'Your comment is awaiting moderation.' );
+ } else {
+ $moderation_note = __( 'Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.' );
+ }
+
+ printf(
+ '<em class="comment-awaiting-moderation">%s</em>',
+ esc_html( $moderation_note )
+ );
+ }
+
+ /**
+ * Gets the comment approval notification opt-in form or message for a pending comment.
+ *
+ * @since 5.7.0
+ *
+ * @param WP_Comment $comment Comment to display.
+ * @return string HTML output.
+ */
+ protected function comment_approval_notification_form( $comment ) {
+ $comment_approval_output = '';
+
+ if ( '0' === $comment->comment_approved && has_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' ) ) {
+ if ( get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) {
+ $comment_approval_output = sprintf(
+ '<p><em class="wp-comment-approved-notification-optedin">%s</em></p>',
+ esc_html__( 'You will receive an email when your comment is approved.' )
+ );
+ } else {
+ $comment_approval_output = sprintf(
+ '<form action="%1$s" method="post">
+ <p>
+ <input type="checkbox" id="wp-comment-approved-notification-optin" name="wp-comment-approved-notification-optin">
+ <label for="wp-comment-approved-notification-optin">
+ %2$s
+ </label>
+ </p>
+ <input type="hidden" name="comment_ID" value="%3$s">
+ <input type="hidden" name="moderation-hash" value="%4$s">
+ <input type="submit" class="button" value="%5$s">
+ </form>',
+ esc_url( site_url( '/wp-comments-post.php' ) ),
+ esc_html__( 'I want to be notified by email when my comment is approved.' ),
+ absint( $comment->comment_ID ),
+ wp_hash( $comment->comment_date_gmt ),
+ esc_html_x( 'Save', 'comment approval notification form' )
+ );
+ }
+ }
+
+ // Disable the backcompat output.
+ $this->needs_comment_approval_notification_output = false;
+
+ // Return the approval notification opt-in form.
+ return $comment_approval_output;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Outputs a single comment.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.6.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -297,12 +391,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $commenter = wp_get_current_commenter();
</span><span class="cx" style="display: block; padding: 0 10px"> $show_pending_links = isset( $commenter['comment_author'] ) && $commenter['comment_author'];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- if ( $commenter['comment_author_email'] ) {
- $moderation_note = __( 'Your comment is awaiting moderation.' );
- } else {
- $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' );
- }
</del><span class="cx" style="display: block; padding: 0 10px"> ?>
</span><span class="cx" style="display: block; padding: 0 10px"> <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
</span><span class="cx" style="display: block; padding: 0 10px"> <?php if ( 'div' !== $args['style'] ) : ?>
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -328,11 +416,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> ?>
</span><span class="cx" style="display: block; padding: 0 10px"> </div>
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- <?php if ( '0' == $comment->comment_approved ) : ?>
- <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
- <br />
- <?php endif; ?>
</del><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <?php
+ // Output the comment moderation feedback if needed.
+ $this->awaiting_moderation_text( $comment );
+
+ // Output the comment approval notification opt-in form if needed.
+ echo $this->comment_approval_notification_form( $comment );
+ ?>
+
</ins><span class="cx" style="display: block; padding: 0 10px"> <div class="comment-meta commentmetadata">
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> printf(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -401,12 +493,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $commenter = wp_get_current_commenter();
</span><span class="cx" style="display: block; padding: 0 10px"> $show_pending_links = ! empty( $commenter['comment_author'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- if ( $commenter['comment_author_email'] ) {
- $moderation_note = __( 'Your comment is awaiting moderation.' );
- } else {
- $moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' );
- }
</del><span class="cx" style="display: block; padding: 0 10px"> ?>
</span><span class="cx" style="display: block; padding: 0 10px"> <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
</span><span class="cx" style="display: block; padding: 0 10px"> <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -450,9 +536,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ?>
</span><span class="cx" style="display: block; padding: 0 10px"> </div><!-- .comment-metadata -->
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- <?php if ( '0' == $comment->comment_approved ) : ?>
- <em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
- <?php endif; ?>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <?php
+ // Output the comment moderation feedback if needed.
+ $this->awaiting_moderation_text( $comment );
+
+ // Output the comment approval notification opt-in form if needed.
+ echo $this->comment_approval_notification_form( $comment );
+ ?>
</ins><span class="cx" style="display: block; padding: 0 10px"> </footer><!-- .comment-meta -->
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> <div class="comment-content">
</span></span></pre></div>
<a id="trunksrcwpincludescommentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/comment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/comment.php 2021-01-30 21:29:39 UTC (rev 50108)
+++ trunk/src/wp-includes/comment.php 2021-01-31 12:48:24 UTC (rev 50109)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2349,6 +2349,114 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Sets the status of a comment.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * The {@see 'wp_set_comment_status'} action is called after the comment is handled.
</span></span></pre></div>
<a id="trunksrcwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2021-01-30 21:29:39 UTC (rev 50108)
+++ trunk/src/wp-includes/default-filters.php 2021-01-31 12:48:24 UTC (rev 50109)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -468,6 +468,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'after_password_reset', 'wp_password_change_notification' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'register_new_user', 'wp_send_new_user_notifications' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'edit_user_created_user', 'wp_send_new_user_notifications', 10, 2 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // REST API actions.
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'init', 'rest_api_init' );
</span></span></pre></div>
<a id="trunktestsphpunittestscommentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/comment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/comment.php 2021-01-30 21:29:39 UTC (rev 50108)
+++ trunk/tests/phpunit/tests/comment.php 2021-01-31 12:48:24 UTC (rev 50109)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -555,6 +555,58 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 33717
+ */
+ public function test_wp_new_comment_notify_comment_author_once_only() {
+ $c = self::factory()->comment->create(
+ array(
+ 'comment_post_ID' => self::$post_id,
+ 'comment_approved' => '0',
+ 'comment_author_email' => 'foo@bar.mail',
+ )
+ );
+
+ // The comment author subscribed to receive an email once comment is approved.
+ update_comment_meta( $c, '_wp_comment_author_notification_optin', true );
+
+ // For the purpose of the test we are removing this hook to directly use the function to notify the comment author.
+ remove_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' );
+
+ // Approve the comment.
+ wp_set_comment_status( $c, 'approve' );
+
+ $sent = wp_new_comment_notify_comment_author( $c );
+ $resent = wp_new_comment_notify_comment_author( $c );
+
+ $this->assertTrue( $sent );
+ $this->assertFalse( $resent );
+ }
+
+ /**
+ * @ticket 33717
+ */
+ public function test_wp_new_comment_notify_comment_author_has_not_opted_in() {
+ $c = self::factory()->comment->create(
+ array(
+ 'comment_post_ID' => self::$post_id,
+ 'comment_approved' => '0',
+ 'comment_author_email' => 'bat@man.mail',
+ )
+ );
+
+ // For the purpose of the test we are removing this hook to directly use the function to notify the comment author.
+ remove_action( 'comment_unapproved_to_approved', 'wp_new_comment_notify_comment_author' );
+
+ // Approve the comment.
+ wp_set_comment_status( $c, 'approve' );
+
+ $sent = wp_new_comment_notify_comment_author( $c );
+
+ // The comment author hasn't subscribed to receive an email, no email should be sent.
+ $this->assertFalse( $sent );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 12431
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_wp_new_comment_with_meta() {
</span></span></pre>
</div>
</div>
</body>
</html>