| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Core\Handlers; |
| 6 |
|
| 7 |
/** |
| 8 |
* Base Page Handler |
| 9 |
* |
| 10 |
* Provides common functionality for all page handlers |
| 11 |
*/ |
| 12 |
abstract class BasePageHandler |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Handle the page request |
| 16 |
* |
| 17 |
* @param array $route_data Route data from RouteMatcher |
| 18 |
* @return bool True if handled successfully |
| 19 |
*/ |
| 20 |
abstract public function handle(array $route_data): bool; |
| 21 |
|
| 22 |
/** |
| 23 |
* Set query vars for backward compatibility |
| 24 |
* |
| 25 |
* @param array $vars Query vars to set |
| 26 |
*/ |
| 27 |
protected function setQueryVars(array $vars): void |
| 28 |
{ |
| 29 |
global $wp_query; |
| 30 |
|
| 31 |
foreach ($vars as $key => $value) { |
| 32 |
$wp_query->set($key, $value); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Set 404 status |
| 38 |
*/ |
| 39 |
protected function set404(): void |
| 40 |
{ |
| 41 |
global $wp_query; |
| 42 |
|
| 43 |
$wp_query->set_404(); |
| 44 |
status_header(404); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Prevent 404 handling and set 200 status |
| 49 |
*/ |
| 50 |
protected function prevent404(): void |
| 51 |
{ |
| 52 |
global $wp_query; |
| 53 |
|
| 54 |
$wp_query->is_404 = false; |
| 55 |
status_header(200); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Set global variable |
| 60 |
* |
| 61 |
* @param string $name Variable name |
| 62 |
* @param mixed $value Variable value |
| 63 |
*/ |
| 64 |
protected function setGlobal(string $name, $value): void |
| 65 |
{ |
| 66 |
$GLOBALS[$name] = $value; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Log error message |
| 71 |
* |
| 72 |
* @param string $message Error message |
| 73 |
*/ |
| 74 |
protected function logError(string $message): void |
| 75 |
{ |
| 76 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Exit with optional message |
| 82 |
* |
| 83 |
* @param string|null $message Optional exit message |
| 84 |
*/ |
| 85 |
protected function exit(?string $message = null): void |
| 86 |
{ |
| 87 |
if ($message && defined('WP_DEBUG') && WP_DEBUG) { |
| 88 |
echo '<!-- ' . esc_html($message) . ' -->'; |
| 89 |
} |
| 90 |
exit; |
| 91 |
} |
| 92 |
} |
| 93 |
|