Skip to content

Commit

Permalink
🚩 add experimental flag for WordPress request handler (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
QWp6t authored Feb 6, 2024
1 parent 92491e9 commit 0076a67
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/Roots/Acorn/Bootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,19 @@ protected function bootHttp(ApplicationContract $app)
/** @var \Illuminate\Routing\Route $route */
$route = $app->make('router')->getRoutes()->match($request);

/** @var array $config */
$config = $app->config->get('router.wordpress', ['web' => 'web', 'api' => 'api']);
if ($route->getName() !== 'wordpress_request') {
$this->registerRequestHandler($kernel, $request, $route);
} elseif (env('ACORN_ENABLE_EXPERIMENTAL_WORDPRESS_REQUEST_HANDLER', false)) {
$this->registerWordPressRequestHandler($kernel, $request, $route, $app->config->get('router.wordpress', ['web' => 'web', 'api' => 'api']));
}

$this->registerRequestHandler($kernel, $request, $route, $config);
add_filter('do_parse_request', function ($doParse, \WP $wp, $extraQueryVars) use ($route) {
if ($route->getName() === 'wordpress_request') {
return $doParse;
}

return apply_filters('acorn/router/do_parse_request', $doParse, $wp, $extraQueryVars);
}, 100, 3);
}

/**
Expand All @@ -228,7 +237,8 @@ protected function bootHttp(ApplicationContract $app)
protected function registerWordPressRoute(ApplicationContract $app)
{
$app->make('router')
->any('{any?}', fn () => tap(response(''), function (Response $response) {
->any('{any?}', fn () => tap(response(''), function (Response $response) use ($app) {
dd('eloel');
foreach (headers_list() as $header) {
[$header, $value] = explode(': ', $header, 2);
if (! headers_sent()) {
Expand All @@ -237,6 +247,10 @@ protected function registerWordPressRoute(ApplicationContract $app)
$response->header($header, $value);
}

if ($app->hasDebugModeEnabled()) {
$response->header('X-Powered-By', $app->version());
}

$content = '';

$levels = ob_get_level();
Expand All @@ -256,6 +270,22 @@ protected function registerWordPressRoute(ApplicationContract $app)
* @return void
*/
protected function registerRequestHandler(
\Illuminate\Contracts\Http\Kernel $kernel,
\Illuminate\Http\Request $request,
?\Illuminate\Routing\Route $route
) {
add_filter('do_parse_request', function ($doParse, \WP $wp, $extraQueryVars) use ($route) {
if (! $route) {
return $doParse;
}

return apply_filters('acorn/router/do_parse_request', $doParse, $wp, $extraQueryVars);
}, 100, 3);

add_action('parse_request', fn () => $this->handleRequest($kernel, $request));
}

protected function registerWordPressRequestHandler(
\Illuminate\Contracts\Http\Kernel $kernel,
\Illuminate\Http\Request $request,
?\Illuminate\Routing\Route $route,
Expand All @@ -274,20 +304,6 @@ protected function registerRequestHandler(
return; // Let WordPress handle these requests
}

add_filter('do_parse_request', function ($doParse, \WP $wp, $extraQueryVars) use ($route) {
if (! $route) {
return $doParse;
}

return apply_filters('acorn/router/do_parse_request', $doParse, $wp, $extraQueryVars);
}, 100, 3);

if ($route->getName() !== 'wordpress_request') {
add_action('parse_request', fn () => $this->handleRequest($kernel, $request));

return;
}

$route->middleware(preg_match('/^wp-json(\/.*)?/', $request->path()) ? $config['api'] : $config['web']);

ob_start();
Expand Down

0 comments on commit 0076a67

Please sign in to comment.