Skip to content

Commit

Permalink
Refactor/improve how we determine 'has_paid_plan_for_product()'.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottprogrammer committed Nov 22, 2024
1 parent f674558 commit 8ab9836
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 299 deletions.
39 changes: 11 additions & 28 deletions projects/packages/my-jetpack/src/products/class-anti-spam.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public static function get_features() {
}

/**
* Get the product-slugs of the paid plans for this product (not including bundles)
* Get the product-slugs of the paid plans for this product.
* (Do not include bundle plans, unless it's a bundle plan itself).
*
* @return array
*/
Expand All @@ -127,41 +128,23 @@ public static function get_paid_plan_product_slugs() {
}

/**
* 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
* Determine if the site has an Akismet plan.
*
* @return bool - whether an API key was found
*/
public static function has_paid_plan_for_product() {
$products_with_anti_spam = array(
'jetpack_anti_spam',
'jetpack_complete',
'jetpack_security',
'jetpack_personal',
'jetpack_premium',
'jetpack_business',
);
// 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 );

// Check for existing plans
$purchases_data = Wpcom_Products::get_site_current_purchases();
if ( is_wp_error( $purchases_data ) ) {
return $fallback;
if ( parent::has_paid_plan_for_product() ) {
return true;
}
// As a fallback, we're checking if the site has an API key for Akismet.
// 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
$akismet_api_key = apply_filters( 'akismet_get_api_key', defined( 'WPCOM_API_KEY' ) ? constant( 'WPCOM_API_KEY' ) : get_option( 'wordpress_api_key' ) );
if ( ! empty( $akismet_api_key ) ) {
return true;

if ( is_array( $purchases_data ) && ! empty( $purchases_data ) ) {
foreach ( $purchases_data as $purchase ) {
foreach ( $products_with_anti_spam as $product ) {
if ( strpos( $purchase->product_slug, $product ) !== false ) {
return true;
}
}
}
}

return $fallback;
return false;
}

/**
Expand Down
35 changes: 2 additions & 33 deletions projects/packages/my-jetpack/src/products/class-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,38 +200,6 @@ private static function get_state_from_wpcom() {
return $status;
}

/**
* Checks whether the current plan (or purchases) of the site already supports the product
*
* @return boolean
*/
public static function has_paid_plan_for_product() {
$plans_with_backup = array_merge(
static::get_paid_bundles_that_include_product(),
static::get_paid_plan_product_slugs()
);

$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 ) {
foreach ( $plans_with_backup as $plan ) {
if ( strpos( $purchase->product_slug, $plan ) !== false ) {
return true;
}
}
}
}

$rewind_data = static::get_state_from_wpcom();
if ( is_wp_error( $rewind_data ) ) {
return false;
}
return is_object( $rewind_data ) && isset( $rewind_data->state ) && 'unavailable' !== $rewind_data->state;
}

/**
* Return product bundles list
* that supports the product.
Expand Down Expand Up @@ -267,7 +235,8 @@ public static function get_manage_url() {
}

/**
* Get the product-slugs of the paid plans for this product (not including bundles)
* Get the product-slugs of the paid plans for this product.
* (Do not include bundle plans, unless it's a bundle plan itself).
*
* @return array
*/
Expand Down
24 changes: 2 additions & 22 deletions projects/packages/my-jetpack/src/products/class-boost.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,27 +348,6 @@ public static function get_pricing_for_ui() {
);
}

/**
* Checks whether the current plan (or purchases) of the site already supports the product
*
* @return boolean
*/
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 ) {
// Boost is available as standalone bundle and as part of the Complete plan.
if ( strpos( $purchase->product_slug, 'jetpack_boost' ) !== false || str_starts_with( $purchase->product_slug, 'jetpack_complete' ) ) {
return true;
}
}
}
return false;
}

/**
* Get the URL where the user manages the product
*
Expand Down Expand Up @@ -400,7 +379,8 @@ public static function do_product_specific_activation( $current_result ) {
}

/**
* Get the product-slugs of the paid plans for this product (not including bundles)
* Get the product-slugs of the paid plans for this product.
* (Do not include bundle plans, unless it's a bundle plan itself).
*
* @return array
*/
Expand Down
14 changes: 14 additions & 0 deletions projects/packages/my-jetpack/src/products/class-complete.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ public static function has_required_plan() {
return false;
}

/**
* Get the product-slugs of the paid plans for this product.
* (Do not include bundle plans, unless it's a bundle plan itself).
*
* @return array
*/
public static function get_paid_plan_product_slugs() {
return array(
'jetpack_complete',
'jetpack_complete_monthly',
'jetpack_complete_bi_yearly',
);
}

/**
* Checks whether product is a bundle.
*
Expand Down
24 changes: 2 additions & 22 deletions projects/packages/my-jetpack/src/products/class-creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,27 +318,6 @@ public static function get_wpcom_monthly_product_slug() {
return 'jetpack_creator_monthly';
}

/**
* Checks whether the current plan (or purchases) of the site already supports the product
*
* @return boolean
*/
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 ) {
// Creator is available as standalone bundle and as part of the Complete plan.
if ( strpos( $purchase->product_slug, 'jetpack_creator' ) !== false || str_starts_with( $purchase->product_slug, 'jetpack_complete' ) ) {
return true;
}
}
}
return false;
}

