| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Providers; |
| 6 |
|
| 7 |
use Yatra\Core\ServiceProvider; |
| 8 |
|
| 9 |
/** |
| 10 |
* Route Service Provider |
| 11 |
* |
| 12 |
* Registers REST API routes by loading the routes/api.php file |
| 13 |
* during the 'rest_api_init' hook. |
| 14 |
* |
| 15 |
* @package Yatra\Providers |
| 16 |
*/ |
| 17 |
class RouteServiceProvider extends ServiceProvider |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Routes file path |
| 21 |
*/ |
| 22 |
private string $routesFile; |
| 23 |
|
| 24 |
/** |
| 25 |
* Register services |
| 26 |
*/ |
| 27 |
public function register(): void |
| 28 |
{ |
| 29 |
$this->routesFile = YATRA_PLUGIN_PATH . 'routes/api.php'; |
| 30 |
|
| 31 |
|
| 32 |
// Register routes on rest_api_init hook with higher priority to ensure it runs early |
| 33 |
add_action('rest_api_init', [$this, 'registerRoutes'], 5); |
| 34 |
|
| 35 |
// Fix REST API rewrite rules if they're not working |
| 36 |
add_action('init', [$this, 'fixRestApiRewrites'], 999); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Register REST API routes |
| 41 |
*/ |
| 42 |
public function registerRoutes(): void |
| 43 |
{ |
| 44 |
|
| 45 |
// Prevent duplicate registration |
| 46 |
static $registered = false; |
| 47 |
if ($registered) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
if (!file_exists($this->routesFile)) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
// Load routes registry |
| 56 |
require_once $this->routesFile; |
| 57 |
|
| 58 |
$registered = true; |
| 59 |
|
| 60 |
// Debug logging (only when explicitly enabled) |
| 61 |
if (defined('YATRA_DEBUG_API') && YATRA_DEBUG_API) { |
| 62 |
$this->logRegisteredRoutes(); |
| 63 |
} |
| 64 |
|
| 65 |
// Debug: Log route registration completion |
| 66 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Fix REST API rewrite rules |
| 72 |
*/ |
| 73 |
public function fixRestApiRewrites(): void |
| 74 |
{ |
| 75 |
// Add manual rewrite rule for REST API |
| 76 |
add_rewrite_rule( |
| 77 |
'^wp-json/yatra/v1/downloads/([0-9]+)/download-url/?$', |
| 78 |
'index.php?rest_route=/yatra/v1/downloads/$matches[1]/download-url', |
| 79 |
'top' |
| 80 |
); |
| 81 |
|
| 82 |
add_rewrite_rule( |
| 83 |
'^wp-json/yatra/v1/(.*)?$', |
| 84 |
'index.php?rest_route=/yatra/v1/$matches[1]', |
| 85 |
'top' |
| 86 |
); |
| 87 |
|
| 88 |
// Ensure REST API rewrite rules are properly flushed |
| 89 |
if (!get_option('yatra_rest_api_flushed')) { |
| 90 |
flush_rewrite_rules(false); |
| 91 |
update_option('yatra_rest_api_flushed', true); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Log all registered routes for debugging |
| 97 |
*/ |
| 98 |
private function logRegisteredRoutes(): void |
| 99 |
{ |
| 100 |
$server = rest_get_server(); |
| 101 |
$routes = $server->get_routes('yatra/v1'); |
| 102 |
|
| 103 |
// Group routes by resource |
| 104 |
$grouped = []; |
| 105 |
foreach (array_keys($routes) as $route) { |
| 106 |
$parts = explode('/', trim($route, '/')); |
| 107 |
$resource = $parts[1] ?? 'root'; |
| 108 |
if (!isset($grouped[$resource])) { |
| 109 |
$grouped[$resource] = 0; |
| 110 |
} |
| 111 |
$grouped[$resource]++; |
| 112 |
} |
| 113 |
|
| 114 |
foreach ($grouped as $resource => $count) { |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|