PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.3
Yatra – Travel Booking & Tour Operator Software v3.0.3
3.0.16 3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 All 84 releases
yatra / app / Hooks / CacheHooks.php

CacheHooks.php in Yatra – Travel Booking & Tour Operator Software 3.0.3, at app/Hooks/CacheHooks.php

353 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Hooks;
6
7 use Yatra\Services\CacheService;
8 use Yatra\Utils\Cache;
9 use Yatra\Utils\Logger;
10
11 /**
12 * Cache Invalidation Hooks
13 *
14 * Automatically invalidate caches when data changes
15 */
16 class CacheHooks
17 {
18 /**
19 * Initialize cache hooks
20 */
21 public static function init(): void
22 {
23 // Trip-related hooks
24 add_action('yatra_trip_created', [self::class, 'onTripCreated'], 10, 1);
25 add_action('yatra_trip_updated', [self::class, 'onTripUpdated'], 10, 1);
26 add_action('yatra_trip_deleted', [self::class, 'onTripDeleted'], 10, 1);
27
28 // Booking-related hooks
29 add_action('yatra_booking_created', [self::class, 'onBookingCreated'], 10, 1);
30 add_action('yatra_booking_updated', [self::class, 'onBookingUpdated'], 10, 1);
31 add_action('yatra_booking_deleted', [self::class, 'onBookingDeleted'], 10, 1);
32 add_action('yatra_booking_status_changed', [self::class, 'onBookingStatusChanged'], 10, 3);
33
34 // Customer-related hooks
35 add_action('yatra_customer_created', [self::class, 'onCustomerCreated'], 10, 1);
36 add_action('yatra_customer_updated', [self::class, 'onCustomerUpdated'], 10, 1);
37 add_action('yatra_customer_deleted', [self::class, 'onCustomerDeleted'], 10, 1);
38
39 // Activity-related hooks
40 add_action('yatra_activity_created', [self::class, 'onActivityCreated'], 10, 1);
41 add_action('yatra_activity_updated', [self::class, 'onActivityUpdated'], 10, 1);
42 add_action('yatra_activity_deleted', [self::class, 'onActivityDeleted'], 10, 1);
43
44 // Itinerary-specific hooks
45 add_action('yatra_itinerary_day_created', [self::class, 'onItineraryDayCreated'], 10, 2);
46 add_action('yatra_itinerary_day_updated', [self::class, 'onItineraryDayUpdated'], 10, 2);
47 add_action('yatra_itinerary_day_deleted', [self::class, 'onItineraryDayDeleted'], 10, 1);
48 add_action('yatra_itinerary_activity_created', [self::class, 'onItineraryActivityCreated'], 10, 2);
49 add_action('yatra_itinerary_activity_updated', [self::class, 'onItineraryActivityUpdated'], 10, 2);
50 add_action('yatra_itinerary_activity_deleted', [self::class, 'onItineraryActivityDeleted'], 10, 1);
51
52 // Destination-related hooks
53 add_action('yatra_destination_created', [self::class, 'onDestinationCreated'], 10, 1);
54 add_action('yatra_destination_updated', [self::class, 'onDestinationUpdated'], 10, 1);
55 add_action('yatra_destination_deleted', [self::class, 'onDestinationDeleted'], 10, 1);
56
57 // Payment-related hooks
58 add_action('yatra_payment_created', [self::class, 'onPaymentCreated'], 10, 1);
59 add_action('yatra_payment_updated', [self::class, 'onPaymentUpdated'], 10, 1);
60
61 // General cache management hooks
62 add_action('yatra_cache_warm_up', [self::class, 'warmUpCache']);
63 add_action('yatra_cache_clear_all', [self::class, 'clearAllCaches']);
64
65 // WordPress hooks for related data changes
66 add_action('wp_insert_post', [self::class, 'onPostChange'], 10, 2);
67 add_action('wp_update_post', [self::class, 'onPostChange'], 10, 2);
68 add_action('wp_delete_post', [self::class, 'onPostDeleted'], 10, 1);
69
70 // Schedule cache cleanup
71 if (!wp_next_scheduled('yatra_cache_cleanup')) {
72 wp_schedule_event(time(), 'daily', 'yatra_cache_cleanup');
73 }
74 add_action('yatra_cache_cleanup', [self::class, 'cleanupExpiredCache']);
75 }
76
77 /**
78 * Trip event handlers
79 */
80 public static function onTripCreated(int $tripId): void
81 {
82 Cache::invalidateAfterTripWrite('create', $tripId);
83 Logger::info("Cache invalidated for trip creation", ['trip_id' => $tripId]);
84 }
85
86 public static function onTripUpdated(int $tripId): void
87 {
88 Cache::invalidateAfterTripWrite('update', $tripId);
89 Logger::info("Cache invalidated for trip update", ['trip_id' => $tripId]);
90 }
91
92 public static function onTripDeleted(int $tripId): void
93 {
94 Cache::invalidateAfterTripWrite('delete', $tripId);
95 Logger::info("Cache invalidated for trip deletion", ['trip_id' => $tripId]);
96 }
97
98 /**
99 * Booking event handlers
100 */
101 public static function onBookingCreated(int $bookingId): void
102 {
103 Cache::invalidateAfterBookingWrite($bookingId);
104 Logger::info("Cache invalidated for booking creation", ['booking_id' => $bookingId]);
105 }
106
107 public static function onBookingUpdated(int $bookingId): void
108 {
109 Cache::invalidateAfterBookingWrite($bookingId);
110 Logger::info("Cache invalidated for booking update", ['booking_id' => $bookingId]);
111 }
112
113 public static function onBookingDeleted(int $bookingId): void
114 {
115 Cache::invalidateAfterBookingWrite($bookingId);
116 Logger::info("Cache invalidated for booking deletion", ['booking_id' => $bookingId]);
117 }
118
119 public static function onBookingStatusChanged(int $bookingId, string $oldStatus, string $newStatus): void
120 {
121 Cache::invalidateAfterBookingWrite($bookingId);
122 Logger::info("Cache invalidated for booking status change", [
123 'booking_id' => $bookingId,
124 'old_status' => $oldStatus,
125 'new_status' => $newStatus,
126 ]);
127 }
128
129 /**
130 * Customer event handlers
131 */
132 public static function onCustomerCreated(int $customerId): void
133 {
134 self::invalidateStatsCaches();
135 Logger::info("Cache invalidated for customer creation", ['customer_id' => $customerId]);
136 }
137
138 public static function onCustomerUpdated(int $customerId): void
139 {
140 CacheService::invalidateEntity('customer', $customerId);
141 Logger::info("Cache invalidated for customer update", ['customer_id' => $customerId]);
142 }
143
144 public static function onCustomerDeleted(int $customerId): void
145 {
146 CacheService::invalidateEntity('customer', $customerId);
147 self::invalidateStatsCaches();
148 Logger::info("Cache invalidated for customer deletion", ['customer_id' => $customerId]);
149 }
150
151 /**
152 * Activity event handlers
153 */
154 public static function onActivityCreated(int $activityId): void
155 {
156 self::invalidateListingCaches();
157 Logger::info("Cache invalidated for activity creation", ['activity_id' => $activityId]);
158 }
159
160 public static function onActivityUpdated(int $activityId): void
161 {
162 CacheService::invalidateEntity('activity', $activityId);
163 self::invalidateListingCaches();
164 Logger::info("Cache invalidated for activity update", ['activity_id' => $activityId]);
165 }
166
167 public static function onActivityDeleted(int $activityId): void
168 {
169 CacheService::invalidateEntity('activity', $activityId);
170 self::invalidateListingCaches();
171 Logger::info("Cache invalidated for activity deletion", ['activity_id' => $activityId]);
172 }
173
174 /**
175 * Destination event handlers
176 */
177 public static function onDestinationCreated(int $destinationId): void
178 {
179 self::invalidateListingCaches();
180 Logger::info("Cache invalidated for destination creation", ['destination_id' => $destinationId]);
181 }
182
183 public static function onDestinationUpdated(int $destinationId): void
184 {
185 CacheService::invalidateEntity('destination', $destinationId);
186 self::invalidateListingCaches();
187 Logger::info("Cache invalidated for destination update", ['destination_id' => $destinationId]);
188 }
189
190 public static function onDestinationDeleted(int $destinationId): void
191 {
192 CacheService::invalidateEntity('destination', $destinationId);
193 self::invalidateListingCaches();
194 Logger::info("Cache invalidated for destination deletion", ['destination_id' => $destinationId]);
195 }
196
197 /**
198 * Payment event handlers
199 */
200 public static function onPaymentCreated(int $paymentId): void
201 {
202 self::invalidateStatsCaches();
203 Logger::info("Cache invalidated for payment creation", ['payment_id' => $paymentId]);
204 }
205
206 public static function onPaymentUpdated(int $paymentId): void
207 {
208 self::invalidateStatsCaches();
209 Logger::info("Cache invalidated for payment update", ['payment_id' => $paymentId]);
210 }
211
212 /**
213 * WordPress post change handler
214 */
215 public static function onPostChange(int $postId, \WP_Post $post): void
216 {
217 // Only handle Yatra-related post types
218 if (in_array($post->post_type, ['yatra_trip', 'yatra_booking', 'yatra_customer'])) {
219 self::invalidateListingCaches();
220 self::invalidateStatsCaches();
221 Logger::debug("Cache invalidated for WordPress post change", [
222 'post_id' => $postId,
223 'post_type' => $post->post_type
224 ]);
225 }
226 }
227
228 /**
229 * WordPress post deletion handler
230 */
231 public static function onPostDeleted(int $postId): void
232 {
233 // Clear all caches as we don't know the post type
234 self::invalidateListingCaches();
235 self::invalidateStatsCaches();
236 Logger::debug("Cache invalidated for WordPress post deletion", ['post_id' => $postId]);
237 }
238
239 /**
240 * Cache management handlers
241 */
242 public static function warmUpCache(): void
243 {
244 CacheService::warmUpCache();
245 Logger::info("Cache warm-up completed");
246 }
247
248 public static function clearAllCaches(): void
249 {
250 Cache::flush();
251 Logger::info("All caches cleared via hook");
252 }
253
254 /**
255 * Clean up expired cache entries
256 */
257 public static function cleanupExpiredCache(): void
258 {
259 // This would be implemented based on the cache backend
260 // For now, we'll just log the cleanup attempt
261 Logger::info("Cache cleanup scheduled task executed");
262
263 // Clear old log files
264 Logger::cleanup();
265 }
266
267 /**
268 * Helper methods for cache invalidation
269 */
270 private static function invalidateListingCaches(): void
271 {
272 Cache::invalidateListingCaches();
273 }
274
275 private static function invalidateStatsCaches(): void
276 {
277 Cache::clearByPrefix(Cache::PREFIX_STATS);
278 Cache::invalidateDashboardReportCaches();
279 }
280
281 /**
282 * Manual cache invalidation for admin use
283 */
284 public static function invalidateAll(): void
285 {
286 Cache::flush();
287 Logger::info("Manual cache invalidation triggered");
288 }
289
290 /**
291 * Get cache status for admin dashboard
292 */
293 public static function getCacheStatus(): array
294 {
295 return [
296 'enabled' => true,
297 'backends' => Cache::getAvailableBackends(),
298 'stats' => Cache::getCacheStats(),
299 'performance' => CacheService::getPerformanceMetrics(),
300 ];
301 }
302
303 /**
304 * Itinerary Day event handlers
305 */
306 public static function onItineraryDayCreated(int $dayId, array $data): void
307 {
308 self::invalidateListingCaches();
309 Cache::clearByPrefix('yatra_trip_');
310 Logger::info("Cache invalidated for itinerary day creation", ['day_id' => $dayId]);
311 }
312
313 public static function onItineraryDayUpdated(int $dayId, array $data): void
314 {
315 Cache::delete(Cache::PREFIX_TRIP_DATA . ($data['trip_id'] ?? ''));
316 self::invalidateListingCaches();
317 Cache::clearByPrefix('yatra_trip_');
318 Logger::info("Cache invalidated for itinerary day update", ['day_id' => $dayId]);
319 }
320
321 public static function onItineraryDayDeleted(int $dayId): void
322 {
323 self::invalidateListingCaches();
324 Cache::clearByPrefix('yatra_trip_');
325 Logger::info("Cache invalidated for itinerary day deletion", ['day_id' => $dayId]);
326 }
327
328 /**
329 * Itinerary Activity event handlers
330 */
331 public static function onItineraryActivityCreated(int $activityId, array $data): void
332 {
333 self::invalidateListingCaches();
334 Cache::clearByPrefix('yatra_trip_');
335 Logger::info("Cache invalidated for itinerary activity creation", ['activity_id' => $activityId]);
336 }
337
338 public static function onItineraryActivityUpdated(int $activityId, array $data): void
339 {
340 Cache::delete(Cache::PREFIX_TRIP_DATA . ($data['trip_id'] ?? ''));
341 self::invalidateListingCaches();
342 Cache::clearByPrefix('yatra_trip_');
343 Logger::info("Cache invalidated for itinerary activity update", ['activity_id' => $activityId]);
344 }
345
346 public static function onItineraryActivityDeleted(int $activityId): void
347 {
348 self::invalidateListingCaches();
349 Cache::clearByPrefix('yatra_trip_');
350 Logger::info("Cache invalidated for itinerary activity deletion", ['activity_id' => $activityId]);
351 }
352 }
353