Make WordPress Core

Changeset 41657


Ignore:
Timestamp:
10/01/2017 01:06:43 PM (9 years ago)
Author:
adamsilverstein
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r41553 r41657  
    771771                /** @lends WPApiBaseModel.prototype  */
    772772                {
     773                        initialize: function( attributes, options ) {
     774
     775                                /**
     776                                 * Determine if a model requires ?force=true to actually delete them.
     777                                 */
     778                                if (
     779                                        ! _.isEmpty(
     780                                                _.filter(
     781                                                        this.endpoints,
     782                                                        function( endpoint ) {
     783                                                                return (
     784
     785                                                                        // Does the method support DELETE?
     786                                                                        'DELETE' === endpoint.methods[0] &&
     787
     788                                                                        // Exclude models that support trash (Post, Page).
     789                                                                        (
     790                                                                                ! _.isUndefined( endpoint.args.force ) &&
     791                                                                                ! _.isUndefined( endpoint.args.force.description ) &&
     792                                                                                'Whether to bypass trash and force deletion.' !== endpoint.args.force.description
     793                                                                        )
     794                                                                );
     795                                                        }
     796                                                )
     797                                        )
     798                                ) {
     799                                        this.requireForceForDelete = true;
     800                                }
     801                        },
     802
    773803                        /**
    774804                         * Set nonce header before every Backbone sync.
    … …  
    12671297                                                methods: modelRoute.route.methods,
    12681298
    1269                                                 initialize: function( attributes, options ) {
    1270                                                         wp.api.WPApiBaseModel.prototype.initialize.call( this, attributes, options );
    1271 
    1272                                                         /**
    1273                                                          * Posts and pages support trashing, other types don't support a trash
    1274                                                          * and require that you pass ?force=true to actually delete them.
    1275                                                          *
    1276                                                          * @todo we should be getting trashability from the Schema, not hard coding types here.
    1277                                                          */
    1278                                                         if (
    1279                                                                 'Posts' !== this.name &&
    1280                                                                 'Pages' !== this.name &&
    1281                                                                 _.includes( this.methods, 'DELETE' )
    1282                                                         ) {
    1283                                                                 this.requireForceForDelete = true;
    1284                                                         }
    1285                                                 }
     1299                                                // Include the array of route endpoints for easy reference.
     1300                                                endpoints: modelRoute.route.endpoints
    12861301                                        } );
    12871302                                } else {
    … …  
    13181333
    13191334                                                // Include the array of route methods for easy reference.
    1320                                                 methods: modelRoute.route.methods
     1335                                                methods: modelRoute.route.methods,
     1336
     1337                                                // Include the array of route endpoints for easy reference.
     1338                                                endpoints: modelRoute.route.endpoints
    13211339                                        } );
    13221340                                }
  • trunk/tests/qunit/wp-includes/js/wp-api.js

    r41334 r41657  
    344344        });
    345345
     346        // Test that models have the correct requireForceForDelete setting.
     347        var modelsThatNeedrequireForceForDelete = [
     348                { name: 'Category', expect: true },
     349                { name: 'Comment', expect: undefined },
     350                { name: 'Media', expect: undefined },
     351                { name: 'Page', expect: undefined },
     352                { name: 'PageRevision', expect: true },
     353                { name: 'Post', expect: undefined },
     354                { name: 'PostRevision', expect: true },
     355                { name: 'Status', expect: undefined },
     356                { name: 'Tag', expect: true },
     357                { name: 'Taxonomy', expect: undefined },
     358                { name: 'Type', expect: undefined },
     359                { name: 'User', expect: true }
     360        ];
     361
     362        _.each( modelsThatNeedrequireForceForDelete, function( model ) {
     363                QUnit.test( 'Test requireForceForDelete is correct for ' + model.name, function( assert ) {
     364                        var done = assert.async();
     365                        assert.expect( 1 );
     366                        wp.api.loadPromise.done( function() {
     367
     368                                // Instantiate the model.
     369                                var theModel = new wp.api.models[ model.name ]();
     370
     371                                // Verify the model's requireForceForDelete is set as expected.
     372                                assert.equal(
     373                                        theModel.requireForceForDelete,
     374                                        model.expect,
     375                                        'wp.api.models.' + model.name + '.requireForceForDelete should be ' + model.expect + '.'
     376                                );
     377
     378                                // Trigger Qunit async completion.
     379                                done();
     380                        } );
     381                } );
     382        } );
     383
    346384} )( window.QUnit );
Note: See TracChangeset for help on using the changeset viewer.