Skip to content

Commit

Permalink
Comments: overrides emails only when Jetpack is active and connected,…
Browse files Browse the repository at this point in the history
… and if all emails have WordPress.com accounts
  • Loading branch information
Rodrigo Iloro committed Nov 14, 2017
1 parent b22555c commit 87dea84
Showing 1 changed file with 58 additions and 100 deletions.
158 changes: 58 additions & 100 deletions moderation-emails-override.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,7 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) {
$emails[] = $author->user_email;
}

/**
* Filters the list of email addresses to receive a comment notification.
*
* By default, only post authors are notified of comments. This filter allows
* others to be added.
*
* @since 3.7.0
*
* @param array $emails An array of email addresses to receive a comment notification.
* @param int $comment_id The comment ID.
*/
/** This filter is documented in core/src/wp-includes/pluggable.php */
$emails = apply_filters( 'comment_notification_recipients', $emails, $comment->comment_ID );
$emails = array_filter( $emails );

Expand All @@ -50,18 +40,7 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) {
// Facilitate unsetting below without knowing the keys.
$emails = array_flip( $emails );

/**
* Filters whether to notify comment authors of their comments on their own posts.
*
* By default, comment authors aren't notified of their comments on their own
* posts. This filter allows you to override that.
*
* @since 3.8.0
*
* @param bool $notify Whether to notify the post author of their own comment.
* Default false.
* @param int $comment_id The comment ID.
*/
/** This filter is documented in core/src/wp-includes/pluggable.php */
$notify_author = apply_filters( 'comment_notification_notify_author', false, $comment->comment_ID );

// The comment was left by the author
Expand Down Expand Up @@ -95,6 +74,14 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) {
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$comment_content = wp_specialchars_decode( $comment->comment_content );

function is_user_connected( $email ) {
$user = get_user_by( 'email', $email );
return Jetpack::is_user_connected( $user->ID );
}
$moderate_on_wpcom = ! in_array( false, array_map( 'is_user_connected', $emails ) );

$primary_site_slug = Jetpack::build_raw_urls( get_home_url() );

switch ( $comment->comment_type ) {
case 'trackback':
/* translators: 1: Post title */
Expand Down Expand Up @@ -130,18 +117,26 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) {
$subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
break;
}
$notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
$notify_message .= sprintf( __('Permalink: %s'), get_comment_link( $comment ) ) . "\r\n";

$primary_site_slug = Jetpack::build_raw_urls( get_home_url() );
$notify_message .= $moderate_on_wpcom
? "https://wordpress.com/comments/all/{$primary_site_slug}/{$comment->comment_post_ID}/\r\n\r\n"
: get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";

$notify_message .= sprintf( __('Permalink: %s'), get_comment_link( $comment ) ) . "\r\n";

if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) {
if ( EMPTY_TRASH_DAYS ) {
$notify_message .= sprintf( __( 'Trash it: %s' ), "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=trash" ) . "\r\n";
$notify_message .= sprintf( __( 'Trash it: %s' ), $moderate_on_wpcom
? "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=trash"
: admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
} else {
$notify_message .= sprintf( __( 'Delete it: %s' ), "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=delete" ) . "\r\n";
$notify_message .= sprintf( __( 'Delete it: %s' ), $moderate_on_wpcom
? "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=delete"
: admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
}
$notify_message .= sprintf( __( 'Spam it: %s' ), "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=spam" ) . "\r\n";
$notify_message .= sprintf( __( 'Spam it: %s' ), $moderate_on_wpcom ?
"https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=spam"
: admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
}

$wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
Expand All @@ -162,34 +157,13 @@ function wp_notify_postauthor( $comment_id, $deprecated = null ) {
if ( isset($reply_to) )
$message_headers .= $reply_to . "\n";

/**
* Filters the comment notification email text.
*
* @since 1.5.2
*
* @param string $notify_message The comment notification email text.
* @param int $comment_id Comment ID.
*/
/** This filter is documented in core/src/wp-includes/pluggable.php */
$notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID );

/**
* Filters the comment notification email subject.
*
* @since 1.5.2
*
* @param string $subject The comment notification email subject.
* @param int $comment_id Comment ID.
*/
/** This filter is documented in core/src/wp-includes/pluggable.php */
$subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID );

/**
* Filters the comment notification email headers.
*
* @since 1.5.2
*
* @param string $message_headers Headers for the comment notification email.
* @param int $comment_id Comment ID.
*/
/** This filter is documented in core/src/wp-includes/pluggable.php */
$message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID );

foreach ( $emails as $email ) {
Expand Down Expand Up @@ -223,14 +197,7 @@ function wp_notify_moderator($comment_id) {

$maybe_notify = get_option( 'moderation_notify' );

/**
* Filters whether to send the site moderator email notifications, overriding the site setting.
*
* @since 4.4.0
*
* @param bool $maybe_notify Whether to notify blog moderator.
* @param int $comment_ID The id of the comment for the notification.
*/
/** This filter is documented in core/src/wp-includes/pluggable.php */
$maybe_notify = apply_filters( 'notify_moderator', $maybe_notify, $comment_id );

if ( ! $maybe_notify ) {
Expand Down Expand Up @@ -293,68 +260,59 @@ function wp_notify_moderator($comment_id) {
break;
}

/** This filter is documented in core/src/wp-includes/pluggable.php */
$emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id );

function is_user_connected( $email ) {
$user = get_user_by( 'email', $email );
return Jetpack::is_user_connected( $user->ID );
}

$moderate_on_wpcom = ! in_array( false, array_map( 'is_user_connected', $emails ) );

$primary_site_slug = Jetpack::build_raw_urls( get_home_url() );

/* translators: Comment moderation. 1: Comment action URL */
$notify_message .= sprintf( __( 'Approve it: %s' ), "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=approve" ) . "\r\n";
$notify_message .= sprintf( __( 'Approve it: %s' ), $moderate_on_wpcom
? "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=approve"
: admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) ). "\r\n";

if ( EMPTY_TRASH_DAYS ) {
/* translators: Comment moderation. 1: Comment action URL */
$notify_message .= sprintf( __( 'Trash it: %s' ), "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=trash" ) . "\r\n";
$notify_message .= sprintf( __( 'Trash it: %s' ), $moderate_on_wpcom
? "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=trash"
: admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) ) . "\r\n";
} else {
/* translators: Comment moderation. 1: Comment action URL */
$notify_message .= sprintf( __( 'Delete it: %s' ), "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=delete" ) . "\r\n";
$notify_message .= sprintf( __( 'Delete it: %s' ), $moderate_on_wpcom
? "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=delete"
: admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) ) . "\r\n";
}

