Another attempt to fix the http blog redirects.
[WebKit-https.git] / Websites / webkit.org / blog / wp-includes / js / wp-util.js
1 /* global _wpUtilSettings */
2 window.wp = window.wp || {};
3
4 (function ($) {
5         // Check for the utility settings.
6         var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings;
7
8         /**
9          * wp.template( id )
10          *
11          * Fetch a JavaScript template for an id, and return a templating function for it.
12          *
13          * @param  {string} id   A string that corresponds to a DOM element with an id prefixed with "tmpl-".
14          *                       For example, "attachment" maps to "tmpl-attachment".
15          * @return {function}    A function that lazily-compiles the template requested.
16          */
17         wp.template = _.memoize(function ( id ) {
18                 var compiled,
19                         /*
20                          * Underscore's default ERB-style templates are incompatible with PHP
21                          * when asp_tags is enabled, so WordPress uses Mustache-inspired templating syntax.
22                          *
23                          * @see trac ticket #22344.
24                          */
25                         options = {
26                                 evaluate:    /<#([\s\S]+?)#>/g,
27                                 interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
28                                 escape:      /\{\{([^\}]+?)\}\}(?!\})/g,
29                                 variable:    'data'
30                         };
31
32                 return function ( data ) {
33                         compiled = compiled || _.template( $( '#tmpl-' + id ).html(), null, options );
34                         return compiled( data );
35                 };
36         });
37
38         // wp.ajax
39         // ------
40         //
41         // Tools for sending ajax requests with JSON responses and built in error handling.
42         // Mirrors and wraps jQuery's ajax APIs.
43         wp.ajax = {
44                 settings: settings.ajax || {},
45
46                 /**
47                  * wp.ajax.post( [action], [data] )
48                  *
49                  * Sends a POST request to WordPress.
50                  *
51                  * @param  {string} action The slug of the action to fire in WordPress.
52                  * @param  {object} data   The data to populate $_POST with.
53                  * @return {$.promise}     A jQuery promise that represents the request.
54                  */
55                 post: function( action, data ) {
56                         return wp.ajax.send({
57                                 data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
58                         });
59                 },
60
61                 /**
62                  * wp.ajax.send( [action], [options] )
63                  *
64                  * Sends a POST request to WordPress.
65                  *
66                  * @param  {string} action  The slug of the action to fire in WordPress.
67                  * @param  {object} options The options passed to jQuery.ajax.
68                  * @return {$.promise}      A jQuery promise that represents the request.
69                  */
70                 send: function( action, options ) {
71                         if ( _.isObject( action ) ) {
72                                 options = action;
73                         } else {
74                                 options = options || {};
75                                 options.data = _.extend( options.data || {}, { action: action });
76                         }
77
78                         options = _.defaults( options || {}, {
79                                 type:    'POST',
80                                 url:     wp.ajax.settings.url,
81                                 context: this
82                         });
83
84                         return $.Deferred( function( deferred ) {
85                                 // Transfer success/error callbacks.
86                                 if ( options.success )
87                                         deferred.done( options.success );
88                                 if ( options.error )
89                                         deferred.fail( options.error );
90
91                                 delete options.success;
92                                 delete options.error;
93
94                                 // Use with PHP's wp_send_json_success() and wp_send_json_error()
95                                 $.ajax( options ).done( function( response ) {
96                                         // Treat a response of `1` as successful for backwards
97                                         // compatibility with existing handlers.
98                                         if ( response === '1' || response === 1 )
99                                                 response = { success: true };
100
101                                         if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
102                                                 deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
103                                         else
104                                                 deferred.rejectWith( this, [response] );
105                                 }).fail( function() {
106                                         deferred.rejectWith( this, arguments );
107                                 });
108                         }).promise();
109                 }
110         };
111
112 }(jQuery));