PluginProbe
Yatra – Travel Booking & Tour Operator Software / trunk
Yatra – Travel Booking & Tour Operator Software vtrunk
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 2.0.10 2.0.11 All 82 releases
yatra / app / Core / Handlers / AccountPageHandler.php

AccountPageHandler.php in Yatra – Travel Booking & Tour Operator Software trunk, at app/Core/Handlers/AccountPageHandler.php

135 lines 4.0 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\Core\Handlers;
6
7 use Yatra\Core\Routing\UrlParser;
8 use Yatra\Services\SettingsService;
9
10 /**
11 * Account Page Handler
12 *
13 * Handles account page requests
14 */
15 class AccountPageHandler extends BasePageHandler
16 {
17 /**
18 * Handle account page request
19 *
20 * @param array $route_data Route data from RouteMatcher
21 * @return bool True if handled successfully
22 */
23 public function handle(array $route_data): bool
24 {
25 $page = (string) ($route_data['page'] ?? 'dashboard');
26 $base = (string) ($route_data['base'] ?? SettingsService::getAccountBase());
27
28 // Email-change confirmation (WordPress core pattern). The emailed link
29 // lands here as a normal front-end request, so the WordPress auth cookie
30 // identifies the customer — unlike a REST GET, which carries no nonce and
31 // would be treated as anonymous. Handled before anything else so the
32 // token is consumed and we redirect away cleanly.
33 $emailToken = isset($_GET['yatra_email_token'])
34 ? sanitize_text_field(wp_unslash((string) $_GET['yatra_email_token']))
35 : '';
36 if ($emailToken !== '') {
37 $this->confirmEmailChange($emailToken, $base); // always redirects + exits
38 }
39
40 if (!$this->isValidAccountPage($page)) {
41 return false;
42 }
43
44 $tab = $this->accountPageToReactTab($page);
45 $path = UrlParser::getCleanRequestPath();
46 if ($path !== $base && str_starts_with($path, $base . '/')) {
47 wp_safe_redirect(add_query_arg('tab', $tab, home_url('/' . $base . '/')));
48 exit;
49 }
50
51 $currentTab = isset($_GET['tab']) ? sanitize_key((string) $_GET['tab']) : '';
52 if ($currentTab !== '' && $currentTab !== $tab) {
53 wp_safe_redirect(add_query_arg('tab', $tab));
54 exit;
55 }
56
57 $this->setupPageEnvironment('singular', [
58 'title' => __('My Account', 'yatra'),
59 'post_type' => 'page',
60 'post_name' => $base,
61 ]);
62
63 $this->setQueryVars([
64 'yatra_account_page' => $page,
65 ]);
66
67 $GLOBALS['yatra_loading_react_account_page'] = true;
68
69 return $this->selectTemplate('account-page', null, 'account');
70 }
71
72 /**
73 * Confirm a pending account email change from the emailed link, then redirect
74 * back to the Profile tab with a success/error flag. Mirrors WordPress core's
75 * confirmation step (logged-out visitors are bounced through login and returned
76 * here to finish). Always redirects and exits.
77 */
78 private function confirmEmailChange(string $token, string $base): void
79 {
80 $accountUrl = home_url('/' . trailingslashit($base));
81
82 if (!is_user_logged_in()) {
83 $returnUrl = add_query_arg('yatra_email_token', rawurlencode($token), $accountUrl);
84 wp_safe_redirect(wp_login_url($returnUrl));
85 exit;
86 }
87
88 $result = (new \Yatra\Services\CustomerService())
89 ->confirmEmailChange(get_current_user_id(), $token);
90
91 wp_safe_redirect(add_query_arg(
92 [
93 'tab' => 'profile',
94 'email_change' => empty($result['success']) ? 'error' : 'success',
95 ],
96 $accountUrl
97 ));
98 exit;
99 }
100
101 /**
102 * @return list<string>
103 */
104 private function validAccountPages(): array
105 {
106 return [
107 'dashboard',
108 'profile',
109 'bookings',
110 'payments',
111 'documents',
112 'saved-trips',
113 'wishlist',
114 'settings',
115 ];
116 }
117
118 private function isValidAccountPage(string $page): bool
119 {
120 return in_array($page, $this->validAccountPages(), true);
121 }
122
123 private function accountPageToReactTab(string $page): string
124 {
125 switch ($page) {
126 case 'wishlist':
127 return 'saved-trips';
128 case 'settings':
129 return 'profile';
130 default:
131 return $page;
132 }
133 }
134 }
135