Notifications
Clear all

Comment approved notification - don't send for comments on custom post type

14 Posts
3 Users
3 Reactions
2,722 Views
(@salubritas)
Active Member
Joined: 5 years ago
Posts: 15
Topic starter  

I have the comment approved email notification set up and working using wpDiscuz.

I am now using a plugin (GeoDirectory) which uses custom post types and has its own system for displaying comments and sending comment notifications.

When a comment on this CPT is approved, both wpDiscuz and GeoDirectory send an email notification, which I do not want to happen.

Is there a way (a filter maybe?) to stop wpDiscuz sending the comment approved email for comments on a custom post type? I still want wpDiscuz to send the comment approved email for comments on ordinary posts.


   
Quote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 7 years ago
Posts: 6191
 

Hi@salubritas,

Please follow the steps below:

1. Open the wp-content/plugins/wpdiscuz/utils/class.WpdiscuzHelperEmail.php file

2. Find notifyOnApproving function

3. replace with the following one:

public function notifyOnApproving($comment) {
       $wpdiscuz = wpDiscuz();
       if ($comment && $wpdiscuz->helper->isLoadWpdiscuz(get_post($comment->comment_post_ID))) {
           $user = $comment->user_id ? get_userdata($comment->user_id) : null;
           if ($user) {
               $email = $user->user_email;
           } else {
               $email = $comment->comment_author_email;
           }           $subject = $this->optionsSerialized->phrases['wc_comment_approved_email_subject'];
           $message = $this->optionsSerialized->phrases['wc_comment_approved_email_message'];
           $siteUrl = get_site_url();
           $blogTitle = get_option('blogname');
           $postTitle = get_the_title($comment->comment_post_ID);
           if (strpos($message, '[SITE_URL]') !== false) {
               $message = str_replace('[SITE_URL]', $siteUrl, $message);
           }
           if (strpos($message, '[POST_URL]') !== false) {
               $postPermalink = get_permalink($comment->comment_post_ID);
               $message = str_replace('[POST_URL]', $postPermalink, $message);
           }
           if (strpos($message, '[BLOG_TITLE]') !== false) {
               $message = str_replace('[BLOG_TITLE]', $blogTitle, $message);
           }
           if (strpos($message, '[POST_TITLE]') !== false) {
               $message = str_replace('[POST_TITLE]', $postTitle, $message);
           }
           if (strpos($message, '[COMMENT_URL]') !== false) {
               $commentPermalink = get_comment_link($comment->comment_ID);
               $message = str_replace('[COMMENT_URL]', $commentPermalink, $message);
           }
           if (strpos($message, '[COMMENT_AUTHOR]') !== false) {
               $message = str_replace('[COMMENT_AUTHOR]', $comment->comment_author, $message);
           }
           if (strpos($message, '[COMMENT_CONTENT]') !== false) {
               $message = str_replace('[COMMENT_CONTENT]', $comment->comment_content, $message);
           }
           if (strpos($subject, '[BLOG_TITLE]') !== false) {
               $subject = str_replace('[BLOG_TITLE]', $blogTitle, $subject);
           }
           if (strpos($subject, '[POST_TITLE]') !== false) {
               $subject = str_replace('[POST_TITLE]', $postTitle, $subject);
           }
           $headers = array();
           $mailContentType = apply_filters('wp_mail_content_type', 'text/html');
           $fromName = apply_filters('wp_mail_from_name', $blogTitle);
           $fromName = html_entity_decode($fromName, ENT_QUOTES);
           $parsedUrl = parse_url($siteUrl);
           $domain = isset($parsedUrl['host']) ? WpdiscuzHelper::fixEmailFrom($parsedUrl['host']) : '';
           $fromEmail = 'no-reply@' . $domain;
           $fromEmail = apply_filters('wp_mail_from', $fromEmail);
           $headers[] = "Content-Type:  $mailContentType; charset=UTF-8";
           $headers[] = "From: " . $fromName . " <" . $fromEmail . "> \r\n";
           $subject = html_entity_decode($subject, ENT_QUOTES);
           $message = html_entity_decode($message, ENT_QUOTES);
           wp_mail($email, $subject, do_shortcode($message), $headers);
       }
   }

   
Salubritas reacted
ReplyQuote
(@salubritas)
Active Member
Joined: 5 years ago
Posts: 15
Topic starter  

Thank you, that worked.

Is there any chance of a future change so it can be done with a filter, or a check for a replacement class file in the theme (like you have done for WpdiscuzWalker)? Or any method other than changing the plugin code directly.


   
ReplyQuote
(@salubritas)
Active Member
Joined: 5 years ago
Posts: 15
Topic starter  

Or is this change going to make it into the plugin anyway?

I'm not sure what isLoadWpdiscuz does but it sounds like a neat solution if it checks for wpDiscuz being used on that particular post.


   
ReplyQuote
Astghik
(@astgh)
Illustrious Member Admin
Joined: 7 years ago
Posts: 6191
 
