| 1 |
/* global jpConnect */ |
| 2 |
|
| 3 |
jQuery( document ).ready( function( $ ) { |
| 4 |
var connectButton = $( '.jp-connect-button, .jp-banner__alt-connect-button' ).eq( 0 ); |
| 5 |
var tosText = $( '.jp-connect-full__tos-blurb' ); |
| 6 |
var jetpackConnectIframe = $( '<iframe class="jp-jetpack-connect__iframe" />' ); |
| 7 |
var connectionHelpSections = $( |
| 8 |
'#jetpack-connection-cards, .jp-connect-full__dismiss-paragraph' |
| 9 |
); |
| 10 |
|
| 11 |
connectButton.on( 'click', function( event ) { |
| 12 |
event.preventDefault(); |
| 13 |
|
| 14 |
if ( connectionHelpSections.length ) { |
| 15 |
connectionHelpSections.fadeOut( 600 ); |
| 16 |
} |
| 17 |
|
| 18 |
jetpackConnectButton.selectAndStartConnectionFlow(); |
| 19 |
} ); |
| 20 |
|
| 21 |
var jetpackConnectButton = { |
| 22 |
isRegistering: false, |
| 23 |
isPaidPlan: false, |
| 24 |
selectAndStartConnectionFlow: function() { |
| 25 |
var connectionHelpSections = $( '#jetpack-connection-cards' ); |
| 26 |
if ( connectionHelpSections.length ) { |
| 27 |
connectionHelpSections.fadeOut( 600 ); |
| 28 |
} |
| 29 |
|
| 30 |
if ( ! jetpackConnectButton.isRegistering ) { |
| 31 |
if ( 'original' === jpConnect.forceVariation ) { |
| 32 |
// Forcing original connection flow, `JETPACK_SHOULD_USE_CONNECTION_IFRAME = false` |
| 33 |
// or we're dealing with Safari which has issues with handling 3rd party cookies. |
| 34 |
jetpackConnectButton.handleOriginalFlow(); |
| 35 |
} else if ( 'in_place' === jpConnect.forceVariation ) { |
| 36 |
// Forcing new connection flow, `JETPACK_SHOULD_USE_CONNECTION_IFRAME = true`. |
| 37 |
jetpackConnectButton.handleConnectInPlaceFlow(); |
| 38 |
} else { |
| 39 |
// Forcing A/B test driven connection flow variation, `JETPACK_SHOULD_USE_CONNECTION_IFRAME` not defined. |
| 40 |
jetpackConnectButton.startConnectionFlow(); |
| 41 |
} |
| 42 |
} |
| 43 |
}, |
| 44 |
startConnectionFlow: function() { |
| 45 |
var abTestName = 'jetpack_connect_in_place_v3'; |
| 46 |
|
| 47 |
$.ajax( { |
| 48 |
url: 'https://public-api.wordpress.com/wpcom/v2/abtest/' + abTestName, |
| 49 |
type: 'GET', |
| 50 |
error: jetpackConnectButton.handleConnectionError, |
| 51 |
data: jpConnect.identity, |
| 52 |
xhrFields: { |
| 53 |
withCredentials: true, |
| 54 |
}, |
| 55 |
crossDomain: true, |
| 56 |
success: function( data ) { |
| 57 |
if ( data && 'in_place' === data.variation ) { |
| 58 |
jetpackConnectButton.handleConnectInPlaceFlow(); |
| 59 |
return; |
| 60 |
} |
| 61 |
jetpackConnectButton.handleOriginalFlow(); |
| 62 |
}, |
| 63 |
} ); |
| 64 |
}, |
| 65 |
handleOriginalFlow: function() { |
| 66 |
window.location = connectButton.attr( 'href' ); |
| 67 |
}, |
| 68 |
handleConnectInPlaceFlow: function() { |
| 69 |
// Alternative connection buttons should redirect to the main one for the "connect in place" flow. |
| 70 |
if ( connectButton.hasClass( 'jp-banner__alt-connect-button' ) ) { |
| 71 |
window.location = jpConnect.connectInPlaceUrl; |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
jetpackConnectButton.isRegistering = true; |
| 76 |
tosText.hide(); |
| 77 |
connectButton.hide(); |
| 78 |
jetpackConnectButton.triggerLoadingState(); |
| 79 |
|
| 80 |
var registerUrl = jpConnect.apiBaseUrl + '/connection/register'; |
| 81 |
|
| 82 |
// detect Calypso Env and add to API URL |
| 83 |
if ( window.Initial_State && window.Initial_State.calypsoEnv ) { |
| 84 |
registerUrl = |
| 85 |
registerUrl + '?' + $.param( { calypso_env: window.Initial_State.calypsoEnv } ); |
| 86 |
} |
| 87 |
|
| 88 |
$.ajax( { |
| 89 |
url: registerUrl, |
| 90 |
type: 'POST', |
| 91 |
data: { |
| 92 |
registration_nonce: jpConnect.registrationNonce, |
| 93 |
_wpnonce: jpConnect.apiNonce, |
| 94 |
}, |
| 95 |
error: jetpackConnectButton.handleConnectionError, |
| 96 |
success: jetpackConnectButton.handleConnectionSuccess, |
| 97 |
} ); |
| 98 |
}, |
| 99 |
triggerLoadingState: function() { |
| 100 |
var loadingText = $( '<span>' ) |
| 101 |
.addClass( 'jp-connect-full__button-container-loading' ) |
| 102 |
.text( jpConnect.buttonTextRegistering ) |
| 103 |
.appendTo( '.jp-connect-full__button-container' ); |
| 104 |
|
| 105 |
var spinner = $( '<div>' ).addClass( 'jp-spinner' ); |
| 106 |
var spinnerOuter = $( '<div>' ) |
| 107 |
.addClass( 'jp-spinner__outer' ) |
| 108 |
.appendTo( spinner ); |
| 109 |
$( '<div>' ) |
| 110 |
.addClass( 'jp-spinner__inner' ) |
| 111 |
.appendTo( spinnerOuter ); |
| 112 |
loadingText.after( spinner ); |
| 113 |
}, |
| 114 |
handleConnectionSuccess: function( data ) { |
| 115 |
jetpackConnectButton.fetchPlanType(); |
| 116 |
window.addEventListener( 'message', jetpackConnectButton.receiveData ); |
| 117 |
jetpackConnectIframe.attr( 'src', data.authorizeUrl ); |
| 118 |
jetpackConnectIframe.on( 'load', function() { |
| 119 |
jetpackConnectIframe.show(); |
| 120 |
$( '.jp-connect-full__button-container' ).hide(); |
| 121 |
} ); |
| 122 |
jetpackConnectIframe.hide(); |
| 123 |
$( '.jp-connect-full__button-container' ).after( jetpackConnectIframe ); |
| 124 |
}, |
| 125 |
fetchPlanType: function() { |
| 126 |
$.ajax( { |
| 127 |
url: jpConnect.apiBaseUrl + '/site', |
| 128 |
type: 'GET', |
| 129 |
data: { |
| 130 |
_wpnonce: jpConnect.apiSiteDataNonce, |
| 131 |
}, |
| 132 |
success: function( data ) { |
| 133 |
var siteData = JSON.parse( data.data ); |
| 134 |
jetpackConnectButton.isPaidPlan = ! siteData.plan.is_free; |
| 135 |
}, |
| 136 |
} ); |
| 137 |
}, |
| 138 |
receiveData: function( event ) { |
| 139 |
if ( |
| 140 |
event.origin === jpConnect.jetpackApiDomain && |
| 141 |
event.source === jetpackConnectIframe.get( 0 ).contentWindow && |
| 142 |
event.data === 'close' |
| 143 |
) { |
| 144 |
window.removeEventListener( 'message', this.receiveData ); |
| 145 |
jetpackConnectButton.handleAuthorizationComplete(); |
| 146 |
} |
| 147 |
}, |
| 148 |
handleAuthorizationComplete: function() { |
| 149 |
jetpackConnectButton.isRegistering = false; |
| 150 |
|
| 151 |
if ( jetpackConnectButton.isPaidPlan ) { |
| 152 |
window.location.assign( jpConnect.dashboardUrl ); |
| 153 |
} else { |
| 154 |
window.location.assign( jpConnect.plansPromptUrl ); |
| 155 |
} |
| 156 |
window.location.reload( true ); |
| 157 |
}, |
| 158 |
handleConnectionError: function( error ) { |
| 159 |
jetpackConnectButton.isRegistering = false; |
| 160 |
jetpackConnectButton.handleOriginalFlow(); |
| 161 |
}, |
| 162 |
}; |
| 163 |
|
| 164 |
// When we visit /wp-admin/admin.php?page=jetpack#/setup, immediately start the connection flow. |
| 165 |
var hash = location.hash.replace( /#\//, '' ); |
| 166 |
if ( 'setup' === hash ) { |
| 167 |
if ( connectionHelpSections.length ) { |
| 168 |
connectionHelpSections.hide(); |
| 169 |
} |
| 170 |
|
| 171 |
jetpackConnectButton.selectAndStartConnectionFlow(); |
| 172 |
} |
| 173 |
} ); |
| 174 |
|