Advertisement
Guest User

Untitled

a guest
Dec 20th, 2017
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.              * Set nonce header before every Backbone sync.
  3.              *
  4.              * @param {string} method.
  5.              * @param {Backbone.Model} model.
  6.              * @param {{beforeSend}, *} options.
  7.              * @returns {*}.
  8.              */
  9.             sync: function( method, model, options ) {
  10.                 var beforeSend;
  11.  
  12.                 options = options || {};
  13.  
  14.                 // Remove date_gmt if null.
  15.                 if ( _.isNull( model.get( 'date_gmt' ) ) ) {
  16.                     model.unset( 'date_gmt' );
  17.                 }
  18.  
  19.                 // Remove slug if empty.
  20.                 if ( _.isEmpty( model.get( 'slug' ) ) ) {
  21.                     model.unset( 'slug' );
  22.                 }
  23.  
  24.                 if ( _.isFunction( model.nonce ) && ! _.isUndefined( model.nonce() ) && ! _.isNull( model.nonce() ) ) {
  25.                     beforeSend = options.beforeSend;
  26.  
  27.                     // @todo enable option for jsonp endpoints
  28.                     // options.dataType = 'jsonp';
  29.  
  30.                     // Include the nonce with requests.
  31.                     options.beforeSend = function( xhr ) {
  32.                         xhr.setRequestHeader( 'X-WP-Nonce', model.nonce() );
  33.  
  34.                         if ( beforeSend ) {
  35.                             return beforeSend.apply( this, arguments );
  36.                         }
  37.                     };
  38.  
  39.                     // Update the nonce when a new nonce is returned with the response.
  40.                     options.complete = function( xhr ) {
  41.                         var returnedNonce = xhr.getResponseHeader( 'X-WP-Nonce' );
  42.  
  43.                         if ( returnedNonce && _.isFunction( model.nonce ) && model.nonce() !== returnedNonce ) {
  44.                             model.endpointModel.set( 'nonce', returnedNonce );
  45.                         }
  46.                     };
  47.                 }
  48.  
  49.                 // Add '?force=true' to use delete method when required.
  50.                 if ( this.requireForceForDelete && 'delete' === method ) {
  51.                     model.url = model.url() + '?force=true';
  52.                 }
  53.                 return Backbone.sync( method, model, options );
  54.             },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement