1 /* global _wpUtilSettings */
2 window.wp = window.wp || {};
5 // Check for the utility settings.
6 var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings;
11 * Fetch a JavaScript template for an id, and return a templating function for it.
13 * @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-".
14 * For example, "attachment" maps to "tmpl-attachment".
15 * @return {function} A function that lazily-compiles the template requested.
17 wp.template = _.memoize(function ( id ) {
20 * Underscore's default ERB-style templates are incompatible with PHP
21 * when asp_tags is enabled, so WordPress uses Mustache-inspired templating syntax.
23 * @see trac ticket #22344.
26 evaluate: /<#([\s\S]+?)#>/g,
27 interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
28 escape: /\{\{([^\}]+?)\}\}(?!\})/g,
32 return function ( data ) {
33 compiled = compiled || _.template( $( '#tmpl-' + id ).html(), options );
34 return compiled( data );
41 // Tools for sending ajax requests with JSON responses and built in error handling.
42 // Mirrors and wraps jQuery's ajax APIs.
44 settings: settings.ajax || {},
47 * wp.ajax.post( [action], [data] )
49 * Sends a POST request to WordPress.
51 * @param {(string|object)} action The slug of the action to fire in WordPress or options passed
53 * @param {object=} data Optional. The data to populate $_POST with.
54 * @return {$.promise} A jQuery promise that represents the request,
55 * decorated with an abort() method.
57 post: function( action, data ) {
59 data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
64 * wp.ajax.send( [action], [options] )
66 * Sends a POST request to WordPress.
68 * @param {(string|object)} action The slug of the action to fire in WordPress or options passed
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.
74 send: function( action, options ) {
75 var promise, deferred;
76 if ( _.isObject( action ) ) {
79 options = options || {};
80 options.data = _.extend( options.data || {}, { action: action });
83 options = _.defaults( options || {}, {
85 url: wp.ajax.settings.url,
89 deferred = $.Deferred( function( deferred ) {
90 // Transfer success/error callbacks.
91 if ( options.success )
92 deferred.done( options.success );
94 deferred.fail( options.error );
96 delete options.success;
99 // Use with PHP's wp_send_json_success() and wp_send_json_error()
100 deferred.jqXHR = $.ajax( options ).done( function( response ) {
101 // Treat a response of 1 as successful for backward compatibility with existing handlers.
102 if ( response === '1' || response === 1 )
103 response = { success: true };
105 if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
106 deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
108 deferred.rejectWith( this, [response] );
109 }).fail( function() {
110 deferred.rejectWith( this, arguments );
114 promise = deferred.promise();
115 promise.abort = function() {
116 deferred.jqXHR.abort();