Skip to content

Commit

Permalink
Update/my jetpack status handling (#37217)
Browse files Browse the repository at this point in the history
* improve the active/ inactive status handling in My Jetpack with better free and plan checks

* changelog

* Don't yet remove extra product classes

* Add better handling for connection error states

* Add one more caveat to first site conneciton status

* Change condition for first site connection stauts

* Fix boost unit test in My Jetpack

* Version madness

* Add some additional checks for products that have overrides of is_active

* Adjustment to hybrid module activation

* Add more handling for when a product has a disabled module and no plan

* more updates for hybrid products

* Switch to explicit check for inactive module

* Additional check for module name

* Version bump

* A few bugfixes

* update language on cards for missing user connection

* Add a required plan check for disabled modules

* version management

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/9197569491

Upstream-Ref: Automattic/jetpack@ad40b62
  • Loading branch information
jboland88 authored and matticbot committed May 22, 2024
1 parent 884b427 commit e56685a
Show file tree
Hide file tree
Showing 22 changed files with 287 additions and 206 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"automattic/jetpack-config": "^2.0.2",
"automattic/jetpack-connection": "^2.8.4",
"automattic/jetpack-identity-crisis": "^0.20.0",
"automattic/jetpack-my-jetpack": "^4.23.3",
"automattic/jetpack-my-jetpack": "^4.24.0-alpha",
"automattic/jetpack-search": "^0.44.7",
"automattic/jetpack-stats": "^0.12.5",
"automattic/jetpack-status": "^3.2.0",
Expand Down
8 changes: 8 additions & 0 deletions jetpack_vendor/automattic/jetpack-my-jetpack/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.24.0-alpha] - unreleased

This is an alpha version! The changes listed here are not final.

### Changed
- Improving the active/ inactive statuses on My Jetpack

