' ).addClass( 'jp-spinner__outer' ).appendTo( spinner );
$( '
' ).addClass( 'jp-spinner__inner' ).appendTo( spinnerOuter );
loadingText.after( spinner );
},
handleAuthorizeInPlaceFlow: function ( data ) {
window.addEventListener( 'message', jetpackConnectButton.receiveData );
jetpackConnectIframe.attr(
'src',
data.authorizeUrl + '&from=' + connectButtonFrom + '&iframe_source=jetpack-connect-main'
);
jetpackConnectIframe.on( 'load', function () {
jetpackConnectIframe.show();
$( '.jp-connect-full__button-container' ).hide();
$( '#jp-connect-full__step1-header' ).hide();
$( '#jp-connect-full__step2-header' ).show();
} );
jetpackConnectIframe.hide();
$( '.jp-connect-full__button-container' ).after( jetpackConnectIframe );
// At this point we are pretty sure if things work out that we will be loading the admin script
var link = document.createElement( 'link' );
link.rel = 'preload';
link.as = 'script';
link.href = jpConnect.preFetchScript;
document.head.appendChild( link );
},
fetchPlanType: function () {
return $.ajax( {
url: jpConnect.apiBaseUrl + '/site',
type: 'GET',
data: {
_wpnonce: jpConnect.apiSiteDataNonce,
},
success: function ( data ) {
var siteData = JSON.parse( data.data );
jetpackConnectButton.isPaidPlan =
siteData.options.is_pending_plan || ! siteData.plan.is_free;
},
} );
},
receiveData: function ( event ) {
if (
event.origin !== jpConnect.jetpackApiDomain ||
event.source !== jetpackConnectIframe.get( 0 ).contentWindow
) {
return;
}
switch ( event.data ) {
case 'close':
window.removeEventListener( 'message', this.receiveData );
jetpackConnectButton.handleAuthorizationComplete();
break;
case 'wpcom_nocookie':
jetpackConnectIframe.hide();
jetpackConnectButton.handleConnectionError();
break;
}
},
handleAuthorizationComplete: function () {
jetpackConnectButton.isRegistering = false;
// Fetch plan type late to make sure any stored license keys have been
// attached to the site during the connection.
jetpackConnectButton.fetchPlanType().always( function () {
if ( ! jetpackConnectButton.isPaidPlan ) {
window.location.assign( jpConnect.plansPromptUrl );
return;
}
var parser = document.createElement( 'a' );
parser.href = jpConnect.dashboardUrl;
var reload =
window.location.pathname === parser.pathname &&
window.location.hash.length &&
parser.hash.length;
window.location.assign( jpConnect.dashboardUrl );
if ( reload ) {
// The Jetpack admin page has hashes in the URLs, so we need to reload the page after .assign()
window.location.reload( true );
}
} );
},
handleConnectionError: function ( error ) {
jetpackConnectButton.isRegistering = false;
// If something goes wrong, we take users to Calypso.
window.location = connectButton.attr( 'href' );
},
};
// In case the parameter has been manually set in the URL after redirect.
connectButtonFrom = location.hash.split( '&from=' )[ 1 ];
} );