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-- |
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 ) { |
|
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 | 40 |
return compiled( data ); |
41 |
}; |
|
42 |
}); |
|
43 |
||
16 | 44 |
/* |
45 |
* wp.ajax |
|
46 |
* ------ |
|
47 |
* |
|
48 |
* Tools for sending ajax requests with JSON responses and built in error handling. |
|
49 |
* Mirrors and wraps jQuery's ajax APIs. |
|
50 |
*/ |
|
0 | 51 |
wp.ajax = { |
52 |
settings: settings.ajax || {}, |
|
53 |
||
54 |
/** |
|
55 |
* wp.ajax.post( [action], [data] ) |
|
56 |
* |
|
57 |
* Sends a POST request to WordPress. |
|
58 |
* |
|
16 | 59 |
* @param {(string|Object)} action The slug of the action to fire in WordPress or options passed |
60 |
* to jQuery.ajax. |
|
61 |
* @param {Object=} data Optional. The data to populate $_POST with. |
|
62 |
* @return {$.promise} A jQuery promise that represents the request, |
|
63 |
* decorated with an abort() method. |
|
0 | 64 |
*/ |
65 |
post: function( action, data ) { |
|
66 |
return wp.ajax.send({ |
|
67 |
data: _.isObject( action ) ? action : _.extend( data || {}, { action: action }) |
|
68 |
}); |
|
69 |
}, |
|
70 |
||
71 |
/** |
|
72 |
* wp.ajax.send( [action], [options] ) |
|
73 |
* |
|
74 |
* Sends a POST request to WordPress. |
|
75 |
* |
|
16 | 76 |
* @param {(string|Object)} action The slug of the action to fire in WordPress or options passed |
77 |
* to jQuery.ajax. |
|
78 |
* @param {Object=} options Optional. The options passed to jQuery.ajax. |
|
79 |
* @return {$.promise} A jQuery promise that represents the request, |
|
80 |
* decorated with an abort() method. |
|
0 | 81 |
*/ |
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 | 84 |
if ( _.isObject( action ) ) { |
85 |
options = action; |
|
86 |
} else { |
|
87 |
options = options || {}; |
|
88 |
options.data = _.extend( options.data || {}, { action: action }); |
|
89 |
} |
|
90 |
||
91 |
options = _.defaults( options || {}, { |
|
92 |
type: 'POST', |
|
93 |
url: wp.ajax.settings.url, |
|
94 |
context: this |
|
95 |
}); |
|
96 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
deferred = $.Deferred( function( deferred ) { |
0 | 98 |
// Transfer success/error callbacks. |
16 | 99 |
if ( options.success ) { |
0 | 100 |
deferred.done( options.success ); |
16 | 101 |
} |
102 |
||
103 |
if ( options.error ) { |
|
0 | 104 |
deferred.fail( options.error ); |
16 | 105 |
} |
0 | 106 |
|
107 |
delete options.success; |
|
108 |
delete options.error; |
|
109 |
||
16 | 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 | 113 |
if ( response === '1' || response === 1 ) { |
0 | 114 |
response = { success: true }; |
16 | 115 |
} |
0 | 116 |
|
16 | 117 |
if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) { |
18 | 118 |
|
119 |
// When handling a media attachments request, get the total attachments from response headers. |
|
120 |
var context = this; |
|
121 |
deferred.done( function() { |
|
122 |
if ( |
|
123 |
action && |
|
124 |
action.data && |
|
125 |
'query-attachments' === action.data.action && |
|
126 |
deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) && |
|
127 |
deferred.jqXHR.getResponseHeader( 'X-WP-Total' ) |
|
128 |
) { |
|
129 |
context.totalAttachments = parseInt( deferred.jqXHR.getResponseHeader( 'X-WP-Total' ), 10 ); |
|
130 |
} else { |
|
131 |
context.totalAttachments = 0; |
|
132 |
} |
|
133 |
} ); |
|
0 | 134 |
deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] ); |
16 | 135 |
} else { |
0 | 136 |
deferred.rejectWith( this, [response] ); |
16 | 137 |
} |
0 | 138 |
}).fail( function() { |
139 |
deferred.rejectWith( this, arguments ); |
|
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 | 150 |
} |
151 |
}; |
|
152 |
||
153 |
}(jQuery)); |