PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.4
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.4
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / view / javascript / preview_site.js
nitropack / view / javascript Last commit date
admin_bar_menu.js 4 months ago elementor_cache_integration.js 5 months ago gravity_forms.js 2 months ago math_captcha.js 1 year ago nitropackUI.js 2 months ago np_notices.js 3 months ago np_safemode.js 1 year ago np_select2.js 2 months ago np_select2.min.js 2 months ago np_settings.js 2 months ago popper.min.js 1 year ago post_clear_cache.js 4 months ago preview_site.js 3 months ago system_report.js 4 months ago widgets_ajax.js 2 months ago
preview_site.js
315 lines
1 jQuery(document).ready(function ($) {
2 class nitropackPreview {
3 constructor() {
4 this.initial_settings = {
5 optimizationLevel: {
6 name: "strong",
7 level: null,
8 },
9 };
10 this.modals = this.initModals();
11 this.getInitialModeName();
12
13 this.init();
14
15 this.modeSwitchClick();
16 this.previewHomeButton();
17 this.goLiveButton();
18 this.openIntercomBubble();
19 }
20 initModals() {
21 const modals = {
22 processing_html_modal: new Modal(document.getElementById("processing-html-modal")),
23 processing_html_success_modal: new Modal(document.getElementById("processing-html-success-modal")),
24 processing_html_error_modal: new Modal(document.getElementById("processing-html-error-modal")),
25 go_live_modal: new Modal(document.getElementById("go-live-modal")),
26 };
27 return modals;
28 }
29
30 init() {
31 this.startPreviewPolling(this.initial_settings.optimizationLevel.name);
32 this.closeModal();
33 }
34 closeModal() {
35 const nitroSelf = this;
36 $(".modal-wrapper .close-modal").click(function () {
37 let id = $(this).closest(".modal-wrapper").attr("id");
38 id = id.replace(/-/g, "_");
39 nitroSelf.hideModal(id);
40 });
41 }
42 getInitialModeName() {
43 const mode = $(".modes-container .mode.active").data("mode");
44 if (!mode) {
45 return;
46 }
47 this.initial_settings.optimizationLevel.name = mode;
48 return mode;
49 }
50 levels() {
51 return {
52 1: "standard",
53 2: "medium",
54 3: "strong",
55 4: "ludicrous",
56 5: "custom",
57 };
58 }
59 getLevelIntByName(name) {
60 const levels = this.levels();
61 for (const [intLevel, levelName] of Object.entries(levels)) {
62 if (levelName === name) {
63 return parseInt(intLevel);
64 }
65 }
66 return null;
67 }
68 /* Polling function to check if the preview homepage is cached.
69 It has maximum retry time of 60 seconds and interval of 5 seconds
70 */
71 startPreviewPolling(mode_name) {
72 this.showModal("processing_html_modal");
73
74 const maxRetryTime = 60000; // 60 seconds in milliseconds
75 const retryInterval = 5000; // 5 seconds between retries
76 const startTime = Date.now();
77 let retryCount = 0;
78 const nitroSelf = this;
79
80 const pollPreviewStatus = () => {
81 const currentTime = Date.now();
82 const elapsedTime = currentTime - startTime;
83 retryCount++;
84
85 $.post(
86 ajaxurl,
87 {
88 action: "nitropack_is_homepage_preview_cached",
89 nonce: nitroNonce,
90 mode_name,
91 },
92 function (response) {
93 try {
94 var resp = JSON.parse(response);
95 if (resp.preview === 1) {
96 nitroSelf.hideModal("processing_html_modal");
97 //display status modal only once in init before we set the mode
98 if (!nitroSelf.initial_settings.optimizationLevel.level) {
99 nitroSelf.showModal("processing_html_success_modal");
100 } else {
101 NitropackUI.triggerToast(
102 "success",
103 'Home page optimized with <strong class="capitalized">' + mode_name + "</strong> mode.",
104 );
105 }
106 nitroSelf.initial_settings.optimizationLevel.name = mode_name;
107 nitroSelf.initial_settings.optimizationLevel.level = nitroSelf.getLevelIntByName(mode_name);
108 } else {
109 // Check if we should continue polling
110 if (elapsedTime < maxRetryTime) {
111 setTimeout(pollPreviewStatus, retryInterval);
112 } else {
113 nitroSelf.showPreviewError(mode_name);
114 }
115 }
116 } catch (error) {
117 if (elapsedTime < maxRetryTime) {
118 setTimeout(pollPreviewStatus, retryInterval);
119 } else {
120 nitroSelf.hideModal("processing_html_modal");
121 nitroSelf.showModal("processing_html_error_modal");
122 }
123 }
124 },
125 ).fail(function (xhr, status, error) {
126 nitroSelf.showModal("processing_html_error_modal");
127 if (Date.now() - startTime < maxRetryTime) {
128 setTimeout(pollPreviewStatus, retryInterval);
129 } else {
130 nitroSelf.hideModal("processing_html_modal");
131 nitroSelf.showModal("processing_html_error_modal");
132 }
133 });
134 };
135
136 pollPreviewStatus();
137 }
138
139 setOptimizationMode = (mode_int, mode_name) => {
140 this.saveOptimizationMode(mode_int, mode_name)
141 .then((response) => {
142 var resp = JSON.parse(response);
143 if (resp.type === "success") {
144 this.startPreviewPolling(mode_name);
145 }
146 })
147 .catch((error) => {
148 console.error("Failed to save settings:", error);
149 });
150 };
151
152 saveOptimizationMode = (mode_int, mode_name) => {
153 const nitroSelf = this;
154 return $.post(
155 ajaxurl,
156 {
157 action: "nitropack_set_optimization_mode",
158 nonce: np_settings.nitroNonce,
159 mode_int,
160 mode_name,
161 },
162 function (response) {
163 var resp = JSON.parse(response);
164 if (resp.type == "success") {
165 nitroSelf.applyOptimizationCosmetics(mode_name);
166 nitroSelf.initial_settings.optimizationLevel.int = mode_int;
167 nitroSelf.initial_settings.optimizationLevel.name = mode_name;
168 } else {
169 NitropackUI.triggerToast("error", resp.message);
170 }
171 },
172 );
173 };
174
175 showPreviewError = (mode) => {
176 this.showModal("processing_html_error_modal");
177
178 const nitroSelf = this;
179 $("#processing-html-error-modal .btn-primary")
180 .off("click")
181 .on("click", function () {
182 nitroSelf.hideModal("processing_html_error_modal");
183 nitroSelf.startPreviewPolling(mode);
184 });
185
186 this.hideModal("processing_html_modal");
187 };
188
189 ajaxPassOnboarding() {
190 $.ajax({
191 url: ajaxurl,
192 type: "POST",
193 data: {
194 action: "nitropack_passed_onboarding",
195 nonce: nitroNonce,
196 },
197 dataType: "json",
198 success: function (resp) {
199 NitropackUI.triggerToast(resp.type, resp.message);
200 if (resp.status === 1 && resp.redirect) {
201 window.location.href = resp.redirect;
202 }
203 },
204 error: function (xhr) {
205 NitropackUI.triggerToast("error", "Something went wrong");
206 },
207 });
208 }
209
210 applyOptimizationCosmetics(mode) {
211 $(".mode").removeClass("active");
212 $(".mode:not(.disabled) .select-mode").removeClass("selected").text(np_onboarding.select_mode).prepend("");
213 let active_mode_container = $(".mode").filter(`[data-mode="${mode}"]`);
214 active_mode_container.addClass("active");
215 active_mode_container
216 .find(".select-mode")
217 .addClass("selected")
218 .text(np_onboarding.active_mode)
219 .prepend(
220 '<svg class="icon check" xmlns="http://www.w3.org/2000/svg" width="12" height="9" viewBox="0 0 12 9" fill="none"><path d="M11.4674 0.792969L4.13411 8.1263L0.800781 4.79297" stroke="#4600CC" stroke-linecap="round" stroke-linejoin="round"/></svg>',
221 );
222 $(".active-mode").text(mode);
223 }
224
225 modeSwitchClick() {
226 const nitroSelf = this;
227 const modes_btn = ".mode .select-mode";
228
229 /* Change optimization mode when clicking on a mode button */
230 $(document).on("click", modes_btn, function () {
231 const mode_name = $(this).closest(".mode").data("mode"),
232 mode_int = $(this).closest(".mode").index() + 1;
233
234 const preview_url = $(".preview-home").attr("href");
235 const url = new URL(preview_url);
236 url.searchParams.set("previewmode", mode_name);
237 $(".preview-home").attr("href", url.toString());
238
239 nitroSelf.setOptimizationMode(mode_int, mode_name);
240 });
241 }
242 previewHomeButton() {
243 $(".preview-home").on("click", function (e) {
244 const active_mode_name = $(".mode.active").data("mode");
245 const existingCookie = document.cookie.split("; ").find((row) => row.startsWith("nitropack_preview_mode="));
246
247 let modes = [];
248 if (existingCookie) {
249 const existingModes = existingCookie.split("=")[1];
250 modes = existingModes ? existingModes.split(",") : [];
251 }
252
253 if (!modes.includes(active_mode_name)) {
254 modes.push(active_mode_name);
255 }
256
257 document.cookie = `nitropack_preview_mode=${modes.join(",")}; path=/; max-age=3600`;
258 });
259 }
260 goLiveButton() {
261 const nitroSelf = this;
262 $("#go-live").on("click", function (e) {
263 const active_mode_name = $(".mode.active").data("mode");
264 $("#go-live-modal .popup-body .active-mode").text($(".mode.active h3").text());
265
266 const activeModeCookie = document.cookie.split("; ").find((row) => row.startsWith("nitropack_preview_mode="));
267
268 let is_mode_previewed = false;
269 if (activeModeCookie) {
270 const cookieValue = activeModeCookie.split("=")[1];
271 const modes = cookieValue ? cookieValue.split(",") : [];
272 is_mode_previewed = modes.includes(active_mode_name);
273 }
274
275 if (is_mode_previewed) {
276 nitroSelf.ajaxPassOnboarding();
277 } else {
278 nitroSelf.showModal("go_live_modal");
279 $("#go-live-modal .close-modal")
280 .off("click")
281 .on("click", function () {
282 nitroSelf.hideModal("go_live_modal");
283 });
284 }
285 });
286
287 $("#go-live-modal .go-live").on("click", function (e) {
288 nitroSelf.ajaxPassOnboarding();
289 });
290 }
291 openIntercomBubble() {
292 $("#processing-html-error-modal .contact-support").on("click", function () {
293 $(".intercom-launcher").trigger("click");
294 });
295 }
296 showModal(modalName) {
297 if (this.modals[modalName]) {
298 this.modals[modalName].show();
299 } else {
300 console.error(`Modal '${modalName}' not found`);
301 }
302 }
303 hideModal(modalName) {
304 if (this.modals[modalName]) {
305 this.modals[modalName].hide();
306 } else {
307 console.error(`Modal '${modalName}' not found`);
308 }
309 }
310 }
311
312 const NitroPackPreview = new nitropackPreview();
313 window.NitroPackPreview = NitroPackPreview;
314 });
315