wp/wp-includes/js/wp-util.js
author ymh <ymh.work@gmail.com>
Mon, 08 Sep 2025 19:44:41 +0200
changeset 23 417f20492bf7
parent 21 48c4eec2b7e6
permissions -rw-r--r--
Update Docker configuration and plugin versions - Upgrade MariaDB from 10.6 to 11 with auto-upgrade support - Add WordPress debug environment variable to FPM container - Update PHP-FPM Dockerfile base image - Update Include Mastodon Feed plugin with bug fixes and improvements - Update Portfolio plugin (v2.58) with latest translations and demo data enhancements - Remove old README.md from Mastodon Feed plugin 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
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 ) {
21
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    39
			if ( ! document.getElementById( 'tmpl-' + id ) ) {
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    40
				throw new Error( 'Template not found: ' + '#tmpl-' + id );
48c4eec2b7e6 Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    41
			}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    42
			compiled = compiled || _.template( $( '#tmpl-' + id ).html(),  options );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
			return compiled( data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
		};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
	});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
16
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
	 * wp.ajax
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    49
	 * ------
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    50
	 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    51
	 * 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
    52
	 * Mirrors and wraps jQuery's ajax APIs.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    53
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
	wp.ajax = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
		settings: settings.ajax || {},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
		 * wp.ajax.post( [action], [data] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		 * Sends a POST request to WordPress.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
		 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    62
		 * @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
    63
		 *                                 to jQuery.ajax.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    64
		 * @param {Object=}         data   Optional. The data to populate $_POST with.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    65
		 * @return {$.promise} A jQuery promise that represents the request,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    66
		 *                     decorated with an abort() method.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
		post: function( action, data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
			return wp.ajax.send({
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
				data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
		/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		 * wp.ajax.send( [action], [options] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
		 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
		 * Sends a POST request to WordPress.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
		 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    79
		 * @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
    80
		 *                                  to jQuery.ajax.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    81
		 * @param {Object=}         options Optional. The options passed to jQuery.ajax.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    82
		 * @return {$.promise} A jQuery promise that represents the request,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    83
		 *                     decorated with an abort() method.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
		send: function( action, options ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    86
			var promise, deferred;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
			if ( _.isObject( action ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
				options = action;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
				options = options || {};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
				options.data = _.extend( options.data || {}, { action: action });
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
			options = _.defaults( options || {}, {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
				type:    'POST',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
				url:     wp.ajax.settings.url,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
				context: this
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   100
			deferred = $.Deferred( function( deferred ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
				// Transfer success/error callbacks.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   102
				if ( options.success ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
					deferred.done( options.success );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   104
				}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   105
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   106
				if ( options.error ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
					deferred.fail( options.error );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   108
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
				delete options.success;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
				delete options.error;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   113
				// 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
   114
				deferred.jqXHR = $.ajax( options ).done( function( response ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   115
					// 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
   116
					if ( response === '1' || response === 1 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
						response = { success: true };
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   118
					}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   120
					if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   121
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   122
						// 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
   123
						var context = this;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   124
						deferred.done( function() {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   125
							if (
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   126
								action &&
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   127
								action.data &&
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   128
								'query-attachments' === action.data.action &&
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   129
								deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) &&
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   130
								deferred.jqXHR.getResponseHeader( 'X-WP-Total' )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   131
							) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   132
								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
   133
							} else {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   134
								context.totalAttachments = 0;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   135
							}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   136
						} );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
						deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   138
					} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
						deferred.rejectWith( this, [response] );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   140
					}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
				}).fail( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
					deferred.rejectWith( this, arguments );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
				});
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   144
			});
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   145
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   146
			promise = deferred.promise();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   147
			promise.abort = function() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   148
				deferred.jqXHR.abort();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   149
				return this;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   150
			};
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   151
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   152
			return promise;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
	};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
}(jQuery));