' )
.addClass( 'jp-spinner__outer' )
.appendTo( spinner );
$( '
' )
.addClass( 'jp-spinner__inner' )
.appendTo( spinnerOuter );
loadingText.after( spinner );
},
handleConnectionSuccess: function( data ) {
jetpackConnectButton.fetchPlanType();
window.addEventListener( 'message', jetpackConnectButton.receiveData );
jetpackConnectIframe.attr( 'src', data.authorizeUrl + '&from=' + connectButtonFrom );
jetpackConnectIframe.on( 'load', function() {
jetpackConnectIframe.show();
$( '.jp-connect-full__button-container' ).hide();
} );
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() {
$.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 &&
event.data === 'close'
) {
window.removeEventListener( 'message', this.receiveData );
jetpackConnectButton.handleAuthorizationComplete();
}
},
handleAuthorizationComplete: function() {
jetpackConnectButton.isRegistering = false;
if ( jetpackConnectButton.isPaidPlan ) {
window.location.assign( jpConnect.dashboardUrl );
} else {
window.location.assign( jpConnect.plansPromptUrl );
}
// The Jetpack admin page has hashes in the URLs, so we need to reload the page after .assign()
if ( window.location.hash ) {
window.location.reload( true );
}
},
handleConnectionError: function( error ) {
jetpackConnectButton.isRegistering = false;
jetpackConnectButton.handleOriginalFlow();
},
};
// When we visit /wp-admin/admin.php?page=jetpack#/setup, immediately start the connection flow.
var hash = location.hash.replace( /#\//, '' );
if ( 'setup' === hash ) {
if ( connectionHelpSections.length ) {
connectionHelpSections.hide();
}
jetpackConnectButton.selectAndStartConnectionFlow();
}
} );