Skip to content

Commit 6930755

Browse files
added cachebuster for admin
1 parent 1b78b00 commit 6930755

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

cache-everything.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* Plugin Name: Cache Everything
44
* Plugin URI: https://github.com/AsyncAlchemist
55
* Description: A simple plugin to cache everything in Wordpress.
6-
* Version: 0.25
6+
* Version: 0.26
77
* Author: Taylor Selden
88
* Author URI: https://github.com/AsyncAlchemist
99
*/
1010
define('CACHE_EVERYTHING_JS_URL', 'wp-content/plugins/cache-everything/js');
1111
define('CACHE_EVERYTHING_CSS_URL', 'wp-content/plugins/cache-everything/css');
12-
define('CACHE_EVERYTHING_VERSION', '0.25');
12+
define('CACHE_EVERYTHING_VERSION', '0.26');
1313

1414
require_once(plugin_dir_path(__FILE__) . 'handle-js-request.php');
1515
require_once(plugin_dir_path(__FILE__) . 'handle-css-request.php');

handle-css-request.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ function cache_everything_handle_css_request() {
99
if (get_query_var('cache_everything_css')) {
1010
header("Content-Type: text/css; charset=UTF-8");
1111

12+
// Cache control headers
13+
$max_age = get_option('cache_everything_max_age', 604800); // Default to 1 week in seconds if not set
14+
$stale_while_revalidate = get_option('cache_everything_stale_while_revalidate', 86400);
15+
$expires_time = gmdate('D, d M Y H:i:s', time() + $max_age) . ' GMT'; // Calculate the Expires header value
16+
header("Cache-Control: public, max-age=$max_age, stale-while-revalidate=$stale_while_revalidate"); // Use the max age from settings
17+
header("Expires: $expires_time");
18+
header("X-WCE-Cache: public, max-age=$max_age, stale-while-revalidate=$stale_while_revalidate");
19+
1220
// Get all user rules
1321
$all_roles = cache_everything_get_role_slugs();
1422

public/js/cache-everything.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,44 @@ function isUser() {
149149
return window.wce_userRoles && window.wce_userRoles.status && window.wce_userRoles.status.toLowerCase() === 'user';
150150
}
151151

152+
/**
153+
* Checks if the user is an administrator and either rewrites every link on the page to use a cachebuster
154+
* or reloads the current page with a cachebuster, depending on whether the WordPress admin bar is present.
155+
* This function utilizes the isRole function to determine if the user has an administrator role.
156+
* If the user is an administrator, the current page is not in the wp-admin directory, and the Elementor editor is not active,
157+
* and if the wp-admin bar is not present, it reloads the current page with a cachebuster. Otherwise, it appends a timestamp
158+
* as a query parameter to each link on the page to ensure the content is not served from cache.
159+
*/
160+
function rewriteLinksOrReloadForAdmin() {
161+
const isEditorActive = document.querySelector('.elementor-editor-active') !== null;
162+
const isWpAdminBarPresent = document.getElementById('wpadminbar') !== null;
163+
164+
debugPrint(`Checking admin role and page conditions for cachebuster application. Admin: ${isRole('administrator')}, WP Admin: ${window.location.href.includes('/wp-admin/')}, Editor Active: ${isEditorActive}, WP Admin Bar Present: ${isWpAdminBarPresent}`);
165+
166+
if (isRole('administrator') && !window.location.href.includes('/wp-admin/') && !isEditorActive) {
167+
if (!isWpAdminBarPresent) {
168+
// Reload the current page with a cachebuster if the wp-admin bar is not present
169+
const currentUrl = window.location.href;
170+
const hasQueryParams = currentUrl.includes('?');
171+
const cachebusterUrl = `${currentUrl}${hasQueryParams ? '&' : '?'}cachebuster=${new Date().getTime()}`;
172+
debugPrint(`Reloading page for admin without WP Admin Bar: ${cachebusterUrl}`);
173+
window.location.href = cachebusterUrl;
174+
} else {
175+
// Get all the links on the page and append a cachebuster to each if the wp-admin bar is present
176+
const links = document.querySelectorAll('a:not(#wpadminbar a)');
177+
const timestamp = new Date().getTime();
178+
179+
links.forEach(link => {
180+
const currentUrl = link.href;
181+
const hasQueryParams = currentUrl.includes('?');
182+
const newUrl = `${currentUrl}${hasQueryParams ? '&' : '?'}cachebuster=${timestamp}`;
183+
link.href = newUrl;
184+
});
185+
debugPrint(`Appended cachebuster to links for admin with WP Admin Bar present.`);
186+
}
187+
}
188+
}
189+
152190
/**
153191
* Checks if the specified role is included for the user, case-insensitively.
154192
* @param {string} roleName - The name of the role to check.
@@ -452,7 +490,10 @@ addScriptToDOM();
452490
// Check if the DOM is already loaded
453491
if (document.readyState === "loading") {
454492
document.addEventListener('DOMContentLoaded', setupPrefetching);
493+
document.addEventListener('DOMContentLoaded', rewriteLinksOrReloadForAdmin);
494+
455495
} else {
456496
// DOMContentLoaded has already fired, call the function directly
457497
setupPrefetching();
498+
rewriteLinksOrReloadForAdmin();
458499
}

0 commit comments

Comments
 (0)