Нет описания

connect-button.js 6.5KB

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