WordPress.org

Make WordPress Core

Changeset 42047


Ignore:
Timestamp:
10/31/2017 02:53:44 AM (9 months ago)
Author:
adamsilverstein
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/wp-api.js

    r41678 r42047  
    804804    'use strict';
    805805
    806     var wpApiSettings = window.wpApiSettings || {};
     806    var wpApiSettings = window.wpApiSettings || {},
     807    trashableTypes    = [ 'Comment', 'Media', 'Comment', 'Post', 'Page', 'Status', 'Taxonomy', 'Type' ];
    807808
    808809    /**
     
    812813        /** @lends WPApiBaseModel.prototype  */
    813814        {
    814             initialize: function( attributes, options ) {
     815
     816            // Initialize the model.
     817            initialize: function() {
    815818
    816819                /**
    817                  * Determine if a model requires ?force=true to actually delete them.
    818                  */
    819                 if (
    820                     ! _.isEmpty(
    821                         _.filter(
    822                             this.endpoints,
    823                             function( endpoint ) {
    824                                 return (
    825 
    826                                     // Does the method support DELETE?
    827                                     'DELETE' === endpoint.methods[0] &&
    828 
    829                                     // Exclude models that support trash (Post, Page).
    830                                     (
    831                                         ! _.isUndefined( endpoint.args.force ) &&
    832                                         ! _.isUndefined( endpoint.args.force.description ) &&
    833                                         'Whether to bypass trash and force deletion.' !== endpoint.args.force.description
    834                                     )
    835                                 );
    836                             }
    837                         )
    838                     )
    839                 ) {
     820                * Types that don't support trashing require passing ?force=true to delete.
     821                *
     822                */
     823                if ( -1 === _.indexOf( trashableTypes, this.name ) ) {
    840824                    this.requireForceForDelete = true;
    841825                }
Note: See TracChangeset for help on using the changeset viewer.