Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rest API: add class to handle wp_navigation post type schema #4949

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Aug 2, 2023

Background

This PR is an attempt to resolve the concerns in https://core.trac.wordpress.org/ticket/58910: a hook (wp_add_fields_to_navigation_fallback_embedded_links) was inserted via rest_{$this->post_type}_item_schema to manipulate the wp_navigation post object rest response. It was added to a new file in Core and also referred to "navigation fallback", when in fact it was being applied to all wp_navigation post types.

The PR that backported these changes originally for 6.3: #4713

What this PR does

The strategy in this PR is to create a new rest API class - WP_REST_Navigation_Controller - to overwrite the get_item_schema method of WP_REST_Posts_Controller.

The controller class for the wp_navigation custom post type has been changed from WP_REST_Posts_Controller to WP_REST_Navigation_Controller.

wp_add_fields_to_navigation_fallback_embedded_links and the file it lives in has been deleted. The unit test is now covered in WP_REST_Navigation_Fallback_Controller_Test.

Tests have been added for this class.

If this PR is merged, it will require a backport to Gutenberg.

cc @scruffian @getdave @SergeyBiryukov

Trac ticket: https://core.trac.wordpress.org/ticket/58910


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

- This is an attempt to resolve the concerns in https://core.trac.wordpress.org/ticket/58910: a hook was inserted via `rest_{$this->post_type}_item_schema` to manipulate the  `wp_navigation` post object rest response. It was added to a new file in Core and also referred to "navigation fallback", when in fact it was being applied to all wp_navigation post types.
- Adding tests
@scruffian
Copy link

This was the original approach we wanted to take. See WordPress/gutenberg#50032

Initially I was returning the full Navigation post object from the REST endpoint. However, this required the introduction of a endpoint schema that married with the full Post object.

I spoke to Core REST folks about this and they advised that as we'd (collectively) agreed not to extend the Posts Controller, the most suitable option was simply to return a reference to the Navigation Post (i.e. the id) and then include a self link within the response which could optionally be embedded.

This resulted in a more manageable schema whilst also retaining the ability to access the full post object as part of the request if necessary.

@SergeyBiryukov
Copy link
Member

Thanks for the PR! An alternative approach was merged in r56793, and this could be explored in a future release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants