Guidelines: Fix fatal when rest_api_init fires before init#78350
Conversation
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.
…acks 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.
|
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @xavier.lozano.carreras@a8c.com. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
There was a conflict while trying to cherry-pick the commit to the wp/7.0 branch. Please resolve the conflict manually and create a PR to the wp/7.0 branch. PRs to wp/7.0 are similar to PRs to trunk, but you should base your PR on the wp/7.0 branch instead of trunk. |
…pick #78350) (#78477) * 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 <8511199+xavier-lc@users.noreply.github.com> Co-authored-by: Xavier Lozano Carreras <xavier.lozano.carreras@a8c.com> Co-authored-by: xavier-lc <xavilc@git.wordpress.org> Co-authored-by: mikachan <mikachan@git.wordpress.org>
What?
Ensures the
wp_guidelinecustom post type is registered before anyrest_api_initcallback runs, so the Guidelines feature does not fatal whenrest_api_initfires beforeinitpriority 10.Why?
Gutenberg_Guidelines_Post_Type::register()is hooked toinitat priority 10. Therest_api_initcallbacks inlib/experimental/guidelines/index.phpthen assume the post type exists:Gutenberg_Content_Guidelines_REST_ControllerandGutenberg_Content_Guidelines_Revisions_Controllerare instantiated viaparent::__construct( 'wp_guideline' ), where the WP core parent constructors dereferenceget_post_type_object( $parent_post_type )without a null check (WP_REST_Posts_Controller::__construct,WP_REST_Revisions_Controller::__construct).Gutenberg_Guidelines_Post_Type::register_post_meta()registers meta with revisions support, which_doing_it_wrongs if the parent post type does not yet declarerevisionssupport.initnormally fires beforerest_api_init, but anything that callsrest_get_server()early — for example a host plugin that needs an internal REST dispatch duringplugins_loaded— boots the REST server, which firesrest_api_initimmediately. In that window the post type is not yet registered and the callbacks above fatal.This was observed in production on WordPress.com when activating the Gutenberg
gutenberg-guidelinesexperiment alongside hosting code that boots the REST server duringplugins_loaded:How?
Adds a single priority-1
rest_api_initcallback that registers the post type if it does not already exist: