REST API JS Client: Extend custom nonce functionality to collections.

This brings the improved nonce handling from [41553] to collections to remove the direct `wpApiSettings.nonce` dependency.

Props adamsilverstein, ocean90, swissspidy.
Fixes #43265.
Built from https://develop.svn.wordpress.org/trunk@42851


git-svn-id: http://core.svn.wordpress.org/trunk@42681 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2018-03-18 17:04:30 +00:00
parent aaf1fc64c4
commit 3138d89e1e
3 changed files with 31 additions and 7 deletions

View File

@ -990,18 +990,28 @@
var beforeSend, success,
self = this;
options = options || {};
beforeSend = options.beforeSend;
options = options || {};
// If we have a localized nonce, pass that along with each sync.
if ( 'undefined' !== typeof wpApiSettings.nonce ) {
if ( _.isFunction( model.nonce ) && ! _.isUndefined( model.nonce() ) && ! _.isNull( model.nonce() ) ) {
beforeSend = options.beforeSend;
// Include the nonce with requests.
options.beforeSend = function( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
xhr.setRequestHeader( 'X-WP-Nonce', model.nonce() );
if ( beforeSend ) {
return beforeSend.apply( self, arguments );
}
};
// Update the nonce when a new nonce is returned with the response.
options.complete = function( xhr ) {
var returnedNonce = xhr.getResponseHeader( 'X-WP-Nonce' );
if ( returnedNonce && _.isFunction( model.nonce ) && model.nonce() !== returnedNonce ) {
model.endpointModel.set( 'nonce', returnedNonce );
}
};
}
// When reading, add pagination data.
@ -1405,6 +1415,13 @@
return new loadingObjects.models[ modelClassName ]( attrs, options );
},
// Track nonces at the Endpoint level.
nonce: function() {
return routeModel.get( 'nonce' );
},
endpointModel: routeModel,
// Include a reference to the original class name.
name: collectionClassName,
@ -1432,6 +1449,13 @@
return new loadingObjects.models[ modelClassName ]( attrs, options );
},
// Track nonces at the Endpoint level.
nonce: function() {
return routeModel.get( 'nonce' );
},
endpointModel: routeModel,
// Include a reference to the original class name.
name: collectionClassName,

File diff suppressed because one or more lines are too long

View File

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