| 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->find($bookingId); |
| 52 |
|
| 53 |
if (!$booking || empty($booking->email)) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
// Check if customer has already reviewed |
| 58 |
if (self::hasCustomerReviewed($bookingId, $booking->customer_id)) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
// Get trip details |
| 63 |
$tripRepository = new \Yatra\Repositories\TripRepository(); |
| 64 |
$trip = $tripRepository->find($booking->trip_id); |
| 65 |
|
| 66 |
if (!$trip) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$review_url = get_permalink($trip->id) . '#reviews'; |
| 71 |
$customerName = trim((string) (($booking->first_name ?? '') . ' ' . ($booking->last_name ?? ''))); |
| 72 |
if ($customerName === '') { |
| 73 |
$customerName = __('there', 'yatra'); |
| 74 |
} |
| 75 |
$tripName = (string) ($trip->title ?? ''); |
| 76 |
|
| 77 |
$subject = sprintf( |
| 78 |
/* translators: 1: site name, 2: trip title */ |
| 79 |
__('⭐ [%1$s] How was %2$s?', 'yatra'), |
| 80 |
get_bloginfo('name'), |
| 81 |
$tripName !== '' ? $tripName : __('your trip', 'yatra') |
| 82 |
); |
| 83 |
|
| 84 |
$body = EmailTemplateDefaults::renderCoreReviewReminderHtml( |
| 85 |
$customerName, |
| 86 |
$tripName !== '' ? $tripName : __('your trip', 'yatra'), |
| 87 |
$review_url |
| 88 |
); |
| 89 |
|
| 90 |
EmailService::send($booking->email, $subject, $body, ['Content-Type: text/html; charset=UTF-8']); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Check if customer has already reviewed the trip |
| 95 |
* |
| 96 |
* @param int $bookingId Booking ID |
| 97 |
* @param int $customerId Customer ID |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
private static function hasCustomerReviewed(int $bookingId, int $customerId): bool |
| 101 |
{ |
| 102 |
global $wpdb; |
| 103 |
|
| 104 |
$reviewsTable = \Yatra\Database\Tables\ReviewsTable::getTableName(); |
| 105 |
|
| 106 |
$count = $wpdb->get_var($wpdb->prepare( |
| 107 |
"SELECT COUNT(*) FROM {$reviewsTable} |
| 108 |
WHERE booking_id = %d OR customer_id = %d", |
| 109 |
$bookingId, |
| 110 |
$customerId |
| 111 |
)); |
| 112 |
|
| 113 |
return $count > 0; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Initialize review reminder cron |
| 118 |
*/ |
| 119 |
public static function init(): void |
| 120 |
{ |
| 121 |
add_action('yatra_send_review_reminder', [self::class, 'sendReminder']); |
| 122 |
} |
| 123 |
} |
| 124 |
|