[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/ -> wp-util.js (source)

   1  /**
   2   * @output wp-includes/js/wp-util.js
   3   */
   4  
   5  /* global _wpUtilSettings */
   6  
   7  /** @namespace wp */
   8  window.wp = window.wp || {};
   9  
  10  (function ($) {
  11      // Check for the utility settings.
  12      var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings;
  13  
  14      /**
  15       * wp.template( id )
  16       *
  17       * Fetch a JavaScript template for an id, and return a templating function for it.
  18       *
  19       * @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-".
  20       *                    For example, "attachment" maps to "tmpl-attachment".
  21       * @return {function} A function that lazily-compiles the template requested.
  22       */
  23      wp.template = _.memoize(function ( id ) {
  24          var compiled,
  25              /*
  26               * Underscore's default ERB-style templates are incompatible with PHP
  27               * when asp_tags is enabled, so WordPress uses Mustache-inspired templating syntax.
  28               *
  29               * @see trac ticket #22344.
  30               */
  31              options = {
  32                  evaluate:    /<#([\s\S]+?)#>/g,
  33                  interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
  34                  escape:      /\{\{([^\}]+?)\}\}(?!\})/g,
  35                  variable:    'data'
  36              };
  37  
  38          return function ( data ) {
  39              compiled = compiled || _.template( $( '#tmpl-' + id ).html(),  options );
  40              return compiled( data );
  41          };
  42      });
  43  
  44      /*
  45       * wp.ajax
  46       * ------
  47       *
  48       * Tools for sending ajax requests with JSON responses and built in error handling.
  49       * Mirrors and wraps jQuery's ajax APIs.
  50       */
  51      wp.ajax = {
  52          settings: settings.ajax || {},
  53  
  54          /**
  55           * wp.ajax.post( [action], [data] )
  56           *
  57           * Sends a POST request to WordPress.
  58           *
  59           * @param {(string|Object)} action The slug of the action to fire in WordPress or options passed
  60           *                                 to jQuery.ajax.
  61           * @param {Object=}         data   Optional. The data to populate $_POST with.
  62           * @return {$.promise} A jQuery promise that represents the request,
  63           *                     decorated with an abort() method.
  64           */
  65          post: function( action, data ) {
  66              return wp.ajax.send({
  67                  data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
  68              });
  69          },
  70  
  71          /**
  72           * wp.ajax.send( [action], [options] )
  73           *
  74           * Sends a POST request to WordPress.
  75           *
  76           * @param {(string|Object)} action  The slug of the action to fire in WordPress or options passed
  77           *                                  to jQuery.ajax.
  78           * @param {Object=}         options Optional. The options passed to jQuery.ajax.
  79           * @return {$.promise} A jQuery promise that represents the request,
  80           *                     decorated with an abort() method.
  81           */
  82          send: function( action, options ) {
  83              var promise, deferred;
  84              if ( _.isObject( action ) ) {
  85                  options = action;
  86              } else {
  87                  options = options || {};
  88                  options.data = _.extend( options.data || {}, { action: action });
  89              }
  90  
  91              options = _.defaults( options || {}, {
  92                  type:    'POST',
  93                  url:     wp.ajax.settings.url,
  94                  context: this
  95              });
  96  
  97              deferred = $.Deferred( function( deferred ) {
  98                  // Transfer success/error callbacks.
  99                  if ( options.success ) {
 100                      deferred.done( options.success );
 101                  }
 102  
 103                  if ( options.error ) {
 104                      deferred.fail( options.error );
 105                  }
 106  
 107                  delete options.success;
 108                  delete options.error;
 109  
 110                  // Use with PHP's wp_send_json_success() and wp_send_json_error().
 111                  deferred.jqXHR = $.ajax( options ).done( function( response ) {
 112                      // Treat a response of 1 as successful for backward compatibility with existing handlers.
 113                      if ( response === '1' || response === 1 ) {
 114                          response = { success: true };
 115                      }
 116  
 117                      if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) {
 118  
 119                          // When handling a media attachments request, get the total attachments from response headers.
 120                          var context = this;
 121                          deferred.done( function() {
 122                              if (
 123                                  action &&
 124                                  action.data &&
 125                                  'query-attachments' === action.data.action &&
 126                                  deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) &&
 127                                  deferred.jqXHR.getResponseHeader( 'X-WP-Total' )
 128                              ) {
 129                                  context.totalAttachments = parseInt( deferred.jqXHR.getResponseHeader( 'X-WP-Total' ), 10 );
 130                              } else {
 131                                  context.totalAttachments = 0;
 132                              }
 133                          } );
 134                          deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
 135                      } else {
 136                          deferred.rejectWith( this, [response] );
 137                      }
 138                  }).fail( function() {
 139                      deferred.rejectWith( this, arguments );
 140                  });
 141              });
 142  
 143              promise = deferred.promise();
 144              promise.abort = function() {
 145                  deferred.jqXHR.abort();
 146                  return this;
 147              };
 148  
 149              return promise;
 150          }
 151      };
 152  
 153  }(jQuery));


Generated: Sun Oct 13 01:00:02 2024 Cross-referenced by PHPXref 0.7.1