Posted by: @salubritas

Or is this change going to make it into the plugin anyway?

yes, we're going to add these changes in the plugin core. 


   
Salubritas reacted
ReplyQuote
(@salubritas)
Active Member
Joined: 5 years ago
Posts: 15
Topic starter  

Sorry for the late response but I've just noticed that this isn't working as expected.

It has actually stopped the comment approved notification being sent for all post types.

I tried to track down the problem and I think it is because isLoadWpdiscuz checks for is_singular().

Because comment approval happens in admin there is no post currently being viewed and is_singular() will always return false, if I understand it correctly.

I have reverted the change. As a workaround I have set the priority of the other plugin's transition_comment_status filter ahead of wpDiscuz, then got it to remove_all_actions from that filter when it is called to prevent the doubling up of emails.

It works, but not a very nice solution.


   
Astghik reacted
ReplyQuote
Elvina
(@elvina)
Support
Joined: 5 years ago
Posts: 1403
 

Hi @salubritas,

Please replace the code provided by Astghik with the following one:

public function notifyOnApproving($comment) {
if ($comment) {
$wpdiscuz = wpDiscuz();
$isLoadWpdiscuz = false;
$post = get_post($comment->comment_post_ID);
if ($post && is_object($post)) {
$form = $wpdiscuz->wpdiscuzForm->getForm($post->ID);
$isLoadWpdiscuz = $form->getFormID() && (comments_open($post) || $post->comment_count) && post_type_supports($post->post_type, "comments");
}
if ($isLoadWpdiscuz) {
$user = $comment->user_id ? get_userdata($comment->user_id) : null;
if ($user) {
$email = $user->user_email;
} else {
$email = $comment->comment_author_email;
}
$siteUrl = get_site_url();
$blogTitle = get_option("blogname");
$postTitle = get_the_title($comment->comment_post_ID);
$search = ["[SITE_URL]", "[POST_URL]", "[BLOG_TITLE]", "[POST_TITLE]", "[COMMENT_URL]", "[COMMENT_AUTHOR]", "[COMMENT_CONTENT]"];
$replace = [$siteUrl, get_permalink($comment->comment_post_ID), $blogTitle, $postTitle, get_comment_link($comment->comment_ID), $comment->comment_author, wpautop($comment->comment_content)];
$message = str_replace($search, $replace, $this->options->phrases["wc_comment_approved_email_message"]);
$subject = str_replace(["[BLOG_TITLE]", "[POST_TITLE]"], [$blogTitle, $postTitle], $this->options->phrases["wc_comment_approved_email_subject"]);
$headers = [];
$fromName = html_entity_decode($blogTitle, ENT_QUOTES);
$parsedUrl = parse_url($siteUrl);
$domain = isset($parsedUrl["host"]) ? WpdiscuzHelper::fixEmailFrom($parsedUrl["host"]) : "";
$fromEmail = "no-reply@" . $domain;
$headers[] = "Content-Type: text/html; charset=UTF-8";
$headers[] = "From: " . $fromName . " <" . $fromEmail . "> \r\n";
$subject = html_entity_decode($subject, ENT_QUOTES);
$message = html_entity_decode($message, ENT_QUOTES);
wp_mail($email, $subject, do_shortcode($message), $headers);
}
}
}

   
ReplyQuote
(@salubritas)
Active Member
Joined: 5 years ago
Posts: 15
Topic starter  

I tried that and now get these errors logged on approving a comment:

[29-Oct-2019 10:17:33 UTC] PHP Notice: Undefined property: WpdiscuzHelperEmail::$options in ..\wp-content\plugins\wpdiscuz\utils\class.WpdiscuzHelperEmail.php on line 346
[29-Oct-2019 10:17:33 UTC] PHP Notice: Trying to get property of non-object in ..\wp-content\plugins\wpdiscuz\utils\class.WpdiscuzHelperEmail.php on line 346
[29-Oct-2019 10:17:33 UTC] PHP Notice: Undefined property: WpdiscuzHelperEmail::$options in ..\wp-content\plugins\wpdiscuz\utils\class.WpdiscuzHelperEmail.php on line 347
[29-Oct-2019 10:17:33 UTC] PHP Notice: Trying to get property of non-object in ..\wp-content\plugins\wpdiscuz\utils\class.WpdiscuzHelperEmail.php on line 347

I'm guessing it is dependent on changes made elsewhere in the class.


   
ReplyQuote
Elvina
(@elvina)
Support
Joined: 5 years ago
Posts: 1403
 

@salubritas,

Please try this code:

