| @@ -3,8 +3,11 @@ | ||
| 3 | 3 | declare(strict_types=1); |
| 4 | 4 | |
| 5 | 5 | namespace Yatra\Shortcodes; |
| 6 | 6 | |
| 7 | +use Yatra\Helpers\TripListingFilterBuilder; | |
| 8 | +use Yatra\Services\SettingsService; | |
| 9 | + | |
| 7 | 10 | /** |
| 8 | 11 | * Activity Shortcode |
| 9 | 12 | * |
| 10 | 13 | * Displays activity listings with associated trips using trip-listing-card.php template |
| @@ -20,10 +23,14 @@ | ||
| 20 | 23 | 'show_trip_count' => 'yes', |
| 21 | 24 | 'show_description' => 'yes', |
| 22 | 25 | 'show_image' => 'yes', |
| 23 | 26 | 'show_pagination' => 'yes', // Default to show pagination like trip shortcode |
| 24 | - 'activity' => '', // Specific activity slug(s), comma separated | |
| 25 | - 'hide_empty' => 'yes', | |
| 27 | + 'activity' => '', // Classification IDs, comma-separated | |
| 28 | + // hide_empty defaults to 'no' to preserve the historical | |
| 29 | + // behavior (show every activity, even ones with zero | |
| 30 | + // trips). Operators that prefer the empty-archive defense | |
| 31 | + // opt in with hide_empty="yes". | |
| 32 | + 'hide_empty' => 'no', | |
| 26 | 33 | 'title' => 'Activity Listings' |
| 27 | 34 | ]); |
| 28 | 35 | } |
| 29 | 36 | |
| @@ -53,14 +60,15 @@ | ||
| 53 | 60 | 'total_found' => $activities_data['total_found'] ?? 0, |
| 54 | 61 | 'per_page' => $per_page |
| 55 | 62 | ]; |
| 56 | 63 | |
| 57 | - // Enqueue shortcode-specific CSS | |
| 64 | + $activityCssPath = YATRA_PLUGIN_PATH . 'assets/css/shortcodes/activity-shortcode.css'; | |
| 65 | + $activityCssVer = is_readable($activityCssPath) ? YATRA_VERSION . '.' . filemtime($activityCssPath) : YATRA_VERSION; | |
| 58 | 66 | wp_enqueue_style( |
| 59 | 67 | 'yatra-activity-shortcode', |
| 60 | 68 | YATRA_PLUGIN_URL . 'assets/css/shortcodes/activity-shortcode.css', |
| 61 | - [], | |
| 62 | - YATRA_VERSION | |
| 69 | + \Yatra\Providers\FrontendAssetsProvider::shortcodeStyleDependencies(), | |
| 70 | + $activityCssVer | |
| 63 | 71 | ); |
| 64 | 72 | |
| 65 | 73 | // Enqueue shortcode-specific JavaScript |
| 66 | 74 | wp_enqueue_script( |
| @@ -106,12 +114,16 @@ | ||
| 106 | 114 | 'order_by' => 'name', |
| 107 | 115 | 'order' => $atts['order'] === 'asc' ? 'ASC' : 'DESC' |
| 108 | 116 | ]; |
| 109 | 117 | |
| 110 | - // Filter by specific activities if provided | |
| 111 | - if (!empty($atts['activity'])) { | |
| 112 | - $args['where']['slug'] = explode(',', $atts['activity']); | |
| 113 | - } | |
| 118 | + $args['where'] = $args['where'] ?? []; | |
| 119 | + TripListingFilterBuilder::applyTaxonomyWhere( | |
| 120 | + $args['where'], | |
| 121 | + $atts, | |
| 122 | + 'activityIds', | |
| 123 | + 'activity_ids', | |
| 124 | + 'activity' | |
| 125 | + ); | |
| 114 | 126 | |
| 115 | 127 | // Get total count for pagination |
| 116 | 128 | $count_args = $args; |
| 117 | 129 | unset($count_args['limit']); |
| @@ -125,12 +137,9 @@ | ||
| 125 | 137 | |
| 126 | 138 | $activities = []; |
| 127 | 139 | |
| 128 | 140 | foreach ($result as $activityData) { |
| 129 | - // Debug: Log each activity being processed | |
| 130 | - if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 131 | - error_log('Yatra ActivityShortcode Processing activity: ' . $activityData->name . ' (ID: ' . $activityData->id . ')'); | |
| 132 | - } | |
| 141 | + | |
| 133 | 142 | |
| 134 | 143 | // Get real trip data for this activity using classification tables |
| 135 | 144 | global $wpdb; |
| 136 | 145 | |
| @@ -185,20 +194,14 @@ | ||
| 185 | 194 | WHERE trip_id IN ({$placeholders}) |
| 186 | 195 | AND status = 'approved'", |
| 187 | 196 | ...$trip_ids |
| 188 | 197 | )); |
| 198 | + | |
| 189 | 199 | |
| 190 | - if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 191 | - error_log('ACTIVITY REVIEWS QUERY: ' . print_r($reviews, true)); | |
| 192 | - } | |
| 193 | - | |
| 194 | 200 | foreach ($reviews as $review) { |
| 195 | 201 | $total_rating_sum += $review->rating * $review->review_count; |
| 196 | 202 | $total_review_count += $review->review_count; |
| 197 | - | |
| 198 | - if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 199 | - error_log('ACTIVITY RATING CALCULATION: Added ' . $review->rating . ' * ' . $review->review_count . ' = ' . ($review->rating * $review->review_count)); | |
| 200 | - } | |
| 203 | + | |
| 201 | 204 | } |
| 202 | 205 | } |
| 203 | 206 | |
| 204 | 207 | // Calculate average rating only for trips that actually have reviews |
| @@ -259,12 +262,25 @@ | ||
| 259 | 262 | 'difficulty' => !empty($difficulties) ? $this->getMostCommonDifficulty($difficulties) : null |
| 260 | 263 | ]; |
| 261 | 264 | } |
| 262 | 265 | |
| 263 | - // Filter out empty activities if requested | |
| 266 | + // Filter out activities that have no published trips. | |
| 267 | + // | |
| 268 | + // Previously this only checked term name/slug presence — | |
| 269 | + // which never actually fires because all valid terms have | |
| 270 | + // both. As a result the shortcode would render activity | |
| 271 | + // cards with "0 trips" badges, leading users to click | |
| 272 | + // into empty archive pages. With hide_empty=yes we now | |
| 273 | + // drop any activity whose trip_count (computed above | |
| 274 | + // from TripClassificationsTable JOIN TripsTable WHERE | |
| 275 | + // status=publish) is zero. Term-metadata sanity is also | |
| 276 | + // preserved as a secondary safety check so we don't | |
| 277 | + // render orphan terms. | |
| 264 | 278 | if ($atts['hide_empty'] === 'yes') { |
| 265 | - $activities = array_filter($activities, function($activity) { | |
| 266 | - return !empty($activity['term']->name) && !empty($activity['term']->slug); | |
| 279 | + $activities = array_filter($activities, static function ($activity) { | |
| 280 | + return (int) ($activity['trip_count'] ?? 0) > 0 | |
| 281 | + && !empty($activity['term']->name) | |
| 282 | + && !empty($activity['term']->slug); | |
| 267 | 283 | }); |
| 268 | 284 | } |
| 269 | 285 | |
| 270 | 286 | // Calculate pagination data |
| @@ -280,11 +296,9 @@ | ||
| 280 | 296 | 'per_page' => $per_page |
| 281 | 297 | ]; |
| 282 | 298 | |
| 283 | 299 | } catch (\Exception $e) { |
| 284 | - if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 285 | - error_log('Yatra ActivityShortcode Error: ' . $e->getMessage()); | |
| 286 | - } | |
| 300 | + | |
| 287 | 301 | return []; |
| 288 | 302 | } |
| 289 | 303 | } |
| 290 | 304 | |
| @@ -347,12 +361,9 @@ | ||
| 347 | 361 | |
| 348 | 362 | // Fallback to placeholder |
| 349 | 363 | $fallback_url = YATRA_PLUGIN_URL . 'assets/images/placeholder.png'; |
| 350 | 364 | |
| 351 | - // Debug: Log the image URL being used | |
| 352 | - if (defined('WP_DEBUG') && WP_DEBUG) { | |
| 353 | - error_log('Yatra ActivityShortcode Using fallback image: ' . $fallback_url); | |
| 354 | - } | |
| 365 | + | |
| 355 | 366 | |
| 356 | 367 | return $fallback_url; |
| 357 | 368 | } |
| 358 | 369 | |
| @@ -360,11 +371,16 @@ | ||
| 360 | 371 | * Get activity link |
| 361 | 372 | */ |
| 362 | 373 | private function getActivityLink($activity): string |
| 363 | 374 | { |
| 364 | - // Try to get permalink from activity service or construct it | |
| 365 | 375 | if (isset($activity->slug)) { |
| 366 | - return home_url("/activity/{$activity->slug}/"); | |
| 376 | + // Use permalink helper so global base + plain permalinks are respected | |
| 377 | + if (function_exists('yatra_get_activity_permalink')) { | |
| 378 | + return yatra_get_activity_permalink($activity); | |
| 379 | + } | |
| 380 | + | |
| 381 | + $base = SettingsService::getActivityBase(); | |
| 382 | + return home_url('/' . $base . '/' . $activity->slug . '/'); | |
| 367 | 383 | } |
| 368 | 384 | |
| 369 | 385 | return '#'; // Fallback |
| 370 | 386 | } |