| 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 |
if (!$this->isValidAccountPage($page)) { |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
$tab = $this->accountPageToReactTab($page); |
| 33 |
$path = UrlParser::getCleanRequestPath(); |
| 34 |
if ($path !== $base && str_starts_with($path, $base . '/')) { |
| 35 |
wp_safe_redirect(add_query_arg('tab', $tab, home_url('/' . $base . '/'))); |
| 36 |
exit; |
| 37 |
} |
| 38 |
|
| 39 |
$currentTab = isset($_GET['tab']) ? sanitize_key((string) $_GET['tab']) : ''; |
| 40 |
if ($currentTab !== '' && $currentTab !== $tab) { |
| 41 |
wp_safe_redirect(add_query_arg('tab', $tab)); |
| 42 |
exit; |
| 43 |
} |
| 44 |
|
| 45 |
$this->prevent404(); |
| 46 |
|
| 47 |
$this->setQueryVars([ |
| 48 |
'yatra_account_page' => $page, |
| 49 |
]); |
| 50 |
|
| 51 |
$template_path = YATRA_PLUGIN_PATH . 'templates/account-page.php'; |
| 52 |
|
| 53 |
if (!file_exists($template_path)) { |
| 54 |
$this->logError("Account page template not found: {$template_path}"); |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
$GLOBALS['yatra_loading_react_account_page'] = true; |
| 59 |
|
| 60 |
include $template_path; |
| 61 |
$this->exit(); |
| 62 |
|
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @return list<string> |
| 68 |
*/ |
| 69 |
private function validAccountPages(): array |
| 70 |
{ |
| 71 |
return [ |
| 72 |
'dashboard', |
| 73 |
'profile', |
| 74 |
'bookings', |
| 75 |
'payments', |
| 76 |
'documents', |
| 77 |
'saved-trips', |
| 78 |
'wishlist', |
| 79 |
'settings', |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
private function isValidAccountPage(string $page): bool |
| 84 |
{ |
| 85 |
return in_array($page, $this->validAccountPages(), true); |
| 86 |
} |
| 87 |
|
| 88 |
private function accountPageToReactTab(string $page): string |
| 89 |
{ |
| 90 |
switch ($page) { |
| 91 |
case 'wishlist': |
| 92 |
return 'saved-trips'; |
| 93 |
case 'settings': |
| 94 |
return 'profile'; |
| 95 |
default: |
| 96 |
return $page; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|