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.
 
 
 
 
 

125 lines
3.8 KiB

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