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 / post_clear_cache.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
post_clear_cache.js
199 lines
1 (function ($) {
2 "use strict";
3
4 // Constants
5 const CACHE_TYPES = {
6 PURGE: "purge",
7 INVALIDATE: "invalidate",
8 };
9
10 const AJAX_ACTIONS = {
11 purge: "nitropack_purge_single_cache",
12 invalidate: "nitropack_invalidate_single_cache",
13 };
14
15 const ICONS = {
16 loading: "/view/images/loading.svg",
17 success: "/view/images/check.svg",
18 error: "/view/images/x-mark.svg",
19 };
20
21 const SELECTORS = {
22 purgeButton: ".nitropack-purge-single",
23 invalidateButton: ".nitropack-invalidate-single",
24 metaBox: "#nitropack_manage_cache_box",
25 loadingIcon: ".icon.loading",
26 };
27
28 const UI_TIMEOUT_CLEAN = 3000;
29
30 // Module state
31 let statusHideTimeout = null;
32
33 /**
34 * Initialize the cache management functionality
35 */
36 function init() {
37 // Use event delegation for better performance
38 $(document).on("click", SELECTORS.purgeButton, handlePurgeClick);
39 $(document).on("click", SELECTORS.invalidateButton, handleInvalidateClick);
40 }
41
42 /**
43 * Handle click on purge cache button
44 */
45 function handlePurgeClick(event) {
46 event.preventDefault();
47 const $button = $(this);
48 const postId = $button.data("post_id");
49 const postUrl = $button.data("post_url");
50
51 cleanSingleCache(postId, postUrl, CACHE_TYPES.PURGE, $button);
52 }
53
54 /**
55 * Handle click on invalidate cache button
56 */
57 function handleInvalidateClick(event) {
58 event.preventDefault();
59 const $button = $(this);
60 const postId = $button.data("post_id");
61 const postUrl = $button.data("post_url");
62
63 cleanSingleCache(postId, postUrl, CACHE_TYPES.INVALIDATE, $button);
64 }
65
66 /**
67 * Create loading icon HTML
68 * @returns {string} HTML for loading icon
69 */
70 function createLoadingIcon() {
71 const iconUrl = np_post_clear_cache.nitro_plugin_url + ICONS.loading;
72 return `<img src="${iconUrl}" width="14" class="icon loading" style="vertical-align: middle; margin-left: .25rem;" alt="Loading..."/>`;
73 }
74
75 /**
76 * Check if we are on a single post page (metabox context)
77 * @returns {boolean}
78 */
79 function isSinglePostPage() {
80 return $(SELECTORS.metaBox).length > 0;
81 }
82
83 /**
84 * Show loading state in UI
85 * @param {jQuery} $button - The button element that was clicked
86 */
87 function showLoadingState($button) {
88 const loadingIcon = createLoadingIcon();
89 $button.css("pointer-events", "none");
90 $button.attr("disabled", true);
91
92 // Show loading icon based on context
93 if (!isSinglePostPage()) {
94 // Listing page - show icon next to button
95 $(loadingIcon).insertAfter($button);
96 } else {
97 $button.append(loadingIcon);
98 }
99 }
100
101 /**
102 * Show success state in UI
103 * @param {jQuery} $button - The button element that was clicked
104 */
105 function showSuccessState($button) {
106 if (!isSinglePostPage()) {
107 const successIconUrl = np_post_clear_cache.nitro_plugin_url + ICONS.success;
108 $button.next(".icon").attr("src", successIconUrl);
109 } else {
110 $button.find(".icon").attr("src", np_post_clear_cache.nitro_plugin_url + ICONS.success);
111 }
112 }
113
114 /**
115 * Show error state in UI
116 * @param {jQuery} $button - The button element that was clicked
117 */
118 function showErrorState($button) {
119 if (!isSinglePostPage()) {
120 const errorIconUrl = np_post_clear_cache.nitro_plugin_url + ICONS.error;
121 $button.next(".icon").attr("src", errorIconUrl);
122 } else {
123 $button.find(".icon").attr("src", np_post_clear_cache.nitro_plugin_url + ICONS.error);
124 }
125 }
126
127 /**
128 * Clean up UI after operation completes
129 * @param {jQuery} $button - The button element that was clicked
130 */
131 function cleanupUI($button) {
132 // Clear any existing timeout
133 if (statusHideTimeout) {
134 clearTimeout(statusHideTimeout);
135 }
136
137 // Schedule status message hide and cleanup
138 statusHideTimeout = setTimeout(() => {
139 $(SELECTORS.loadingIcon).remove();
140 }, UI_TIMEOUT_CLEAN);
141 $button.css("pointer-events", "auto");
142 // Re-enable button
143 $button.attr("disabled", false);
144 }
145
146 /**
147 * Perform cache clean operation via AJAX
148 * @param {number} postId - The post ID
149 * @param {string|array} postUrl - The post URL(s)
150 * @param {string} type - Cache operation type (purge or invalidate)
151 * @param {jQuery} $button - The button element that was clicked
152 */
153 function cleanSingleCache(postId, postUrl, type, $button) {
154 // Validate input
155 if (!postId) {
156 console.error("NitroPack: Invalid post ID");
157 return;
158 }
159
160 // Normalize postUrl
161 postUrl = postUrl || [];
162
163 // Get appropriate AJAX action
164 const action = AJAX_ACTIONS[type];
165 if (!action) {
166 console.error("NitroPack: Invalid cache type:", type);
167 return;
168 }
169
170 // Show loading state
171 showLoadingState($button);
172
173 // Perform AJAX request
174 $.ajax({
175 url: ajaxurl,
176 type: "POST",
177 data: {
178 action: action,
179 postId: postId,
180 postUrl: postUrl,
181 nonce: np_post_clear_cache.nitroNonce,
182 },
183 })
184 .done(() => {
185 showSuccessState($button);
186 })
187 .fail((jqXHR, textStatus, errorThrown) => {
188 console.error("NitroPack: Cache operation failed:", textStatus, errorThrown);
189 showErrorState($button);
190 })
191 .always(() => {
192 cleanupUI($button);
193 });
194 }
195
196 // Initialize when DOM is ready
197 $(document).ready(init);
198 })(jQuery);
199