PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.4
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.4
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 / status.js

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

226 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 var WPO_Status_Report = (function($) {
2 var report_initialized = false;
3 var report_initializing = false;
4
5 return function() {
6 if (false !== report_initialized || false !== report_initializing) return;
7
8 var replaceable_md_tags;
9
10 /**
11 * Use WordPress Fetch API to show directory size information, update HTML and plain text report
12 */
13 function fetch_directory_sizes() {
14 if ('undefined' === typeof wp || 'undefined' === typeof wp.apiFetch) {
15 // WordPress 4.9 fallback
16 data_not_available();
17 return;
18 }
19 wp.apiFetch({
20 path: 'wp-site-health/v1/directory-sizes'
21 }).then(function(data) {
22 var tag;
23
24 for(var fieldname in data) {
25 $('#wpo-value-wp-paths-sizes-' + fieldname).html(data[fieldname].size);
26
27 tag = '{{[wp-paths-sizes-' + fieldname + ']}}';
28 replace_md_report_tag(tag, data[fieldname].size);
29
30 delete replaceable_md_tags[fieldname];
31 }
32
33 for(var fieldname in replaceable_md_tags) {
34 if (null !== replaceable_md_tags[fieldname]) {
35 replace_md_report_tag(replaceable_md_tags[fieldname], wpoptimize.data_not_available);
36 $('#wpo-value-wp-paths-sizes-' + fieldname).html(wpoptimize.data_not_available);
37 }
38 }
39 }).catch(function(e) {
40 data_not_available();
41 });
42 }
43
44 function data_not_available() {
45 var elems = document.querySelectorAll('.wpo-ajax-field-wp-paths-sizes');
46 elems.forEach(function(e) {
47 e.innerHTML = wpoptimize.data_not_available;
48 });
49 }
50
51 /**
52 * Generate system report
53 */
54 function generate_system_report() {
55 report_initializing = true;
56 $('#wpo-generate-status-report-text').html(wpoptimize.please_wait + " " + '<i id="wpo-preview-loader"><img width="12" height="12" src="' + wpoptimize.spinner_src + '"></i>');
57
58 wp_optimize.send_command('generate_status_report', {}, function (resp) {
59 $("#wpo-status-report-container").html(resp.html);
60
61 attach_click_handlers();
62 fill_settings_debug_report();
63 fetch_directory_sizes();
64
65 replaceable_md_tags = resp.replaceable_md_tags;
66
67 report_initialized = true;
68 }).always(function() {
69 report_initializing = false;
70 });
71 };
72
73 /**
74 * Show/Hide plain text markdown report button click events and log download
75 */
76 function attach_click_handlers() {
77 var server_info = $('#wpo-server-info');
78 var btn_show_status_report = $('#wpo-show-status-report-btn');
79 var btn_copy_status_report = $('#wpo-copy-status-report-btn');
80 var server_info_report = $('#wpo-server-info-report');
81 btn_show_status_report.on('click', function(e) {
82 var to_show = server_info_report.is(':hidden');
83 if (true === to_show) {
84 $(this).text(wpoptimize.hide);
85 server_info.prepend(server_info_report);
86 server_info_report.show();
87 server_info_report.trigger('select');
88
89 // give event loop some time to render the select() method we just called
90 setTimeout(function(o) { o.scrollTop(0); }, 50, server_info_report);
91 } else {
92 $(this).text(wpoptimize.show);
93 server_info.append(server_info_report);
94 server_info_report.hide();
95 }
96 });
97
98 (function(btn) {
99 btn.on('click', function(event) {
100 event.preventDefault();
101
102 var server_info_report = $('#wpo-server-info-report');
103 try {
104 if (clipboard in navigator) {
105 navigator.clipboard.writeText(server_info_report.val());
106 show_clipboard_action_result(wpoptimize.clipboard_success);
107 } else {
108 throw new Error();
109 }
110 } catch(e) {
111 const textArea = document.createElement('textarea');
112 textArea.value = server_info_report.val();
113 textArea.style.opacity = 0;
114 var container = btn.parent();
115 container.append(textArea);
116 textArea.focus();
117 textArea.select();
118 try {
119 const success = document.execCommand('copy');
120 if (success) {
121 show_clipboard_action_result(wpoptimize.clipboard_success);
122 } else {
123 throw new Error("Exec command failed, could not copy");
124 }
125 } catch (err) {
126 console.log(err);
127 show_clipboard_action_result(wpoptimize.clipboard_failed);
128 }
129 $(textArea).remove();
130 }
131 });
132 })(btn_copy_status_report);
133
134 /**
135 * Display the result for clipboard copy action
136 *
137 * @param {string} msg The result message
138 */
139 function show_clipboard_action_result(msg) {
140 $("#wpo-copy-action-result").html(msg);
141 $("#wpo-copy-action-result").show();
142 setTimeout(function() { $("#wpo-copy-action-result").hide(); }, 1500);
143 }
144
145 $('#wpo-download-logs').on('click', function() {
146 $('#wpo-generate-zip-file-text').html(wpoptimize.please_wait + " " + '<i id="wpo-preview-loader"><img width="12" height="12" src="' + wpoptimize.spinner_src + '"></i>');
147 $(this).prop('disabled', true);
148
149 wp_optimize.send_command('generate_logs_zip', {}, function (resp) {
150 const zip = new JSZip();
151
152 resp.data.forEach(function(ziplog) {
153 if (ziplog.compressed) {
154 zip.file(ziplog.name + '.gz', atob(ziplog.src), {binary: true});
155 } else {
156 zip.file(ziplog.name, atob(ziplog.src), {binary: true});
157 }
158 });
159
160 zip.generateAsync({type : "blob", compression: "DEFLATE"}).then(function(logs) {
161 var blob = new Blob([logs]);
162 const url = window.URL.createObjectURL(blob);
163 const a = document.createElement('a');
164 a.style.display = 'none';
165 a.href = url;
166
167 var now = new Date().toJSON().replace(/:/g, '_');
168 a.download = 'logs-' + now + '.zip';
169 document.body.appendChild(a);
170 a.click();
171 window.URL.revokeObjectURL(url);
172
173 $('#wpo-generate-zip-file-text').html('');
174 $('#wpo-download-logs').prop('disabled', false);
175 });
176 });
177 });
178
179 $("#wpo-download-status-report-btn").on('click', function() {
180 var blob = new Blob([server_info_report.val()]);
181 const url = window.URL.createObjectURL(blob);
182 const a = document.createElement('a');
183 a.style.display = 'none';
184 a.href = url;
185
186 var now = new Date().toJSON().replace(/:/g, '_');
187 a.download = 'wp-optimize-report-' + now + '.txt';
188 document.body.appendChild(a);
189 a.click();
190 window.URL.revokeObjectURL(url);
191 });
192 }
193
194 /**
195 * Insert the plugin settings section in HTML and plain text report
196 */
197 function fill_settings_debug_report() {
198 var report = JSON.stringify(WP_Optimize.build_settings(), null, 2);
199
200 $('#wpo-general-settings').html(report);
201
202 replace_md_report_tag('{{[wpo-general-settings]}}', report);
203 }
204
205 /**
206 * Helper to insert text in textarea object with markdown report contents
207 *
208 * @param {string} tag The search string to find the replaceable tag
209 * @param {string} text The actual text that needs to be in the report
210 */
211 function replace_md_report_tag(tag, text) {
212 var markdown_report = $('#wpo-server-info-report').html();
213 markdown_report = markdown_report.replace(tag, text);
214 $('#wpo-server-info-report').html(markdown_report);
215 }
216
217 // Attach tab change
218 $('#wp-optimize-wrap').on('tab-change/wpo_settings/status', function(e) {
219 e.preventDefault();
220
221 if (false === report_initialized) {
222 generate_system_report();
223 }
224 });
225 }
226 })(jQuery);