| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Ajax; |
| 6 |
|
| 7 |
/** |
| 8 |
* AJAX handlers for Trip Shortcode |
| 9 |
*/ |
| 10 |
class TripShortcodeAjax |
| 11 |
{ |
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
add_action('wp_ajax_yatra_trip_shortcode_load', [$this, 'loadTrips']); |
| 15 |
add_action('wp_ajax_nopriv_yatra_trip_shortcode_load', [$this, 'loadTrips']); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Load trips via AJAX for pagination |
| 20 |
*/ |
| 21 |
public function loadTrips(): void |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
// Verify nonce |
| 26 |
if (!wp_verify_nonce($_POST['nonce'] ?? '', 'yatra_trip_shortcode_nonce')) { |
| 27 |
|
| 28 |
wp_die(esc_html__('Security check failed', 'yatra'), '', ['response' => 403]); |
| 29 |
} |
| 30 |
|
| 31 |
// Parse shortcode attributes |
| 32 |
$atts = $_POST['atts'] ?? []; |
| 33 |
$page = (int) ($_POST['page'] ?? 1); |
| 34 |
|
| 35 |
// Add page to attributes for AJAX pagination |
| 36 |
$atts['current_page'] = $page; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
try { |
| 41 |
$tripShortcode = new \Yatra\Shortcodes\TripShortcode(); |
| 42 |
$trips_data = $tripShortcode->getTrips($atts); |
| 43 |
|
| 44 |
// Prepare data for template - extract variables for template scope |
| 45 |
$trips = [ |
| 46 |
'trips' => $trips_data['trips'] ?? [], |
| 47 |
'max_pages' => $trips_data['max_pages'] ?? 1, |
| 48 |
'current_page' => $trips_data['current_page'] ?? 1, |
| 49 |
'total_found' => $trips_data['total_found'] ?? 0, |
| 50 |
'debug_info' => [ |
| 51 |
'args_used' => [], |
| 52 |
'raw_count' => count($trips_data['trips'] ?? []) |
| 53 |
] |
| 54 |
]; |
| 55 |
|
| 56 |
// Extract variables to make them available in template |
| 57 |
$atts = $atts; |
| 58 |
$max_pages = $trips_data['max_pages'] ?? 1; |
| 59 |
$current_page = $trips_data['current_page'] ?? 1; |
| 60 |
$total_found = $trips_data['total_found'] ?? 0; |
| 61 |
$columns = (int) $atts['columns']; |
| 62 |
$column_class = 'yatra-tour-grid-' . min(max($columns, 1), 6); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
// Load the template content with variables in scope |
| 67 |
ob_start(); |
| 68 |
// Extract variables to make them available in template (matching other shortcode pattern) |
| 69 |
$trips = [ |
| 70 |
'trips' => $trips_data['trips'] ?? [], |
| 71 |
'max_pages' => $trips_data['max_pages'] ?? 1, |
| 72 |
'current_page' => $trips_data['current_page'] ?? 1, |
| 73 |
'total_found' => $trips_data['total_found'] ?? 0 |
| 74 |
]; |
| 75 |
$max_pages = $trips_data['max_pages'] ?? 1; |
| 76 |
$current_page = $trips_data['current_page'] ?? 1; |
| 77 |
$total_found = $trips_data['total_found'] ?? 0; |
| 78 |
$columns = (int) $atts['columns']; |
| 79 |
$column_class = 'yatra-tour-grid-' . min(max($columns, 1), 6); |
| 80 |
|
| 81 |
// Ensure atts includes the current_page for data-atts |
| 82 |
$atts['current_page'] = $current_page; |
| 83 |
|
| 84 |
|
| 85 |
include YATRA_PLUGIN_PATH . 'templates/shortcodes/trip.php'; |
| 86 |
$html = ob_get_clean(); |
| 87 |
|
| 88 |
wp_send_json_success([ |
| 89 |
'html' => $html, |
| 90 |
'current_page' => $trips_data['current_page'] ?? 1, |
| 91 |
'max_pages' => $trips_data['max_pages'] ?? 1, |
| 92 |
'total_found' => $trips_data['total_found'] ?? 0 |
| 93 |
]); |
| 94 |
|
| 95 |
} catch (\Exception $e) { |
| 96 |
|
| 97 |
wp_send_json_error([ |
| 98 |
'message' => (defined('WP_DEBUG') && WP_DEBUG) |
| 99 |
? 'Error loading trips: ' . $e->getMessage() |
| 100 |
: __('Unable to load trips. Please try again.', 'yatra'), |
| 101 |
]); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|