/**
* Get the product-slugs of the paid bundles/plans that this product/module is included in
*
Expand All @@ -353,7 +332,8 @@ public static function get_paid_bundles_that_include_product() {
}

/**
* Get the product-slugs of the paid plans for this product (not including bundles)
* Get the product-slugs of the paid plans for this product.
* (Do not include bundle plans, unless it's a bundle plan itself).
*
* @return array
*/
Expand Down
2 changes: 2 additions & 0 deletions projects/packages/my-jetpack/src/products/class-crm.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ public static function has_paid_plan_for_product() {
public static function get_paid_bundles_that_include_product() {
return array(
'jetpack_complete',
'jetpack_complete_monthly',
'jetpack_complete_bi_yearly',
);
}

Expand Down
14 changes: 14 additions & 0 deletions projects/packages/my-jetpack/src/products/class-growth.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ public static function has_required_plan() {
return false;
}

/**
* Get the product-slugs of the paid plans for this product.
* (Do not include bundle plans, unless it's a bundle plan itself).
*
* @return array
*/
public static function get_paid_plan_product_slugs() {
return array(
'jetpack_growth_yearly',
'jetpack_growth_monthly',
'jetpack_growth_bi_yearly',
);
}

/**
* Checks whether the product is a bundle
*
Expand Down
30 changes: 9 additions & 21 deletions projects/packages/my-jetpack/src/products/class-jetpack-ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class Jetpack_Ai extends Product {
*/
public static $has_free_offering = true;

/**
* The feature slug that identifies the paid plan
*
* @var string
*/
public static $feature_identifying_paid_plan = 'ai-assistant';

/**
* Get the Product info for the API
*
Expand Down Expand Up @@ -445,7 +452,8 @@ public static function get_wpcom_bi_yearly_product_slug() {
}

/**
* Get the product-slugs of the paid plans for this product (not including bundles)
* Get the product-slugs of the paid plans for this product.
* (Do not include bundle plans, unless it's a bundle plan itself).
*
* @return array
*/
Expand All @@ -457,26 +465,6 @@ public static function get_paid_plan_product_slugs() {
);
}

/**
* Checks whether the site has a paid plan for this product
*
* @return boolean
*/
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;
}

/**
* Checks whether the product can be upgraded to a different product.
*
Expand Down
51 changes: 45 additions & 6 deletions projects/packages/my-jetpack/src/products/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,42 @@ public static function get_standalone_info() {
}

/**
* Checks whether the site has a paid plan for the product
* This ignores free products, it only checks if there is a purchase that supports the product
* Checks whether the site has a paid plan for the product.
*
* This function relies on the product's `$feature_identifying_paid_plan` and `get_paid_plan_product_slugs()` function.
* If the product does not define a `$feature_identifying_paid_plan`, be sure the product includes functions for both
* `get_paid_plan_product_slugs()` and `get_paid_bundles_that_include_product()` which return all the product slugs and
* bundle slugs that include the product, respectively.
*
* @return boolean
*/
public static function has_paid_plan_for_product() {
// First check site features (if there's a feature that identifies the paid plan)
if ( static::$feature_identifying_paid_plan ) {
if ( static::does_site_have_feature( static::$feature_identifying_paid_plan ) ) {
return true;
}
}
// Otherwise check site purchases
$plans_with_product = array_merge(
static::get_paid_bundles_that_include_product(),
static::get_paid_plan_product_slugs()
);

$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 ) {
foreach ( $plans_with_product as $plan ) {
if ( strpos( $purchase->product_slug, $plan ) !== false ) {
return true;
}
}
}
}

return false;
}

Expand All @@ -421,7 +451,8 @@ public static function has_any_plan_for_product() {
}

/**
* Get the product-slugs of the paid plans for this product (not including bundles).
* Get the product-slugs of the paid plans for this product.
* (Do not include bundle plans, unless it's a bundle plan itself).
*
* @return array
*/
Expand All @@ -430,20 +461,28 @@ public static function get_paid_plan_product_slugs() {
}

/**
* Get the product-slugs of the paid bundles/plans that this product/module is included in
* Get the product-slugs of the paid bundles/plans that this product/module is included in.
*
* This function relies on the product's `$feature_identifying_paid_plan`
* If the product does not define a `$feature_identifying_paid_plan`, be sure to include this
* function in the product's class and have it return all the paid bundle slugs that include
* the product.
*
* @return array
*/
public static function get_paid_bundles_that_include_product() {
if ( static::is_bundle_product() ) {
return array();
}
$features = static::get_site_features_from_wpcom();
if ( is_wp_error( $features ) ) {
return array();
}
$idendifying_feature = static::$feature_identifying_paid_plan;
if ( empty( $features['available'] ) || empty( $idendifying_feature ) ) {
if ( empty( $features['available'] ) ) {
return array();
}
$paid_bundles = $features['available']->$idendifying_feature;
$paid_bundles = $features['available']->$idendifying_feature ?? array();
$current_bundle = Wpcom_Products::get_site_current_plan();

if ( in_array( static::$feature_identifying_paid_plan, $current_bundle['features']['active'], true ) ) {
Expand Down
Loading

0 comments on commit 8ab9836

Please sign in to comment.