#40600 closed enhancement (invalid)
get_item filter for hijacking single post request
Reported by: | Owned by: | ||
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description
Hi,
I want to propose a filter in the get_item() method.
The use case is as follows;
- Content from a WP REST API enabled repository is being shown on a site through a 'virtual' post mechanism.
- The managers of the repository site use a custom way of setting whether or not a item is active, e.g via a metakey.
- Once an end-user bookmarks this 'virtual' post URL, or if the 'virtual' post is accessed via menu's or internal links, it will try to retrieve the content from the repository.
- If the managers of the repository site change the active property via its metakey value, the content should not come up anymore with a REST request.
The method I have in place right now has its own drawbacks, see: #40580, and from a performance standpoint it may not be very nice to first build the entire post object and then destroy in the end. Also using the rest_prepare_{$this->post_type} filter in this way isn't following its return policy as well (should be an WP_REST_Request not an WP_Error object)
With this filter one can now create a way for this to work.
I will add the patch and an unit test for this new filter.
Like to hear your thoughts.
Thanks,
Ruud
Attachments (2)
Change History (7)
#2
@
7 years ago
Hi,
This may still be a valid addition to core but I managed to get the job done via extending the WP_REST_Posts_Controller class and using my own custom class to add the desired additional check in the get_item() method.
Use your own custom Controller via the post-type registration:
'rest_controller_class' => 'Custom_REST_Posts_Controller',
Thanks,
Ruud
#3
@
7 years ago
It seems this would be best serviced by just creating a subclass of WP_REST_Posts_Controller
and using that subclass for the CTP via register_post_type
. Any reason that route not be possible?
#4
@
7 years ago
- Resolution set to invalid
- Status changed from new to closed
Hi Joe,
Thanks for your feedback.
You are right, subclassing the post controller was entirely possible and solved my use case.
At first I was looking for a way to alter the default way it works via filters, therefor I came up with this proposal for this additional filter. In hindsight subclassing the post controller is a good way to solve this.
Unless you see more merits for this proposed filter, I would be OK with an 'wontfix' solution for this trac-ticket. Perhaps the documentation (or inline docs) could benefit some more on this topic.
Thanks,
Ruud
get_item() with rest_pre_get_item() filter