PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.6.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.6.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 3.8.0 All 110 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.6.0, at js/send-command.js

182 lines 5.9 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
108 /**
109 * Parse JSON string, including automatically detecting unwanted extra input and skipping it
110 *
111 * @param {string|object} json_mix_str - JSON string which need to parse and convert to object
112 *
113 * @throws SyntaxError|String (including passing on what JSON.parse may throw) if a parsing error occurs.
114 *
115 * @return mixed parsed JSON object. Will only return if parsing is successful (otherwise, will throw)
116 */
117 function wpo_parse_json(json_mix_str) {
118 // When using wp_send_json to return the value, the format is already parsed.
119 if ('object' === typeof json_mix_str) return json_mix_str;
120
121 // Just try it - i.e. the 'default' case where things work (which can include extra whitespace/line-feeds, and simple strings, etc.).
122 try {
123 var result = JSON.parse(json_mix_str);
124 return result;
125 } catch (e) {
126 console.log("WPO: Exception when trying to parse JSON (1) - will attempt to fix/re-parse");
127 console.log(json_mix_str);
128 }
129
130 var json_start_pos = json_mix_str.indexOf('{');
131 var json_last_pos = json_mix_str.lastIndexOf('}');
132
133 // Case where some php notice may be added after or before json string
134 if (json_start_pos > -1 && json_last_pos > -1) {
135 var json_str = json_mix_str.slice(json_start_pos, json_last_pos + 1);
136 try {
137 var parsed = JSON.parse(json_str);
138 return parsed;
139 } catch (e) {
140 console.log("WPO: Exception when trying to parse JSON (2) - will attempt to fix/re-parse based upon bracket counting");
141
142 var cursor = json_start_pos;
143 var open_count = 0;
144 var last_character = '';
145 var inside_string = false;
146
147 // Don't mistake this for a real JSON parser. Its aim is to improve the odds in real-world cases seen, not to arrive at universal perfection.
148 while ((open_count > 0 || cursor == json_start_pos) && cursor <= json_last_pos) {
149
150 var current_character = json_mix_str.charAt(cursor);
151
152 if (!inside_string && '{' == current_character) {
153 open_count++;
154 } else if (!inside_string && '}' == current_character) {
155 open_count--;
156 } else if ('"' == current_character && '\\' != last_character) {
157 inside_string = inside_string ? false : true;
158 }
159
160 last_character = current_character;
161 cursor++;
162 }
163
164 console.log("Started at cursor="+json_start_pos+", ended at cursor="+cursor+" with result following:");
165 console.log(json_mix_str.substring(json_start_pos, cursor));
166
167 try {
168 var parsed = JSON.parse(json_mix_str.substring(json_start_pos, cursor));
169 // console.log('WPO: JSON re-parse successful');
170 return parsed;
171 } catch (e) {
172 // Throw it again, so that our function works just like JSON.parse() in its behaviour.
173 throw e;
174 }
175
176 }
177 }
178
179 throw "WPO: could not parse the JSON";
180
181 }
182