home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / js / api-request.js < prev    next >
Encoding:
JavaScript  |  2017-09-08  |  2.3 KB  |  88 lines

  1. /**
  2.  * Thin jQuery.ajax wrapper for WP REST API requests.
  3.  *
  4.  * Currently only applies to requests that do not use the `wp-api.js` Backbone
  5.  * client library, though this may change.  Serves several purposes:
  6.  *
  7.  * - Allows overriding these requests as needed by customized WP installations.
  8.  * - Sends the REST API nonce as a request header.
  9.  * - Allows specifying only an endpoint namespace/path instead of a full URL.
  10.  *
  11.  * @since     4.9.0
  12.  */
  13.  
  14. ( function( $ ) {
  15.     var wpApiSettings = window.wpApiSettings;
  16.  
  17.     function apiRequest( options ) {
  18.         options = apiRequest.buildAjaxOptions( options );
  19.         return apiRequest.transport( options );
  20.     }
  21.  
  22.     apiRequest.buildAjaxOptions = function( options ) {
  23.         var url = options.url;
  24.         var path = options.path;
  25.         var namespaceTrimmed, endpointTrimmed;
  26.         var headers, addNonceHeader, headerName;
  27.  
  28.         if (
  29.             typeof options.namespace === 'string' &&
  30.             typeof options.endpoint === 'string'
  31.         ) {
  32.             namespaceTrimmed = options.namespace.replace( /^\/|\/$/g, '' );
  33.             endpointTrimmed = options.endpoint.replace( /^\//, '' );
  34.             if ( endpointTrimmed ) {
  35.                 path = namespaceTrimmed + '/' + endpointTrimmed;
  36.             } else {
  37.                 path = namespaceTrimmed;
  38.             }
  39.         }
  40.         if ( typeof path === 'string' ) {
  41.             url = wpApiSettings.root + path.replace( /^\//, '' );
  42.         }
  43.  
  44.         // If ?_wpnonce=... is present, no need to add a nonce header.
  45.         addNonceHeader = ! ( options.data && options.data._wpnonce );
  46.  
  47.         headers = options.headers || {};
  48.  
  49.         // If an 'X-WP-Nonce' header (or any case-insensitive variation
  50.         // thereof) was specified, no need to add a nonce header.
  51.         if ( addNonceHeader ) {
  52.             for ( headerName in headers ) {
  53.                 if ( headers.hasOwnProperty( headerName ) ) {
  54.                     if ( headerName.toLowerCase() === 'x-wp-nonce' ) {
  55.                         addNonceHeader = false;
  56.                         break;
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.  
  62.         if ( addNonceHeader ) {
  63.             // Do not mutate the original headers object, if any.
  64.             headers = $.extend( {
  65.                 'X-WP-Nonce': wpApiSettings.nonce
  66.             }, headers );
  67.         }
  68.  
  69.         // Do not mutate the original options object.
  70.         options = $.extend( {}, options, {
  71.             headers: headers,
  72.             url: url
  73.         } );
  74.  
  75.         delete options.path;
  76.         delete options.namespace;
  77.         delete options.endpoint;
  78.  
  79.         return options;
  80.     };
  81.  
  82.     apiRequest.transport = $.ajax;
  83.  
  84.     /** @namespace wp */
  85.     window.wp = window.wp || {};
  86.     window.wp.apiRequest = apiRequest;
  87. } )( jQuery );
  88.