| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Services; |
| 6 |
|
| 7 |
use Yatra\Repositories\TripRepository; |
| 8 |
|
| 9 |
/** |
| 10 |
* Sample merge-tag data for email previews and tests. |
| 11 |
* Trip context: pass {@see self::forTemplateKey()} a trip ID to hydrate trip_* tags from the database. |
| 12 |
* |
| 13 |
* @see apply_filters('yatra_email_template_preview_variables', ...) |
| 14 |
* @see apply_filters('yatra_email_template_trip_variables', ...) |
| 15 |
*/ |
| 16 |
final class EmailTemplateSampleData |
| 17 |
{ |
| 18 |
/** |
| 19 |
* @param int|null $tripId Optional Yatra trip ID to merge real trip_name, trip_url, trip_id |
| 20 |
* @return array<string, string> |
| 21 |
*/ |
| 22 |
public static function forTemplateKey(string $templateKey, ?int $tripId = null): array |
| 23 |
{ |
| 24 |
$vars = self::baseSamples(); |
| 25 |
if ($tripId !== null && $tripId > 0) { |
| 26 |
$vars = self::mergeTripContext($tripId, $vars); |
| 27 |
} |
| 28 |
|
| 29 |
/** @var array<string, string> $filtered */ |
| 30 |
$filtered = apply_filters('yatra_email_template_preview_variables', $vars, $templateKey, $tripId); |
| 31 |
|
| 32 |
return TransactionalEmailTemplateService::mergeTemplateVariables($filtered); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @return array<string, string> |
| 37 |
*/ |
| 38 |
private static function baseSamples(): array |
| 39 |
{ |
| 40 |
$currency = SettingsService::getCurrency(); |
| 41 |
$travelSample = date_i18n(get_option('date_format'), strtotime('+14 days')); |
| 42 |
|
| 43 |
return [ |
| 44 |
'customer_name' => __('Alex Traveler', 'yatra'), |
| 45 |
'customer_first_name' => 'Alex', |
| 46 |
'customer_last_name' => __('Traveler', 'yatra'), |
| 47 |
'customer_email' => 'alex@example.com', |
| 48 |
'customer_phone' => '+1 555 0100', |
| 49 |
'booking_reference' => 'YTR-PREVIEW-001', |
| 50 |
'booking_id' => '42', |
| 51 |
'trip_id' => '101', |
| 52 |
'trip_name' => __('Sample Mountain Trek', 'yatra'), |
| 53 |
'travel_date' => $travelSample, |
| 54 |
'travelers_count' => '2', |
| 55 |
'total_amount_formatted' => yatra_format_price(2499.0, $currency), |
| 56 |
'amount_due_formatted' => yatra_format_price(499.0, $currency), |
| 57 |
'currency' => $currency, |
| 58 |
'booking_status' => __('confirmed', 'yatra'), |
| 59 |
'payment_status' => __('pending', 'yatra'), |
| 60 |
'intro_paragraph' => __('Thank you for your booking. This preview uses sample data.', 'yatra'), |
| 61 |
'details_html' => '<p><strong>' . esc_html__('Note', 'yatra') . ':</strong> ' . esc_html__('Optional line items appear here.', 'yatra') . '</p>', |
| 62 |
'footer_note' => __('We look forward to hosting you.', 'yatra'), |
| 63 |
'payment_amount_formatted' => yatra_format_price(500.0, $currency), |
| 64 |
'payment_method' => __('Credit card', 'yatra'), |
| 65 |
'transaction_id' => 'txn_preview_001', |
| 66 |
'reminder_days' => '7', |
| 67 |
'days_until_trip' => '7', |
| 68 |
'reminder_extra_html' => '<p>' . esc_html__('Reminder: check your packing list before departure.', 'yatra') . '</p>', |
| 69 |
'trip_url' => home_url('/'), |
| 70 |
'review_url' => home_url('/#reviews'), |
| 71 |
'admin_url' => admin_url('admin.php?page=yatra'), |
| 72 |
'booking_url' => home_url('/my-account/bookings/42'), |
| 73 |
'message' => __('Could you confirm whether airport pickup is included on day one?', 'yatra'), |
| 74 |
'response' => __('Hi Alex, yes — pickup is included for all guests. Safe travels!', 'yatra'), |
| 75 |
'original_message' => __('Interested in the April departure dates.', 'yatra'), |
| 76 |
]; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param array<string, string> $vars |
| 81 |
* @return array<string, string> |
| 82 |
*/ |
| 83 |
private static function mergeTripContext(int $tripId, array $vars): array |
| 84 |
{ |
| 85 |
try { |
| 86 |
$repo = new TripRepository(); |
| 87 |
$trip = $repo->findWithRelations($tripId, true); |
| 88 |
if ($trip && !empty($trip->id)) { |
| 89 |
$vars['trip_id'] = (string) (int) $trip->id; |
| 90 |
if (!empty($trip->title)) { |
| 91 |
$vars['trip_name'] = (string) $trip->title; |
| 92 |
} |
| 93 |
if (!empty($trip->slug)) { |
| 94 |
$vars['trip_url'] = home_url('/' . SettingsService::getTripBase() . '/' . rawurlencode((string) $trip->slug) . '/'); |
| 95 |
} |
| 96 |
} |
| 97 |
} catch (\Throwable $e) { |
| 98 |
// Leave sample trip_* as-is |
| 99 |
} |
| 100 |
|
| 101 |
/** @var array<string, string> */ |
| 102 |
return apply_filters('yatra_email_template_trip_variables', $vars, $tripId); |
| 103 |
} |
| 104 |
|
| 105 |
public static function isPreviewableTemplateKey(string $templateKey): bool |
| 106 |
{ |
| 107 |
if (TransactionalEmailTemplateService::coreTemplateKeyToType($templateKey) !== null) { |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
return EmailTemplateDefaults::proSystemTemplate($templateKey) !== null; |
| 112 |
} |
| 113 |
} |
| 114 |
|