author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
9 | 1 |
/** |
2 |
* @output wp-includes/js/wp-util.js |
|
3 |
*/ |
|
4 |
||
5 | 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 | 8 |
window.wp = window.wp || {}; |
9 |
||
10 |
(function ($) { |
|
11 |
// Check for the utility settings. |
|
12 |
var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings; |
|
13 |
||
14 |
/** |
|
15 |
* wp.template( id ) |
|
16 |
* |
|
5 | 17 |
* Fetch a JavaScript template for an id, and return a templating function for it. |
0 | 18 |
* |
16 | 19 |
* @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-". |
20 |
* For example, "attachment" maps to "tmpl-attachment". |
|
21 |
* @return {function} A function that lazily-compiles the template requested. |
|
0 | 22 |
*/ |
23 |
wp.template = _.memoize(function ( id ) { |
|
24 |
var compiled, |
|
5 | 25 |
/* |
26 |
* Underscore's default ERB-style templates are incompatible with PHP |
|
27 |
* when asp_tags is enabled, so WordPress uses Mustache-inspired templating syntax. |
|
28 |
* |
|
29 |
* @see trac ticket #22344. |
|
30 |
*/ |
|
0 | 31 |
options = { |
32 |
evaluate: /<#([\s\S]+?)#>/g, |
|
33 |
interpolate: /\{\{\{([\s\S]+?)\}\}\}/g, |
|
34 |
escape: /\{\{([^\}]+?)\}\}(?!\})/g, |
|
35 |
variable: 'data' |
|
36 |
}; |
|
37 |
||
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 | 43 |
return compiled( data ); |
44 |
}; |
|
45 |
}); |
|
46 |
||
16 | 47 |
/* |
48 |
* wp.ajax |
|
49 |
* ------ |
|
50 |
* |
|
51 |
* Tools for sending ajax requests with JSON responses and built in error handling. |
|
52 |
* Mirrors and wraps jQuery's ajax APIs. |
|
53 |
*/ |
|
0 | 54 |
wp.ajax = { |
55 |
settings: settings.ajax || {}, |
|
56 |
||
57 |
/** |
|
58 |
* wp.ajax.post( [action], [data] ) |
|
59 |
* |
|
60 |
* Sends a POST request to WordPress. |
|
61 |
* |
|
16 | 62 |
* @param {(string|Object)} action The slug of the action to fire in WordPress or options passed |
63 |
* to jQuery.ajax. |
|
64 |
* @param {Object=} data Optional. The data to populate $_POST with. |
|
65 |
* @return {$.promise} A jQuery promise that represents the request, |
|
66 |
* decorated with an abort() method. |
|
0 | 67 |
*/ |
68 |
post: function( action, data ) { |
|
69 |
return wp.ajax.send({ |
|
70 |
data: _.isObject( action ) ? action : _.extend( data || {}, { action: action }) |
|
71 |
}); |
|
72 |
}, |
|
73 |
||
74 |
/** |
|
75 |
* wp.ajax.send( [action], [options] ) |
|
76 |
* |
|
77 |
* Sends a POST request to WordPress. |
|
78 |
* |
|
16 | 79 |
* @param {(string|Object)} action The slug of the action to fire in WordPress or options passed |
80 |
* to jQuery.ajax. |
|
81 |
* @param {Object=} options Optional. The options passed to jQuery.ajax. |
|
82 |
* @return {$.promise} A jQuery promise that represents the request, |
|
83 |
* decorated with an abort() method. |
|
0 | 84 |
*/ |
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 | 87 |
if ( _.isObject( action ) ) { |
88 |
options = action; |
|
89 |
} else { |
|
90 |
options = options || {}; |
|
91 |
options.data = _.extend( options.data || {}, { action: action }); |
|
92 |
} |
|
93 |
||
94 |
options = _.defaults( options || {}, { |
|
95 |
type: 'POST', |
|
96 |
url: wp.ajax.settings.url, |
|
97 |
context: this |
|
98 |
}); |
|
99 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
deferred = $.Deferred( function( deferred ) { |
0 | 101 |
// Transfer success/error callbacks. |
16 | 102 |
if ( options.success ) { |
0 | 103 |
deferred.done( options.success ); |
16 | 104 |
} |
105 |
||
106 |
if ( options.error ) { |
|
0 | 107 |
deferred.fail( options.error ); |
16 | 108 |
} |
0 | 109 |
|
110 |
delete options.success; |
|
111 |
delete options.error; |
|
112 |
||
16 | 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 | 116 |
if ( response === '1' || response === 1 ) { |
0 | 117 |
response = { success: true }; |
16 | 118 |
} |
0 | 119 |
|
16 | 120 |
if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) { |
18 | 121 |
|
122 |
// When handling a media attachments request, get the total attachments from response headers. |
|
123 |
var context = this; |
|
124 |
deferred.done( function() { |
|
125 |
if ( |
|
126 |
action && |
|
127 |
action.data && |
|
128 |
'query-attachments' === action.data.action && |
|
129 |
deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) && |
|
130 |
deferred.jqXHR.getResponseHeader( 'X-WP-Total' ) |
|
131 |
) { |
|
132 |
context.totalAttachments = parseInt( deferred.jqXHR.getResponseHeader( 'X-WP-Total' ), 10 ); |
|
133 |
} else { |
|
134 |
context.totalAttachments = 0; |
|
135 |
} |
|
136 |
} ); |
|
0 | 137 |
deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] ); |
16 | 138 |
} else { |
0 | 139 |
deferred.rejectWith( this, [response] ); |
16 | 140 |
} |
0 | 141 |
}).fail( function() { |
142 |
deferred.rejectWith( this, arguments ); |
|
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 | 153 |
} |
154 |
}; |
|
155 |
||
156 |
}(jQuery)); |