| 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 |
// Debug: Log AJAX request |
| 24 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 25 |
error_log('Yatra AJAX - Request received: ' . print_r($_POST, true)); |
| 26 |
} |
| 27 |
|
| 28 |
// Verify nonce |
| 29 |
if (!wp_verify_nonce($_POST['nonce'] ?? '', 'yatra_trip_shortcode_nonce')) { |
| 30 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 31 |
error_log('Yatra AJAX - Security check failed'); |
| 32 |
} |
| 33 |
wp_die(esc_html__('Security check failed', 'yatra'), '', ['response' => 403]); |
| 34 |
} |
| 35 |
|
| 36 |
// Parse shortcode attributes |
| 37 |
$atts = $_POST['atts'] ?? []; |
| 38 |
$page = (int) ($_POST['page'] ?? 1); |
| 39 |
|
| 40 |
// Add page to attributes for AJAX pagination |
| 41 |
$atts['current_page'] = $page; |
| 42 |
|
| 43 |
// Debug: Log the page being set |
| 44 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 45 |
error_log('Yatra Trip AJAX - Setting current_page in atts to: ' . $page); |
| 46 |
} |
| 47 |
|
| 48 |
try { |
| 49 |
$tripShortcode = new \Yatra\Shortcodes\TripShortcode(); |
| 50 |
$trips_data = $tripShortcode->getTrips($atts); |
| 51 |
|
| 52 |
// Prepare data for template - extract variables for template scope |
| 53 |
$trips = [ |
| 54 |
'trips' => $trips_data['trips'] ?? [], |
| 55 |
'max_pages' => $trips_data['max_pages'] ?? 1, |
| 56 |
'current_page' => $trips_data['current_page'] ?? 1, |
| 57 |
'total_found' => $trips_data['total_found'] ?? 0, |
| 58 |
'debug_info' => [ |
| 59 |
'args_used' => [], |
| 60 |
'raw_count' => count($trips_data['trips'] ?? []) |
| 61 |
] |
| 62 |
]; |
| 63 |
|
| 64 |
// Extract variables to make them available in template |
| 65 |
$atts = $atts; |
| 66 |
$max_pages = $trips_data['max_pages'] ?? 1; |
| 67 |
$current_page = $trips_data['current_page'] ?? 1; |
| 68 |
$total_found = $trips_data['total_found'] ?? 0; |
| 69 |
$columns = (int) $atts['columns']; |
| 70 |
$column_class = 'yatra-tour-grid-' . min(max($columns, 1), 6); |
| 71 |
|
| 72 |
// Debug: Log AJAX data with comprehensive pagination info |
| 73 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 74 |
error_log('=== YATRA TRIP AJAX DEBUG ==='); |
| 75 |
error_log('Yatra AJAX - Requested page: ' . $page); |
| 76 |
error_log('Yatra AJAX - Trips count: ' . count($trips_data['trips'] ?? [])); |
| 77 |
error_log('Yatra AJAX - Total found: ' . $total_found); |
| 78 |
error_log('Yatra AJAX - Max pages: ' . $max_pages); |
| 79 |
error_log('Yatra AJAX - Current page: ' . $current_page); |
| 80 |
error_log('Yatra AJAX - Attributes received: ' . print_r($atts, true)); |
| 81 |
error_log('Yatra AJAX - Is last page: ' . ($current_page === $max_pages ? 'YES' : 'NO')); |
| 82 |
error_log('Yatra AJAX - Current page type: ' . gettype($current_page)); |
| 83 |
error_log('Yatra AJAX - Max pages type: ' . gettype($max_pages)); |
| 84 |
error_log('=== END TRIP AJAX DEBUG ==='); |
| 85 |
} |
| 86 |
|
| 87 |
// Load the template content with variables in scope |
| 88 |
ob_start(); |
| 89 |
// Extract variables to make them available in template (matching other shortcode pattern) |
| 90 |
$trips = [ |
| 91 |
'trips' => $trips_data['trips'] ?? [], |
| 92 |
'max_pages' => $trips_data['max_pages'] ?? 1, |
| 93 |
'current_page' => $trips_data['current_page'] ?? 1, |
| 94 |
'total_found' => $trips_data['total_found'] ?? 0 |
| 95 |
]; |
| 96 |
$max_pages = $trips_data['max_pages'] ?? 1; |
| 97 |
$current_page = $trips_data['current_page'] ?? 1; |
| 98 |
$total_found = $trips_data['total_found'] ?? 0; |
| 99 |
$columns = (int) $atts['columns']; |
| 100 |
$column_class = 'yatra-tour-grid-' . min(max($columns, 1), 6); |
| 101 |
|
| 102 |
// Ensure atts includes the current_page for data-atts |
| 103 |
$atts['current_page'] = $current_page; |
| 104 |
|
| 105 |
// Debug: Log final atts being passed to template |
| 106 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 107 |
error_log('Yatra Trip AJAX - Final atts for template: ' . print_r($atts, true)); |
| 108 |
error_log('Yatra Trip AJAX - current_page in atts: ' . ($atts['current_page'] ?? 'NOT SET')); |
| 109 |
} |
| 110 |
|
| 111 |
include YATRA_PLUGIN_PATH . 'templates/shortcodes/trip.php'; |
| 112 |
$html = ob_get_clean(); |
| 113 |
|
| 114 |
wp_send_json_success([ |
| 115 |
'html' => $html, |
| 116 |
'current_page' => $trips_data['current_page'] ?? 1, |
| 117 |
'max_pages' => $trips_data['max_pages'] ?? 1, |
| 118 |
'total_found' => $trips_data['total_found'] ?? 0 |
| 119 |
]); |
| 120 |
|
| 121 |
} catch (\Exception $e) { |
| 122 |
// Log error for debugging |
| 123 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 124 |
error_log('Yatra TripShortcode AJAX Error: ' . $e->getMessage()); |
| 125 |
error_log('Yatra TripShortcode AJAX Trace: ' . $e->getTraceAsString()); |
| 126 |
} |
| 127 |
|
| 128 |
wp_send_json_error([ |
| 129 |
'message' => (defined('WP_DEBUG') && WP_DEBUG) |
| 130 |
? 'Error loading trips: ' . $e->getMessage() |
| 131 |
: __('Unable to load trips. Please try again.', 'yatra'), |
| 132 |
]); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|