Make WordPress Core


Ignore:
Timestamp:
01/31/2021 12:48:24 PM (4 years ago)
Author:
johnbillion
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r49936 r50109  
    23502350
    23512351/**
     2352 * Notifies the comment author when their comment gets approved.
     2353 *
     2354 * This notification is only sent once when the comment status
     2355 * changes from unapproved to approved.
     2356 *
     2357 * @since 5.7.0
     2358 *
     2359 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object.
     2360 * @return bool Whether the email was sent.
     2361 */
     2362function wp_new_comment_notify_comment_author( $comment_id ) {
     2363    $comment = get_comment( $comment_id );
     2364
     2365    if ( ! $comment ) {
     2366        return false;
     2367    }
     2368
     2369    $post = get_post( $comment->comment_post_ID );
     2370
     2371    if ( ! $post ) {
     2372        return false;
     2373    }
     2374
     2375    // Make sure the comment author can be notified by email.
     2376    if ( empty( $comment->comment_author_email ) ) {
     2377        return false;
     2378    }
     2379
     2380    if ( ! get_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin', true ) ) {
     2381        return false;
     2382    }
     2383
     2384    /**
     2385     * The blogname option is escaped with esc_html when
     2386     * saved into the database, we need to reverse this for
     2387     * the plain text area of the email.
     2388     */
     2389    $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
     2390
     2391    $subject = sprintf(
     2392        /* translators: 1: blog name, 2: post title */
     2393        __( '[%1$s] Your comment on "%2$s" has been approved' ),
     2394        $blogname,
     2395        $post->post_title
     2396    );
     2397
     2398    if ( ! empty( $comment->comment_author ) ) {
     2399        $notify_message = sprintf(
     2400            /* translators: 1: comment author's name */
     2401            __( 'Howdy %s,' ),
     2402            $comment->comment_author
     2403        ) . "\r\n\r\n";
     2404    } else {
     2405        $notify_message = __( 'Howdy,' ) . "\r\n\r\n";
     2406    }
     2407
     2408    $notify_message .= sprintf(
     2409        /* translators: 1: post title */
     2410        __( 'Your comment on "%s" has been approved.' ),
     2411        $post->post_title
     2412    ) . "\r\n\r\n";
     2413
     2414    $notify_message .= sprintf(
     2415        /* translators: 1: comment permalink */
     2416        __( 'View comment: %s' ),
     2417        get_comment_link( $comment )
     2418    ) . "\r\n";
     2419
     2420    $email = array(
     2421        'to'      => $comment->comment_author_email,
     2422        'subject' => $subject,
     2423        'message' => $notify_message,
     2424        'headers' => '',
     2425    );
     2426
     2427    /**
     2428     * Filters the contents of the email sent to notify a comment author that their comment was approved.
     2429     *
     2430     * Content should be formatted for transmission via wp_mail().
     2431     *
     2432     * @since 5.7.0
     2433     *
     2434     * @param array      $email   {
     2435     *     Used to build wp_mail().
     2436     *
     2437     *     @type string $to      The email address of the comment author.
     2438     *     @type string $subject The subject of the email.
     2439     *     @type string $message The content of the email.
     2440     *     @type string $headers Headers.
     2441     * }
     2442     * @param WP_Comment $comment Comment object.
     2443     */
     2444    $email = apply_filters( 'comment_approval_notification', $email, $comment );
     2445
     2446    $sent = wp_mail(
     2447        $email['to'],
     2448        wp_specialchars_decode( $email['subject'] ),
     2449        $email['message'],
     2450        $email['headers']
     2451    );
     2452
     2453    // Delete the opt-in now the notification has been sent.
     2454    delete_comment_meta( $comment->comment_ID, '_wp_comment_author_notification_optin' );
     2455
     2456    return $sent;
     2457}
     2458
     2459/**
    23522460 * Sets the status of a comment.
    23532461 *
Note: See TracChangeset for help on using the changeset viewer.