class-jetpack-subscribe-overlay.php
2 years ago
subscribe-overlay.css
2 years ago
subscribe-overlay.js
2 years ago
subscribe-overlay.js
60 lines
| 1 | const { domReady } = wp; |
| 2 | |
| 3 | domReady( function () { |
| 4 | const overlay = document.querySelector( '.jetpack-subscribe-overlay' ); |
| 5 | const overlayDismissedCookie = 'jetpack_post_subscribe_overlay_dismissed'; |
| 6 | const hasOverlayDismissedCookie = |
| 7 | document.cookie && document.cookie.indexOf( overlayDismissedCookie ) > -1; |
| 8 | |
| 9 | if ( ! overlay || hasOverlayDismissedCookie ) { |
| 10 | return; |
| 11 | } |
| 12 | |
| 13 | const close = document.querySelector( '.jetpack-subscribe-overlay__close' ); |
| 14 | close.onclick = function ( event ) { |
| 15 | event.preventDefault(); |
| 16 | closeOverlay(); |
| 17 | }; |
| 18 | |
| 19 | const toContent = document.querySelector( '.jetpack-subscribe-overlay__to-content' ); |
| 20 | // User can edit overlay, and could remove to content link. |
| 21 | if ( toContent ) { |
| 22 | toContent.onclick = function ( event ) { |
| 23 | event.preventDefault(); |
| 24 | closeOverlay(); |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | const form = document.querySelector( '.jetpack-subscribe-overlay form' ); |
| 29 | if ( form ) { |
| 30 | form.addEventListener( 'submit', closeOverlay ); |
| 31 | } |
| 32 | |
| 33 | function closeOverlayOnEscapeKeydown( event ) { |
| 34 | if ( event.key === 'Escape' ) { |
| 35 | closeOverlay(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | function openOverlay() { |
| 40 | overlay.classList.add( 'open' ); |
| 41 | document.body.classList.add( 'jetpack-subscribe-overlay-open' ); |
| 42 | setOverlayDismissedCookie(); |
| 43 | window.addEventListener( 'keydown', closeOverlayOnEscapeKeydown ); |
| 44 | } |
| 45 | |
| 46 | function closeOverlay() { |
| 47 | overlay.classList.remove( 'open' ); |
| 48 | document.body.classList.remove( 'jetpack-subscribe-overlay-open' ); |
| 49 | window.removeEventListener( 'keydown', closeOverlayOnEscapeKeydown ); |
| 50 | } |
| 51 | |
| 52 | 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=/;`; |
| 56 | } |
| 57 | |
| 58 | openOverlay(); |
| 59 | } ); |
| 60 |