Skip to content

Commit

Permalink
[Boost] Fix getting started loop (#31648)
Browse files Browse the repository at this point in the history
* lint

* changelog

* remove boost main class from lint excludes

* Move the connection first-time special case logic out to the main jetpack boost class

---------

Co-authored-by: Mark George <thingalon@gmail.com>
  • Loading branch information
thingalon and Mark George committed Jun 30, 2023
1 parent e83c0fd commit 9ad06c9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<script lang="ts">
import { onMount } from 'svelte';
import { derived, get, writable } from 'svelte/store';
import { derived, writable } from 'svelte/store';
import { Snackbar } from '@wordpress/components';
import ActivateLicense from '../../elements/ActivateLicense.svelte';
import ReactComponent from '../../elements/ReactComponent.svelte';
import { BoostPricingTable } from '../../react-components/BoostPricingTable';
import Header from '../../sections/Header.svelte';
import config, { markGetStartedComplete } from '../../stores/config';
import { connection } from '../../stores/connection';
import { modulesState, modulesStatePending } from '../../stores/modules';
import { recordBoostEvent } from '../../utils/analytics';
import { getUpgradeURL } from '../../utils/upgrade';
Expand All @@ -32,83 +31,77 @@
}
);
const ensureConnection = async () => {
let connectionStore;
connection.subscribe( value => {
connectionStore = value;
} );
if ( connectionStore.connected ) {
recordBoostEvent( 'using_existing_connection', {} );
return;
}
await connection.initialize();
if ( ! connectionStore.connected ) {
throw connectionStore.error;
/**
* Mark that getting started is completed, and head to the next page.
*
* @param {string} externalPage If specified, head to the external page instead of staying on the dashboard.
*/
function finishGettingStarted( externalPage?: string ) {
markGetStartedComplete();
if ( externalPage ) {
window.location.href = externalPage;
} else {
navigate( '/', { replace: true } );
}
}
recordBoostEvent( 'established_connection', {} );
};
const chooseFreePlan = async () => {
/**
* User clicked "Free plan"
*/
async function chooseFreePlan() {
initiatingFreePlan = true;
try {
await ensureConnection();
// Allow opening the boost settings page. The actual flag is changed in the backend by enabling the critical-css module below.
markGetStartedComplete();
await recordBoostEvent( 'free_cta_from_getting_started_page_in_plugin', {} );
// Wait for the module to become active.
// Otherwise Critical CSS Generation will fail to start.
$modulesState.critical_css.active = true;
const unsubscribe = modulesStatePending.subscribe( isPending => {
if ( isPending === false ) {
unsubscribe();
navigate( '/' );
}
} );
// Make sure there is a Jetpack connection and record this selection.
await Promise.all( [
connection.initialize(),
recordBoostEvent( 'free_cta_from_getting_started_page_in_plugin', {} ),
] );
// Head to the settings page.
finishGettingStarted();
} catch ( e ) {
// Un-dismiss snackbar on error. Actual error comes from connection object.
dismissedSnackbar.set( false );
} finally {
initiatingFreePlan = false;
}
};
}
const choosePaidPlan = async () => {
/**
* User clicked Premium.
*/
async function choosePaidPlan() {
initiatingPaidPlan = true;
try {
await ensureConnection();
// Make sure there is a Jetpack connection and record this selection.
await Promise.all( [
connection.initialize(),
recordBoostEvent( 'premium_cta_from_getting_started_page_in_plugin', {} ),
] );
// Check if the site is already on a premium plan and go directly to settings if so.
if ( get( config ).isPremium ) {
// Allow opening the boost settings page.
markGetStartedComplete();
await recordBoostEvent( 'premium_cta_from_getting_started_page_in_plugin', {} );
navigate( '/', { replace: true } );
if ( $config.isPremium ) {
finishGettingStarted();
return;
}
window.location.href = getUpgradeURL();
// Go to the purchase flow.
finishGettingStarted( getUpgradeURL() );
} catch ( e ) {
// Un-dismiss snackbar on error. Actual error comes from connection object.
dismissedSnackbar.set( false );
} finally {
initiatingPaidPlan = false;
}
};
}
onMount( () => {
// If we don't have pricing data, we should skip the page and go directly to settings.
if ( typeof pricing.yearly === 'undefined' ) {
// Allow opening the boost settings page.
markGetStartedComplete();
navigate( '/', { replace: true } );
finishGettingStarted();
}
} );
</script>
Expand Down
32 changes: 27 additions & 5 deletions projects/plugins/boost/app/class-jetpack-boost.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_Storage;
use Automattic\Jetpack_Boost\Lib\Setup;
use Automattic\Jetpack_Boost\Lib\Site_Health;
use Automattic\Jetpack_Boost\Lib\Status;
use Automattic\Jetpack_Boost\Modules\Modules_Setup;
use Automattic\Jetpack_Boost\REST_API\Endpoints\Config_State;
use Automattic\Jetpack_Boost\REST_API\Endpoints\List_Site_Urls;
Expand Down Expand Up @@ -107,6 +108,7 @@ public function __construct() {
add_action( 'init', array( $this, 'init_textdomain' ) );

add_action( 'handle_environment_change', array( $this, 'handle_environment_change' ), 10, 2 );
add_action( 'jetpack_boost_connection_established', array( $this, 'handle_jetpack_connection' ) );

// Fired when plugin ready.
do_action( 'jetpack_boost_loaded', $this );
Expand Down Expand Up @@ -139,6 +141,19 @@ public static function activate() {
Analytics::record_user_event( 'activate_plugin' );
}

/**
* Plugin connected to Jetpack handler.
*/
public function handle_jetpack_connection() {
if ( Config::is_getting_started() ) {
// Special case: when getting started, ensure that the Critical CSS module is enabled.
$status = new Status( 'critical_css' );
$status->update( true );
}

Config::clear_getting_started();
}

/**
* Plugin deactivation handler. Clear cache, and reset admin notices.
*/
Expand Down Expand Up @@ -213,16 +228,23 @@ public function uninstall() {
$this->deactivate();

// Delete all Jetpack Boost options.
$wpdb->query(
//phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$option_names = $wpdb->get_col(
"
SELECT `option_name`
FROM `$wpdb->options`
WHERE `option_name` LIKE 'jetpack_boost_%';
"
DELETE
FROM `$wpdb->options`
WHERE `option_name` LIKE 'jetpack_boost_%'
"
);
//phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching

foreach ( $option_names as $option_name ) {
delete_option( $option_name );
}

// Delete stored Critical CSS.
( new Critical_CSS_Storage() )->clear();

// Delete all transients created by boost.
Transient::delete_by_prefix( '' );

Expand Down
8 changes: 4 additions & 4 deletions projects/plugins/boost/app/lib/class-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,14 @@ public function is_connected() {
*/
public function register() {
if ( $this->is_connected() ) {
Analytics::record_user_event( 'connect_site' );

Analytics::record_user_event( 'using_existing_connection' );
return true;
}

$result = $this->manager->register();

if ( ! is_wp_error( $result ) ) {
Analytics::record_user_event( 'connect_site' );

Analytics::record_user_event( 'established_connection' );
Premium_Features::clear_cache();
}

Expand Down Expand Up @@ -204,6 +202,8 @@ public function create_connection_endpoint( \WP_REST_Request $request ) { // php
return $response;
}

do_action( 'jetpack_boost_connection_established' );

return rest_ensure_response( $this->get_connection_api_response() );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fix a potential 'loop' getting stuck in Getting started state.
1 change: 0 additions & 1 deletion tools/phpcs-excludelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"projects/packages/sync/src/replicastore/class-table-checksum-users.php",
"projects/packages/sync/src/replicastore/class-table-checksum.php",
"projects/plugins/beta/src/class-utils.php",
"projects/plugins/boost/app/class-jetpack-boost.php",
"projects/plugins/crm/ZeroBSCRM.php",
"projects/plugins/crm/admin/activation/before-you-go.php",
"projects/plugins/crm/admin/activation/welcome-to-jpcrm.php",
Expand Down

0 comments on commit 9ad06c9

Please sign in to comment.