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.

Merge of [42851] to the 4.9 branch.

Props adamsilverstein, ocean90, swissspidy.
See #43265.
Built from https://develop.svn.wordpress.org/branches/4.9@42853


git-svn-id: http://core.svn.wordpress.org/branches/4.9@42683 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2018-03-18 20:00:40 +00:00
parent baba344254
commit 7faddf9e50
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 = '4.9.5-alpha-42850';
$wp_version = '4.9.5-alpha-42853';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.