wp/wp-includes/js/wp-util.js
author ymh <ymh.work@gmail.com>
Wed, 21 Sep 2022 18:19:35 +0200
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 21 48c4eec2b7e6
permissions -rw-r--r--
Site enmi version 09/2022
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 ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   118
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   119
						// When handling a media attachments request, get the total attachments from response headers.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   120
						var context = this;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   121
						deferred.done( function() {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   122
							if (
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   123
								action &&
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   124
								action.data &&
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   125
								'query-attachments' === action.data.action &&
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   126
								deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) &&
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   127
								deferred.jqXHR.getResponseHeader( 'X-WP-Total' )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   128
							) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   129
								context.totalAttachments = parseInt( deferred.jqXHR.getResponseHeader( 'X-WP-Total' ), 10 );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   130
							} else {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   131
								context.totalAttachments = 0;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   132
							}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   133
						} );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
						deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   135
					} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
						deferred.rejectWith( this, [response] );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   137
					}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
				}).fail( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
					deferred.rejectWith( this, arguments );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
				});
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   141
			});
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   142
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   143
			promise = deferred.promise();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   144
			promise.abort = function() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   145
				deferred.jqXHR.abort();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   146
				return this;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   147
			};
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   148
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   149
			return promise;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
	};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
}(jQuery));