From 3c91d7740b607573603719110e6b49a265f90152 Mon Sep 17 00:00:00 2001 From: nmetahri Date: Wed, 25 Nov 2026 16:43:04 +0100 Subject: [PATCH] fix(wordpress): run the query once every provider has booted, so REST requests get their hooks --- CHANGELOG.md | 4 ++++ src/WordPress/QueryTrait.php | 16 +++++++++------- .../WordPress/DeferredTemplateRedirectTest.php | 12 +++++++----- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 447ff3ae..3b559677 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/Pollora/framework/compare/v13.44.0...develop) +### Fixed + +- **REST requests get the hooks the application's providers add in `boot()`.** WordPress answers a REST request (`/wp-json/…`, `/?rest_route=…`) while it parses the request, inside `wp()`, and exits. `wp()` ran from `WordPressServiceProvider::boot()`, before the application's and the theme's providers had booted, so `app()->isBooted()` was `false` there and every filter, action, binding or REST route those providers add in `boot()` was missing: block editor saves did not fire a `save_post` hooked there, and a route registered there answered `rest_no_route`. `wp()` now runs in `App::booted()`, just before `template_redirect` (#419), still before routing. A provider that reads the main query (`is_*()`, `get_queried_object()`, query vars) in its `boot()` now runs before it is parsed: read it from a `booted()` callback, a hook such as `wp`, or the route. A project that hooked in `register()` to work around this can move its hooks back to `boot()`; one that hooked in both gets them twice. + ## [v13.44.0](https://github.com/Pollora/framework/compare/v13.43.4...v13.44.0) - 2026-11-24 ### Changed diff --git a/src/WordPress/QueryTrait.php b/src/WordPress/QueryTrait.php index 84d6be0f..4d2c7c0c 100644 --- a/src/WordPress/QueryTrait.php +++ b/src/WordPress/QueryTrait.php @@ -84,11 +84,13 @@ private function isWordPressInstalled(): bool * like robots.txt, favicon, feeds and trackbacks. Normal template resolution * is delegated to the Laravel routing system. * - * `template_redirect` runs once every provider has booted, still before - * routing: WordPress is loaded from this provider's boot, and the theme's - * providers, registered by functions.php, boot after it. A plugin that + * The query and `template_redirect` run once every provider has booted, + * still before routing: WordPress is loaded from this provider's boot, and + * the application's providers, and the theme's registered by functions.php, + * boot after it. A REST request, answered by WordPress while it parses the + * request, then gets the hooks those providers add, and a plugin that * renders a page from `template_redirect` (WooCommerce's Review Order 404) - * then gets the theme's view composers and shared data. + * gets the theme's view composers and shared data. * * @throws WordPressNotLoadedException If WordPress core functions are not available */ @@ -104,11 +106,11 @@ protected function runWp(): void return; } - // Initialize WordPress for the current request - wp(); - App::booted(function (): void { $this->withWordPressErrorHandling(function (): void { + // Initialize WordPress for the current request + wp(); + $this->handleTemplateRedirect(); // For normal requests, let Laravel routing handle template resolution diff --git a/tests/Unit/WordPress/DeferredTemplateRedirectTest.php b/tests/Unit/WordPress/DeferredTemplateRedirectTest.php index 9360767f..2317fb49 100644 --- a/tests/Unit/WordPress/DeferredTemplateRedirectTest.php +++ b/tests/Unit/WordPress/DeferredTemplateRedirectTest.php @@ -9,9 +9,11 @@ use Pollora\WordPress\QueryTrait; /** - * WordPress is loaded from a provider's boot, and the theme's providers boot - * after it: template_redirect waits for every provider to have booted, so a - * page a plugin renders from it gets the theme's composers and shared data (#419). + * WordPress is loaded from a provider's boot, and the application's and the + * theme's providers boot after it: the query and template_redirect wait for + * every provider to have booted. A REST request, answered while the query parses + * the request, then gets the hooks those providers add, and a page a plugin + * renders from template_redirect gets the theme's composers and shared data (#419). */ beforeEach(function (): void { $this->originalScript = $_SERVER['SCRIPT_FILENAME'] ?? null; @@ -83,12 +85,12 @@ private function withWordPressErrorHandling(callable $callback): void }; } -it('runs the query at once and template_redirect once every provider has booted', function (): void { +it('runs the query and template_redirect once every provider has booted', function (): void { $runner = wordPressRunner($this->action); $runner->run(); - expect($this->fired)->toBe(['wp']) + expect($this->fired)->toBe([]) ->and($this->bootedCallbacks)->toHaveCount(1); ($this->bootedCallbacks[0])();