/* translators: Comment moderation. 1: Comment action URL */
$notify_message .= sprintf( __( 'Spam it: %s' ), "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=spam" ) . "\r\n";
$notify_message .= sprintf( __( 'Spam it: %s' ), $moderate_on_wpcom
? "https://wordpress.com/comment/{$primary_site_slug}/{$comment_id}?action=spam"
: admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) ) . "\r\n";

/* translators: Comment moderation. 1: Number of comments awaiting approval */
$notify_message .= sprintf( _n('Currently %s comment is waiting for approval. Please visit the moderation panel:',
'Currently %s comments are waiting for approval. Please visit the moderation panel:', $comments_waiting), number_format_i18n($comments_waiting) ) . "\r\n";
$notify_message .= admin_url( "edit-comments.php?comment_status=moderated#wpbody-content" ) . "\r\n";

$notify_message .= $moderate_on_wpcom
? "https://wordpress.com/comments/pending/{$primary_site_slug}/"
: admin_url( "edit-comments.php?comment_status=moderated#wpbody-content" ) . "\r\n";

/* translators: Comment moderation notification email subject. 1: Site name, 2: Post title */
$subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title );
$message_headers = '';

/**
* Filters the list of recipients for comment moderation emails.
*
* @since 3.7.0
*
* @param array $emails List of email addresses to notify for comment moderation.
* @param int $comment_id Comment ID.
*/
$emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id );

/**
* Filters the comment moderation email text.
*
* @since 1.5.2
*
* @param string $notify_message Text of the comment moderation email.
* @param int $comment_id Comment ID.
*/
/** This filter is documented in core/src/wp-includes/pluggable.php */
$notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id );

/**
* Filters the comment moderation email subject.
*
* @since 1.5.2
*
* @param string $subject Subject of the comment moderation email.
* @param int $comment_id Comment ID.
*/
/** This filter is documented in core/src/wp-includes/pluggable.php */
$subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id );

/**
* Filters the comment moderation email headers.
*
* @since 2.8.0
*
* @param string $message_headers Headers for the comment moderation email.
* @param int $comment_id Comment ID.
*/
/** This filter is documented in core/src/wp-includes/pluggable.php */
$message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id );

foreach ( $emails as $email ) {
Expand Down

0 comments on commit 87dea84

Please sign in to comment.