REST API: JS client - Enumerate endpoints supporting trash.

Improve the logic determining which endpoints support the trash by enumerating them. Endpoints that don't support the trash require `force=true` when deleting. The previous approach relied on the `force` argument description, which is a translated string and was fragile. In the future, we can expose whether an endpoint supports the trash as part of its schema and automate this logic.

Props Soean.
Fixes #40672.



git-svn-id: https://develop.svn.wordpress.org/trunk@42047 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein 2017-10-31 02:53:44 +00:00
parent f19d308ece
commit 0a964a48f8
1 changed files with 9 additions and 25 deletions

View File

@ -803,7 +803,8 @@
'use strict';
var wpApiSettings = window.wpApiSettings || {};
var wpApiSettings = window.wpApiSettings || {},
trashableTypes = [ 'Comment', 'Media', 'Comment', 'Post', 'Page', 'Status', 'Taxonomy', 'Type' ];
/**
* Backbone base model for all models.
@ -811,32 +812,15 @@
wp.api.WPApiBaseModel = Backbone.Model.extend(
/** @lends WPApiBaseModel.prototype */
{
initialize: function( attributes, options ) {
// Initialize the model.
initialize: function() {
/**
* Determine if a model requires ?force=true to actually delete them.
*/
if (
! _.isEmpty(
_.filter(
this.endpoints,
function( endpoint ) {
return (
// Does the method support DELETE?
'DELETE' === endpoint.methods[0] &&
// Exclude models that support trash (Post, Page).
(
! _.isUndefined( endpoint.args.force ) &&
! _.isUndefined( endpoint.args.force.description ) &&
'Whether to bypass trash and force deletion.' !== endpoint.args.force.description
)
);
}
)
)
) {
* Types that don't support trashing require passing ?force=true to delete.
*
*/
if ( -1 === _.indexOf( trashableTypes, this.name ) ) {
this.requireForceForDelete = true;
}
},