Skip to content

Commit

Permalink
My Jetpack: Improve product cache handling on Jetpack AI (#34428)
Browse files Browse the repository at this point in the history
* Only shows the price when the price information is available

* Support cache busting from the get_product method

* Renew product cache if tiers are enabled but there is no tier info on the cache

* Add changelog file

* Check for wp_error on feature data to handle Jetpack not being connected
  • Loading branch information
lhkowalski authored Dec 4, 2023
1 parent bbcb370 commit ce7b3c7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ const ProductDetailCard = ( {
) ) }
</ul>

{ needsPurchase && (
{ needsPurchase && discountPrice && (
<>
<div className={ styles[ 'price-container' ] }>
{ discountPrice < price && (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

My Jetpack: Fix outdated product cache issue when enabling tiers.
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.1.0",
"version": "4.1.1-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
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Initializer {
*
* @var string
*/
const PACKAGE_VERSION = '4.1.0';
const PACKAGE_VERSION = '4.1.1-alpha';

/**
* HTML container ID for the IDC screen on My Jetpack page.
Expand Down
5 changes: 3 additions & 2 deletions projects/packages/my-jetpack/src/class-wpcom-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ public static function get_products( $skip_cache = false ) {
* Get one product
*
* @param string $product_slug The product slug.
* @param bool $renew_cache A flag to force the cache to be renewed.
*
* @return ?Object The product details if found
*/
public static function get_product( $product_slug ) {
$products = self::get_products();
public static function get_product( $product_slug, $renew_cache = false ) {
$products = self::get_products( $renew_cache );
if ( ! empty( $products->$product_slug ) ) {
return $products->$product_slug;
}
Expand Down
25 changes: 21 additions & 4 deletions projects/packages/my-jetpack/src/products/class-jetpack-ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,33 @@ public static function get_features() {
* @return array Pricing details
*/
public static function get_pricing_for_ui_by_usage_tier( $tier ) {
$product = Wpcom_Products::get_product( static::get_wpcom_product_slug() );
$base_pricing = Wpcom_Products::get_product_pricing( static::get_wpcom_product_slug() );
$product = Wpcom_Products::get_product( static::get_wpcom_product_slug() );

if ( empty( $product ) ) {
return array();
}

if ( empty( $product->price_tier_list ) ) {
return $base_pricing;
// 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;

/*
* when tiers are enabled and the price tier list is empty,
* we may need to renew the cache for the product data so
* we get the new price tier list.
*
* if the list is still empty after the fresh data, we will
* default to empty pricing (by returning an empty array).
*/
if ( empty( $product->price_tier_list ) && $tier_plans_enabled ) {
$product = Wpcom_Products::get_product( static::get_wpcom_product_slug(), true );
}

// get the base pricing for the unlimited plan, for compatibility
$base_pricing = Wpcom_Products::get_product_pricing( static::get_wpcom_product_slug() );

$price_tier_list = $product->price_tier_list;
$yearly_prices = array();

Expand All @@ -219,6 +235,7 @@ public static function get_pricing_for_ui_by_usage_tier( $tier ) {
}
}

// add the base pricing to the list
$prices = array( 1 => $base_pricing );

foreach ( $yearly_prices as $units => $price ) {
Expand Down

0 comments on commit ce7b3c7

Please sign in to comment.