Make WordPress Core

Opened 7 years ago

Closed 4 years ago

Last modified 4 years ago

#44152 closed defect (bug) (fixed)

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

Reported by: dsifford's profile dsifford Owned by: timothyblynjacobs's profile TimothyBlynJacobs
Milestone: 5.6 Priority: normal
Severity: normal Version:
Component: REST API Keywords: has-patch has-unit-tests
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 (7)

#1 @danielbachhuber
7 years ago

@dsifford How would you suggest solving this issue?

#2 @dsifford
7 years 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 7 years ago by dsifford (previous) (diff)

#3 @pento
6 years ago

  • Version trunk deleted

This ticket was mentioned in PR #652 on WordPress/wordpress-develop by TimothyBJacobs.


4 years ago
#4

  • Keywords has-patch has-unit-tests added

#5 @TimothyBlynJacobs
4 years ago

  • Milestone changed from Awaiting Review to 5.6

Now that we have rest_get_route_for_post we can support this in a fairly straightforward way. https://make.wordpress.org/core/2020/07/22/rest-api-changes-in-wordpress-5-5/

#6 @TimothyBlynJacobs
4 years ago

  • Owner set to TimothyBlynJacobs
  • Resolution set to fixed
  • Status changed from new to closed

In 49299:

REST API: Support generating comment up links to custom posts controllers.

The comments controller now uses the rest_get_route_for_post function introduced in WordPress 5.5 to generate the link. This function is filterable to allow for custom controllers to properly define their REST API route.

Props dsifford, TimothyBlynJacobs.
Fixes #44152.

TimothyBJacobs commented on PR #652:


4 years ago
#7

Fixed in b8cdf1be50ab8d6cfb7b3e594bc8558f64376acf.

Note: See TracTickets for help on using tickets.