[ 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   * @since 5.6.0 Added overriding of the "PUT" and "DELETE" methods with "POST".
  13   *              Added an "application/json" Accept header to all requests.
  14   * @output wp-includes/js/api-request.js
  15   */
  16  
  17  ( function( $ ) {
  18      var wpApiSettings = window.wpApiSettings;
  19  
  20  	function apiRequest( options ) {
  21          options = apiRequest.buildAjaxOptions( options );
  22          return apiRequest.transport( options );
  23      }
  24  
  25      apiRequest.buildAjaxOptions = function( options ) {
  26          var url = options.url;
  27          var path = options.path;
  28          var method = options.method;
  29          var namespaceTrimmed, endpointTrimmed, apiRoot;
  30          var headers, addNonceHeader, addAcceptHeader, headerName;
  31  
  32          if (
  33              typeof options.namespace === 'string' &&
  34              typeof options.endpoint === 'string'
  35          ) {
  36              namespaceTrimmed = options.namespace.replace( /^\/|\/$/g, '' );
  37              endpointTrimmed = options.endpoint.replace( /^\//, '' );
  38              if ( endpointTrimmed ) {
  39                  path = namespaceTrimmed + '/' + endpointTrimmed;
  40              } else {
  41                  path = namespaceTrimmed;
  42              }
  43          }
  44          if ( typeof path === 'string' ) {
  45              apiRoot = wpApiSettings.root;
  46              path = path.replace( /^\//, '' );
  47  
  48              // API root may already include query parameter prefix
  49              // if site is configured to use plain permalinks.
  50              if ( 'string' === typeof apiRoot && -1 !== apiRoot.indexOf( '?' ) ) {
  51                  path = path.replace( '?', '&' );
  52              }
  53  
  54              url = apiRoot + path;
  55          }
  56  
  57          // If ?_wpnonce=... is present, no need to add a nonce header.
  58          addNonceHeader = ! ( options.data && options.data._wpnonce );
  59          addAcceptHeader = true;
  60  
  61          headers = options.headers || {};
  62  
  63          for ( headerName in headers ) {
  64              if ( ! headers.hasOwnProperty( headerName ) ) {
  65                  continue;
  66              }
  67  
  68              // If an 'X-WP-Nonce' or 'Accept' header (or any case-insensitive variation
  69              // thereof) was specified, no need to add the header again.
  70              switch ( headerName.toLowerCase() ) {
  71                  case 'x-wp-nonce':
  72                      addNonceHeader = false;
  73                      break;
  74                  case 'accept':
  75                      addAcceptHeader = false;
  76                      break;
  77              }
  78          }
  79  
  80          if ( addNonceHeader ) {
  81              // Do not mutate the original headers object, if any.
  82              headers = $.extend( {
  83                  'X-WP-Nonce': wpApiSettings.nonce
  84              }, headers );
  85          }
  86  
  87          if ( addAcceptHeader ) {
  88              headers = $.extend( {
  89                  'Accept': 'application/json, */*;q=0.1'
  90              }, headers );
  91          }
  92  
  93          if ( typeof method === 'string' ) {
  94              method = method.toUpperCase();
  95  
  96              if ( 'PUT' === method || 'DELETE' === method ) {
  97                  headers = $.extend( {
  98                      'X-HTTP-Method-Override': method
  99                  }, headers );
 100  
 101                  method = 'POST';
 102              }
 103          }
 104  
 105          // Do not mutate the original options object.
 106          options = $.extend( {}, options, {
 107              headers: headers,
 108              url: url,
 109              method: method
 110          } );
 111  
 112          delete options.path;
 113          delete options.namespace;
 114          delete options.endpoint;
 115  
 116          return options;
 117      };
 118  
 119      apiRequest.transport = $.ajax;
 120  
 121      /** @namespace wp */
 122      window.wp = window.wp || {};
 123      window.wp.apiRequest = apiRequest;
 124  } )( jQuery );


Generated: Mon Oct 7 01:00:03 2024 Cross-referenced by PHPXref 0.7.1