| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Core\Handlers; |
| 6 |
|
| 7 |
use Yatra\Core\Assets\ListingAssetManager; |
| 8 |
|
| 9 |
/** |
| 10 |
* Listing Page Handler |
| 11 |
* |
| 12 |
* Handles listing page requests (destinations, activities, categories) |
| 13 |
*/ |
| 14 |
class ListingPageHandler extends BasePageHandler |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Handle listing page request |
| 18 |
* |
| 19 |
* @param array $route_data Route data from RouteMatcher |
| 20 |
* @return bool True if handled successfully |
| 21 |
*/ |
| 22 |
public function handle(array $route_data): bool |
| 23 |
{ |
| 24 |
|
| 25 |
$listing_type = $route_data['listing_type']; |
| 26 |
$base = $route_data['base']; |
| 27 |
|
| 28 |
// Determine template based on listing type |
| 29 |
$template_map = [ |
| 30 |
'trip' => 'listing-trip', |
| 31 |
'destination' => 'listing-destination', |
| 32 |
'activity' => 'listing-activity', |
| 33 |
'category' => 'listing-trip-category', |
| 34 |
]; |
| 35 |
|
| 36 |
if (!isset($template_map[$listing_type])) { |
| 37 |
$this->logError("Unknown listing type: {$listing_type}"); |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
$template = $template_map[$listing_type]; |
| 42 |
|
| 43 |
// Configure $wp_query + virtual WP_Post so FSE block themes resolve an archive |
| 44 |
// template (not 404.html) and render the site header normally. |
| 45 |
$this->setupPageEnvironment('archive', [ |
| 46 |
'title' => ucfirst($listing_type) . ' ' . __('Listing', 'yatra'), |
| 47 |
'post_type' => 'page', |
| 48 |
'post_name' => $base, |
| 49 |
]); |
| 50 |
|
| 51 |
// Set up query vars for backward compatibility |
| 52 |
$this->setQueryVars([ |
| 53 |
'yatra_page' => $base, |
| 54 |
]); |
| 55 |
|
| 56 |
// Load trip data for trip listing pages |
| 57 |
if ($listing_type === 'trip') { |
| 58 |
$tripListingService = new \Yatra\Services\TripListingService(); |
| 59 |
|
| 60 |
// Pagination: route (pretty /trip/page/N/), query var, then ?paged= / ?page= |
| 61 |
$page = max(1, (int) ($route_data['paged'] ?? (get_query_var('paged') ?: ($_GET['paged'] ?? $_GET['page'] ?? 1)))); |
| 62 |
|
| 63 |
$requestParams = [ |
| 64 |
'page' => $page, |
| 65 |
'per_page' => \yatra_get_posts_per_page(), |
| 66 |
's' => isset($_GET['s']) ? (string) wp_unslash($_GET['s']) : '', |
| 67 |
'destination' => $_GET['destination'] ?? '', |
| 68 |
'activity' => $_GET['activity'] ?? '', |
| 69 |
'trip_category' => $_GET['trip_category'] ?? ($_GET['category'] ?? ''), |
| 70 |
'price_min' => $_GET['price_min'] ?? '', |
| 71 |
'price_max' => $_GET['price_max'] ?? '', |
| 72 |
'budget' => $_GET['budget'] ?? '', |
| 73 |
'duration_min' => $_GET['duration_min'] ?? '', |
| 74 |
'duration_max' => $_GET['duration_max'] ?? '', |
| 75 |
'duration' => $_GET['duration'] ?? '', |
| 76 |
'rating_min' => $_GET['rating_min'] ?? '', |
| 77 |
'rating' => $_GET['rating'] ?? [], |
| 78 |
'difficulty' => $_GET['difficulty'] ?? [], |
| 79 |
'categories' => $_GET['categories'] ?? [], |
| 80 |
'destinations' => $_GET['destinations'] ?? [], |
| 81 |
'activities' => $_GET['activities'] ?? [], |
| 82 |
'trip_type' => $_GET['trip_type'] ?? '', |
| 83 |
'special_offers' => $_GET['offers'] ?? ($_GET['special_offers'] ?? []), |
| 84 |
'booking_options' => $_GET['booking'] ?? ($_GET['booking_options'] ?? []), |
| 85 |
'age_suitability' => $_GET['age'] ?? ($_GET['age_suitability'] ?? []), |
| 86 |
'accommodation' => $_GET['accommodation'] ?? [], |
| 87 |
'included_services' => $_GET['services'] ?? ($_GET['included_services'] ?? []), |
| 88 |
'sort' => $_GET['sort'] ?? '', |
| 89 |
'attributes' => (isset($_GET['attributes']) && is_array($_GET['attributes'])) ? wp_unslash($_GET['attributes']) : [], |
| 90 |
]; |
| 91 |
|
| 92 |
// Get filtered trips |
| 93 |
$tripData = $tripListingService->getFilteredTrips($requestParams); |
| 94 |
|
| 95 |
$GLOBALS['yatra_trip_list'] = $tripData; |
| 96 |
$GLOBALS['yatra_trip_listing_context'] = (new \Yatra\Services\TripListingPageContextFactory())->buildForTripArchive( |
| 97 |
$tripData, |
| 98 |
(string) ($_SERVER['REQUEST_URI'] ?? ''), |
| 99 |
$GLOBALS['yatra_taxonomy_context'] ?? null, |
| 100 |
$tripListingService->getFilterData() |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
// SEO meta, canonical, and JSON-LD are handled by {@see \Yatra\Managers\SEOManager} + {@see \Yatra\Services\SEOService}. |
| 105 |
|
| 106 |
return $this->selectTemplate($template, new ListingAssetManager($listing_type), 'listing-' . $listing_type); |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|