PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.5
Yatra – Travel Booking & Tour Operator Software v3.0.5
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 / ReviewHooks.php

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

182 lines 6.6 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\Repositories\ReviewRepository;
8 use Yatra\Services\EnquiryService;
9
10 /**
11 * Review and Enquiry AJAX Hooks
12 *
13 * Handles AJAX requests for reviews and enquiries
14 */
15 class ReviewHooks
16 {
17 /**
18 * Initialize review and enquiry hooks
19 */
20 public static function init(): void
21 {
22 // AJAX handler for review submission
23 add_action('wp_ajax_yatra_submit_review', [self::class, 'handleReviewSubmission']);
24 add_action('wp_ajax_nopriv_yatra_submit_review', [self::class, 'handleReviewSubmissionNoPriv']);
25
26 // AJAX handler for enquiry submission (allow both logged in and guest users)
27 add_action('wp_ajax_yatra_submit_enquiry', [self::class, 'handleEnquirySubmission']);
28 add_action('wp_ajax_nopriv_yatra_submit_enquiry', [self::class, 'handleEnquirySubmission']);
29 }
30
31 /**
32 * Handle review submission (logged-in users)
33 */
34 public static function handleReviewSubmission(): void
35 {
36 // Verify nonce
37 if (!wp_verify_nonce($_POST['yatra_review_nonce'] ?? '', 'yatra_submit_review')) {
38 wp_send_json_error(['message' => __('Security check failed.', 'yatra')]);
39 }
40
41 // Check if user is logged in
42 if (!is_user_logged_in()) {
43 wp_send_json_error(['message' => __('You must be logged in to submit a review.', 'yatra')]);
44 }
45
46 $user_id = get_current_user_id();
47
48 // Validate required fields
49 $trip_id = (int) ($_POST['trip_id'] ?? 0);
50 $rating = (int) ($_POST['rating'] ?? 0);
51 $title = sanitize_text_field($_POST['title'] ?? '');
52 $content = sanitize_textarea_field($_POST['content'] ?? '');
53
54 // Validate trip ID
55 if ($trip_id <= 0) {
56 wp_send_json_error(['message' => __('Invalid trip.', 'yatra')]);
57 }
58
59 // Validate rating
60 if ($rating < 1 || $rating > 5) {
61 wp_send_json_error(['message' => __('Please provide a valid rating.', 'yatra')]);
62 }
63
64 // Validate title and content
65 if (empty($title) || empty($content)) {
66 wp_send_json_error(['message' => __('Please provide both title and content for your review.', 'yatra')]);
67 }
68
69 $reviewRepository = new ReviewRepository();
70
71 // Check if this is an edit or new review
72 $action_type = $_POST['action_type'] ?? 'create';
73 $review_id = (int) ($_POST['review_id'] ?? 0);
74
75 if ($action_type === 'edit' && $review_id > 0) {
76 // Editing existing review
77 $existing_review = yatra_get_user_review($trip_id, $user_id);
78
79 if (!$existing_review || (int) $existing_review->id !== $review_id) {
80 wp_send_json_error(['message' => __('Review not found or you do not have permission to edit it.', 'yatra')]);
81 }
82
83 $result = $reviewRepository->update($review_id, [
84 'rating' => $rating,
85 'title' => $title,
86 'content' => $content,
87 'updated_at' => current_time('mysql'),
88 ]);
89
90 if ($result) {
91 wp_send_json_success(['message' => __('Your review has been updated!', 'yatra')]);
92 } else {
93 wp_send_json_error(['message' => __('Failed to update review. Please try again.', 'yatra')]);
94 }
95 } else {
96 // Creating new review
97 if (!yatra_can_review($trip_id, $user_id)) {
98 wp_send_json_error(['message' => __('You cannot review this trip.', 'yatra')]);
99 }
100
101 // Get user info
102 $user = wp_get_current_user();
103
104 $result = $reviewRepository->create([
105 'trip_id' => $trip_id,
106 'user_id' => $user_id,
107 'rating' => $rating,
108 'title' => $title,
109 'content' => $content,
110 'author_name' => $user->display_name ?: $user->user_login,
111 'author_email' => $user->user_email,
112 'status' => \Yatra\Services\SettingsService::autoApproveReviews() ? 'approved' : 'pending',
113 'created_at' => current_time('mysql'),
114 'updated_at' => current_time('mysql'),
115 ]);
116
117 if ($result) {
118 wp_send_json_success(['message' => __('Your review has been submitted!', 'yatra')]);
119 } else {
120 wp_send_json_error(['message' => __('Failed to submit review. Please try again.', 'yatra')]);
121 }
122 }
123 }
124
125 /**
126 * Handle review submission (non-logged-in users)
127 */
128 public static function handleReviewSubmissionNoPriv(): void
129 {
130 wp_send_json_error(['message' => __('You must be logged in to submit a review.', 'yatra')]);
131 }
132
133 /**
134 * Handle enquiry submission
135 */
136 public static function handleEnquirySubmission(): void
137 {
138 // Verify nonce
139 if (!wp_verify_nonce($_POST['nonce'] ?? '', 'yatra_enquiry_nonce')) {
140 wp_send_json_error(['message' => __('Security check failed.', 'yatra')]);
141 }
142
143 // Validate required fields
144 $name = sanitize_text_field($_POST['name'] ?? '');
145 $email = sanitize_email($_POST['email'] ?? '');
146 $phone = sanitize_text_field($_POST['phone'] ?? '');
147 $message = sanitize_textarea_field($_POST['message'] ?? '');
148 $trip_id = (int) ($_POST['trip_id'] ?? 0);
149 $adults = (int) ($_POST['adults'] ?? 0);
150 $children = (int) ($_POST['children'] ?? 0);
151
152 if (empty($name) || empty($email) || empty($message)) {
153 wp_send_json_error(['message' => __('Please fill in all required fields.', 'yatra')]);
154 }
155
156 if (!is_email($email)) {
157 wp_send_json_error(['message' => __('Please provide a valid email address.', 'yatra')]);
158 }
159
160 if ($adults < 0 || $children < 0 || ($adults + $children) === 0) {
161 wp_send_json_error(['message' => __('Please specify at least one traveler.', 'yatra')]);
162 }
163
164 $enquiryService = new EnquiryService();
165 $result = $enquiryService->createEnquiry([
166 'trip_id' => $trip_id > 0 ? $trip_id : null,
167 'name' => $name,
168 'email' => $email,
169 'phone' => $phone ?: null,
170 'message' => $message,
171 'adults' => $adults,
172 'children' => $children,
173 ]);
174
175 if (!empty($result['success'])) {
176 wp_send_json_success(['message' => $result['message'] ?? __('Your enquiry has been submitted successfully!', 'yatra')]);
177 }
178
179 wp_send_json_error(['message' => $result['message'] ?? __('Failed to submit enquiry. Please try again.', 'yatra')]);
180 }
181 }
182