public function notifyOnApproving($comment) {
       if ($comment) {
           $wpdiscuz = wpDiscuz();
           $isLoadWpdiscuz = false;
           $post = get_post($comment->comment_post_ID);
           if ($post && is_object($post)) {
               $form = $wpdiscuz->wpdiscuzForm->getForm($post->ID);
               $isLoadWpdiscuz = $form->getFormID() && (comments_open($post) || $post->comment_count) && post_type_supports($post->post_type, "comments");
           }
           if ($isLoadWpdiscuz) {
               $user = $comment->user_id ? get_userdata($comment->user_id) : null;
               if ($user) {
                   $email = $user->user_email;
               } else {
                   $email = $comment->comment_author_email;
               } $subject = $this->optionsSerialized->phrases['wc_comment_approved_email_subject'];
               $message = $this->optionsSerialized->phrases['wc_comment_approved_email_message'];
               $siteUrl = get_site_url();
               $blogTitle = get_option('blogname');
               $postTitle = get_the_title($comment->comment_post_ID);
               if (strpos($message, '[SITE_URL]') !== false) {
                   $message = str_replace('[SITE_URL]', $siteUrl, $message);
               }
               if (strpos($message, '[POST_URL]') !== false) {
                   $postPermalink = get_permalink($comment->comment_post_ID);
                   $message = str_replace('[POST_URL]', $postPermalink, $message);
               }
               if (strpos($message, '[BLOG_TITLE]') !== false) {
                   $message = str_replace('[BLOG_TITLE]', $blogTitle, $message);
               }
               if (strpos($message, '[POST_TITLE]') !== false) {
                   $message = str_replace('[POST_TITLE]', $postTitle, $message);
               }
               if (strpos($message, '[COMMENT_URL]') !== false) {
                   $commentPermalink = get_comment_link($comment->comment_ID);
                   $message = str_replace('[COMMENT_URL]', $commentPermalink, $message);
               }
               if (strpos($message, '[COMMENT_AUTHOR]') !== false) {
                   $message = str_replace('[COMMENT_AUTHOR]', $comment->comment_author, $message);
               }
               if (strpos($message, '[COMMENT_CONTENT]') !== false) {
                   $message = str_replace('[COMMENT_CONTENT]', $comment->comment_content, $message);
               }
               if (strpos($subject, '[BLOG_TITLE]') !== false) {
                   $subject = str_replace('[BLOG_TITLE]', $blogTitle, $subject);
               }
               if (strpos($subject, '[POST_TITLE]') !== false) {
                   $subject = str_replace('[POST_TITLE]', $postTitle, $subject);
               }
               $headers = array();
               $mailContentType = apply_filters('wp_mail_content_type', 'text/html');
               $fromName = apply_filters('wp_mail_from_name', $blogTitle);
               $fromName = html_entity_decode($fromName, ENT_QUOTES);
               $parsedUrl = parse_url($siteUrl);
               $domain = isset($parsedUrl['host']) ? WpdiscuzHelper::fixEmailFrom($parsedUrl['host']) : '';
               $fromEmail = 'no-reply@' . $domain;
               $fromEmail = apply_filters('wp_mail_from', $fromEmail);
               $headers[] = "Content-Type:  $mailContentType; charset=UTF-8";
               $headers[] = "From: " . $fromName . " <" . $fromEmail . "> \r\n";
               $subject = html_entity_decode($subject, ENT_QUOTES);
               $message = html_entity_decode($message, ENT_QUOTES);
               wp_mail($email, $subject, do_shortcode($message), $headers);
           }
       }
   }

 


   
ReplyQuote
(@salubritas)
Active Member
Joined: 5 years ago
Posts: 15
Topic starter  

Thanks. I see you put an update out yesterday and I assume this is included?

When I get some time I will install the update on my dev environment and retest this.


   
ReplyQuote
Elvina
(@elvina)
Support
Joined: 5 years ago
Posts: 1403
 

@salubritas,

I'm sorry, it hasn't been included in the latest update.

However, the code will be added in the next version for sure.


   
ReplyQuote
(@salubritas)
Active Member
Joined: 5 years ago
Posts: 15
Topic starter  

Oh OK.... it looks like you have another update out through now - 5.3.4.

Which version is the fix above for? 5.3.2? I'm guessing there are code changes in 5.3.3 which is why you provided a different version before. I see "Code Optimization" in the changelog for 5.3.3.

Is this fix in 5.3.4? I don't see it in the changelog.

I don't want to apply the fix and then have the update overwrite it.

I'm thinking the best thing is to wait for the update which includes this, seeing as I have my own workaround for it now. Will it be clear when this is fixed in the changelog visible in WP Admin? Or is that just a summary?


   
ReplyQuote
Elvina
(@elvina)
Support
Joined: 5 years ago
Posts: 1403
 

@salubritas,

Please don't worry about this.

Changes will be included in the next version update.

Will it be clear when this is fixed in the changelog visible in WP Admin? Or is that just a summary?

Sure, it will added to the changelog.


   
ReplyQuote
(@salubritas)
Active Member
Joined: 5 years ago
Posts: 15
Topic starter  

OK thanks. I'll look out for the next update and check the changelog.


   
ReplyQuote
Share:
Scroll to top