Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add site lifecycle status guess to My Jetpack #35815

Merged
merged 14 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions projects/packages/my-jetpack/_inc/state/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ const welcomeBanner = ( state = {}, action ) => {
}
};

const lifecycleStats = ( state = {}, action ) => {
switch ( action.type ) {
default:
return state;
}
};

const reducers = combineReducers( {
products,
backupRewindableEvents,
Expand All @@ -308,6 +315,7 @@ const reducers = combineReducers( {
stats,
statsCounts,
welcomeBanner,
lifecycleStats,
} );

export default reducers;
24 changes: 24 additions & 0 deletions projects/packages/my-jetpack/_inc/state/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ const getStatsCounts = state => {
return state.statsCounts?.data;
};

const getUserStats = state => {
return state.userStats;
};
robertsreberski marked this conversation as resolved.
Show resolved Hide resolved

const getGuessedSiteLifecycleStatus = state => {
const { modules, purchases, features, plugins, isSiteConnected, isUserConnected } =
getUserStats( state );

if ( purchases.length === 0 ) {
robertsreberski marked this conversation as resolved.
Show resolved Hide resolved
if ( ! isUserConnected || ! isSiteConnected || modules.length === 0 || plugins.length === 1 ) {
return 'brand-new';
}

return 'new';
}

if ( purchases.length === 1 || features.length < 10 ) {
return 'settling-in';
}

return 'established';
};

const isFetchingStatsCounts = state => {
return state.statsCounts?.isFetching || false;
};
Expand All @@ -169,6 +192,7 @@ const selectors = {
...countBackupItemsSelectors,
...statsCountsSelectors,
getWelcomeBannerHasBeenDismissed,
getGuessedSiteLifecycleStatus,
};

export default selectors;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add site lifecycle status guess to My Jetpack
99 changes: 99 additions & 0 deletions projects/packages/my-jetpack/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Automattic\Jetpack\Terms_Of_Service;
use Automattic\Jetpack\Tracking;
use Jetpack;
use Jetpack_Core_API_Site_Endpoint;

/**
* The main Initializer class that registers the admin menu and eneuque the assets.
Expand All @@ -35,7 +36,7 @@
*
* @var string
*/
const PACKAGE_VERSION = '4.10.1-alpha';

Check failure on line 39 in projects/packages/my-jetpack/src/class-initializer.php

View workflow job for this annotation

GitHub Actions / Changelogger validity

Version mismatch, expected 4.11.0-alpha but found 4.10.1-alpha! You might use `tools/project-version.sh -f -u 4.11.0-alpha packages/my-jetpack` or `tools/fixup-project-versions.sh` to fix this.

/**
* HTML container ID for the IDC screen on My Jetpack page.
Expand Down Expand Up @@ -214,6 +215,14 @@
'IDCContainerID' => static::get_idc_container_id(),
'userIsAdmin' => current_user_can( 'manage_options' ),
'userIsNewToJetpack' => self::is_jetpack_user_new(),
'lifecycleStats' => array(
'jetpackPlugins' => self::get_installed_jetpack_plugins(),
'isSiteConnected' => self::is_site_connected(),
'isUserConnected' => self::is_user_connected(),
'purchases' => self::get_purchases(),
'modules' => self::get_active_modules(),
'features' => self::get_active_features(),
),
'isStatsModuleActive' => $modules->is_active( 'stats' ),
'isUserFromKnownHost' => self::is_user_from_known_host(),
'isCommercial' => self::is_commercial_site(),
Expand Down Expand Up @@ -246,6 +255,96 @@
}
}

/**
* Get product slugs of the active purchases
*
* @return array
*/
public static function get_purchases() {
$purchases = Wpcom_Products::get_site_current_purchases();
if ( is_wp_error( $purchases ) ) {
return array();
}

return array_map(
function ( $purchase ) {
return $purchase->product_slug;
},
$purchases
);
}

/**
* Get installed Jetpack plugins
*
* @return array
*/
public static function get_installed_jetpack_plugins() {
$plugin_slugs = array_keys( Plugins_Installer::get_plugins() );
$plugin_slugs = array_map(
static function ( $slug ) {
$parts = explode( '/', $slug );
if ( empty( $parts ) ) {
return '';
}
// Return the last segment of the filepath without the PHP extension
return str_replace( '.php', '', $parts[ count( $parts ) - 1 ] );
},
$plugin_slugs
);

return array_values( array_intersect( self::JETPACK_PLUGIN_SLUGS, $plugin_slugs ) );
}

/**
* Get active modules (except ones enabled by default)
*
* @return array
*/
public static function get_active_modules() {
$modules = new Modules();
$active_modules = $modules->get_active();

// if the Jetpack plugin is active, filter out the modules that are active by default
if ( class_exists( 'Jetpack' ) && ! empty( $active_modules ) ) {
$active_modules = array_diff( $active_modules, Jetpack::get_default_modules() );
}
return $active_modules;
}

/**
* Get features that can be used on the website (through purchases or free features)
*
* @return array
*/
public static function get_active_features() {
require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php';
$payload = Jetpack_Core_API_Site_Endpoint::get_features();
$features_json = json_decode( $payload->data['data'] );

return $features_json->active;
}

/**
* Determine if this Jetpack site is connected to our backend
*
* @return bool
*/
public static function is_site_connected() {
$connection = new Connection_Manager();
return $connection->is_connected();
}

/**
* Determine if this Jetpack site has a user connected
*
* @return bool
*/
public static function is_user_connected() {
$connection = new Connection_Manager();
return $connection->is_user_connected();
}
robertsreberski marked this conversation as resolved.
Show resolved Hide resolved

/**
* Determine if the current user is "new" to Jetpack
* This is used to vary some messaging in My Jetpack
Expand Down
Loading