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