#63670 closed enhancement (fixed)
Add trashability to REST API posts controller
| Reported by: | isabel_brison | Owned by: | Mamaduka |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.2 |
| Component: | REST API | Version: | 6.9 |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description (last modified by )
Would be good to have a trashable property to the posts schema that checks the "rest_{$this->post_type}_trashable" filter and, in the case of attachment post type, the MEDIA_TRASH constant too.
This will be needed to display the correct actions for media items in data views, and also for other post types to which the filter may be applied.
Change History (18)
This ticket was mentioned in PR #9210 on WordPress/wordpress-develop by @isabel_brison.
17 months ago
#1
- Keywords has-patch added
@mukesh27 commented on PR #9210:
17 months ago
#2
The unit tests getting failed. Needs some updates in https://github.com/WordPress/wordpress-develop/blob/6b57942740049a76749904c69a030570de098789/tests/phpunit/tests/rest-api/rest-attachments-controller.php#L1668
17 months ago
#3
For consistency, this should use the same behaviour as delete_item with the same filter:
https://github.com/WordPress/wordpress-develop/blob/cddf5d8e55bdd1b675b58be89b798dfc615f9c7d/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L1091-L1109
Given that this isn't directly part of the data, imo it would make most sense to handle this via get_available_actions() and expose it as a self-link, rather than a field in the post.
@isabel_brison commented on PR #9210:
17 months ago
#4
Given that this isn't directly part of the data, imo it would make most sense to handle this via
get_available_actions()and expose it as a self-link, rather than a field in the post.
Hmm yeah I wondered about that, but get_available_actions deals more with actions the user has permission for, whereas MEDIA_TRASH and the trashable hook are post type-related settings.
If I'm honest I also don't love the ergonomics of exposing actions as links considering that we're usually looking for a boolean value, and we're having to sort of deduce it from the existence of a link 😅
#5
@
17 months ago
@isabel_brison When you assign reviewer, can you assign the maintainers of the REST API as well please.
#6
@
16 months ago
@spacedmonkey sorry about that! I've been away from core for some months and guess that was enough to forget procedures :')
Which of the REST API maintainers are active besides Ryan (who has commented already) and yourself? I'd be happy to have your thoughts on the PR btw!
#7
@
16 months ago
- Description modified (diff)
- Summary Add trashability to REST API attachment controller → Add trashability to REST API posts controller
Thinking about this more, and on a suggestion by @talldanwp, I'm updating the scope of this ticket to apply not just to the attachment controller, but to the posts controller. The logic is that there are other default post types not trashable by default (such as templates), but the "rest_{$this->post_type}_trashable" could potentially be used to change this for any post type.
This ticket was mentioned in PR #14033 on WordPress/wordpress-develop by @Mamaduka.
6 weeks ago
#8
- Keywords has-unit-tests added
Gutenberg PR: https://github.com/WordPress/gutenberg/pull/84045
Alternative to: https://github.com/WordPress/wordpress-develop/pull/9210.
Trac ticket: https://core.trac.wordpress.org/ticket/63670
PR adds a new wp:action-trash action link to posts and comment endpoints, which can be used to check if the entity allows the trash action.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude
Model(s): Opus 5.5
Used for: Porting the code from the source PR; reviewed by me.
@Mamaduka commented on PR #9210:
6 weeks ago
#9
Came across this PR while searching for existing tickets. Here's an alternative using action links: #14033.
#10
@
6 weeks ago
- Milestone Awaiting Review → 7.2
Create an alternative patch for this. Tentatively adding this to the WP 7.2 milestone.
The new action link will help us resolve two block editor issues:
@TimothyBlynJacobs commented on PR #14033:
5 weeks ago
#11
Did we rule out the possibility of just communicating this with the schema? We should be able to detect using the presence of required on the force option.
@Mamaduka commented on PR #14033:
5 weeks ago
#12
Did we rule out the possibility of just communicating this with the schema? We should be able to detect using the presence of
requiredon theforceoption.
I don't think we can use per-record filters (rest_{type}_trashable) at the route level; given their nature, links seem like a good option.
The links are also easy to use in the UI where we render large lists, like DataViews and Notes.
@Mamaduka commented on PR #14033:
5 weeks ago
#13
Thanks for the review, @TimothyBJacobs!
@isabel_brison commented on PR #9210:
5 weeks ago
#14
Closing in favour of #14033.
@isabel_brison commented on PR #14033:
5 weeks ago
#15
Thanks for sorting this out @Mamaduka!
This ticket was mentioned in PR #14518 on WordPress/wordpress-develop by @jorgefilipecosta.
4 weeks ago
#17
Ports the Gutenberg PHPUnit tests that have no Core counterpart yet, for code Core already has.
This PR adds:
test_trash_action_does_not_exist_in_view_contextandtest_trash_action_does_not_exist_when_user_cannot_deleteto the posts controller tests, from rest-trash-action-link-test.php.test_homepage_action_links_exist_for_pages_in_a_collectionto the pages controller tests, from rest-homepage-action-links-test.php (https://github.com/WordPress/gutenberg/pull/84932). It checks the compactedwp:action-set-homepageandwp:action-set-posts-pagekeys that the core actions read.test_get_item_schema_describes_table_column_stylesto the view config controller tests, from class-gutenberg-rest-view-config-controller-7-2-test.php.
It leaves out test_get_items_wp_navigation_default_view, which needs the wp_navigation view config of https://github.com/WordPress/wordpress-develop/pull/13296.
Trac tickets: https://core.trac.wordpress.org/ticket/63670, https://core.trac.wordpress.org/ticket/66327, https://core.trac.wordpress.org/ticket/65981
Testing Instructions
Verify unit tests are passing:
npm run test:php -- --group restapi --filter 'test_trash_action_does_not_exist|test_homepage_action_links|test_get_item_schema'
Use of AI Tools
AI assistance: Yes
Used for: porting the tests from Gutenberg and drafting this description; reviewed and edited by me.
Adding a
trashableproperty to the attachment schema that checks the MEDIA_TRASH constant.Will be needed to display the correct actions for media items in data views.
Trac ticket: https://core.trac.wordpress.org/ticket/63670