[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/js/ -> bp-api-request.js (source)

   1  /**
   2   * jQuery.ajax wrapper for BP REST API requests.
   3   *
   4   * @since  5.0.0
   5   * @output bp-core/js/bp-api-request.js
   6   */
   7  /* global bpApiSettings */
   8  window.bp = window.bp || {};
   9  
  10  ( function( wp, bp, $ ) {
  11      // Bail if not set
  12      if ( typeof bpApiSettings === 'undefined' ) {
  13          return;
  14      }
  15  
  16      bp.isRestEnabled = true;
  17  
  18      // Polyfill wp.apiRequest if WordPress < 4.9
  19      bp.apiRequest = function( options ) {
  20          var bpRequest;
  21  
  22          if ( ! options.dataType ) {
  23              options.dataType = 'json';
  24          }
  25  
  26          // WordPress is >= 4.9.0.
  27          if ( wp.apiRequest ) {
  28              bpRequest = wp.apiRequest( options );
  29  
  30          // WordPress is < 4.9.0.
  31          } else {
  32              var url = bpApiSettings.root;
  33  
  34              if ( options.path ) {
  35                  url = url + options.path.replace( /^\//, '' );
  36              }
  37  
  38              if ( ! options.url ) {
  39                  options.url = url;
  40              }
  41  
  42              // Add The nonce only when needed.
  43              if ( -1 !== options.url.indexOf( url ) ) {
  44                  options.beforeSend = function( xhr ) {
  45                      xhr.setRequestHeader( 'X-WP-Nonce', bpApiSettings.nonce );
  46                  };
  47              }
  48  
  49              bpRequest = $.ajax( options );
  50          }
  51  
  52          return bpRequest.then( null, function( result ) {
  53              var errorObject = {
  54                  code: 'unexpected_error',
  55                  message: bpApiSettings.unexpectedError,
  56                  data: {
  57                      status: 404
  58                  }
  59              };
  60  
  61              if ( result && result.responseJSON ) {
  62                  errorObject = result.responseJSON;
  63              }
  64  
  65              return errorObject;
  66          } );
  67      };
  68  
  69  } )( window.wp || {}, window.bp, jQuery );


Generated: Sat Nov 16 01:01:36 2019 Cross-referenced by PHPXref 0.7.1