From 178f21cfcbbf323ef68059842d43c6e6381fb0d9 Mon Sep 17 00:00:00 2001 From: kuznev Date: Tue, 17 Oct 2023 17:56:06 +0300 Subject: [PATCH] Add review approved user email --- includes/components/class-review.php | 50 +++++++++++++++++++++ includes/emails/class-review-approve.php | 56 ++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 includes/emails/class-review-approve.php diff --git a/includes/components/class-review.php b/includes/components/class-review.php index b20efc5..27b4dfd 100644 --- a/includes/components/class-review.php +++ b/includes/components/class-review.php @@ -9,6 +9,7 @@ use HivePress\Helpers as hp; use HivePress\Models; +use HivePress\Emails; // Exit if accessed directly. defined( 'ABSPATH' ) || exit; @@ -59,6 +60,9 @@ public function __construct( $args = [] ) { add_filter( 'hivepress/v1/templates/review_view_block', [ $this, 'alter_review_view_block' ] ); + // Send approved review user email. + add_action( 'transition_comment_status', [ $this, 'send_approved_review_email' ], 10, 3 ); + parent::__construct( $args ); } @@ -415,4 +419,50 @@ public function alter_review_view_block( $template ) { return $template; } + + /** + * Send approved user email + * + * @param string $new_status New comment status. + * @param string $old_status Old comment status. + * @param WP_Comment $comment Comment object. + */ + public function send_approved_review_email( $new_status, $old_status, $comment ) { + if ( ! get_option( 'hp_review_enable_moderation' ) || 'hp_review' !== $comment->comment_type || 'approved' !== $new_status ) { + return; + } + + // Get review. + $review = Models\Review::query()->get_by_id( $comment->comment_ID ); + + if ( ! $review ) { + return; + } + + // Get user. + $user = $review->get_author(); + + // Get listing. + $listing = $review->get_listing(); + + if ( ! $user || ! $listing ) { + return; + } + + // Send email. + ( new Emails\Review_Approve( + [ + 'recipient' => $user->get_email(), + + 'tokens' => [ + 'user' => $user, + 'listing' => $listing, + 'review' => $review, + 'user_name' => $user->get_display_name(), + 'listing_title' => $listing->get_title(), + 'listing_url' => hivepress()->router->get_url( 'listing_view_page', [ 'listing_id' => $listing->get_id() ] ), + ], + ] + ) )->send(); + } } diff --git a/includes/emails/class-review-approve.php b/includes/emails/class-review-approve.php new file mode 100644 index 0000000..8fda1c6 --- /dev/null +++ b/includes/emails/class-review-approve.php @@ -0,0 +1,56 @@ + esc_html__( 'Review Approved', 'hivepress-reviews' ), + 'recipient' => hivepress()->translator->get_string( 'user' ), + 'tokens' => [ 'user_name', 'listing_title', 'listing_url', 'user', 'listing', 'review' ], + ], + $meta + ); + + parent::init( $meta ); + } + + /** + * Class constructor. + * + * @param array $args Email arguments. + */ + public function __construct( $args = [] ) { + $args = hp\merge_arrays( + [ + 'subject' => esc_html__( 'Review Approved', 'hivepress-reviews' ), + 'body' => esc_html__( 'Hi, %user_name%! Your review for "%listing_title%" has been approved, click on the following link to view it: %listing_url%', 'hivepress-reviews' ), + ], + $args + ); + + parent::__construct( $args ); + } +}