| 1 |
<?php |
| 2 |
/** |
| 3 |
* Review Reminder Service |
| 4 |
* |
| 5 |
* Handles sending review reminders to customers |
| 6 |
* |
| 7 |
* @package Yatra\Services |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
declare(strict_types=1); |
| 12 |
|
| 13 |
namespace Yatra\Services; |
| 14 |
|
| 15 |
class ReviewReminderService |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Schedule review reminder for a booking |
| 19 |
* |
| 20 |
* @param int $bookingId Booking ID |
| 21 |
*/ |
| 22 |
public static function scheduleReminder(int $bookingId): void |
| 23 |
{ |
| 24 |
// Check if reviews are enabled |
| 25 |
if (!SettingsService::reviewsEnabled()) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
$reminder_days = SettingsService::getInt('review_reminder_days', 7); |
| 30 |
|
| 31 |
if ($reminder_days <= 0) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
// Schedule reminder using WordPress cron |
| 36 |
$timestamp = time() + ($reminder_days * DAY_IN_SECONDS); |
| 37 |
|
| 38 |
if (!wp_next_scheduled('yatra_send_review_reminder', [$bookingId])) { |
| 39 |
wp_schedule_single_event($timestamp, 'yatra_send_review_reminder', [$bookingId]); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Send review reminder email |
| 45 |
* |
| 46 |
* @param int $bookingId Booking ID |
| 47 |
*/ |
| 48 |
public static function sendReminder(int $bookingId): void |
| 49 |
{ |
| 50 |
$bookingRepository = new \Yatra\Repositories\BookingRepository(); |
| 51 |
$booking = $bookingRepository->findWithTrip($bookingId); |
| 52 |
|
| 53 |
if (!$booking || empty($booking->contact_email)) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
// Check if customer has already reviewed |
| 58 |
if (self::hasCustomerReviewed($bookingId, (int) ($booking->customer_id ?? 0))) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
// Get trip details |
| 63 |
$tripRepository = new \Yatra\Repositories\TripRepository(); |
| 64 |
$trip = $tripRepository->find((int) ($booking->trip_id ?? 0)); |
| 65 |
|
| 66 |
if (!$trip) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$review_url = get_permalink($trip->id) . '#reviews'; |
| 71 |
$vars = TransactionalEmailTemplateService::variablesFromBooking($booking); |
| 72 |
$vars['review_url'] = esc_url($review_url); |
| 73 |
$vars['completion_date'] = date_i18n(get_option('date_format')); |
| 74 |
|
| 75 |
TransactionalEmailTemplateService::sendIfEnabled( |
| 76 |
TransactionalEmailTemplateService::TYPE_REVIEW_REQUEST, |
| 77 |
(string) $booking->contact_email, |
| 78 |
$vars |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Check if customer has already reviewed the trip |
| 84 |
* |
| 85 |
* @param int $bookingId Booking ID |
| 86 |
* @param int $customerId Customer ID |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
private static function hasCustomerReviewed(int $bookingId, int $customerId): bool |
| 90 |
{ |
| 91 |
global $wpdb; |
| 92 |
|
| 93 |
$reviewsTable = \Yatra\Database\Tables\ReviewsTable::getTableName(); |
| 94 |
|
| 95 |
$count = $wpdb->get_var($wpdb->prepare( |
| 96 |
"SELECT COUNT(*) FROM {$reviewsTable} |
| 97 |
WHERE booking_id = %d OR customer_id = %d", |
| 98 |
$bookingId, |
| 99 |
$customerId |
| 100 |
)); |
| 101 |
|
| 102 |
return $count > 0; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Initialize review reminder cron |
| 107 |
*/ |
| 108 |
public static function init(): void |
| 109 |
{ |
| 110 |
add_action('yatra_send_review_reminder', [self::class, 'sendReminder']); |
| 111 |
} |
| 112 |
} |
| 113 |
|