Skip to content

Commit

Permalink
Add review approved user email
Browse files Browse the repository at this point in the history
  • Loading branch information
kuznev committed Oct 17, 2023
1 parent 1cd96d0 commit 178f21c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
50 changes: 50 additions & 0 deletions includes/components/class-review.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use HivePress\Helpers as hp;
use HivePress\Models;
use HivePress\Emails;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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();
}
}
56 changes: 56 additions & 0 deletions includes/emails/class-review-approve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Review approve email.
*
* @package HivePress\Emails
*/

namespace HivePress\Emails;

use HivePress\Helpers as hp;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
* Review approve email class.
*
* @class Review_Approve
*/
class Review_Approve extends Email {

/**
* Class initializer.
*
* @param array $meta Form meta.
*/
public static function init( $meta = [] ) {
$meta = hp\merge_arrays(
[
'label' => 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 );
}
}

0 comments on commit 178f21c

Please sign in to comment.