| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Ajax; |
| 6 |
|
| 7 |
/** |
| 8 |
* AJAX for {@see \Yatra\Shortcodes\TripCategoryShortcode} pagination. |
| 9 |
*/ |
| 10 |
class TripCategoryShortcodeAjax |
| 11 |
{ |
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
add_action('wp_ajax_yatra_trip_category_shortcode_load', [$this, 'loadCategories']); |
| 15 |
add_action('wp_ajax_nopriv_yatra_trip_category_shortcode_load', [$this, 'loadCategories']); |
| 16 |
} |
| 17 |
|
| 18 |
public function loadCategories(): void |
| 19 |
{ |
| 20 |
if (!wp_verify_nonce($_POST['nonce'] ?? '', 'yatra_trip_category_shortcode_nonce')) { |
| 21 |
wp_die(esc_html__('Security check failed', 'yatra'), '', ['response' => 403]); |
| 22 |
} |
| 23 |
|
| 24 |
$atts = $_POST['atts'] ?? []; |
| 25 |
if (!is_array($atts)) { |
| 26 |
$atts = []; |
| 27 |
} |
| 28 |
|
| 29 |
$page = (int) ($_POST['page'] ?? 1); |
| 30 |
$atts['current_page'] = $page; |
| 31 |
|
| 32 |
try { |
| 33 |
$shortcode = new \Yatra\Shortcodes\TripCategoryShortcode(); |
| 34 |
$data = $shortcode->getCategories($atts); |
| 35 |
|
| 36 |
$categories = $data['categories'] ?? []; |
| 37 |
$max_pages = $data['max_pages'] ?? 1; |
| 38 |
$current_page = $data['current_page'] ?? 1; |
| 39 |
$total_found = $data['total_found'] ?? 0; |
| 40 |
$columns = (int) ($atts['columns'] ?? 3); |
| 41 |
$column_class = 'yatra-destination-grid-' . min(max($columns, 1), 6); |
| 42 |
|
| 43 |
ob_start(); |
| 44 |
include YATRA_PLUGIN_PATH . 'templates/shortcodes/trip-category.php'; |
| 45 |
$html = ob_get_clean(); |
| 46 |
|
| 47 |
wp_send_json_success([ |
| 48 |
'html' => $html, |
| 49 |
'current_page' => $current_page, |
| 50 |
'max_pages' => $max_pages, |
| 51 |
'total_found' => $total_found, |
| 52 |
]); |
| 53 |
} catch (\Exception $e) { |
| 54 |
wp_send_json_error([ |
| 55 |
'message' => (defined('WP_DEBUG') && WP_DEBUG) |
| 56 |
? 'Error loading categories: ' . $e->getMessage() |
| 57 |
: __('Unable to load categories. Please try again.', 'yatra'), |
| 58 |
]); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|