Skip to content

Commit

Permalink
Add site lifecycle status guess to My Jetpack (#35815)
Browse files Browse the repository at this point in the history
* Add all stats needed to perform user type check

* Add selector combining and guessing

* changelog

* Refactor naming

* Update changelog message

* Improved conditions and added commentary

* Improve conditions

* Filter out default features

* Remove features stats

* Improve logic commentary

* Update version
  • Loading branch information
robertsreberski committed Feb 27, 2024
1 parent a90eb2f commit ed3222a
Show file tree
Hide file tree
Showing 24 changed files with 175 additions and 21 deletions.
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 @@ -274,6 +274,13 @@ const welcomeBanner = ( state = {}, action ) => {
}
};

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

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

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

const getLifecycleStats = state => {
return state.lifecycleStats;
};

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

// 'new' = no purchases + less than 3 modules
if ( purchases.length === 0 && modules.length < 3 ) {
// 'brand-new' = 'new' + (no user or site connection + no modules + only one plugin)
if (
( ! isUserConnected || ! isSiteConnected ) &&
modules.length === 0 &&
plugins.length === 1
) {
return 'brand-new';
}

return 'new';
}

// 'settling-in' = 1 purchase and less than 10 modules
if ( purchases.length === 1 && modules.length < 10 ) {
return 'settling-in';
}

// 'established' = 2 or more purchases or 10 or more modules
return 'established';
};

const isFetchingStatsCounts = state => {
return state.statsCounts?.isFetching || false;
};
Expand All @@ -163,6 +194,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
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"link-template": "https://github.com/Automattic/jetpack-my-jetpack/compare/${old}...${new}"
},
"branch-alias": {
"dev-trunk": "4.12.x-dev"
"dev-trunk": "4.13.x-dev"
},
"version-constants": {
"::PACKAGE_VERSION": "src/class-initializer.php"
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-my-jetpack",
"version": "4.12.1-alpha",
"version": "4.13.0-alpha",
"description": "WP Admin page with information and configuration shared among all Jetpack stand-alone plugins",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/my-jetpack/#readme",
"bugs": {
Expand Down
67 changes: 66 additions & 1 deletion projects/packages/my-jetpack/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Initializer {
*
* @var string
*/
const PACKAGE_VERSION = '4.12.1-alpha';
const PACKAGE_VERSION = '4.13.0-alpha';

/**
* HTML container ID for the IDC screen on My Jetpack page.
Expand Down Expand Up @@ -189,6 +189,7 @@ public static function enqueue_scripts() {
)
);
$modules = new Modules();
$connection = new Connection_Manager();
$speed_score_history = new Speed_Score_History( wp_parse_url( get_site_url(), PHP_URL_HOST ) );
wp_localize_script(
'my_jetpack_main_app',
Expand All @@ -214,6 +215,13 @@ public static function enqueue_scripts() {
'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' => $connection->is_connected(),
'isUserConnected' => $connection->is_user_connected(),
'purchases' => self::get_purchases(),
'modules' => self::get_active_modules(),
),
'isStatsModuleActive' => $modules->is_active( 'stats' ),
'isUserFromKnownHost' => self::is_user_from_known_host(),
'isCommercial' => self::is_commercial_site(),
Expand Down Expand Up @@ -246,6 +254,63 @@ public static function enqueue_scripts() {
}
}

/**
* 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;
}

/**
* Determine if the current user is "new" to Jetpack
* This is used to vary some messaging in My Jetpack
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/backup/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/boost/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/jetpack/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/migration/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/protect/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/search/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/social/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/starter-plugin/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


Loading

0 comments on commit ed3222a

Please sign in to comment.