PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.1
Jetpack – WP Security, Backup, Speed, & Growth v8.1
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 8.1, at _inc/connect-button.js

181 lines 5.8 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, .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 // At this point we are pretty sure if things work out that we will be loading the admin script
126 var link = document.createElement( 'link' );
127 link.rel = 'preload';
128 link.as = 'script';
129 link.href = jpConnect.preFetchScript;
130 document.head.appendChild( link );
131 },
132 fetchPlanType: function() {
133 $.ajax( {
134 url: jpConnect.apiBaseUrl + '/site',
135 type: 'GET',
136 data: {
137 _wpnonce: jpConnect.apiSiteDataNonce,
138 },
139 success: function( data ) {
140 var siteData = JSON.parse( data.data );
141 jetpackConnectButton.isPaidPlan = ! siteData.plan.is_free;
142 },
143 } );
144 },
145 receiveData: function( event ) {
146 if (
147 event.origin === jpConnect.jetpackApiDomain &&
148 event.source === jetpackConnectIframe.get( 0 ).contentWindow &&
149 event.data === 'close'
150 ) {
151 window.removeEventListener( 'message', this.receiveData );
152 jetpackConnectButton.handleAuthorizationComplete();
153 }
154 },
155 handleAuthorizationComplete: function() {
156 jetpackConnectButton.isRegistering = false;
157
158 if ( jetpackConnectButton.isPaidPlan ) {
159 window.location.assign( jpConnect.dashboardUrl );
160 } else {
161 window.location.assign( jpConnect.plansPromptUrl );
162 }
163 window.location.reload( true );
164 },
165 handleConnectionError: function( error ) {
166 jetpackConnectButton.isRegistering = false;
167 jetpackConnectButton.handleOriginalFlow();
168 },
169 };
170
171 // When we visit /wp-admin/admin.php?page=jetpack#/setup, immediately start the connection flow.
172 var hash = location.hash.replace( /#\//, '' );
173 if ( 'setup' === hash ) {
174 if ( connectionHelpSections.length ) {
175 connectionHelpSections.hide();
176 }
177
178 jetpackConnectButton.selectAndStartConnectionFlow();
179 }
180 } );
181