REST API JavaScript Client: improve support for model deletion/trash.

Update the way and location the JavaScript client determines which models/endpoints require the `force=true` parameter when being deleted to avoid a `rest_trash_not_supported` error. Identify models with endpoints that support DELETE, excluding those that support the trash (posts and pages by default). Also, move the check into the default `wp.api.WPApiBaseModel.initialize()` function.

Props caercam, euthelup.
Fixes #40672.


Built from https://develop.svn.wordpress.org/trunk@41657


git-svn-id: http://core.svn.wordpress.org/trunk@41491 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Adam Silverstein 2017-10-01 13:07:45 +00:00
parent 40d84f7503
commit e23b4e9269
3 changed files with 38 additions and 20 deletions

View File

@ -770,6 +770,36 @@
wp.api.WPApiBaseModel = Backbone.Model.extend(
/** @lends WPApiBaseModel.prototype */
{
initialize: function( attributes, options ) {
/**
* 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
)
);
}
)
)
) {
this.requireForceForDelete = true;
}
},
/**
* Set nonce header before every Backbone sync.
*
@ -1266,23 +1296,8 @@
// Include the array of route methods for easy reference.
methods: modelRoute.route.methods,
initialize: function( attributes, options ) {
wp.api.WPApiBaseModel.prototype.initialize.call( this, attributes, options );
/**
* Posts and pages support trashing, other types don't support a trash
* and require that you pass ?force=true to actually delete them.
*
* @todo we should be getting trashability from the Schema, not hard coding types here.
*/
if (
'Posts' !== this.name &&
'Pages' !== this.name &&
_.includes( this.methods, 'DELETE' )
) {
this.requireForceForDelete = true;
}
}
// Include the array of route endpoints for easy reference.
endpoints: modelRoute.route.endpoints
} );
} else {
@ -1317,7 +1332,10 @@
name: modelClassName,
// Include the array of route methods for easy reference.
methods: modelRoute.route.methods
methods: modelRoute.route.methods,
// Include the array of route endpoints for easy reference.
endpoints: modelRoute.route.endpoints
} );
}

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.9-alpha-41656';
$wp_version = '4.9-alpha-41657';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.