PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.8
Jetpack – WP Security, Backup, Speed, & Growth v7.8
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / _inc / connect-button.js

connect-button.js in Jetpack – WP Security, Backup, Speed, & Growth 7.8, at _inc/connect-button.js

140 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* global jpConnect */
2
3 jQuery( document ).ready( function( $ ) {
4 var connectButton = $( '.jp-connect-button' );
5 var tosText = $( '.jp-connect-full__tos-blurb' );
6 connectButton.click( function( event ) {
7 event.preventDefault();
8 $( '#jetpack-connection-cards' ).fadeOut( 600 );
9 if ( ! jetpackConnectButton.isRegistering ) {
10 if ( 'original' === jpConnect.forceVariation ) {
11 // Forcing original connection flow, `JETPACK_SHOULD_USE_CONNECTION_IFRAME = false`
12 // or we're dealing with Safari which has issues with handling 3rd party cookies.
13 jetpackConnectButton.handleOriginalFlow();
14 } else if ( 'in_place' === jpConnect.forceVariation ) {
15 // Forcing new connection flow, `JETPACK_SHOULD_USE_CONNECTION_IFRAME = true`.
16 jetpackConnectButton.handleConnectInPlaceFlow();
17 } else {
18 // Forcing A/B test driven connection flow variation, `JETPACK_SHOULD_USE_CONNECTION_IFRAME` not defined.
19 jetpackConnectButton.startConnectionFlow();
20 }
21 }
22 } );
23 var jetpackConnectIframe = $( '<iframe class="jp-jetpack-connect__iframe" />' );
24
25 var jetpackConnectButton = {
26 isRegistering: false,
27 isPaidPlan: false,
28 startConnectionFlow: function() {
29 var abTestName = 'jetpack_connect_in_place_v2';
30 $.ajax( {
31 url: 'https://public-api.wordpress.com/wpcom/v2/abtest/' + abTestName,
32 type: 'GET',
33 error: jetpackConnectButton.handleConnectionError,
34 xhrFields: {
35 withCredentials: true,
36 },
37 crossDomain: true,
38 success: function( data ) {
39 if ( data && 'in_place' === data.variation ) {
40 jetpackConnectButton.handleConnectInPlaceFlow();
41 return;
42 }
43 jetpackConnectButton.handleOriginalFlow();
44 },
45 } );
46 },
47 handleOriginalFlow: function() {
48 window.location = connectButton.attr( 'href' );
49 },
50 handleConnectInPlaceFlow: function() {
51 jetpackConnectButton.isRegistering = true;
52 tosText.hide();
53 connectButton.hide();
54 jetpackConnectButton.triggerLoadingState();
55
56 var registerUrl = jpConnect.apiBaseUrl + '/connection/register';
57
58 // detect Calypso Env and add to API URL
59 if ( window.Initial_State && window.Initial_State.calypsoEnv ) {
60 registerUrl =
61 registerUrl + '?' + $.param( { calypso_env: window.Initial_State.calypsoEnv } );
62 }
63
64 $.ajax( {
65 url: registerUrl,
66 type: 'POST',
67 data: {
68 registration_nonce: jpConnect.registrationNonce,
69 _wpnonce: jpConnect.apiNonce,
70 },
71 error: jetpackConnectButton.handleConnectionError,
72 success: jetpackConnectButton.handleConnectionSuccess,
73 } );
74 },
75 triggerLoadingState: function() {
76 var loadingText = $( '<span>' )
77 .addClass( 'jp-connect-full__button-container-loading' )
78 .text( jpConnect.buttonTextRegistering )
79 .appendTo( '.jp-connect-full__button-container' );
80
81 var spinner = $( '<div>' ).addClass( 'jp-spinner' );
82 var spinnerOuter = $( '<div>' )
83 .addClass( 'jp-spinner__outer' )
84 .appendTo( spinner );
85 $( '<div>' )
86 .addClass( 'jp-spinner__inner' )
87 .appendTo( spinnerOuter );
88 loadingText.after( spinner );
89 },
90 handleConnectionSuccess: function( data ) {
91 jetpackConnectButton.fetchPlanType();
92 window.addEventListener( 'message', jetpackConnectButton.receiveData );
93 jetpackConnectIframe.attr( 'src', data.authorizeUrl );
94 jetpackConnectIframe.on( 'load', function() {
95 jetpackConnectIframe.show();
96 $( '.jp-connect-full__button-container' ).hide();
97 } );
98 jetpackConnectIframe.hide();
99 $( '.jp-connect-full__button-container' ).after( jetpackConnectIframe );
100 },
101 fetchPlanType: function() {
102 $.ajax( {
103 url: jpConnect.apiBaseUrl + '/site',
104 type: 'GET',
105 data: {
106 _wpnonce: jpConnect.apiSiteDataNonce,
107 },
108 success: function( data ) {
109 var siteData = JSON.parse( data.data );
110 jetpackConnectButton.isPaidPlan = ! siteData.plan.is_free;
111 },
112 } );
113 },
114 receiveData: function( event ) {
115 if (
116 event.origin === jpConnect.jetpackApiDomain &&
117 event.source === jetpackConnectIframe.get( 0 ).contentWindow &&
118 event.data === 'close'
119 ) {
120 window.removeEventListener( 'message', this.receiveData );
121 jetpackConnectButton.handleAuthorizationComplete();
122 }
123 },
124 handleAuthorizationComplete: function() {
125 jetpackConnectButton.isRegistering = false;
126
127 if ( jetpackConnectButton.isPaidPlan ) {
128 window.location.assign( jpConnect.dashboardUrl );
129 } else {
130 window.location.assign( jpConnect.plansPromptUrl );
131 }
132 window.location.reload( true );
133 },
134 handleConnectionError: function( error ) {
135 jetpackConnectButton.isRegistering = false;
136 jetpackConnectButton.handleOriginalFlow();
137 },
138 };
139 } );
140