| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Shortcodes; |
| 6 |
|
| 7 |
/** |
| 8 |
* Search Shortcode |
| 9 |
* |
| 10 |
* Displays advanced tour search functionality |
| 11 |
*/ |
| 12 |
class SearchShortcode extends BaseShortcode |
| 13 |
{ |
| 14 |
private static bool $tripSearchAssetsQueued = false; |
| 15 |
|
| 16 |
public function __construct() |
| 17 |
{ |
| 18 |
parent::__construct('yatra_search', [ |
| 19 |
'show_filters' => 'yes', |
| 20 |
'show_categories' => 'yes', |
| 21 |
'show_destinations' => 'yes', |
| 22 |
'show_activities' => 'yes', |
| 23 |
'show_price_range' => 'yes', |
| 24 |
'show_duration' => 'yes', |
| 25 |
'show_difficulty' => 'yes', |
| 26 |
'placeholder' => 'Search for tours...', |
| 27 |
'button_text' => 'Search Tours' |
| 28 |
]); |
| 29 |
|
| 30 |
// [yatra_search] may appear in posts, widgets, builders, FSE templates, or do_shortcode() |
| 31 |
// with no reliable way to detect that before output. Enqueue on every public frontend |
| 32 |
// request so styles always print in wp_head (avoids FOUC). Rules are scoped to |
| 33 |
// .yatra-trip-search-shortcode in trip-search-shortcode.css. |
| 34 |
add_action('wp_enqueue_scripts', [self::class, 'enqueueTripSearchAssets'], 10); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Enqueue trip search bar CSS/JS for head output. Idempotent per request. |
| 39 |
*/ |
| 40 |
public static function enqueueTripSearchAssets(): void |
| 41 |
{ |
| 42 |
if (is_admin() || is_feed()) { |
| 43 |
return; |
| 44 |
} |
| 45 |
if (self::$tripSearchAssetsQueued) { |
| 46 |
return; |
| 47 |
} |
| 48 |
self::$tripSearchAssetsQueued = true; |
| 49 |
|
| 50 |
$tripSearchCssPath = YATRA_PLUGIN_PATH . 'assets/css/shortcodes/trip-search-shortcode.css'; |
| 51 |
$tripSearchCssVer = is_readable($tripSearchCssPath) |
| 52 |
? (string) filemtime($tripSearchCssPath) |
| 53 |
: YATRA_VERSION; |
| 54 |
wp_enqueue_style( |
| 55 |
'yatra-trip-search-shortcode', |
| 56 |
YATRA_PLUGIN_URL . 'assets/css/shortcodes/trip-search-shortcode.css', |
| 57 |
[], |
| 58 |
$tripSearchCssVer |
| 59 |
); |
| 60 |
|
| 61 |
$tripSearchJsPath = YATRA_PLUGIN_PATH . 'assets/js/trip-search-shortcode.js'; |
| 62 |
$tripSearchJsVer = is_readable($tripSearchJsPath) |
| 63 |
? (string) filemtime($tripSearchJsPath) |
| 64 |
: YATRA_VERSION; |
| 65 |
wp_enqueue_script( |
| 66 |
'yatra-trip-search-shortcode', |
| 67 |
YATRA_PLUGIN_URL . 'assets/js/trip-search-shortcode.js', |
| 68 |
['jquery'], |
| 69 |
$tripSearchJsVer, |
| 70 |
true |
| 71 |
); |
| 72 |
|
| 73 |
$listing_url = \function_exists('yatra_get_trip_listing_url') |
| 74 |
? \yatra_get_trip_listing_url() |
| 75 |
: \trailingslashit(\home_url('/trip/')); |
| 76 |
|
| 77 |
wp_localize_script( |
| 78 |
'yatra-trip-search-shortcode', |
| 79 |
'yatraTripSearchConfig', |
| 80 |
[ |
| 81 |
'listingUrl' => $listing_url, |
| 82 |
] |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Render the search shortcode content |
| 88 |
*/ |
| 89 |
protected function renderContent(array $atts): string |
| 90 |
{ |
| 91 |
$atts = shortcode_atts($this->default_attributes, $atts, $this->tag); |
| 92 |
|
| 93 |
// Same source as trip listing sidebar filters: published classifications. |
| 94 |
// TripRepository::*ForSearch() only returned rows linked via trip_classifications, |
| 95 |
// so empty relations hid all dropdown options. |
| 96 |
$destinationRepository = new \Yatra\Repositories\DestinationRepository(); |
| 97 |
$activityRepository = new \Yatra\Repositories\ActivityRepository(); |
| 98 |
$destinations = $destinationRepository->getPublished(); |
| 99 |
$activities = $activityRepository->getPublished(); |
| 100 |
$tripListingService = new \Yatra\Services\TripListingService(); |
| 101 |
$tripRepository = new \Yatra\Repositories\TripRepository(); |
| 102 |
|
| 103 |
$listing_url = \function_exists('yatra_get_trip_listing_url') |
| 104 |
? \yatra_get_trip_listing_url() |
| 105 |
: \trailingslashit(\home_url('/trip/')); |
| 106 |
|
| 107 |
return $this->loadTemplate('shortcodes/trip-search.php', [ |
| 108 |
'atts' => $atts, |
| 109 |
'destinations' => $destinations, |
| 110 |
'activities' => $activities, |
| 111 |
'listing_url' => $listing_url, |
| 112 |
'duration_bounds' => $tripRepository->getDurationDaysBounds(), |
| 113 |
'budget_presets' => $tripListingService->getSearchBudgetPresets(), |
| 114 |
]); |
| 115 |
} |
| 116 |
} |
| 117 |
|