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

198 lines 6.6 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 // Sections that only show up in the first Set Up screen
8 var connectionHelpSections = $(
9 '#jetpack-connection-cards, .jp-connect-full__dismiss-paragraph, .jp-connect-full__testimonial'
10 );
11 // Sections that only show up in the "Authorize user" screen
12 var authenticationHelpSections = $( '#jp-authenticate-no_user_test_mode' );
13 var connectButtonFrom = '';
14
15 authenticationHelpSections.hide();
16
17 connectButton.on( 'click', function ( event ) {
18 event.preventDefault();
19
20 if ( 'undefined' === typeof URLSearchParams ) {
21 connectButtonFrom = '';
22 } else {
23 var searchParams = new URLSearchParams( $( this ).prop( 'search' ) );
24 connectButtonFrom = searchParams && searchParams.get( 'from' );
25 }
26
27 if ( connectionHelpSections.length ) {
28 connectionHelpSections.fadeOut( 600 );
29 }
30
31 jetpackConnectButton.selectAndStartConnectionFlow();
32 } );
33
34 var jetpackConnectButton = {
35 isRegistering: false,
36 isPaidPlan: false,
37 selectAndStartConnectionFlow: function () {
38 var connectionHelpSections = $( '#jetpack-connection-cards, .jp-connect-full__testimonial' );
39 if ( connectionHelpSections.length ) {
40 connectionHelpSections.fadeOut( 600 );
41 }
42
43 if ( ! jetpackConnectButton.isRegistering ) {
44 if ( 'original' === jpConnect.forceVariation ) {
45 // Forcing original connection flow, `JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME = true`
46 // or we're dealing with Safari which has issues with handling 3rd party cookies.
47 jetpackConnectButton.handleOriginalFlow();
48 } else {
49 // Default in-place connection flow.
50 jetpackConnectButton.handleConnectInPlaceFlow();
51 }
52 }
53 },
54 handleOriginalFlow: function () {
55 window.location = connectButton.attr( 'href' );
56 },
57 handleConnectInPlaceFlow: function () {
58 // Alternative connection buttons should redirect to the main one for the "connect in place" flow.
59 if ( connectButton.hasClass( 'jp-banner__alt-connect-button' ) ) {
60 // Make sure we don't lose the `from` parameter, if set.
61 var fromParam = ( connectButtonFrom && '&from=' + connectButtonFrom ) || '';
62 window.location = jpConnect.connectInPlaceUrl + fromParam;
63 return;
64 }
65
66 jetpackConnectButton.isRegistering = true;
67 tosText.hide();
68 connectButton.hide();
69 jetpackConnectButton.triggerLoadingState();
70
71 var registerUrl = jpConnect.apiBaseUrl + '/connection/register';
72
73 // detect Calypso Env and add to API URL
74 if ( window.Initial_State && window.Initial_State.calypsoEnv ) {
75 registerUrl =
76 registerUrl + '?' + $.param( { calypso_env: window.Initial_State.calypsoEnv } );
77 }
78
79 $.ajax( {
80 url: registerUrl,
81 type: 'POST',
82 data: {
83 registration_nonce: jpConnect.registrationNonce,
84 _wpnonce: jpConnect.apiNonce,
85 },
86 error: jetpackConnectButton.handleConnectionError,
87 success: jetpackConnectButton.handleConnectionSuccess,
88 } );
89 },
90 triggerLoadingState: function () {
91 var loadingText = $( '<span>' )
92 .addClass( 'jp-connect-full__button-container-loading' )
93 .text( jpConnect.buttonTextRegistering )
94 .appendTo( '.jp-connect-full__button-container' );
95
96 var spinner = $( '<div>' ).addClass( 'jp-spinner' );
97 var spinnerOuter = $( '<div>' ).addClass( 'jp-spinner__outer' ).appendTo( spinner );
98 $( '<div>' ).addClass( 'jp-spinner__inner' ).appendTo( spinnerOuter );
99 loadingText.after( spinner );
100 },
101 handleConnectionSuccess: function ( data ) {
102 window.addEventListener( 'message', jetpackConnectButton.receiveData );
103 jetpackConnectIframe.attr( 'src', data.authorizeUrl + '&from=' + connectButtonFrom );
104 jetpackConnectIframe.on( 'load', function () {
105 jetpackConnectIframe.show();
106 $( '.jp-connect-full__button-container' ).hide();
107 authenticationHelpSections.show();
108 } );
109 jetpackConnectIframe.hide();
110 $( '.jp-connect-full__button-container' ).after( jetpackConnectIframe );
111
112 // At this point we are pretty sure if things work out that we will be loading the admin script
113 var link = document.createElement( 'link' );
114 link.rel = 'preload';
115 link.as = 'script';
116 link.href = jpConnect.preFetchScript;
117 document.head.appendChild( link );
118 },
119 fetchPlanType: function () {
120 return $.ajax( {
121 url: jpConnect.apiBaseUrl + '/site',
122 type: 'GET',
123 data: {
124 _wpnonce: jpConnect.apiSiteDataNonce,
125 },
126 success: function ( data ) {
127 var siteData = JSON.parse( data.data );
128 jetpackConnectButton.isPaidPlan =
129 siteData.options.is_pending_plan || ! siteData.plan.is_free;
130 },
131 } );
132 },
133 receiveData: function ( event ) {
134 if (
135 event.origin !== jpConnect.jetpackApiDomain ||
136 event.source !== jetpackConnectIframe.get( 0 ).contentWindow
137 ) {
138 return;
139 }
140
141 switch ( event.data ) {
142 case 'close':
143 window.removeEventListener( 'message', this.receiveData );
144 jetpackConnectButton.handleAuthorizationComplete();
145 break;
146 case 'wpcom_nocookie':
147 jetpackConnectIframe.hide();
148 jetpackConnectButton.handleConnectionError();
149 break;
150 }
151 },
152 handleAuthorizationComplete: function () {
153 jetpackConnectButton.isRegistering = false;
154
155 // Fetch plan type late to make sure any stored license keys have been
156 // attached to the site during the connection.
157 jetpackConnectButton.fetchPlanType().always( function () {
158 if ( ! jetpackConnectButton.isPaidPlan ) {
159 window.location.assign( jpConnect.plansPromptUrl );
160 return;
161 }
162
163 var parser = document.createElement( 'a' );
164 parser.href = jpConnect.dashboardUrl;
165 var reload =
166 window.location.pathname === parser.pathname &&
167 window.location.hash.length &&
168 parser.hash.length;
169
170 window.location.assign( jpConnect.dashboardUrl );
171
172 if ( reload ) {
173 // The Jetpack admin page has hashes in the URLs, so we need to reload the page after .assign()
174 window.location.reload( true );
175 }
176 } );
177 },
178 handleConnectionError: function ( error ) {
179 jetpackConnectButton.isRegistering = false;
180 jetpackConnectButton.handleOriginalFlow();
181 },
182 };
183
184 // When we visit /wp-admin/admin.php?page=jetpack#/setup, immediately start the connection flow.
185 var hash = location.hash.replace( /(#\/setup).*/, 'setup' );
186
187 // In case the parameter has been manually set in the URL after redirect.
188 connectButtonFrom = location.hash.split( '&from=' )[ 1 ];
189
190 if ( 'setup' === hash ) {
191 if ( connectionHelpSections.length ) {
192 connectionHelpSections.hide();
193 }
194
195 jetpackConnectButton.selectAndStartConnectionFlow();
196 }
197 } );
198