wp-util.js 4.5 KB

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