← All changes
|
modules/subscriptions/subscribe-overlay/subscribe-overlay.js
+22
-8
13.5.2
→
16.3-a.5
View file →
| @@ -2,22 +2,37 @@ | ||
| 2 | 2 | |
| 3 | 3 | domReady( function () { |
| 4 | 4 | const overlay = document.querySelector( '.jetpack-subscribe-overlay' ); |
| 5 | 5 | const overlayDismissedCookie = 'jetpack_post_subscribe_overlay_dismissed'; |
| 6 | + const skipUrlParam = 'jetpack_skip_subscription_popup'; | |
| 6 | 7 | const hasOverlayDismissedCookie = |
| 7 | 8 | document.cookie && document.cookie.indexOf( overlayDismissedCookie ) > -1; |
| 8 | 9 | |
| 9 | - if ( ! overlay || hasOverlayDismissedCookie ) { | |
| 10 | + // Subscriber ended up here e.g. from emails: | |
| 11 | + // we won't show the overlay to them in future since they most likely are already a subscriber. | |
| 12 | + function skipOverlay() { | |
| 13 | + const url = new URL( window.location.href ); | |
| 14 | + if ( url.searchParams.has( skipUrlParam ) ) { | |
| 15 | + url.searchParams.delete( skipUrlParam ); | |
| 16 | + window.history.replaceState( {}, '', url ); | |
| 17 | + setOverlayDismissedCookie(); | |
| 18 | + return true; | |
| 19 | + } | |
| 20 | + | |
| 21 | + return false; | |
| 22 | + } | |
| 23 | + | |
| 24 | + if ( ! overlay || hasOverlayDismissedCookie || skipOverlay() ) { | |
| 10 | 25 | return; |
| 11 | 26 | } |
| 12 | 27 | |
| 13 | - const close = document.querySelector( '.jetpack-subscribe-overlay__close' ); | |
| 28 | + const close = overlay.querySelector( '.jetpack-subscribe-overlay__close' ); | |
| 14 | 29 | close.onclick = function ( event ) { |
| 15 | 30 | event.preventDefault(); |
| 16 | 31 | closeOverlay(); |
| 17 | 32 | }; |
| 18 | 33 | |
| 19 | - const toContent = document.querySelector( '.jetpack-subscribe-overlay__to-content' ); | |
| 34 | + const toContent = overlay.querySelector( '.jetpack-subscribe-overlay__to-content' ); | |
| 20 | 35 | // User can edit overlay, and could remove to content link. |
| 21 | 36 | if ( toContent ) { |
| 22 | 37 | toContent.onclick = function ( event ) { |
| 23 | 38 | event.preventDefault(); |
| @@ -24,11 +39,12 @@ | ||
| 24 | 39 | closeOverlay(); |
| 25 | 40 | }; |
| 26 | 41 | } |
| 27 | 42 | |
| 28 | - const form = document.querySelector( '.jetpack-subscribe-overlay form' ); | |
| 43 | + // When the form is submitted, and next modal loads, it'll fire "subscription-modal-loaded" signalling that this form can be hidden. | |
| 44 | + const form = overlay.querySelector( 'form' ); | |
| 29 | 45 | if ( form ) { |
| 30 | - form.addEventListener( 'submit', closeOverlay ); | |
| 46 | + form.addEventListener( 'subscription-modal-loaded', closeOverlay ); | |
| 31 | 47 | } |
| 32 | 48 | |
| 33 | 49 | function closeOverlayOnEscapeKeydown( event ) { |
| 34 | 50 | if ( event.key === 'Escape' ) { |
| @@ -49,11 +65,9 @@ | ||
| 49 | 65 | window.removeEventListener( 'keydown', closeOverlayOnEscapeKeydown ); |
| 50 | 66 | } |
| 51 | 67 | |
| 52 | 68 | function setOverlayDismissedCookie() { |
| 53 | - // Expires in 7 days | |
| 54 | - const expires = new Date( Date.now() + 7 * 86400 * 1000 ).toUTCString(); | |
| 55 | - document.cookie = `${ overlayDismissedCookie }=true; expires=${ expires }; path=/;`; | |
| 69 | + document.cookie = `${ overlayDismissedCookie }=true; path=/;`; | |
| 56 | 70 | } |
| 57 | 71 | |
| 58 | 72 | openOverlay(); |
| 59 | 73 | } ); |