[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/ -> api-request.js (source)

   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   * @output wp-includes/js/api-request.js
  13   */
  14  
  15  ( function( $ ) {
  16      var wpApiSettings = window.wpApiSettings;
  17  
  18  	function apiRequest( options ) {
  19          options = apiRequest.buildAjaxOptions( options );
  20          return apiRequest.transport( options );
  21      }
  22  
  23      apiRequest.buildAjaxOptions = function( options ) {
  24          var url = options.url;
  25          var path = options.path;
  26          var namespaceTrimmed, endpointTrimmed, apiRoot;
  27          var headers, addNonceHeader, headerName;
  28  
  29          if (
  30              typeof options.namespace === 'string' &&
  31              typeof options.endpoint === 'string'
  32          ) {
  33              namespaceTrimmed = options.namespace.replace( /^\/|\/$/g, '' );
  34              endpointTrimmed = options.endpoint.replace( /^\//, '' );
  35              if ( endpointTrimmed ) {
  36                  path = namespaceTrimmed + '/' + endpointTrimmed;
  37              } else {
  38                  path = namespaceTrimmed;
  39              }
  40          }
  41          if ( typeof path === 'string' ) {
  42              apiRoot = wpApiSettings.root;
  43              path = path.replace( /^\//, '' );
  44  
  45              // API root may already include query parameter prefix if site is
  46              // configured to use plain permalinks.
  47              if ( 'string' === typeof apiRoot && -1 !== apiRoot.indexOf( '?' ) ) {
  48                  path = path.replace( '?', '&' );
  49              }
  50  
  51              url = apiRoot + path;
  52          }
  53  
  54          // If ?_wpnonce=... is present, no need to add a nonce header.
  55          addNonceHeader = ! ( options.data && options.data._wpnonce );
  56  
  57          headers = options.headers || {};
  58  
  59          // If an 'X-WP-Nonce' header (or any case-insensitive variation
  60          // thereof) was specified, no need to add a nonce header.
  61          if ( addNonceHeader ) {
  62              for ( headerName in headers ) {
  63                  if ( headers.hasOwnProperty( headerName ) ) {
  64                      if ( headerName.toLowerCase() === 'x-wp-nonce' ) {
  65                          addNonceHeader = false;
  66                          break;
  67                      }
  68                  }
  69              }
  70          }
  71  
  72          if ( addNonceHeader ) {
  73              // Do not mutate the original headers object, if any.
  74              headers = $.extend( {
  75                  'X-WP-Nonce': wpApiSettings.nonce
  76              }, headers );
  77          }
  78  
  79          // Do not mutate the original options object.
  80          options = $.extend( {}, options, {
  81              headers: headers,
  82              url: url
  83          } );
  84  
  85          delete options.path;
  86          delete options.namespace;
  87          delete options.endpoint;
  88  
  89          return options;
  90      };
  91  
  92      apiRequest.transport = $.ajax;
  93  
  94      /** @namespace wp */
  95      window.wp = window.wp || {};
  96      window.wp.apiRequest = apiRequest;
  97  } )( jQuery );


Generated: Mon Sep 16 01:00:03 2019 Cross-referenced by PHPXref 0.7.1