## [4.23.3] - 2024-05-20
### Added
- Add tracks events for dropdown on action buttons. [#37292]
Expand Down Expand Up @@ -1478,6 +1485,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Created package

[4.24.0-alpha]: https://github.com/Automattic/jetpack-my-jetpack/compare/4.23.3...4.24.0-alpha
[4.23.3]: https://github.com/Automattic/jetpack-my-jetpack/compare/4.23.2...4.23.3
[4.23.2]: https://github.com/Automattic/jetpack-my-jetpack/compare/4.23.1...4.23.2
[4.23.1]: https://github.com/Automattic/jetpack-my-jetpack/compare/4.23.0...4.23.1
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-compose', 'wp-data', 'wp-date', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-primitives', 'wp-url'), 'version' => '66698283b5da85a5c874');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-compose', 'wp-data', 'wp-date', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-primitives', 'wp-url'), 'version' => 'd4c59e2f53eb89b0c91b');
32 changes: 16 additions & 16 deletions jetpack_vendor/automattic/jetpack-my-jetpack/build/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion jetpack_vendor/automattic/jetpack-my-jetpack/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"link-template": "https://github.com/Automattic/jetpack-my-jetpack/compare/${old}...${new}"
},
"branch-alias": {
"dev-trunk": "4.23.x-dev"
"dev-trunk": "4.24.x-dev"
},
"version-constants": {
"::PACKAGE_VERSION": "src/class-initializer.php"
Expand Down
2 changes: 0 additions & 2 deletions jetpack_vendor/automattic/jetpack-my-jetpack/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ interface Window {
features: string[];
has_paid_plan_for_product: boolean;
features_by_tier: Array< string >;
has_required_plan: boolean;
has_required_tier: Array< string >;
is_bundle: boolean;
is_plugin_active: boolean;
is_upgradable_by_bundle: string[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Initializer {
*
* @var string
*/
const PACKAGE_VERSION = '4.23.3';
const PACKAGE_VERSION = '4.24.0-alpha';

/**
* HTML container ID for the IDC screen on My Jetpack page.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static function get_product_data_schema() {
'status' => array(
'title' => 'The product status',
'type' => 'string',
'enum' => array( 'active', 'inactive', 'plugin_absent', 'needs_purchase', 'needs_purchase_or_free', 'error' ),
'enum' => array( 'active', 'inactive', 'plugin_absent', 'needs_purchase', 'needs_purchase_or_free', 'needs_first_site_connection', 'user_connection_error', 'site_connection_error' ),
),
'class' => array(
'title' => 'The product class handler',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ public static function get_features() {

/**
* Determine if the site has an Akismet plan by checking for an API key
* Note that some Akismet Plans are free - we're just checking for an API key and don't have the perspective of the plan attached to it here
*
* @return bool - whether an API key was found
*/
public static function has_required_plan() {
public static function has_paid_plan_for_product() {
// Check if the site has an API key for Akismet
$akismet_api_key = apply_filters( 'akismet_get_api_key', defined( 'WPCOM_API_KEY' ) ? constant( 'WPCOM_API_KEY' ) : get_option( 'wordpress_api_key' ) );
$fallback = ! empty( $akismet_api_key );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class Backup extends Hybrid_Product {
*/
public static $has_free_offering = false;

/**
* Whether this product requires a plan to work at all
*
* @var bool
*/
public static $requires_plan = true;

/**
* Get the product name
*
Expand Down Expand Up @@ -190,7 +197,7 @@ private static function get_state_from_wpcom() {
*
* @return boolean
*/
public static function has_required_plan() {
public static function has_paid_plan_for_product() {
$rewind_data = static::get_state_from_wpcom();
if ( is_wp_error( $rewind_data ) ) {
return false;
Expand Down Expand Up @@ -231,13 +238,4 @@ public static function get_manage_url() {
return Redirect::get_url( 'my-jetpack-manage-backup' );
}
}

/**
* Checks whether the Product is active
*
* @return boolean
*/
public static function is_active() {
return parent::is_active() && static::has_required_plan();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public static function get_wpcom_monthly_product_slug() {
*
* @return boolean
*/
public static function has_required_plan() {
public static function has_paid_plan_for_product() {
$purchases_data = Wpcom_Products::get_site_current_purchases();
if ( is_wp_error( $purchases_data ) ) {
return false;
Expand All @@ -345,7 +345,6 @@ public static function has_required_plan() {
* @return boolean
*/
public static function is_upgradable() {
$has_required_plan = self::has_required_plan();
return ! $has_required_plan;
return ! self::has_paid_plan_for_product();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@
* Hybrid products are those that may work both as a stand-alone plugin or with the Jetpack plugin.
*/
abstract class Hybrid_Product extends Product {

/**
* The Jetpack module name, if any.
*
* @var ?string
*/
public static $module_name = null;

/**
* All hybrid products have a standalone plugin
*
Expand Down Expand Up @@ -59,18 +51,6 @@ public static function is_standalone_plugin_active() {
return parent::is_plugin_active();
}

/**
* Checks whether the Jetpack module is active only if a module_name is defined
*
* @return bool
*/
public static function is_module_active() {
if ( static::$module_name ) {
return ( new Modules() )->is_active( static::$module_name );
}
return true;
}

/**
* Checks whether the Product is active
*
Expand Down Expand Up @@ -125,9 +105,13 @@ public static function do_product_specific_activation( $product_activation ) {
}
}

// Only activate the module if the plan supports it
// We don't want to throw an error for a missing plan here since we try activation before purchase
if ( static::has_required_plan() && static::$module_name ) {
if ( ! empty( static::$module_name ) ) {
// Only activate the module if the plan supports it
// We don't want to throw an error for a missing plan here since we try activation before purchase
if ( static::$requires_plan && ! static::has_any_plan_for_product() ) {
return true;
}

$module_activation = ( new Modules() )->activate( static::$module_name, false, false );

if ( ! $module_activation ) {
Expand Down Expand Up @@ -156,7 +140,7 @@ public static function install_and_activate_standalone() {
* Activate the module as well, if the user has a plan
* or the product does not require a plan to work
*/
if ( static::has_required_plan() && static::$module_name ) {
if ( static::has_any_plan_for_product() && isset( static::$module_name ) ) {
$module_activation = ( new Modules() )->activate( static::$module_name, false, false );

if ( ! $module_activation ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function get_tiers() {
*/
public static function get_features_by_tier() {
$current_tier = self::get_current_usage_tier();
$current_description = $current_tier === 0
$current_description = 0 === $current_tier
? __( 'Up to 20 requests', 'jetpack-my-jetpack' )
/* translators: number of requests */
: sprintf( __( 'Up to %d requests per month', 'jetpack-my-jetpack' ), $current_tier );
Expand Down Expand Up @@ -179,7 +179,7 @@ public static function get_current_usage_tier() {

// Bail early if it's not possible to fetch the feature data.
if ( is_wp_error( $info ) ) {
return null;
return 0;
}

$current_tier = isset( $info['current-tier']['value'] ) ? $info['current-tier']['value'] : null;
Expand All @@ -193,7 +193,7 @@ public static function get_current_usage_tier() {
* @return int
*/
public static function get_next_usage_tier() {
if ( ! self::is_site_connected() || ! self::has_required_plan() ) {
if ( ! self::is_site_connected() || ! self::has_paid_plan_for_product() ) {
return 100;
}

Expand Down Expand Up @@ -419,12 +419,23 @@ public static function get_wpcom_bi_yearly_product_slug() {
}

/**
* Checks whether the current plan (or purchases) of the site already supports the product
* Checks whether the site has a paid plan for this product
*
* @return boolean
*/
public static function has_required_plan() {
return static::does_site_have_feature( 'ai-assistant' );
public static function has_paid_plan_for_product() {
$purchases_data = Wpcom_Products::get_site_current_purchases();
if ( is_wp_error( $purchases_data ) ) {
return false;
}
if ( is_array( $purchases_data ) && ! empty( $purchases_data ) ) {
foreach ( $purchases_data as $purchase ) {
if ( str_contains( $purchase->product_slug, 'jetpack_ai' ) ) {
return true;
}
}
}
return false;
}

/**
Expand All @@ -433,11 +444,11 @@ public static function has_required_plan() {
* @return boolean
*/
public static function is_upgradable() {
$has_required_plan = self::has_required_plan();
$current_tier = self::get_current_usage_tier();
$has_ai_feature = static::does_site_have_feature( 'ai-assistant' );
$current_tier = self::get_current_usage_tier();

// Mark as not upgradable if user is on unlimited tier or does not have any plan.
if ( ! $has_required_plan || null === $current_tier || 1 === $current_tier ) {
if ( ! $has_ai_feature || null === $current_tier || 1 === $current_tier ) {
return false;
}

Expand Down

0 comments on commit e56685a

Please sign in to comment.