Make WordPress Core

Opened 17 months ago

Closed 5 weeks ago

Last modified 4 weeks ago

#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 isabel_brison)

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

Adding a trashable property 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

​@rmccue commented on ​PR #9210:


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 @spacedmonkey
17 months ago

@isabel_brison When you assign reviewer, can you assign the maintainers of the REST API as well please.

#6 @isabel_brison
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 @isabel_brison
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 @Mamaduka
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 required on the force option.

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!

#16 @Mamaduka
5 weeks ago

  • Owner set to Mamaduka
  • Resolution → fixed
  • Status new → closed

In 64343:

REST API: Add action-trash link to posts and comments.

Adds a https://api.w.org/action-trash link relation to post and comment responses in the edit context. The link means the current user can move that item to the trash. Whether an item can be trashed is decided per record by EMPTY_TRASH_DAYS, MEDIA_TRASH and the rest_{$type}_trashable filters, so the route schema can't express it. The link exposes it for each item, which helps UIs that render long lists, like DataViews and Notes.

Menu items can only be deleted permanently, so they don't get the link.

Props mamaduka, timothyblynjacobs, adamsilverstein, isabel_brison, peterwilsoncc.
Fixes #63670.

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:

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.

#18 @jorgefilipecosta
4 weeks ago

In 64596:

Tests: Port Gutenberg tests for REST action links and view configs.

Adds the Gutenberg PHPUnit tests that have no Core counterpart yet, for code Core already has:

  • The action-trash link is absent in the view context and when the user cannot delete the post.
  • The action-set-homepage and action-set-posts-page links of pages in a collection are compacted as wp:action-set-homepage and wp:action-set-posts-page, the keys the core actions read.
  • The view config schema describes the table column styles.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14518

Props jorgefilipecosta.
See #63670, #66327, #65981.

Note: See TracTickets for help on using tickets.