| 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 |
|