mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-23 09:37:42 +01:00
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:
parent
aaf1fc64c4
commit
3138d89e1e
@ -990,18 +990,28 @@
|
|||||||
var beforeSend, success,
|
var beforeSend, success,
|
||||||
self = this;
|
self = this;
|
||||||
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
beforeSend = options.beforeSend;
|
|
||||||
|
|
||||||
// If we have a localized nonce, pass that along with each sync.
|
if ( _.isFunction( model.nonce ) && ! _.isUndefined( model.nonce() ) && ! _.isNull( model.nonce() ) ) {
|
||||||
if ( 'undefined' !== typeof wpApiSettings.nonce ) {
|
beforeSend = options.beforeSend;
|
||||||
|
|
||||||
|
// Include the nonce with requests.
|
||||||
options.beforeSend = function( xhr ) {
|
options.beforeSend = function( xhr ) {
|
||||||
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
|
xhr.setRequestHeader( 'X-WP-Nonce', model.nonce() );
|
||||||
|
|
||||||
if ( beforeSend ) {
|
if ( beforeSend ) {
|
||||||
return beforeSend.apply( self, arguments );
|
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.
|
// When reading, add pagination data.
|
||||||
@ -1405,6 +1415,13 @@
|
|||||||
return new loadingObjects.models[ modelClassName ]( attrs, options );
|
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.
|
// Include a reference to the original class name.
|
||||||
name: collectionClassName,
|
name: collectionClassName,
|
||||||
|
|
||||||
@ -1432,6 +1449,13 @@
|
|||||||
return new loadingObjects.models[ modelClassName ]( attrs, options );
|
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.
|
// Include a reference to the original class name.
|
||||||
name: collectionClassName,
|
name: collectionClassName,
|
||||||
|
|
||||||
|
2
wp-includes/js/wp-api.min.js
vendored
2
wp-includes/js/wp-api.min.js
vendored
File diff suppressed because one or more lines are too long
@ -4,7 +4,7 @@
|
|||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @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.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
Loading…
Reference in New Issue
Block a user