PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.18.8
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.18.8
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 1 year ago flowbite.min.js 1 year ago gravity_forms.js 1 year ago math_captcha.js 1 year ago metabox.js 1 year ago nitropackUI.js 1 year ago np_notices.js 7 months ago np_safemode.js 1 year ago np_settings.js 7 months ago popper.min.js 1 year ago preview_site.js 7 months ago widgets_ajax.js 1 year ago
preview_site.js
305 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 this.init();
13 this.modeSwitchClick();
14 this.previewHomeButton();
15 this.goLiveButton();
16 this.openIntercomBubble();
17 }
18 initModals() {
19 const modals = {
20 processing_html_modal: new Modal(document.getElementById("processing-html-modal")),
21 processing_html_success_modal: new Modal(document.getElementById("processing-html-success-modal")),
22 processing_html_error_modal: new Modal(document.getElementById("processing-html-error-modal")),
23 go_live_modal: new Modal(document.getElementById("go-live-modal")),
24 };
25
26 return modals;
27 }
28
29 init() {
30 this.startPreviewPolling(this.initial_settings.optimizationLevel.name);
31 }
32 getInitialModeName() {
33 const mode = $(".modes-container .mode.active").data("mode");
34 if (!mode) {
35 return;
36 }
37 this.initial_settings.optimizationLevel.name = mode;
38 return mode;
39 }
40 levels() {
41 return {
42 1: "standard",
43 2: "medium",
44 3: "strong",
45 4: "ludicrous",
46 5: "custom",
47 };
48 }
49 getLevelIntByName(name) {
50 const levels = this.levels();
51 for (const [intLevel, levelName] of Object.entries(levels)) {
52 if (levelName === name) {
53 return parseInt(intLevel);
54 }
55 }
56 return null;
57 }
58 /* Polling function to check if the preview homepage is cached.
59 It has maximum retry time of 60 seconds and interval of 5 seconds
60 */
61 startPreviewPolling(mode_name) {
62 this.showModal("processing_html_modal");
63
64 const maxRetryTime = 60000; // 60 seconds in milliseconds
65 const retryInterval = 5000; // 5 seconds between retries
66 const startTime = Date.now();
67 let retryCount = 0;
68 const nitroSelf = this;
69
70 const pollPreviewStatus = () => {
71 const currentTime = Date.now();
72 const elapsedTime = currentTime - startTime;
73 retryCount++;
74
75 $.post(
76 ajaxurl,
77 {
78 action: "nitropack_is_homepage_preview_cached",
79 nonce: nitroNonce,
80 mode_name,
81 },
82 function (response) {
83 try {
84 var resp = JSON.parse(response);
85 if (resp.preview === 1) {
86 nitroSelf.hideModal("processing_html_modal");
87 //display status modal only once in init before we set the mode
88 if (!nitroSelf.initial_settings.optimizationLevel.level) {
89 nitroSelf.showModal("processing_html_success_modal");
90 } else {
91 NitropackUI.triggerToast(
92 "success",
93 'Home page optimized with <strong class="capitalized">' + mode_name + "</strong> mode."
94 );
95 }
96 nitroSelf.initial_settings.optimizationLevel.name = mode_name;
97 nitroSelf.initial_settings.optimizationLevel.level = nitroSelf.getLevelIntByName(mode_name);
98 } else {
99 // Check if we should continue polling
100 if (elapsedTime < maxRetryTime) {
101 setTimeout(pollPreviewStatus, retryInterval);
102 } else {
103 nitroSelf.showPreviewError(mode_name);
104 }
105 }
106 } catch (error) {
107 if (elapsedTime < maxRetryTime) {
108 setTimeout(pollPreviewStatus, retryInterval);
109 } else {
110 nitroSelf.hideModal("processing_html_modal");
111 nitroSelf.showModal("processing_html_error_modal");
112 }
113 }
114 }
115 ).fail(function (xhr, status, error) {
116 nitroSelf.showModal("processing_html_error_modal");
117 if (Date.now() - startTime < maxRetryTime) {
118 setTimeout(pollPreviewStatus, retryInterval);
119 } else {
120 nitroSelf.hideModal("processing_html_modal");
121 nitroSelf.showModal("processing_html_error_modal");
122 }
123 });
124 };
125
126 pollPreviewStatus();
127 }
128
129 setOptimizationMode = (mode_int, mode_name) => {
130 this.saveOptimizationMode(mode_int, mode_name)
131 .then((response) => {
132 var resp = JSON.parse(response);
133 if (resp.type === "success") {
134 this.startPreviewPolling(mode_name);
135 }
136 })
137 .catch((error) => {
138 console.error("Failed to save settings:", error);
139 });
140 };
141
142 saveOptimizationMode = (mode_int, mode_name) => {
143 const nitroSelf = this;
144 return $.post(
145 ajaxurl,
146 {
147 action: "nitropack_set_optimization_mode",
148 nonce: np_settings.nitroNonce,
149 mode_int,
150 mode_name,
151 },
152 function (response) {
153 var resp = JSON.parse(response);
154 if (resp.type == "success") {
155 nitroSelf.applyOptimizationCosmetics(mode_name);
156 nitroSelf.initial_settings.optimizationLevel.int = mode_int;
157 nitroSelf.initial_settings.optimizationLevel.name = mode_name;
158 } else {
159 NitropackUI.triggerToast("error", resp.message);
160 }
161 }
162 );
163 };
164
165 showPreviewError = (mode) => {
166 this.showModal("processing_html_error_modal");
167
168 const nitroSelf = this;
169 $("#processing-html-error-modal .btn-primary")
170 .off("click")
171 .on("click", function () {
172 nitroSelf.hideModal("processing_html_error_modal");
173 nitroSelf.startPreviewPolling(mode);
174 });
175
176 this.hideModal("processing_html_modal");
177 };
178
179 ajaxPassOnboarding() {
180 $.ajax({
181 url: ajaxurl,
182 type: "POST",
183 data: {
184 action: "nitropack_passed_onboarding",
185 nonce: nitroNonce,
186 },
187 dataType: "json",
188 success: function (resp) {
189 NitropackUI.triggerToast(resp.type, resp.message);
190 if (resp.status === 1 && resp.redirect) {
191 window.location.href = resp.redirect;
192 }
193 },
194 error: function (xhr) {
195 NitropackUI.triggerToast("error", "Something went wrong");
196 },
197 });
198 }
199
200 applyOptimizationCosmetics(mode) {
201 $(".mode").removeClass("active");
202 $(".mode:not(.disabled) .select-mode").removeClass("selected").text(np_onboarding.select_mode).prepend("");
203 let active_mode_container = $(".mode").filter(`[data-mode="${mode}"]`);
204 active_mode_container.addClass("active");
205 active_mode_container
206 .find(".select-mode")
207 .addClass("selected")
208 .text(np_onboarding.active_mode)
209 .prepend(
210 '<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>'
211 );
212 $(".active-mode").text(mode);
213 }
214
215 modeSwitchClick() {
216 const nitroSelf = this;
217 const modes_btn = ".mode .select-mode";
218
219 /* Change optimization mode when clicking on a mode button */
220 $(document).on("click", modes_btn, function () {
221 const mode_name = $(this).closest(".mode").data("mode"),
222 mode_int = $(this).closest(".mode").index() + 1;
223
224 const preview_url = $(".preview-home").attr("href");
225 const url = new URL(preview_url);
226 url.searchParams.set("previewmode", mode_name);
227 $(".preview-home").attr("href", url.toString());
228
229 nitroSelf.setOptimizationMode(mode_int, mode_name);
230 });
231 }
232 previewHomeButton() {
233 $(".preview-home").on("click", function (e) {
234 const active_mode_name = $(".mode.active").data("mode");
235 const existingCookie = document.cookie.split("; ").find((row) => row.startsWith("nitropack_preview_mode="));
236
237 let modes = [];
238 if (existingCookie) {
239 const existingModes = existingCookie.split("=")[1];
240 modes = existingModes ? existingModes.split(",") : [];
241 }
242
243 if (!modes.includes(active_mode_name)) {
244 modes.push(active_mode_name);
245 }
246
247 document.cookie = `nitropack_preview_mode=${modes.join(",")}; path=/; max-age=3600`;
248 });
249 }
250 goLiveButton() {
251 const nitroSelf = this;
252 $("#go-live").on("click", function (e) {
253 const active_mode_name = $(".mode.active").data("mode");
254 $("#go-live-modal .popup-body .active-mode").text($(".mode.active h3").text());
255
256 const activeModeCookie = document.cookie.split("; ").find((row) => row.startsWith("nitropack_preview_mode="));
257
258 let is_mode_previewed = false;
259 if (activeModeCookie) {
260 const cookieValue = activeModeCookie.split("=")[1];
261 const modes = cookieValue ? cookieValue.split(",") : [];
262 is_mode_previewed = modes.includes(active_mode_name);
263 }
264
265 if (is_mode_previewed) {
266 nitroSelf.ajaxPassOnboarding();
267 } else {
268 nitroSelf.showModal("go_live_modal");
269 $("#go-live-modal .close-modal")
270 .off("click")
271 .on("click", function () {
272 nitroSelf.hideModal("go_live_modal");
273 });
274 }
275 });
276
277 $("#go-live-modal .go-live").on("click", function (e) {
278 nitroSelf.ajaxPassOnboarding();
279 });
280 }
281 openIntercomBubble() {
282 $("#processing-html-error-modal .contact-support").on("click", function () {
283 $(".intercom-launcher").trigger("click");
284 });
285 }
286 showModal(modalName) {
287 if (this.modals[modalName]) {
288 this.modals[modalName].show();
289 } else {
290 console.error(`Modal '${modalName}' not found`);
291 }
292 }
293 hideModal(modalName) {
294 if (this.modals[modalName]) {
295 this.modals[modalName].hide();
296 } else {
297 console.error(`Modal '${modalName}' not found`);
298 }
299 }
300 }
301
302 const NitroPackPreview = new nitropackPreview();
303 window.NitroPackPreview = NitroPackPreview;
304 });
305