Make WordPress Core

Ticket #40422: 40422.3.diff

File 40422.3.diff, 2.9 KB (added by adamsilverstein, 8 years ago)
  • src/wp-includes/js/wp-api.js

    diff --git src/wp-includes/js/wp-api.js src/wp-includes/js/wp-api.js
    index c6ff5a872f..31886d280b 100644
     
    793793                                        model.unset( 'slug' );
    794794                                }
    795795
    796                                 if ( ! _.isUndefined( wpApiSettings.nonce ) && ! _.isNull( wpApiSettings.nonce ) ) {
     796                                if ( _.isFunction( model.nonce ) && ! _.isUndefined( model.nonce() ) && ! _.isNull( model.nonce() ) ) {
    797797                                        beforeSend = options.beforeSend;
    798798
    799799                                        // @todo enable option for jsonp endpoints
    800800                                        // options.dataType = 'jsonp';
    801801
     802                                        // Include the nonce with requests.
    802803                                        options.beforeSend = function( xhr ) {
    803                                                 xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
     804                                                xhr.setRequestHeader( 'X-WP-Nonce', model.nonce() );
    804805
    805806                                                if ( beforeSend ) {
    806807                                                        return beforeSend.apply( this, arguments );
    807808                                                }
    808809                                        };
     810
     811                                        // Update the nonce when a new nonce is returned with the response.
     812                                        options.complete = function( xhr ) {
     813                                                var returnedNonce = xhr.getResponseHeader( 'X-WP-Nonce' );
     814
     815                                                if ( returnedNonce && _.isFunction( model.nonce ) && model.nonce() !== returnedNonce ) {
     816                                                        model.endpointModel.set( 'nonce', returnedNonce );
     817                                                }
     818                                        };
    809819                                }
    810820
    811821                                // Add '?force=true' to use delete method when required.
     
    10481058                defaults: {
    10491059                        apiRoot: wpApiSettings.root,
    10501060                        versionString: wp.api.versionString,
     1061                        nonce: null,
    10511062                        schema: null,
    10521063                        models: {},
    10531064                        collections: {}
     
    10651076                        model.schemaConstructed = deferred.promise();
    10661077
    10671078                        model.schemaModel = new wp.api.models.Schema( null, {
    1068                                 apiRoot: model.get( 'apiRoot' ),
    1069                                 versionString: model.get( 'versionString' )
     1079                                apiRoot:       model.get( 'apiRoot' ),
     1080                                versionString: model.get( 'versionString' ),
     1081                                nonce:         model.get( 'nonce' )
    10701082                        } );
    10711083
    10721084                        // When the model loads, resolve the promise.
     
    12381250                                                        return url;
    12391251                                                },
    12401252
     1253                                                // Track nonces on the Endpoint 'routeModel'.
     1254                                                nonce: function() {
     1255                                                        return routeModel.get( 'nonce' );
     1256                                                },
     1257
     1258                                                endpointModel: routeModel,
     1259
    12411260                                                // Include a reference to the original route object.
    12421261                                                route: modelRoute,
    12431262
     
    12841303                                                        return url;
    12851304                                                },
    12861305
     1306                                                // Track nonces at the Endpoint level.
     1307                                                nonce: function() {
     1308                                                        return routeModel.get( 'nonce' );
     1309                                                },
     1310
     1311                                                endpointModel: routeModel,
     1312
    12871313                                                // Include a reference to the original route object.
    12881314                                                route: modelRoute,
    12891315
     
    14051431                var endpoint, attributes = {}, deferred, promise;
    14061432
    14071433                args                      = args || {};
     1434                attributes.nonce          = args.nonce || wpApiSettings.nonce || '';
    14081435                attributes.apiRoot        = args.apiRoot || wpApiSettings.root || '/wp-json';
    14091436                attributes.versionString  = args.versionString || wpApiSettings.versionString || 'wp/v2/';
    14101437                attributes.schema         = args.schema || null;