You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.6 KiB

1 year ago
  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. if ( ! document.getElementById( 'tmpl-' + id ) ) {
  35. throw new Error( 'Template not found: ' + '#tmpl-' + id );
  36. }
  37. compiled = compiled || _.template( $( '#tmpl-' + id ).html(), options );
  38. return compiled( data );
  39. };
  40. });
  41. /*
  42. * wp.ajax
  43. * ------
  44. *
  45. * Tools for sending ajax requests with JSON responses and built in error handling.
  46. * Mirrors and wraps jQuery's ajax APIs.
  47. */
  48. wp.ajax = {
  49. settings: settings.ajax || {},
  50. /**
  51. * wp.ajax.post( [action], [data] )
  52. *
  53. * Sends a POST request to WordPress.
  54. *
  55. * @param {(string|Object)} action The slug of the action to fire in WordPress or options passed
  56. * to jQuery.ajax.
  57. * @param {Object=} data Optional. The data to populate $_POST with.
  58. * @return {$.promise} A jQuery promise that represents the request,
  59. * decorated with an abort() method.
  60. */
  61. post: function( action, data ) {
  62. return wp.ajax.send({
  63. data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
  64. });
  65. },
  66. /**
  67. * wp.ajax.send( [action], [options] )
  68. *
  69. * Sends a POST request to WordPress.
  70. *
  71. * @param {(string|Object)} action The slug of the action to fire in WordPress or options passed
  72. * to jQuery.ajax.
  73. * @param {Object=} options Optional. The options passed to jQuery.ajax.
  74. * @return {$.promise} A jQuery promise that represents the request,
  75. * decorated with an abort() method.
  76. */
  77. send: function( action, options ) {
  78. var promise, deferred;
  79. if ( _.isObject( action ) ) {
  80. options = action;
  81. } else {
  82. options = options || {};
  83. options.data = _.extend( options.data || {}, { action: action });
  84. }
  85. options = _.defaults( options || {}, {
  86. type: 'POST',
  87. url: wp.ajax.settings.url,
  88. context: this
  89. });
  90. deferred = $.Deferred( function( deferred ) {
  91. // Transfer success/error callbacks.
  92. if ( options.success ) {
  93. deferred.done( options.success );
  94. }
  95. if ( options.error ) {
  96. deferred.fail( options.error );
  97. }
  98. delete options.success;
  99. delete options.error;
  100. // Use with PHP's wp_send_json_success() and wp_send_json_error().
  101. deferred.jqXHR = $.ajax( options ).done( function( response ) {
  102. // Treat a response of 1 as successful for backward compatibility with existing handlers.
  103. if ( response === '1' || response === 1 ) {
  104. response = { success: true };
  105. }
  106. if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) {
  107. // When handling a media attachments request, get the total attachments from response headers.
  108. var context = this;
  109. deferred.done( function() {
  110. if (
  111. action &&
  112. action.data &&
  113. 'query-attachments' === action.data.action &&
  114. deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) &&
  115. deferred.jqXHR.getResponseHeader( 'X-WP-Total' )
  116. ) {
  117. context.totalAttachments = parseInt( deferred.jqXHR.getResponseHeader( 'X-WP-Total' ), 10 );
  118. } else {
  119. context.totalAttachments = 0;
  120. }
  121. } );
  122. deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
  123. } else {
  124. deferred.rejectWith( this, [response] );
  125. }
  126. }).fail( function() {
  127. deferred.rejectWith( this, arguments );
  128. });
  129. });
  130. promise = deferred.promise();
  131. promise.abort = function() {
  132. deferred.jqXHR.abort();
  133. return this;
  134. };
  135. return promise;
  136. }
  137. };
  138. }(jQuery));