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