WordPress.org

Make WordPress Core

Opened 5 weeks ago

Last modified 5 weeks ago

#44152 new defect (bug)

Comments "up" link does not cover custom post types and/or REST controllers

Reported by: dsifford Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: trunk
Component: REST API Keywords:
Focuses: rest-api Cc:

Description

Currently, WP_REST_Comments_Controller only covers built-in post types under the /wp/v2/ namespace when making "up" links.

https://core.trac.wordpress.org/browser/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php#L997

Note that wp/v2 is hard-coded on the line above.

This causes issues if a comment falls under a custom post type with it's own REST controller falling under a different namespace.

Change History (2)

#1 @danielbachhuber
5 weeks ago

@dsifford How would you suggest solving this issue?

#2 @dsifford
5 weeks ago

@danielbachhuber Not totally sure. I'm only very loosely familiar with the internals of the API.

I went in and fixed my singular issue by hooking into rest_prepare_comment and adjusting for the specific post type by doing a similar hard-coding of the endpoint.

One specific thing that tripped me up in the process of doing this is that it doesn't look like it's currently possible to obtain the namespace of a given object type easily (if at all).

The only thing that it appears you have access to is the rest_base and the rest_controller_class which, although still very useful and helpful, only allows someone to build the REST url after the namespace itself.

Digging into WP_REST_Server, I saw that there is a get_namespaces method and an associated get_namespace_index method, but it ostensibly only allows you to obtain an endpoint's namespace if you have a WP_REST_Request instance that is actively hitting that endpoint available. We don't have that in this case.

So yeah, I'm a bit stumped here. Happy to work with you and others on this though if needed.

Best case scenario I'd say would be to add a rest_namespace field to the custom post type. That way, a full REST url could be built from the object itself, rather than just the part after the namespace.

That, or implement a public getter method for the namespace of WP_REST_Controller.

Last edited 5 weeks ago by dsifford (previous) (diff)
Note: See TracTickets for help on using tickets.