diff --git src/wp-includes/js/wp-api.js src/wp-includes/js/wp-api.js
index 31886d280b..e114edf12a 100644
|
|
|
|
| 1227 | 1227 | routeName = 'me'; |
| 1228 | 1228 | } |
| 1229 | 1229 | |
| | 1230 | var initializeModel = function( attributes, options ) { |
| | 1231 | wp.api.WPApiBaseModel.prototype.initialize.call( this, attributes, options ); |
| | 1232 | |
| | 1233 | /** |
| | 1234 | * Determine if a model requires ?force=true to actually delete them. |
| | 1235 | */ |
| | 1236 | if ( |
| | 1237 | ! _.isEmpty( |
| | 1238 | _.filter( |
| | 1239 | this.endpoints, |
| | 1240 | function( endpoint ) { |
| | 1241 | return ( |
| | 1242 | |
| | 1243 | // Does the method support DELETE? |
| | 1244 | 'DELETE' === endpoint.methods[0] && |
| | 1245 | |
| | 1246 | // Exclude models that support trash (Post, Page). |
| | 1247 | ( |
| | 1248 | ! _.isUndefined( endpoint.args.force ) && |
| | 1249 | ! _.isUndefined( endpoint.args.force.description ) && |
| | 1250 | 'Whether to bypass trash and force deletion.' !== endpoint.args.force.description |
| | 1251 | ) |
| | 1252 | ); |
| | 1253 | } |
| | 1254 | ) |
| | 1255 | ) |
| | 1256 | ) { |
| | 1257 | this.requireForceForDelete = true; |
| | 1258 | } |
| | 1259 | }; |
| | 1260 | |
| 1230 | 1261 | // If the model has a parent in its route, add that to its class name. |
| 1231 | 1262 | if ( '' !== parentName && parentName !== routeName ) { |
| 1232 | 1263 | modelClassName = wp.api.utils.capitalizeAndCamelCaseDashes( parentName ) + wp.api.utils.capitalizeAndCamelCaseDashes( routeName ); |
| … |
… |
|
| 1266 | 1297 | // Include the array of route methods for easy reference. |
| 1267 | 1298 | methods: modelRoute.route.methods, |
| 1268 | 1299 | |
| 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 | | } |
| | 1300 | // Include the array of route endpoints for easy reference. |
| | 1301 | endpoints: modelRoute.route.endpoints, |
| | 1302 | |
| | 1303 | initialize: initializeModel |
| 1286 | 1304 | } ); |
| 1287 | 1305 | } else { |
| 1288 | 1306 | |
| … |
… |
|
| 1317 | 1335 | name: modelClassName, |
| 1318 | 1336 | |
| 1319 | 1337 | // Include the array of route methods for easy reference. |
| 1320 | | methods: modelRoute.route.methods |
| | 1338 | methods: modelRoute.route.methods, |
| | 1339 | |
| | 1340 | // Include the array of route endpoints for easy reference. |
| | 1341 | endpoints: modelRoute.route.endpoints, |
| | 1342 | |
| | 1343 | initialize: initializeModel |
| 1321 | 1344 | } ); |
| 1322 | 1345 | } |
| 1323 | 1346 | |
diff --git tests/qunit/wp-includes/js/wp-api.js tests/qunit/wp-includes/js/wp-api.js
index e904ccaec8..b4525bd2a6 100644
|
|
|
|
| 343 | 343 | } ); |
| 344 | 344 | }); |
| 345 | 345 | |
| | 346 | // Test that models have the correct requireForceForDelete setting. |
| | 347 | var modelsThatNeedrequireForceForDelete = [ |
| | 348 | { |
| | 349 | name: 'Category', |
| | 350 | expect: true |
| | 351 | }, |
| | 352 | { |
| | 353 | name: 'Comment', |
| | 354 | expect: undefined |
| | 355 | }, |
| | 356 | { |
| | 357 | name: 'Media', |
| | 358 | expect: undefined |
| | 359 | }, |
| | 360 | { |
| | 361 | name: 'Page', |
| | 362 | expect: undefined |
| | 363 | }, |
| | 364 | { |
| | 365 | name: 'PageRevision', |
| | 366 | expect: true |
| | 367 | }, |
| | 368 | { |
| | 369 | name: 'Post', |
| | 370 | expect: undefined |
| | 371 | }, |
| | 372 | { |
| | 373 | name: 'PostRevision', |
| | 374 | expect: true |
| | 375 | }, |
| | 376 | { |
| | 377 | name: 'Status', |
| | 378 | expect: undefined |
| | 379 | }, |
| | 380 | { |
| | 381 | name: 'Tag', |
| | 382 | expect: true |
| | 383 | }, |
| | 384 | { |
| | 385 | name: 'Taxonomy', |
| | 386 | expect: undefined |
| | 387 | }, |
| | 388 | { |
| | 389 | name: 'Type', |
| | 390 | expect: undefined |
| | 391 | }, |
| | 392 | { |
| | 393 | name: 'User', |
| | 394 | expect: true |
| | 395 | } |
| | 396 | ]; |
| | 397 | |
| | 398 | _.each( modelsThatNeedrequireForceForDelete, function( model ) { |
| | 399 | QUnit.test( 'Test requireForceForDelete is correct for ' + model.name, function( assert ) { |
| | 400 | |
| | 401 | var done = assert.async(); |
| | 402 | |
| | 403 | assert.expect( 1 ); |
| | 404 | |
| | 405 | wp.api.loadPromise.done( function() { |
| | 406 | |
| | 407 | var theModel = new wp.api.models[ model.name ](); |
| | 408 | |
| | 409 | assert.equal( |
| | 410 | theModel.requireForceForDelete, |
| | 411 | model.expect, |
| | 412 | 'wp.api.models.' + model.name + '.requireForceForDelete should be ' + ( model.expect ? 'true' : 'false' ) + '.' |
| | 413 | ); |
| | 414 | |
| | 415 | // Trigger Qunit async completion. |
| | 416 | done(); |
| | 417 | } ); |
| | 418 | } ); |
| | 419 | } ); |
| | 420 | |
| | 421 | |
| 346 | 422 | } )( window.QUnit ); |