From 0a964a48f8cec4ba0ba8cfbe9427fb55dc0842f6 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 31 Oct 2017 02:53:44 +0000 Subject: [PATCH] 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 --- src/wp-includes/js/wp-api.js | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/src/wp-includes/js/wp-api.js b/src/wp-includes/js/wp-api.js index d25a9a7b81..0f5135f3c9 100644 --- a/src/wp-includes/js/wp-api.js +++ b/src/wp-includes/js/wp-api.js @@ -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; } },