| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 /** 2 * @output wp-includes/js/wp-util.js 3 */ 4 5 /* global _wpUtilSettings */ 6 7 /** @namespace wp */ 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 * 17 * Fetch a JavaScript template for an id, and return a templating function for it. 18 * 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. 22 */ 23 wp.template = _.memoize(function ( id ) { 24 var compiled, 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 */ 31 options = { 32 evaluate: /<#([\s\S]+?)#>/g, 33 interpolate: /\{\{\{([\s\S]+?)\}\}\}/g, 34 escape: /\{\{([^\}]+?)\}\}(?!\})/g, 35 variable: 'data' 36 }; 37 38 return function ( data ) { 39 var el = document.querySelector( 'script#tmpl-' + id ); 40 if ( ! el ) { 41 throw new Error( 'Template not found: ' + '#tmpl-' + id ); 42 } 43 compiled = compiled || _.template( $( el ).html(), options ); 44 return compiled( data ); 45 }; 46 }); 47 48 /* 49 * wp.ajax 50 * ------ 51 * 52 * Tools for sending ajax requests with JSON responses and built in error handling. 53 * Mirrors and wraps jQuery's ajax APIs. 54 */ 55 wp.ajax = { 56 settings: settings.ajax || {}, 57 58 /** 59 * wp.ajax.post( [action], [data] ) 60 * 61 * Sends a POST request to WordPress. 62 * 63 * @param {(string|Object)} action The slug of the action to fire in WordPress or options passed 64 * to jQuery.ajax. 65 * @param {Object=} data Optional. The data to populate $_POST with. 66 * @return {$.promise} A jQuery promise that represents the request, 67 * decorated with an abort() method. 68 */ 69 post: function( action, data ) { 70 return wp.ajax.send({ 71 data: _.isObject( action ) ? action : _.extend( data || {}, { action: action }) 72 }); 73 }, 74 75 /** 76 * wp.ajax.send( [action], [options] ) 77 * 78 * Sends a POST request to WordPress. 79 * 80 * @param {(string|Object)} action The slug of the action to fire in WordPress or options passed 81 * to jQuery.ajax. 82 * @param {Object=} options Optional. The options passed to jQuery.ajax. 83 * @return {$.promise} A jQuery promise that represents the request, 84 * decorated with an abort() method. 85 */ 86 send: function( action, options ) { 87 var promise, deferred; 88 if ( _.isObject( action ) ) { 89 options = action; 90 } else { 91 options = options || {}; 92 options.data = _.extend( options.data || {}, { action: action }); 93 } 94 95 options = _.defaults( options || {}, { 96 type: 'POST', 97 url: wp.ajax.settings.url, 98 context: this 99 }); 100 101 deferred = $.Deferred( function( deferred ) { 102 // Transfer success/error callbacks. 103 if ( options.success ) { 104 deferred.done( options.success ); 105 } 106 107 if ( options.error ) { 108 deferred.fail( options.error ); 109 } 110 111 delete options.success; 112 delete options.error; 113 114 // Use with PHP's wp_send_json_success() and wp_send_json_error(). 115 deferred.jqXHR = $.ajax( options ).done( function( response ) { 116 // Treat a response of 1 as successful for backward compatibility with existing handlers. 117 if ( response === '1' || response === 1 ) { 118 response = { success: true }; 119 } 120 121 if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) { 122 123 // When handling a media attachments request, get the total attachments from response headers. 124 var context = this; 125 deferred.done( function() { 126 if ( 127 action && 128 action.data && 129 'query-attachments' === action.data.action && 130 deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) && 131 deferred.jqXHR.getResponseHeader( 'X-WP-Total' ) 132 ) { 133 context.totalAttachments = parseInt( deferred.jqXHR.getResponseHeader( 'X-WP-Total' ), 10 ); 134 } else { 135 context.totalAttachments = 0; 136 } 137 } ); 138 deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] ); 139 } else { 140 deferred.rejectWith( this, [response] ); 141 } 142 }).fail( function() { 143 deferred.rejectWith( this, arguments ); 144 }); 145 }); 146 147 promise = deferred.promise(); 148 promise.abort = function() { 149 deferred.jqXHR.abort(); 150 return this; 151 }; 152 153 return promise; 154 } 155 }; 156 157 }(jQuery));
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Jun 24 08:20:11 2026 | Cross-referenced by PHPXref |