2018-06-28 04:30:15 +02:00
|
|
|
/**
|
|
|
|
* @output wp-includes/js/wp-util.js
|
|
|
|
*/
|
|
|
|
|
2013-11-15 07:24:10 +01:00
|
|
|
/* global _wpUtilSettings */
|
2017-09-08 20:42:49 +02:00
|
|
|
|
|
|
|
/** @namespace wp */
|
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 )
|
|
|
|
*
|
2014-11-05 20:39:22 +01:00
|
|
|
* Fetch a JavaScript template for an id, and return a templating function for it.
|
2013-05-26 08:43:13 +02:00
|
|
|
*
|
|
|
|
* @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,
|
2014-11-05 20:39:22 +01:00
|
|
|
/*
|
|
|
|
* Underscore's default ERB-style templates are incompatible with PHP
|
|
|
|
* when asp_tags is enabled, so WordPress uses Mustache-inspired templating syntax.
|
|
|
|
*
|
|
|
|
* @see trac ticket #22344.
|
|
|
|
*/
|
2013-05-26 08:43:13 +02:00
|
|
|
options = {
|
|
|
|
evaluate: /<#([\s\S]+?)#>/g,
|
|
|
|
interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
|
|
|
|
escape: /\{\{([^\}]+?)\}\}(?!\})/g,
|
|
|
|
variable: 'data'
|
|
|
|
};
|
|
|
|
|
|
|
|
return function ( data ) {
|
2016-06-26 16:26:29 +02:00
|
|
|
compiled = compiled || _.template( $( '#tmpl-' + id ).html(), options );
|
2013-05-26 08:43:13 +02:00
|
|
|
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.
|
|
|
|
*
|
Update/Install: Shiny Updates v2.
Gone are the days of isolation and feelings of "meh", brought on by The Bleak Screen of Sadness. For a shiny knight has arrived to usher our plugins and themes along their arduous journey of installation, updates, and the inevitable fate of ultimate deletion.
Props swissspidy, adamsilverstein, mapk, afragen, ocean90, ryelle, j-falk, michael-arestad, melchoyce, DrewAPicture, AdamSoucie, ethitter, pento, dd32, kraftbj, Ipstenu, jorbin, afercia, stephdau, paulwilde, jipmoors, khag7, svovaf, jipmoors, obenland.
Fixes #22029, #25828, #31002, #31529, #31530, #31773, #33637, #35032.
Built from https://develop.svn.wordpress.org/trunk@37714
git-svn-id: http://core.svn.wordpress.org/trunk@37680 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-06-15 18:37:29 +02:00
|
|
|
* @param {(string|object)} action The slug of the action to fire in WordPress or options passed
|
|
|
|
* to jQuery.ajax.
|
|
|
|
* @param {object=} data Optional. The data to populate $_POST with.
|
2015-06-13 16:30:26 +02:00
|
|
|
* @return {$.promise} A jQuery promise that represents the request,
|
|
|
|
* decorated with an abort() method.
|
2013-05-26 08:58:01 +02:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
Update/Install: Shiny Updates v2.
Gone are the days of isolation and feelings of "meh", brought on by The Bleak Screen of Sadness. For a shiny knight has arrived to usher our plugins and themes along their arduous journey of installation, updates, and the inevitable fate of ultimate deletion.
Props swissspidy, adamsilverstein, mapk, afragen, ocean90, ryelle, j-falk, michael-arestad, melchoyce, DrewAPicture, AdamSoucie, ethitter, pento, dd32, kraftbj, Ipstenu, jorbin, afercia, stephdau, paulwilde, jipmoors, khag7, svovaf, jipmoors, obenland.
Fixes #22029, #25828, #31002, #31529, #31530, #31773, #33637, #35032.
Built from https://develop.svn.wordpress.org/trunk@37714
git-svn-id: http://core.svn.wordpress.org/trunk@37680 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-06-15 18:37:29 +02:00
|
|
|
* @param {(string|object)} action The slug of the action to fire in WordPress or options passed
|
|
|
|
* to jQuery.ajax.
|
|
|
|
* @param {object=} options Optional. The options passed to jQuery.ajax.
|
2015-06-13 16:30:26 +02:00
|
|
|
* @return {$.promise} A jQuery promise that represents the request,
|
|
|
|
* decorated with an abort() method.
|
2013-05-26 08:58:01 +02:00
|
|
|
*/
|
|
|
|
send: function( action, options ) {
|
2015-06-13 16:30:26 +02:00
|
|
|
var promise, deferred;
|
2013-05-26 08:58:01 +02:00
|
|
|
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
|
|
|
|
});
|
|
|
|
|
2015-06-13 16:30:26 +02:00
|
|
|
deferred = $.Deferred( function( deferred ) {
|
2013-05-26 08:58:01 +02:00
|
|
|
// Transfer success/error callbacks.
|
2019-12-15 09:45:05 +01:00
|
|
|
if ( options.success ) {
|
2013-05-26 08:58:01 +02:00
|
|
|
deferred.done( options.success );
|
2019-12-15 09:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( options.error ) {
|
2013-05-26 08:58:01 +02:00
|
|
|
deferred.fail( options.error );
|
2019-12-15 09:45:05 +01:00
|
|
|
}
|
2013-05-26 08:58:01 +02:00
|
|
|
|
|
|
|
delete options.success;
|
|
|
|
delete options.error;
|
|
|
|
|
|
|
|
// Use with PHP's wp_send_json_success() and wp_send_json_error()
|
2015-06-13 16:30:26 +02:00
|
|
|
deferred.jqXHR = $.ajax( options ).done( function( response ) {
|
2016-05-13 20:41:31 +02:00
|
|
|
// Treat a response of 1 as successful for backward compatibility with existing handlers.
|
2019-12-15 09:45:05 +01:00
|
|
|
if ( response === '1' || response === 1 ) {
|
2013-05-26 08:58:01 +02:00
|
|
|
response = { success: true };
|
2019-12-15 09:45:05 +01:00
|
|
|
}
|
2013-05-26 08:58:01 +02:00
|
|
|
|
2019-12-15 09:45:05 +01:00
|
|
|
if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) {
|
2013-05-26 08:58:01 +02:00
|
|
|
deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
|
2019-12-15 09:45:05 +01:00
|
|
|
} else {
|
2013-05-26 08:58:01 +02:00
|
|
|
deferred.rejectWith( this, [response] );
|
2019-12-15 09:45:05 +01:00
|
|
|
}
|
2013-05-26 08:58:01 +02:00
|
|
|
}).fail( function() {
|
|
|
|
deferred.rejectWith( this, arguments );
|
|
|
|
});
|
2015-06-13 16:30:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
promise = deferred.promise();
|
|
|
|
promise.abort = function() {
|
|
|
|
deferred.jqXHR.abort();
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
return promise;
|
2013-05-26 08:58:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-26 08:43:13 +02:00
|
|
|
}(jQuery));
|