@@ -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
453491if ( 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