Skip to content

Commit

Permalink
Newsletters: allow skipping subscription modals with URL query arg (#…
Browse files Browse the repository at this point in the history
…39644)

* Newsletters: allow skipping modals with URL query param

* changelog

* More elegant by cleaning up the URL param

* Use replaceState instead of pushState to avoid leaving history
  • Loading branch information
simison authored Oct 15, 2024
1 parent 922e940 commit c08005e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Newsletters: allow skipping modals with URL query param
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ const { domReady } = wp;
domReady( () => {
const modal = document.querySelector( '.jetpack-subscribe-modal' );
const modalDismissedCookie = 'jetpack_post_subscribe_modal_dismissed';
const skipUrlParam = 'jetpack_skip_subscription_popup';

function hasEnoughTimePassed() {
const lastDismissed = localStorage.getItem( modalDismissedCookie );
return lastDismissed ? Date.now() - lastDismissed > Jetpack_Subscriptions.modalInterval : true;
}

if ( ! modal || ! hasEnoughTimePassed() ) {
// Subscriber ended up here e.g. from emails:
// we won't show the modal to them in future since they most likely are already a subscriber.
function skipModal() {
const url = new URL( window.location.href );
if ( url.searchParams.has( skipUrlParam ) ) {
url.searchParams.delete( skipUrlParam );
window.history.replaceState( {}, '', url );
storeCloseTimestamp();
return true;
}

return false;
}

if ( ! modal || ! hasEnoughTimePassed() || skipModal() ) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@ const { domReady } = wp;
domReady( function () {
const overlay = document.querySelector( '.jetpack-subscribe-overlay' );
const overlayDismissedCookie = 'jetpack_post_subscribe_overlay_dismissed';
const skipUrlParam = 'jetpack_skip_subscription_popup';
const hasOverlayDismissedCookie =
document.cookie && document.cookie.indexOf( overlayDismissedCookie ) > -1;

if ( ! overlay || hasOverlayDismissedCookie ) {
// Subscriber ended up here e.g. from emails:
// we won't show the overlay to them in future since they most likely are already a subscriber.
function skipOverlay() {
const url = new URL( window.location.href );
if ( url.searchParams.has( skipUrlParam ) ) {
url.searchParams.delete( skipUrlParam );
window.history.replaceState( {}, '', url );
setOverlayDismissedCookie();
return true;
}

return false;
}

if ( ! overlay || hasOverlayDismissedCookie || skipOverlay() ) {
return;
}

Expand Down

0 comments on commit c08005e

Please sign in to comment.