PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.1
Jetpack – WP Security, Backup, Speed, & Growth v11.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
160 lines 5.0 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-connect-button--alt' ).eq( 0 );
5 var tosText = $( '.jp-connect-full__tos-blurb' );
6 // Sections that only show up in the first Set Up screen
7 var connectionHelpSections = $(
8 '#jetpack-connection-cards, .jp-connect-full__dismiss-paragraph, .jp-connect-full__testimonial'
9 );
10 // Sections that only show up in the "Authorize user" screen
11 var connectButtonFrom = '';
12
13 connectButton.on( 'click', function ( event ) {
14 event.preventDefault();
15
16 if ( 'undefined' === typeof URLSearchParams ) {
17 connectButtonFrom = '';
18 } else {
19 var searchParams = new URLSearchParams( $( this ).prop( 'search' ) );
20 connectButtonFrom = searchParams && searchParams.get( 'from' );
21 }
22
23 if ( connectionHelpSections.length ) {
24 connectionHelpSections.fadeOut( 600 );
25 }
26
27 jetpackConnectButton.startConnectionFlow();
28 } );
29
30 var jetpackConnectButton = {
31 isRegistering: false,
32 isPaidPlan: false,
33 startConnectionFlow: function () {
34 var connectionHelpSections = $( '#jetpack-connection-cards, .jp-connect-full__testimonial' );
35 if ( connectionHelpSections.length ) {
36 connectionHelpSections.fadeOut( 600 );
37 }
38
39 if ( ! jetpackConnectButton.isRegistering ) {
40 jetpackConnectButton.handleConnection();
41 }
42 },
43 startAuthorizationFlow: function ( data ) {
44 if ( data.alternateAuthorizeUrl ) {
45 window.location = data.alternateAuthorizeUrl;
46 } else {
47 window.location = data.authorizeUrl;
48 }
49 },
50 handleConnection: function () {
51 // Alternative connection buttons should redirect to the main one for the "connect in place" flow.
52 if ( connectButton.attr( 'id' ) === 'jp-connect-button--alt' ) {
53 // Make sure we don't lose the `from` parameter, if set.
54 var fromParam = ( connectButtonFrom && '&from=' + connectButtonFrom ) || '';
55 window.location = jpConnect.connectInPlaceUrl + fromParam;
56 return;
57 }
58
59 jetpackConnectButton.isRegistering = true;
60 tosText.hide();
61 connectButton.hide();
62 jetpackConnectButton.triggerLoadingState();
63
64 var registerUrl = jpConnect.apiBaseUrl + '/connection/register';
65
66 // detect Calypso Env and add to API URL
67 if ( window.Initial_State && window.Initial_State.calypsoEnv ) {
68 registerUrl =
69 registerUrl + '?' + $.param( { calypso_env: window.Initial_State.calypsoEnv } );
70 }
71
72 $.ajax( {
73 url: registerUrl,
74 type: 'POST',
75 data: {
76 registration_nonce: jpConnect.registrationNonce,
77 _wpnonce: jpConnect.apiNonce,
78 from: connectButtonFrom,
79 },
80 error: jetpackConnectButton.handleConnectionError,
81 success: jetpackConnectButton.startAuthorizationFlow,
82 } );
83 },
84 triggerLoadingState: function () {
85 var loadingText = $( '<span>' )
86 .addClass( 'jp-connect-full__button-container-loading' )
87 .text( jpConnect.buttonTextRegistering )
88 .appendTo( '.jp-connect-full__button-container' );
89
90 var spinner = $( '<div>' ).addClass( 'jp-spinner' );
91 var spinnerOuter = $( '<div>' ).addClass( 'jp-spinner__outer' ).appendTo( spinner );
92 $( '<div>' ).addClass( 'jp-spinner__inner' ).appendTo( spinnerOuter );
93 loadingText.after( spinner );
94 },
95 fetchPlanType: function () {
96 return $.ajax( {
97 url: jpConnect.apiBaseUrl + '/site',
98 type: 'GET',
99 data: {
100 _wpnonce: jpConnect.apiSiteDataNonce,
101 },
102 success: function ( data ) {
103 var siteData = JSON.parse( data.data );
104 jetpackConnectButton.isPaidPlan =
105 siteData.options.is_pending_plan || ! siteData.plan.is_free;
106 },
107 } );
108 },
109 receiveData: function ( event ) {
110 if ( event.origin !== jpConnect.jetpackApiDomain ) {
111 return;
112 }
113
114 switch ( event.data ) {
115 case 'close':
116 window.removeEventListener( 'message', this.receiveData );
117 jetpackConnectButton.handleAuthorizationComplete();
118 break;
119 case 'wpcom_nocookie':
120 jetpackConnectButton.handleConnectionError();
121 break;
122 }
123 },
124 handleAuthorizationComplete: function () {
125 jetpackConnectButton.isRegistering = false;
126
127 // Fetch plan type late to make sure any stored license keys have been
128 // attached to the site during the connection.
129 jetpackConnectButton.fetchPlanType().always( function () {
130 if ( ! jetpackConnectButton.isPaidPlan ) {
131 window.location.assign( jpConnect.plansPromptUrl );
132 return;
133 }
134
135 var parser = document.createElement( 'a' );
136 parser.href = jpConnect.dashboardUrl;
137 var reload =
138 window.location.pathname === parser.pathname &&
139 window.location.hash.length &&
140 parser.hash.length;
141
142 window.location.assign( jpConnect.dashboardUrl );
143
144 if ( reload ) {
145 // The Jetpack admin page has hashes in the URLs, so we need to reload the page after .assign()
146 window.location.reload( true );
147 }
148 } );
149 },
150 handleConnectionError: function ( error ) {
151 jetpackConnectButton.isRegistering = false;
152 // If something goes wrong, we take users to Calypso.
153 window.location = connectButton.attr( 'href' );
154 },
155 };
156
157 // In case the parameter has been manually set in the URL after redirect.
158 connectButtonFrom = location.hash.split( '&from=' )[ 1 ];
159 } );
160