From d01d0e8058b7ee265957bf6a6feec4eea2ec4c87 Mon Sep 17 00:00:00 2001 From: Felipe Pozzer Date: Tue, 27 May 2014 18:17:20 -0300 Subject: [PATCH 1/2] Avoiding redefinition of core function by adding a filter for comment notification recipients --- co-authors-plus.php | 103 +++++++++----------------------------------- 1 file changed, 20 insertions(+), 83 deletions(-) diff --git a/co-authors-plus.php b/co-authors-plus.php index e08ecca9..6585bf7b 100644 --- a/co-authors-plus.php +++ b/co-authors-plus.php @@ -107,6 +107,9 @@ function __construct() { // Support Jetpack Open Graph Tags add_filter( 'jetpack_open_graph_tags', array( $this, 'filter_jetpack_open_graph_tags' ), 10, 2 ); + + // Filter to send comment moderation notification e-mail to multiple authors + add_filter( 'comment_moderation_recipients', 'filter_comment_moderation_email_recipients' ); } @@ -1465,90 +1468,24 @@ function wp_notify_postauthor( $comment_id, $comment_type = '' ) { } endif; -if ( !function_exists('wp_notify_moderator') ) : /** - * Notifies the moderator of the blog about a new comment that is awaiting approval. - * This is a modified version of the core function in wp-includes/pluggable.php that - * supports notifs to multiple co-authors. Unfortunately, this is the best way to do it :( - * - * @since 2.6.2 - * - * @param int $comment_id Comment ID - * @return bool Always returns true + * Filter array of moderation notification email addresses + * + * @param array $recipients + * @return array */ -function wp_notify_moderator( $comment_id ) { - global $wpdb; - - if ( 0 == get_option( 'moderation_notify' ) ) - return true; - - $comment = get_comment($comment_id); - $post = get_post($comment->comment_post_ID); - $coauthors = get_coauthors( $post->ID ); - // Send to the administration and to the co-authors if the co-author can modify the comment. - $email_to = array( get_option('admin_email') ); - foreach( $coauthors as $user ) { - if ( user_can($user->ID, 'edit_comment', $comment_id) && !empty($user->user_email) && ( get_option('admin_email') != $user->user_email) ) - $email_to[] = $user->user_email; - } - - $comment_author_domain = @gethostbyaddr($comment->comment_author_IP); - $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'"); - - // The blogname option is escaped with esc_html on the way into the database in sanitize_option - // we want to reverse this for the plain text arena of emails. - $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); +function filter_comment_moderation_email_recipients( $recipients ) { + global $post; + + if ( isset($post->ID) ) { + $coauthors = get_coauthors( $post->ID ); + foreach ( $coauthors as $user ) { + // "edit_comment" capability == "edit_post" capability + if ( user_can($user->ID, 'edit_post', $post->ID) && !empty($user->user_email) ) + $extra_recipients[] = $user->user_email; + } - switch ($comment->comment_type) - { - case 'trackback': - $notify_message = sprintf( __('A new trackback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n"; - $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n"; - $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; - $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n"; - $notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n"; - break; - case 'pingback': - $notify_message = sprintf( __('A new pingback on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n"; - $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n"; - $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; - $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n"; - $notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n"; - break; - default: //Comments - $notify_message = sprintf( __('A new comment on the post "%s" is waiting for your approval'), $post->post_title ) . "\r\n"; - $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n"; - $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n"; - $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n"; - $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n"; - $notify_message .= sprintf( __('Whois : http://whois.arin.net/rest/ip/%s'), $comment->comment_author_IP ) . "\r\n"; - $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n"; - break; + return array_unique( array_merge( $recipients, $extra_recipients ) ); } - - $notify_message .= sprintf( __('Approve it: %s'), admin_url("comment.php?action=approve&c=$comment_id") ) . "\r\n"; - if ( EMPTY_TRASH_DAYS ) - $notify_message .= sprintf( __('Trash it: %s'), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n"; - else - $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n"; - $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n"; - - $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") . "\r\n"; - - $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), $blogname, $post->post_title ); - $message_headers = ''; - - $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id); - $subject = apply_filters('comment_moderation_subject', $subject, $comment_id); - $message_headers = apply_filters('comment_moderation_headers', $message_headers); - - foreach ( $email_to as $email ) - @wp_mail($email, $subject, $notify_message, $message_headers); - - return true; -} -endif; - - + return $recipients; +} \ No newline at end of file From d270bd3ad4216407854adb095a6af954b475df98 Mon Sep 17 00:00:00 2001 From: Felipe Pozzer Date: Wed, 28 May 2014 10:36:43 -0300 Subject: [PATCH 2/2] Adding function prefix / Getting post ID via comment ID / Removing capability check --- co-authors-plus.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/co-authors-plus.php b/co-authors-plus.php index 6585bf7b..d0c03503 100644 --- a/co-authors-plus.php +++ b/co-authors-plus.php @@ -109,7 +109,7 @@ function __construct() { add_filter( 'jetpack_open_graph_tags', array( $this, 'filter_jetpack_open_graph_tags' ), 10, 2 ); // Filter to send comment moderation notification e-mail to multiple authors - add_filter( 'comment_moderation_recipients', 'filter_comment_moderation_email_recipients' ); + add_filter( 'comment_moderation_recipients', 'cap_filter_comment_moderation_email_recipients', 10, 2 ); } @@ -1472,16 +1472,17 @@ function wp_notify_postauthor( $comment_id, $comment_type = '' ) { * Filter array of moderation notification email addresses * * @param array $recipients + * @param int $comment_id * @return array */ -function filter_comment_moderation_email_recipients( $recipients ) { - global $post; - - if ( isset($post->ID) ) { - $coauthors = get_coauthors( $post->ID ); +function cap_filter_comment_moderation_email_recipients( $recipients, $comment_id ) { + $comment = get_comment( $comment_id ); + $post_id = $comment->comment_post_ID; + + if ( isset($post_id) ) { + $coauthors = get_coauthors( $post_id ); foreach ( $coauthors as $user ) { - // "edit_comment" capability == "edit_post" capability - if ( user_can($user->ID, 'edit_post', $post->ID) && !empty($user->user_email) ) + if ( !empty($user->user_email) ) $extra_recipients[] = $user->user_email; }