2013-05-26 08:43:13 +02:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
|
|
|
(function ($) {
|
2013-05-26 08:58:01 +02:00
|
|
|
// Check for the utility settings.
|
|
|
|
var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings;
|
|
|
|
|
2013-05-26 08:43:13 +02:00
|
|
|
/**
|
|
|
|
* wp.template( id )
|
|
|
|
*
|
|
|
|
* Fetches a template by id.
|
|
|
|
*
|
|
|
|
* @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-".
|
|
|
|
* For example, "attachment" maps to "tmpl-attachment".
|
|
|
|
* @return {function} A function that lazily-compiles the template requested.
|
|
|
|
*/
|
|
|
|
wp.template = _.memoize(function ( id ) {
|
|
|
|
var compiled,
|
|
|
|
options = {
|
|
|
|
evaluate: /<#([\s\S]+?)#>/g,
|
|
|
|
interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
|
|
|
|
escape: /\{\{([^\}]+?)\}\}(?!\})/g,
|
|
|
|
variable: 'data'
|
|
|
|
};
|
|
|
|
|
|
|
|
return function ( data ) {
|
|
|
|
compiled = compiled || _.template( $( '#tmpl-' + id ).html(), null, options );
|
|
|
|
return compiled( data );
|
|
|
|
};
|
|
|
|
});
|
2013-05-26 08:58:01 +02:00
|
|
|
|
2013-07-11 02:20:36 +02:00
|
|
|
// wp.ajax
|
2013-05-26 08:58:01 +02:00
|
|
|
// ------
|
|
|
|
//
|
|
|
|
// Tools for sending ajax requests with JSON responses and built in error handling.
|
|
|
|
// Mirrors and wraps jQuery's ajax APIs.
|
2013-07-11 02:20:36 +02:00
|
|
|
wp.ajax = {
|
|
|
|
settings: settings.ajax || {},
|
2013-05-26 08:58:01 +02:00
|
|
|
|
|
|
|
/**
|
2013-07-11 02:20:36 +02:00
|
|
|
* wp.ajax.post( [action], [data] )
|
2013-05-26 08:58:01 +02:00
|
|
|
*
|
|
|
|
* Sends a POST request to WordPress.
|
|
|
|
*
|
|
|
|
* @param {string} action The slug of the action to fire in WordPress.
|
|
|
|
* @param {object} data The data to populate $_POST with.
|
|
|
|
* @return {$.promise} A jQuery promise that represents the request.
|
|
|
|
*/
|
|
|
|
post: function( action, data ) {
|
2013-07-11 02:20:36 +02:00
|
|
|
return wp.ajax.send({
|
2013-05-26 08:58:01 +02:00
|
|
|
data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-07-11 02:20:36 +02:00
|
|
|
* wp.ajax.send( [action], [options] )
|
2013-05-26 08:58:01 +02:00
|
|
|
*
|
|
|
|
* Sends a POST request to WordPress.
|
|
|
|
*
|
|
|
|
* @param {string} action The slug of the action to fire in WordPress.
|
|
|
|
* @param {object} options The options passed to jQuery.ajax.
|
|
|
|
* @return {$.promise} A jQuery promise that represents the request.
|
|
|
|
*/
|
|
|
|
send: function( action, options ) {
|
|
|
|
if ( _.isObject( action ) ) {
|
|
|
|
options = action;
|
|
|
|
} else {
|
|
|
|
options = options || {};
|
|
|
|
options.data = _.extend( options.data || {}, { action: action });
|
|
|
|
}
|
|
|
|
|
|
|
|
options = _.defaults( options || {}, {
|
|
|
|
type: 'POST',
|
2013-07-11 02:20:36 +02:00
|
|
|
url: wp.ajax.settings.url,
|
2013-05-26 08:58:01 +02:00
|
|
|
context: this
|
|
|
|
});
|
|
|
|
|
|
|
|
return $.Deferred( function( deferred ) {
|
|
|
|
// Transfer success/error callbacks.
|
|
|
|
if ( options.success )
|
|
|
|
deferred.done( options.success );
|
|
|
|
if ( options.error )
|
|
|
|
deferred.fail( options.error );
|
|
|
|
|
|
|
|
delete options.success;
|
|
|
|
delete options.error;
|
|
|
|
|
|
|
|
// Use with PHP's wp_send_json_success() and wp_send_json_error()
|
|
|
|
$.ajax( options ).done( function( response ) {
|
|
|
|
// Treat a response of `1` as successful for backwards
|
|
|
|
// compatibility with existing handlers.
|
|
|
|
if ( response === '1' || response === 1 )
|
|
|
|
response = { success: true };
|
|
|
|
|
|
|
|
if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
|
|
|
|
deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
|
|
|
|
else
|
|
|
|
deferred.rejectWith( this, [response] );
|
|
|
|
}).fail( function() {
|
|
|
|
deferred.rejectWith( this, arguments );
|
|
|
|
});
|
|
|
|
}).promise();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-26 08:43:13 +02:00
|
|
|
}(jQuery));
|