-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
VIP Cache Manager: implement "Purge page and its assets" admin bar button #1880
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
283ec4b
VIP Cache Manager: implement "Purge page and its assets" admin bar bu…
rinatkhaziev d3315ff
Merge branch 'master' into add/purge-cache-page-button
rinatkhaziev 2fa146f
Track usage via send_pixel
rinatkhaziev 4c5d320
Address send_pixel feedback: make keys more specific, don't track vip…
rinatkhaziev 1614ed1
Merge branch 'master' into add/purge-cache-page-button
mjangda 488d104
Remove unnecessary template literal
rinatkhaziev 71ef853
ESlint - 'nacin spacin'
rinatkhaziev 5f07add
s/purgeQueried/purgeInProgress/ - better naming
rinatkhaziev 4c77f11
Split compound conditional for auth check into two - the one for perm…
rinatkhaziev 49701c8
Merge branch 'master' into add/purge-cache-page-button
rinatkhaziev 56425cd
Merge branch 'master' into add/purge-cache-page-button
rinatkhaziev be7c89a
Wait until DOMContentLoaded
mjangda b1ff7ab
Prevent the default on button click
rinatkhaziev 879f958
Bail early if we don't have any URLs to purge
mjangda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,4 @@ | |
"eslint": "5.16.0", | ||
"husky": "4.2.3" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
( async () => { | ||
async function postData( url = '', data = {} ) { | ||
// Default options are marked with * | ||
const response = await fetch( url, { | ||
method: 'POST', | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify( { ...data } ) | ||
} ); | ||
return response.json(); | ||
} | ||
|
||
// Indicates whether there's a purge request happening | ||
let purgeInProgress = false; | ||
// Stores the ref to the DOM node | ||
let btn; | ||
|
||
/** | ||
* Grab all the necesary URLs (incl. scripts and CSS) for the purge. | ||
*/ | ||
const getURLsToPurge = () => { | ||
return [document.location.toString()].concat( | ||
Array.from( document.querySelectorAll( 'script[src]' ) ).map( ( { src } ) => src ), | ||
Array.from( document.querySelectorAll( 'link[rel=stylesheet]' ) ).map( ( { href } ) => href ) | ||
) | ||
.filter( url => url.includes( document.location.hostname ) ); | ||
} | ||
|
||
/** | ||
* Cache purge click handler. | ||
* | ||
* @param {Event} e | ||
*/ | ||
async function onClickHandler( e ) { | ||
e.preventDefault(); | ||
|
||
if ( purgeInProgress ) { | ||
return; | ||
} | ||
|
||
let { nonce = '', ajaxurl = '' } = window.VIPPageFlush || {}; | ||
|
||
if ( !( nonce && ajaxurl ) ) { | ||
alert( 'VIP Cache Manager: page cache purging disabled' ); | ||
} | ||
|
||
purgeInProgress = true; | ||
|
||
const urls = getURLsToPurge(); | ||
|
||
try { | ||
const res = await postData( ajaxurl, { nonce, urls } ); | ||
const { success, data } = res; | ||
|
||
btn.textContent = data.result || 'Success'; | ||
btn.disabled = true; | ||
btn.removeEventListener( 'click', onClickHandler ); | ||
} catch ( err ) { | ||
purgeInProgress = false; | ||
btn.textContent = '❌ Cache Purge Failed'; | ||
} | ||
} | ||
|
||
document.addEventListener( 'DOMContentLoaded', () => { | ||
btn = document.querySelector( '#wp-admin-bar-vip-purge-page > .ab-item' ) | ||
if ( btn ) { | ||
btn.addEventListener( 'click', onClickHandler ); | ||
} | ||
} ); | ||
|
||
} )(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there the option to use https://make.wordpress.org/accessibility/handbook/markup/wp-a11y-speak/ here, so that screen reader users get notified on the success / failure of the action?