1 line
2.6 KiB
JavaScript
1 line
2.6 KiB
JavaScript
jQuery(document).ready((function(n){var e=n(".jp-connect-button, .jp-banner__alt-connect-button").eq(0),t=n(".jp-connect-full__tos-blurb"),a=n("#jetpack-connection-cards, .jp-connect-full__dismiss-paragraph, .jp-connect-full__testimonial"),o="";e.on("click",(function(e){if(e.preventDefault(),"undefined"==typeof URLSearchParams)o="";else{var t=new URLSearchParams(n(this).prop("search"));o=t&&t.get("from")}a.length&&a.fadeOut(600),i.startConnectionFlow()}));var i={isRegistering:!1,isPaidPlan:!1,startConnectionFlow:function(){var e=n("#jetpack-connection-cards, .jp-connect-full__testimonial");e.length&&e.fadeOut(600),i.isRegistering||i.handleConnection()},startAuthorizationFlow:function(n){n.alternateAuthorizeUrl?window.location=n.alternateAuthorizeUrl:window.location=n.authorizeUrl},handleConnection:function(){if(e.hasClass("jp-banner__alt-connect-button")){var a=o&&"&from="+o||"";window.location=jpConnect.connectInPlaceUrl+a}else{i.isRegistering=!0,t.hide(),e.hide(),i.triggerLoadingState();var r=jpConnect.apiBaseUrl+"/connection/register";window.Initial_State&&window.Initial_State.calypsoEnv&&(r=r+"?"+n.param({calypso_env:window.Initial_State.calypsoEnv})),n.ajax({url:r,type:"POST",data:{registration_nonce:jpConnect.registrationNonce,_wpnonce:jpConnect.apiNonce,from:o},error:i.handleConnectionError,success:i.startAuthorizationFlow})}},triggerLoadingState:function(){var e=n("<span>").addClass("jp-connect-full__button-container-loading").text(jpConnect.buttonTextRegistering).appendTo(".jp-connect-full__button-container"),t=n("<div>").addClass("jp-spinner"),a=n("<div>").addClass("jp-spinner__outer").appendTo(t);n("<div>").addClass("jp-spinner__inner").appendTo(a),e.after(t)},fetchPlanType:function(){return n.ajax({url:jpConnect.apiBaseUrl+"/site",type:"GET",data:{_wpnonce:jpConnect.apiSiteDataNonce},success:function(n){var e=JSON.parse(n.data);i.isPaidPlan=e.options.is_pending_plan||!e.plan.is_free}})},receiveData:function(n){if(n.origin===jpConnect.jetpackApiDomain)switch(n.data){case"close":window.removeEventListener("message",this.receiveData),i.handleAuthorizationComplete();break;case"wpcom_nocookie":i.handleConnectionError()}},handleAuthorizationComplete:function(){i.isRegistering=!1,i.fetchPlanType().always((function(){if(i.isPaidPlan){var n=document.createElement("a");n.href=jpConnect.dashboardUrl;var e=window.location.pathname===n.pathname&&window.location.hash.length&&n.hash.length;window.location.assign(jpConnect.dashboardUrl),e&&window.location.reload(!0)}else window.location.assign(jpConnect.plansPromptUrl)}))},handleConnectionError:function(n){i.isRegistering=!1,window.location=e.attr("href")}};o=location.hash.split("&from=")[1]})); |