Skip to content

Guidelines: Fix fatal when rest_api_init fires before init (cherry-pick #78350)#78477

Merged
mikachan merged 1 commit into
release/23.2from
release/23.2-cherry-pick
May 20, 2026
Merged

Guidelines: Fix fatal when rest_api_init fires before init (cherry-pick #78350)#78477
mikachan merged 1 commit into
release/23.2from
release/23.2-cherry-pick

Conversation

@paulopmt1
Copy link
Copy Markdown
Contributor

@paulopmt1 paulopmt1 commented May 20, 2026

Summary

Slack-thread
Cherry-picks #78350 into release/23.2.

Original PR: #78350

Hosts can call rest_get_server() during plugins_loaded, which fires rest_api_init before init priority 10 has had a chance to register the wp_guideline post type. The rest_api_init callbacks in lib/experimental/guidelines/index.php dereference the post type object, so without a defensive guard they fatal (controllers) or trip _doing_it_wrong (register_post_meta).

This adds a single priority-1 rest_api_init guard that registers the post type if it doesn't yet exist, plus a regression test.

Test plan

  • PHPUnit passes for phpunit/experimental/guidelines/
  • Manual: confirm no fatal when rest_get_server() is called during plugins_loaded

🤖 Generated with Claude Code

* Guidelines: Add test for rest_api_init firing before init

Hosts can call rest_get_server() during plugins_loaded, which fires rest_api_init before init priority 10 has had a chance to register the wp_guideline post type. The rest_api_init callbacks in lib/experimental/guidelines/index.php dereference the post type object, so without a defensive guard they fatal (controllers) or trip _doing_it_wrong (register_post_meta).

This test simulates that ordering by unregistering the post type and firing rest_api_init directly.

* Guidelines: Ensure post type is registered before rest_api_init callbacks

init normally fires before rest_api_init, but anything that calls rest_get_server() early (e.g. from plugins_loaded) fires rest_api_init before init priority 10 has had a chance to register the wp_guideline post type. When that happens, the rest_api_init callbacks dereference a null post type object:
  - Gutenberg_Content_Guidelines_REST_Controller and Gutenberg_Content_Guidelines_Revisions_Controller fatal in their parent constructors (WP_REST_Posts_Controller::__construct and WP_REST_Revisions_Controller::__construct).
  - Gutenberg_Guidelines_Post_Type::register_post_meta trips a _doing_it_wrong notice about meta keys enabling revisions support on a subtype that does not support revisions.
Add a single priority-1 rest_api_init guard that registers the post type if it does not yet exist, so all subsequent rest_api_init callbacks see a fully-registered post type regardless of how the REST server was booted.

---------

Co-authored-by: Xavier Lozano Carreras <xavier.lozano.carreras@a8c.com>
@paulopmt1 paulopmt1 marked this pull request as ready for review May 20, 2026 18:16
@paulopmt1 paulopmt1 requested a review from spacedmonkey as a code owner May 20, 2026 18:16
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 20, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: xavier-lc <xavilc@git.wordpress.org>
Co-authored-by: mikachan <mikachan@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@paulopmt1 paulopmt1 added [Feature] Guidelines An experimental feature for adding site-wide editorial rules. [Type] Bug An existing feature does not function as intended Backport to Gutenberg Minor Release Pull request that needs to be backported to a Gutenberg minor release labels May 20, 2026
@paulopmt1 paulopmt1 requested a review from mikachan May 20, 2026 19:35
@mikachan mikachan merged commit 614f706 into release/23.2 May 20, 2026
111 of 114 checks passed
@paulopmt1 paulopmt1 added this to the Gutenberg 23.2 milestone May 20, 2026
@paulopmt1 paulopmt1 removed the Backport to Gutenberg Minor Release Pull request that needs to be backported to a Gutenberg minor release label May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Guidelines An experimental feature for adding site-wide editorial rules. [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants