Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions src/WordPress/QueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions tests/Unit/WordPress/DeferredTemplateRedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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])();
Expand Down