PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.21
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.21
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / js / send-command.js

send-command.js in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.21, at js/send-command.js

107 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 var wp_optimize = window.wp_optimize || {};
2
3 /**
4 * Send an action via admin-ajax.php.
5 *
6 * @param {string} action The action to send
7 * @param {[type]} data Data to send
8 * @param {Function} callback Will be called with the results
9 * @param {boolean} json_parse JSON parse the results
10 * @param {object} options Optional extra options; current properties supported are 'timeout' (in milliseconds)
11 *
12 * @return {JSON}
13 */
14 wp_optimize.send_command = function (action, data, callback, json_parse, options) {
15
16 json_parse = ('undefined' === typeof json_parse) ? true : json_parse;
17
18 if (!data) data = {};
19 // If the command doesn't have the property, default to true
20 if (!data.hasOwnProperty('include_ui_elements')) {
21 data.include_ui_elements = true;
22 }
23
24 var ajax_data = {
25 action: 'wp_optimize_ajax',
26 subaction: action,
27 nonce: wp_optimize_send_command_data.nonce,
28 data: data
29 };
30
31 var args = {
32 type: 'post',
33 data: ajax_data,
34 success: function (response) {
35 if (json_parse) {
36 try {
37 var resp = wpo_parse_json(response);
38 } catch (e) {
39 console.log(e);
40 console.log(response);
41 alert(wpoptimize.error_unexpected_response);
42 return;
43 }
44 // If result == false and and error code is provided, show the error and return.
45 if (!resp.result && resp.hasOwnProperty('error_code') && resp.error_code) {
46 wp_optimize.notices.show_notice(resp.error_code, resp.error_message);
47 return;
48 }
49 if ('function' === typeof callback) callback(resp);
50 } else {
51 if (!response.result && response.hasOwnProperty('error_code') && response.error_code) {
52 wp_optimize.notices.show_notice(response.error_code, response.error_message);
53 return;
54 }
55 if ('function' === typeof callback) callback(response);
56 }
57 }
58 };
59
60 // Eventually merge options
61 if ('object' === typeof options) {
62 if (options.hasOwnProperty('timeout')) { args.timeout = options.timeout; }
63 if (options.hasOwnProperty('error') && 'function' === typeof options.error) { args.error = options.error; }
64 }
65
66 return jQuery.ajax(ajaxurl, args);
67 };
68
69
70 /**
71 * JS notices
72 */
73 wp_optimize.notices = {
74 errors: [],
75 show_notice: function(error_code, error_message) {
76 // WPO main page
77 if (jQuery('#wp-optimize-wrap').length) {
78 if (!this.notice) this.add_notice();
79 this.notice.show();
80 if (!this.errors[error_code]) {
81 this.errors[error_code] = jQuery('<p/>').html(error_message).appendTo(this.notice).data('error_code', error_code);
82 }
83 // Post edit page
84 } else if (window.wp && wp.hasOwnProperty('data')) {
85 wp.data.dispatch('core/notices').createNotice(
86 'error',
87 'WP-Optimize: ' + error_message,
88 {
89 isDismissible: true
90 }
91 );
92 // Other locations
93 } else {
94 alert('WP-Optimize: ' + error_message);
95 }
96 },
97 add_notice: function() {
98 this.notice_container = jQuery('<div class="wpo-main-error-notice"></div>').prependTo('#wp-optimize-wrap');
99 this.notice = jQuery('<div class="notice notice-error wpo-notice is-dismissible"><button type="button" class="notice-dismiss"><span class="screen-reader-text">'+commonL10n.dismiss+'</span></button></div>');
100 this.notice.appendTo(this.notice_container);
101 this.notice.on('click', '.notice-dismiss', function(e) {
102 this.notice.hide().find('p').remove();
103 this.errors = [];
104 }.bind(this));
105 }
106 };
107