geminilabs

[site-reviews] send email after responding to a review

Mar 12th, 2019 (edited)
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. /**
  2. * Send an email notification to the review author after responding to a review
  3. * Paste this code in your theme’s functions.php file.
  4. * @param \GeminiLabs\SiteReviews\Review $review
  5. * @param string $response
  6. * @return void
  7. */
  8. add_action('site-reviews/review/responded', function ($review, $response) {
  9.     $hasResponse = !empty($review->response);
  10.     $hasResponseUserId = !empty(get_post_meta($review->ID, '_response_by', true));
  11.     if (empty($response) || $hasResponse || $hasResponseUserId) {
  12.         return; // only send an email if the response is not empty and there is no previous response
  13.     }
  14.     if (empty($review->email)) {
  15.         return; // this review has not been submitted with an email
  16.     }
  17.     if (empty($review->assigned_posts)) {
  18.         return; // this review has not been assigned to any Post
  19.     }
  20.     $postId = $review->assigned_posts[0]; // Get the first assigned Post ID
  21.     $permalink = get_permalink($postId); // Get the permalink of the assigned Post
  22.     $subject = sprintf('[%s], We have responded to your review!', get_bloginfo('name'));
  23.     $message = sprintf('Just letting you know that we have <a href="%s">responded to your review</a>!', $permalink);
  24.     $email = glsr('Modules\Email')->compose([
  25.         'to' => $review->email,
  26.         'subject' => $subject,
  27.         'message' => $message,
  28.     ]);
  29.     $email->send();
  30. }, 10, 2);
  31.  
Add Comment
Please, Sign In to add comment