From 9cf2b4f2766353e4d6ec031bba7c8e638e72f7d1 Mon Sep 17 00:00:00 2001 From: Christian Gastrell Date: Thu, 22 Aug 2024 13:35:56 -0300 Subject: [PATCH] My jetpack: handle tiers enabled better (#38989) * add are_tier_plans_enabled method, use it to determine proper output from the product * change the default output for no paid plan, using default will let the frontend use the generic description * return null if there's no connection * simplify features array and return condition * simplify flag check * restore unreliable default * return generic features on faulty default * handle error on tier plans check method --- .../my-jetpack-handle-tiers-enabled-better | 4 + .../src/products/class-jetpack-ai.php | 73 +++++++++++++++---- 2 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 projects/packages/my-jetpack/changelog/my-jetpack-handle-tiers-enabled-better diff --git a/projects/packages/my-jetpack/changelog/my-jetpack-handle-tiers-enabled-better b/projects/packages/my-jetpack/changelog/my-jetpack-handle-tiers-enabled-better new file mode 100644 index 0000000000000..72600386aa77d --- /dev/null +++ b/projects/packages/my-jetpack/changelog/my-jetpack-handle-tiers-enabled-better @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +My Jetpack: AI product class to handle better disabling tiers diff --git a/projects/packages/my-jetpack/src/products/class-jetpack-ai.php b/projects/packages/my-jetpack/src/products/class-jetpack-ai.php index a65993035db12..eeb28354094c5 100644 --- a/projects/packages/my-jetpack/src/products/class-jetpack-ai.php +++ b/projects/packages/my-jetpack/src/products/class-jetpack-ai.php @@ -93,6 +93,10 @@ public static function get_title() { * @return string[] Slugs of the available tiers */ public static function get_tiers() { + if ( ! self::are_tier_plans_enabled() ) { + return parent::get_tiers(); + } + return array( self::UPGRADED_TIER_SLUG, self::CURRENT_TIER_SLUG, @@ -105,6 +109,10 @@ public static function get_tiers() { * @return array[] Protect features comparison */ public static function get_features_by_tier() { + if ( ! self::are_tier_plans_enabled() ) { + return parent::get_features_by_tier(); + } + $current_tier = self::get_current_usage_tier(); $current_description = 0 === $current_tier ? __( 'Up to 20 requests', 'jetpack-my-jetpack' ) @@ -194,13 +202,15 @@ public static function get_current_usage_tier() { */ public static function get_next_usage_tier() { if ( ! self::is_site_connected() || ! self::has_paid_plan_for_product() ) { + // without site connection we can't know if tiers are enabled or not, + // hence we can't know if the next tier is 100 or 1 (unlimited). return 100; } $info = self::get_ai_assistant_feature(); - // Bail early if it's not possible to fetch the feature data. - if ( is_wp_error( $info ) ) { + // Bail early if it's not possible to fetch the feature data or if it's included in a plan. + if ( is_wp_error( $info ) || empty( $info ) ) { return null; } @@ -253,14 +263,20 @@ public static function get_long_description() { * @return string */ public static function get_features_by_usage_tier( $tier ) { + $is_tier_plan = $tier && intval( $tier ) > 1; + + if ( $tier === 100 && ( ! self::is_site_connected() || ! self::has_paid_plan_for_product() ) ) { + // in these cases, get_next_usage_tier() will return 100 + // 100 is fine as default when tiered plans are enabled, but not otherwise + $is_tier_plan = false; + } + $features = array( - 1 => array( - __( 'Artificial intelligence chatbot', 'jetpack-my-jetpack' ), - __( 'Generate text, tables, lists, and forms', 'jetpack-my-jetpack' ), - __( 'Refine the tone and content to your liking', 'jetpack-my-jetpack' ), - __( 'Get feedback about your post', 'jetpack-my-jetpack' ), - __( 'Seamless WordPress editor integration', 'jetpack-my-jetpack' ), - ), + __( 'Artificial intelligence chatbot', 'jetpack-my-jetpack' ), + __( 'Generate text, tables, lists, and forms', 'jetpack-my-jetpack' ), + __( 'Refine the tone and content to your liking', 'jetpack-my-jetpack' ), + __( 'Get feedback about your post', 'jetpack-my-jetpack' ), + __( 'Seamless WordPress editor integration', 'jetpack-my-jetpack' ), ); $tiered_features = array( @@ -274,7 +290,7 @@ public static function get_features_by_usage_tier( $tier ) { sprintf( __( 'Up to %d requests per month', 'jetpack-my-jetpack' ), $tier ), ); - return isset( $features[ $tier ] ) ? $features[ $tier ] : $tiered_features; + return $is_tier_plan ? $tiered_features : $features; } /** @@ -305,11 +321,7 @@ public static function get_pricing_for_ui_by_usage_tier( $tier ) { return array(); } - // get info about the feature. - $info = self::get_ai_assistant_feature(); - - // flag to indicate if the tiers are enabled, case the info is available. - $tier_plans_enabled = ( ! is_wp_error( $info ) && isset( $info['tier-plans-enabled'] ) ) ? boolval( $info['tier-plans-enabled'] ) : false; + $tier_plans_enabled = self::are_tier_plans_enabled(); /* * when tiers are enabled and the price tier list is empty, @@ -360,6 +372,18 @@ public static function get_pricing_for_ui_by_usage_tier( $tier ) { * @return array Pricing details */ public static function get_pricing_for_ui() { + // no tiers + if ( ! self::are_tier_plans_enabled() ) { + return array_merge( + array( + 'available' => true, + 'wpcom_product_slug' => static::get_wpcom_product_slug(), + ), + // hardcoding 1 as next tier if tiers are not enabled + self::get_pricing_for_ui_by_usage_tier( 1 ) + ); + } + $next_tier = self::get_next_usage_tier(); $current_tier = self::get_current_usage_tier(); $current_call_to_action = $current_tier === 0 @@ -543,6 +567,25 @@ public static function get_ai_assistant_feature() { return \Jetpack_AI_Helper::get_ai_assistance_feature(); } + /** + * Get the AI Assistant tiered plans status + * + * @return boolean + */ + public static function are_tier_plans_enabled() { + $info = self::get_ai_assistant_feature(); + if ( is_wp_error( $info ) ) { + // this is another faulty default value, we'll assume disabled while + // production is enabled + return false; + } + + if ( ! empty( $info ) && isset( $info['tier-plans-enabled'] ) ) { + return boolval( $info['tier-plans-enabled'] ); + } + return false; + } + /** * Checks whether the site is connected to WordPress.com. *