class-jetpack-subscription-modal-on-comment.php
2 years ago
subscription-modal.css
2 years ago
subscription-modal.js
2 years ago
subscription-modal.js
133 lines
| 1 | document.addEventListener( 'DOMContentLoaded', function () { |
| 2 | const modal = document.getElementsByClassName( 'jetpack-subscription-modal' )[ 0 ]; |
| 3 | |
| 4 | if ( ! modal ) { |
| 5 | return; |
| 6 | } |
| 7 | |
| 8 | const close = document.getElementsByClassName( 'jetpack-subscription-modal__close' )[ 0 ]; |
| 9 | |
| 10 | let redirectUrl = ''; |
| 11 | let hasLoaded = false; |
| 12 | |
| 13 | function reloadOnCloseSubscriptionModal( customUrl ) { |
| 14 | const destinationUrl = customUrl ? new URL( customUrl ) : new URL( redirectUrl ); |
| 15 | |
| 16 | // Prevent redirect to external sites. |
| 17 | if ( destinationUrl.hostname !== window.location.hostname ) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | localStorage.setItem( 'jetpack-subscription-modal-on-comment-scroll-to', destinationUrl.hash ); |
| 22 | |
| 23 | // For avoiding Firefox reload, we need to force reload bypassing the cache. |
| 24 | window.location.reload( true ); |
| 25 | } |
| 26 | |
| 27 | function JetpackSubscriptionModalOnCommentMessageListener( event ) { |
| 28 | let message = event && event.data; |
| 29 | if ( typeof message === 'string' ) { |
| 30 | try { |
| 31 | message = JSON.parse( message ); |
| 32 | } catch ( err ) { |
| 33 | return; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | const type = message && message.type; |
| 38 | const data = message && message.data; |
| 39 | |
| 40 | if ( type !== 'subscriptionModalShow' || typeof data.url === 'undefined' ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | if ( ! event.origin.includes( window.location.host ) ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if ( data.email ) { |
| 49 | const emailInput = document.querySelector( |
| 50 | '.jetpack-subscription-modal__modal-content input[type=email]' |
| 51 | ); |
| 52 | if ( ! emailInput ) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | const appSource = document.querySelector( |
| 57 | '.jetpack-subscription-modal__modal-content input[name=app_source]' |
| 58 | ); |
| 59 | if ( ! appSource ) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | emailInput.value = data.email; |
| 64 | if ( data.is_logged_in ) { |
| 65 | emailInput.setAttribute( 'readonly', 'readonly' ); |
| 66 | appSource.value = 'atomic-subscription-modal-li'; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if ( ! hasLoaded ) { |
| 71 | const storedCount = parseInt( |
| 72 | sessionStorage.getItem( 'jetpack-subscription-modal-shown-count' ) |
| 73 | ); |
| 74 | const showCount = ( isNaN( storedCount ) ? 0 : storedCount ) + 1; |
| 75 | sessionStorage.setItem( 'jetpack-subscription-modal-shown-count', showCount ); |
| 76 | |
| 77 | if ( showCount > 5 ) { |
| 78 | new Image().src = |
| 79 | document.location.protocol + |
| 80 | '//pixel.wp.com/g.gif?v=wpcom-no-pv&x_jetpack-subscribe-modal-comm=hidden_views_limit&r=' + |
| 81 | Math.random(); |
| 82 | |
| 83 | reloadOnCloseSubscriptionModal( data.url ); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | new Image().src = |
| 88 | document.location.protocol + |
| 89 | '//pixel.wp.com/g.gif?v=wpcom-no-pv&x_jetpack-subscribe-modal-comm=showed&r=' + |
| 90 | Math.random(); |
| 91 | |
| 92 | modal.classList.toggle( 'open' ); |
| 93 | hasLoaded = true; |
| 94 | redirectUrl = data.url; |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | window.addEventListener( 'message', JetpackSubscriptionModalOnCommentMessageListener ); |
| 100 | |
| 101 | if ( close ) { |
| 102 | close.onclick = function ( event ) { |
| 103 | event.preventDefault(); |
| 104 | modal.classList.toggle( 'open' ); |
| 105 | reloadOnCloseSubscriptionModal(); |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | window.onclick = function ( event ) { |
| 110 | if ( event.target === modal ) { |
| 111 | modal.style.display = 'none'; |
| 112 | reloadOnCloseSubscriptionModal(); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | window.addEventListener( 'load', () => { |
| 117 | // Scroll to the last comment. |
| 118 | const subscriptionScroll = localStorage.getItem( |
| 119 | 'jetpack-subscription-modal-on-comment-scroll-to' |
| 120 | ); |
| 121 | |
| 122 | if ( subscriptionScroll ) { |
| 123 | window.location.hash = subscriptionScroll; |
| 124 | localStorage.removeItem( 'jetpack-subscription-modal-on-comment-scroll-to' ); |
| 125 | |
| 126 | const comment = document.querySelector( subscriptionScroll ); |
| 127 | if ( comment ) { |
| 128 | comment.scrollIntoView( { block: 'center', behavior: 'smooth' } ); |
| 129 | } |
| 130 | } |
| 131 | } ); |
| 132 | } ); |
| 133 |