Skip to content

Commit

Permalink
My Jetpack: Fix Jetpack AI interstitial on disconnected site (#34834)
Browse files Browse the repository at this point in the history
* Set reasonable backend default values for current and next tier when the site is not connected

* Only show the contact us interstitial if the site is connected, and default next tier to 100 when not connected

* Add changelog file

* Prevent remote requests from Jetpack AI product when the site is not connected

* Create private helper function to check if the site is connected
  • Loading branch information
lhkowalski authored Jan 5, 2024
1 parent 3d729fd commit 217022c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { AdminPage, Button, Col, Container, Text } from '@automattic/jetpack-components';
import { useConnection } from '@automattic/jetpack-connection';
import { createInterpolateElement } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import classNames from 'classnames';
Expand Down Expand Up @@ -288,18 +289,21 @@ export function JetpackAIInterstitial() {
const slug = 'jetpack-ai';
const { detail } = useProduct( slug );
const { onClickGoBack } = useGoBack( { slug } );
const { isRegistered } = useConnection();

const nextTier = detail?.[ 'ai-assistant-feature' ]?.[ 'next-tier' ] || null;

if ( ! nextTier ) {
if ( isRegistered && ! nextTier ) {
return <JetpackAIInterstitialMoreRequests onClickGoBack={ onClickGoBack } />;
}

const { hasRequiredPlan } = detail;
const ctaLabel = hasRequiredPlan ? __( 'Upgrade Jetpack AI', 'jetpack-my-jetpack' ) : null;

// Default to 100 requests if the site is not registered/connected.
const nextTierValue = isRegistered ? nextTier?.value : 100;
// Decide the quantity value for the upgrade, but ignore the unlimited tier.
const quantity = nextTier?.value !== 1 ? nextTier?.value : null;
const quantity = nextTierValue !== 1 ? nextTierValue : null;

// Highlight the last feature in the table for all the tiers except the unlimited one.
const highlightLastFeature = nextTier?.value !== 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Significance: patch
Type: changed
Comment: Fixed a bug on how the Jetpack AI interstitial shows when the site is disconnected.
29 changes: 29 additions & 0 deletions projects/packages/my-jetpack/src/products/class-jetpack-ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Automattic\Jetpack\My_Jetpack\Products;

use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\My_Jetpack\Product;
use Automattic\Jetpack\My_Jetpack\Wpcom_Products;

Expand Down Expand Up @@ -80,6 +81,10 @@ public static function get_title() {
* @return int
*/
public static function get_current_usage_tier() {
if ( ! self::is_site_connected() ) {
return 0;
}

$info = self::get_ai_assistant_feature();

// Bail early if it's not possible to fetch the feature data.
Expand All @@ -98,6 +103,10 @@ public static function get_current_usage_tier() {
* @return int
*/
public static function get_next_usage_tier() {
if ( ! self::is_site_connected() ) {
return 100;
}

$info = self::get_ai_assistant_feature();

// Bail early if it's not possible to fetch the feature data.
Expand Down Expand Up @@ -196,6 +205,12 @@ public static function get_features() {
* @return array Pricing details
*/
public static function get_pricing_for_ui_by_usage_tier( $tier ) {

// Bail early if the site is not connected.
if ( ! self::is_site_connected() ) {
return array();
}

$product = Wpcom_Products::get_product( static::get_wpcom_product_slug() );

if ( empty( $product ) ) {
Expand Down Expand Up @@ -363,6 +378,11 @@ public static function get_ai_assistant_feature() {
return array();
}

// Bail early if the site is not connected.
if ( ! self::is_site_connected() ) {
return array();
}

// Check if class exists. If not, try to require it once.
if ( ! class_exists( 'Jetpack_AI_Helper' ) ) {
$class_file_path = JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-ai-helper.php';
Expand All @@ -377,4 +397,13 @@ public static function get_ai_assistant_feature() {

return \Jetpack_AI_Helper::get_ai_assistance_feature();
}

/**
* Checks whether the site is connected to WordPress.com.
*
* @return boolean
*/
private static function is_site_connected() {
return ( new Connection_Manager() )->is_connected();
}
}

0 comments on commit 217022c

Please sign in to comment.