| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Ajax; |
| 6 |
|
| 7 |
/** |
| 8 |
* AJAX handlers for Discount Shortcode |
| 9 |
*/ |
| 10 |
class DiscountShortcodeAjax |
| 11 |
{ |
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
// Use the same AJAX handler as regular trips |
| 15 |
add_action('wp_ajax_yatra_discount_trip_shortcode_load', [$this, 'loadTrips']); |
| 16 |
add_action('wp_ajax_nopriv_yatra_discount_trip_shortcode_load', [$this, 'loadTrips']); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Load trips via AJAX for pagination (same as regular trips) |
| 21 |
*/ |
| 22 |
public function loadTrips(): void |
| 23 |
{ |
| 24 |
|
| 25 |
|
| 26 |
// Verify nonce |
| 27 |
if (!wp_verify_nonce($_POST['nonce'] ?? '', 'yatra_trip_shortcode_nonce')) { |
| 28 |
|
| 29 |
wp_die(esc_html__('Security check failed', 'yatra'), '', ['response' => 403]); |
| 30 |
} |
| 31 |
|
| 32 |
// Parse shortcode attributes |
| 33 |
$atts = $_POST['atts'] ?? []; |
| 34 |
$page = (int) ($_POST['page'] ?? 1); |
| 35 |
|
| 36 |
// Add page to attributes for AJAX pagination |
| 37 |
$atts['current_page'] = $page; |
| 38 |
|
| 39 |
try { |
| 40 |
$discountShortcode = new \Yatra\Shortcodes\DiscountAndDealsShortcode(); |
| 41 |
$trips_data = $discountShortcode->getTrips($atts); |
| 42 |
|
| 43 |
// Prepare data for template - extract variables for template scope |
| 44 |
$trips = [ |
| 45 |
'trips' => $trips_data['trips'] ?? [], |
| 46 |
'max_pages' => $trips_data['max_pages'] ?? 1, |
| 47 |
'current_page' => $trips_data['current_page'] ?? 1, |
| 48 |
'total_found' => $trips_data['total_found'] ?? 0, |
| 49 |
]; |
| 50 |
|
| 51 |
// Additional template variables |
| 52 |
$max_pages = $trips_data['max_pages'] ?? 1; |
| 53 |
$current_page = $trips_data['current_page'] ?? 1; |
| 54 |
$total_found = $trips_data['total_found'] ?? 0; |
| 55 |
|
| 56 |
// Start output buffering |
| 57 |
ob_start(); |
| 58 |
|
| 59 |
// Load the trip template (same as regular trips) |
| 60 |
include YATRA_PLUGIN_PATH . 'templates/shortcodes/trip.php'; |
| 61 |
|
| 62 |
$html = ob_get_clean(); |
| 63 |
|
| 64 |
// Send success response |
| 65 |
wp_send_json_success([ |
| 66 |
'html' => $html, |
| 67 |
'trips' => $trips_data['trips'] ?? [], |
| 68 |
'max_pages' => $max_pages, |
| 69 |
'current_page' => $current_page, |
| 70 |
'total_found' => $total_found, |
| 71 |
]); |
| 72 |
|
| 73 |
} catch (\Exception $e) { |
| 74 |
|
| 75 |
|
| 76 |
wp_send_json_error([ |
| 77 |
'message' => (defined('WP_DEBUG') && WP_DEBUG) |
| 78 |
? 'Error loading discount trips: ' . $e->getMessage() |
| 79 |
: __('Unable to load trips. Please try again.', 'yatra'), |
| 80 |
]); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|