Skip to content

fix(wordpress): run the query once every provider has booted, so REST requests get their hooks - #709

Open
nmetahri wants to merge 5 commits into
Pollora:developfrom
nmetahri:hotfix/rest-requests-after-boot
Open

nmetahri wants to merge 5 commits into
Pollora:developfrom
nmetahri:hotfix/rest-requests-after-boot

Conversation

@nmetahri

Copy link
Copy Markdown

Fixes #708

WordPress answers a REST request (/wp-json/…, /?rest_route=…) from parse_request, inside wp(), and exits. runWp() called wp() from WordPressServiceProvider::boot(), before the application's and the theme's providers had booted, so everything they add in boot() was missing from REST requests, block editor saves included.

wp() now runs in the App::booted() callback #419 added, just before template_redirect, still before routing. WordPress's own order inside wp() is unchanged.

Changes

  • src/WordPress/QueryTrait.php: wp() moved into the App::booted() callback, under the same withWordPressErrorHandling().
  • tests/Unit/WordPress/DeferredTemplateRedirectTest.php: nothing runs before every provider has booted, then wp, template_redirect, pollora_loaded. Fails without the fix.
  • CHANGELOG.md: an Unreleased › Fixed entry, with the behaviour change for providers reading the main query in boot().

How to test

The unit tests run in the console, where runWp() is skipped, so the real check is over HTTP. On a Pollora site:

  1. In an application provider's boot(): Filter::add('probe/hooked', static fn (): bool => true);
  2. In a mu-plugin, a route returning ['booted' => app()->isBooted(), 'hooked' => apply_filters('probe/hooked', false)] (full snippet in the issue).
  3. GET /wp-json/probe/v1/state: false/false before, true/true after.
  4. In the block editor, save a post with a save_post hooked from that boot(): called only after.

Checked on a site with WordPress 6.9, WooCommerce, Polylang and Yoast (FrankenPHP): REST index and routes, home, product, 404, search, robots.txt, feed, sitemap, wp-admin, admin-ajax.php and wp-cron.php answer the same before and after. Details in the issue.

QA

On PHP 8.4, the CI's extensions:

  • Pest: 3996 passed, 2 skipped;
  • type coverage 99.1%, Pint, Rector, architecture and dependency gates pass.

PHPStan reports one error, in Foundation/Providers/ArtisanServiceProvider.php:43 (Illuminate\Console\Application::add() deprecated since Symfony 7.4). That file is untouched; the error comes from the dependencies resolved today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants