| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Ajax; |
| 6 |
|
| 7 |
/** |
| 8 |
* AJAX handlers for Activity Shortcode |
| 9 |
*/ |
| 10 |
class ActivityShortcodeAjax |
| 11 |
{ |
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
add_action('wp_ajax_yatra_activity_shortcode_load', [$this, 'loadActivities']); |
| 15 |
add_action('wp_ajax_nopriv_yatra_activity_shortcode_load', [$this, 'loadActivities']); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Load activities via AJAX for pagination |
| 20 |
*/ |
| 21 |
public function loadActivities(): void |
| 22 |
{ |
| 23 |
// Debug: Log AJAX request |
| 24 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 25 |
error_log('Yatra Activity AJAX - Request received: ' . print_r($_POST, true)); |
| 26 |
$atts = $_POST['atts'] ?? []; |
| 27 |
error_log('Yatra Activity AJAX - per_page parameter: ' . ($atts['per_page'] ?? 'NOT SET')); |
| 28 |
error_log('Yatra Activity AJAX - page parameter: ' . ($_POST['page'] ?? 'NOT SET')); |
| 29 |
} |
| 30 |
|
| 31 |
// Verify nonce |
| 32 |
if (!wp_verify_nonce($_POST['nonce'] ?? '', 'yatra_activity_shortcode_nonce')) { |
| 33 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 34 |
error_log('Yatra Activity AJAX - Security check failed'); |
| 35 |
} |
| 36 |
wp_die(esc_html__('Security check failed', 'yatra'), '', ['response' => 403]); |
| 37 |
} |
| 38 |
|
| 39 |
// Parse shortcode attributes |
| 40 |
$atts = $_POST['atts'] ?? []; |
| 41 |
$page = (int) ($_POST['page'] ?? 1); |
| 42 |
|
| 43 |
// Add page to attributes for AJAX pagination |
| 44 |
$atts['current_page'] = $page; |
| 45 |
|
| 46 |
try { |
| 47 |
$activityShortcode = new \Yatra\Shortcodes\ActivityShortcode(); |
| 48 |
$activities_data = $activityShortcode->getActivities($atts); |
| 49 |
|
| 50 |
// Prepare data for template - extract variables for template scope |
| 51 |
$activities = [ |
| 52 |
'activities' => $activities_data['activities'] ?? [], |
| 53 |
'max_pages' => $activities_data['max_pages'] ?? 1, |
| 54 |
'current_page' => $activities_data['current_page'] ?? 1, |
| 55 |
'total_found' => $activities_data['total_found'] ?? 0 |
| 56 |
]; |
| 57 |
|
| 58 |
// Extract variables to make them available in template |
| 59 |
$atts = $atts; |
| 60 |
$max_pages = $activities_data['max_pages'] ?? 1; |
| 61 |
$current_page = $activities_data['current_page'] ?? 1; |
| 62 |
$total_found = $activities_data['total_found'] ?? 0; |
| 63 |
$columns = (int) $atts['columns']; |
| 64 |
$column_class = 'yatra-activity-grid-' . min(max($columns, 1), 4); |
| 65 |
|
| 66 |
// Debug: Log AJAX data with comprehensive pagination info |
| 67 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 68 |
error_log('=== YATRA ACTIVITY AJAX DEBUG ==='); |
| 69 |
error_log('Yatra Activity AJAX - Requested page: ' . $page); |
| 70 |
error_log('Yatra Activity AJAX - Activities count: ' . count($activities_data['activities'] ?? [])); |
| 71 |
error_log('Yatra Activity AJAX - Total found: ' . $total_found); |
| 72 |
error_log('Yatra Activity AJAX - Max pages: ' . $max_pages); |
| 73 |
error_log('Yatra Activity AJAX - Current page: ' . $current_page); |
| 74 |
error_log('Yatra Activity AJAX - Attributes received: ' . print_r($atts, true)); |
| 75 |
|
| 76 |
// Log each activity being returned |
| 77 |
$activities_list = $activities_data['activities'] ?? []; |
| 78 |
if (!empty($activities_list)) { |
| 79 |
error_log('Activities being returned for page ' . $current_page . ':'); |
| 80 |
foreach ($activities_list as $index => $activity) { |
| 81 |
error_log(' ' . ($index + 1) . '. ' . ($activity['term']->name ?? 'NO NAME') . ' (ID: ' . ($activity['term']->id ?? 'NO ID') . ')'); |
| 82 |
} |
| 83 |
} |
| 84 |
error_log('=== END ACTIVITY 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 trip shortcode pattern) |
| 90 |
$activities = $activities_data['activities'] ?? []; |
| 91 |
$max_pages = $activities_data['max_pages'] ?? 1; |
| 92 |
$current_page = $activities_data['current_page'] ?? 1; |
| 93 |
$total_found = $activities_data['total_found'] ?? 0; |
| 94 |
$columns = (int) $atts['columns']; |
| 95 |
$column_class = 'yatra-activity-grid-' . min(max($columns, 1), 4); |
| 96 |
|
| 97 |
include YATRA_PLUGIN_PATH . 'templates/shortcodes/activity.php'; |
| 98 |
$html = ob_get_clean(); |
| 99 |
|
| 100 |
wp_send_json_success([ |
| 101 |
'html' => $html, |
| 102 |
'current_page' => $activities_data['current_page'] ?? 1, |
| 103 |
'max_pages' => $activities_data['max_pages'] ?? 1, |
| 104 |
'total_found' => $activities_data['total_found'] ?? 0 |
| 105 |
]); |
| 106 |
|
| 107 |
} catch (\Exception $e) { |
| 108 |
// Log error for debugging |
| 109 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 110 |
error_log('Yatra ActivityShortcode AJAX Error: ' . $e->getMessage()); |
| 111 |
error_log('Yatra ActivityShortcode AJAX Trace: ' . $e->getTraceAsString()); |
| 112 |
} |
| 113 |
|
| 114 |
wp_send_json_error([ |
| 115 |
'message' => (defined('WP_DEBUG') && WP_DEBUG) |
| 116 |
? 'Error loading activities: ' . $e->getMessage() |
| 117 |
: __('Unable to load activities. Please try again.', 'yatra'), |
| 118 |
]); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|