| 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 |
// reCAPTCHA v3 (no-op unless the enquiry form is protected in settings). |
| 144 |
$recaptcha = \Yatra\Services\RecaptchaService::verifyForm( |
| 145 |
'enquiry', |
| 146 |
(string) ($_POST['recaptcha_token'] ?? ''), |
| 147 |
$_SERVER['REMOTE_ADDR'] ?? null |
| 148 |
); |
| 149 |
if (empty($recaptcha['success'])) { |
| 150 |
wp_send_json_error(['message' => $recaptcha['message'] ?? __('reCAPTCHA verification failed.', 'yatra')]); |
| 151 |
} |
| 152 |
|
| 153 |
// Validate required fields |
| 154 |
$name = sanitize_text_field($_POST['name'] ?? ''); |
| 155 |
$email = sanitize_email($_POST['email'] ?? ''); |
| 156 |
$phone = sanitize_text_field($_POST['phone'] ?? ''); |
| 157 |
$message = sanitize_textarea_field($_POST['message'] ?? ''); |
| 158 |
$trip_id = (int) ($_POST['trip_id'] ?? 0); |
| 159 |
$adults = (int) ($_POST['adults'] ?? 0); |
| 160 |
$children = (int) ($_POST['children'] ?? 0); |
| 161 |
|
| 162 |
if (empty($name) || empty($email) || empty($message)) { |
| 163 |
wp_send_json_error(['message' => __('Please fill in all required fields.', 'yatra')]); |
| 164 |
} |
| 165 |
|
| 166 |
if (!is_email($email)) { |
| 167 |
wp_send_json_error(['message' => __('Please provide a valid email address.', 'yatra')]); |
| 168 |
} |
| 169 |
|
| 170 |
if ($adults < 0 || $children < 0 || ($adults + $children) === 0) { |
| 171 |
wp_send_json_error(['message' => __('Please specify at least one traveler.', 'yatra')]); |
| 172 |
} |
| 173 |
|
| 174 |
$enquiryService = new EnquiryService(); |
| 175 |
$result = $enquiryService->createEnquiry([ |
| 176 |
'trip_id' => $trip_id > 0 ? $trip_id : null, |
| 177 |
'name' => $name, |
| 178 |
'email' => $email, |
| 179 |
'phone' => $phone ?: null, |
| 180 |
'message' => $message, |
| 181 |
'adults' => $adults, |
| 182 |
'children' => $children, |
| 183 |
]); |
| 184 |
|
| 185 |
if (!empty($result['success'])) { |
| 186 |
wp_send_json_success(['message' => $result['message'] ?? __('Your enquiry has been submitted successfully!', 'yatra')]); |
| 187 |
} |
| 188 |
|
| 189 |
wp_send_json_error(['message' => $result['message'] ?? __('Failed to submit enquiry. Please try again.', 'yatra')]); |
| 190 |
} |
| 191 |
} |
| 192 |
|