wp/wp-includes/js/wp-util.js
author ymh <ymh.work@gmail.com>
Tue, 15 Dec 2020 13:49:49 +0100
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
permissions -rw-r--r--
update enmi-conf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     1
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     2
 * @output wp-includes/js/wp-util.js
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     3
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     4
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     5
/* global _wpUtilSettings */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     6
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     7
/** @namespace wp */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
window.wp = window.wp || {};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
(function ($) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
	// Check for the utility settings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
	var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
	 * wp.template( id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    17
	 * Fetch a JavaScript template for an id, and return a templating function for it.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
	 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    19
	 * @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-".
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    20
	 *                    For example, "attachment" maps to "tmpl-attachment".
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    21
	 * @return {function} A function that lazily-compiles the template requested.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
	wp.template = _.memoize(function ( id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
		var compiled,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    25
			/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
			 * Underscore's default ERB-style templates are incompatible with PHP
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
			 * when asp_tags is enabled, so WordPress uses Mustache-inspired templating syntax.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    28
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
			 * @see trac ticket #22344.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    30
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
			options = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
				evaluate:    /<#([\s\S]+?)#>/g,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
				interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
				escape:      /\{\{([^\}]+?)\}\}(?!\})/g,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
				variable:    'data'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
			};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
		return function ( data ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    39
			compiled = compiled || _.template( $( '#tmpl-' + id ).html(),  options );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
			return compiled( data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
		};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
	});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    44
	/*
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    45
	 * wp.ajax
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    46
	 * ------
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    47
	 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    48
	 * Tools for sending ajax requests with JSON responses and built in error handling.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    49
	 * Mirrors and wraps jQuery's ajax APIs.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    50
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
	wp.ajax = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
		settings: settings.ajax || {},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
		 * wp.ajax.post( [action], [data] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		 * Sends a POST request to WordPress.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
		 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    59
		 * @param {(string|Object)} action The slug of the action to fire in WordPress or options passed
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    60
		 *                                 to jQuery.ajax.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    61
		 * @param {Object=}         data   Optional. The data to populate $_POST with.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    62
		 * @return {$.promise} A jQuery promise that represents the request,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    63
		 *                     decorated with an abort() method.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		post: function( action, data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
			return wp.ajax.send({
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
				data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		 * wp.ajax.send( [action], [options] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
		 * Sends a POST request to WordPress.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    76
		 * @param {(string|Object)} action  The slug of the action to fire in WordPress or options passed
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    77
		 *                                  to jQuery.ajax.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    78
		 * @param {Object=}         options Optional. The options passed to jQuery.ajax.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    79
		 * @return {$.promise} A jQuery promise that represents the request,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    80
		 *                     decorated with an abort() method.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
		send: function( action, options ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    83
			var promise, deferred;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
			if ( _.isObject( action ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
				options = action;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
				options = options || {};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
				options.data = _.extend( options.data || {}, { action: action });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
			options = _.defaults( options || {}, {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
				type:    'POST',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
				url:     wp.ajax.settings.url,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
				context: this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    97
			deferred = $.Deferred( function( deferred ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
				// Transfer success/error callbacks.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    99
				if ( options.success ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
					deferred.done( options.success );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   101
				}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   102
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   103
				if ( options.error ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
					deferred.fail( options.error );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   105
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
				delete options.success;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
				delete options.error;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   110
				// Use with PHP's wp_send_json_success() and wp_send_json_error().
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   111
				deferred.jqXHR = $.ajax( options ).done( function( response ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   112
					// Treat a response of 1 as successful for backward compatibility with existing handlers.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   113
					if ( response === '1' || response === 1 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
						response = { success: true };
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   115
					}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   117
					if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
						deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   119
					} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
						deferred.rejectWith( this, [response] );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   121
					}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
				}).fail( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
					deferred.rejectWith( this, arguments );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
				});
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   125
			});
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   126
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   127
			promise = deferred.promise();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   128
			promise.abort = function() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   129
				deferred.jqXHR.abort();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   130
				return this;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   131
			};
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   132
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   133
			return promise;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
	};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
}(jQuery));