diff --git a/projects/packages/my-jetpack/changelog/update-my-jetpack-get-product-classes b/projects/packages/my-jetpack/changelog/update-my-jetpack-get-product-classes new file mode 100644 index 0000000000000..b41bc16b78914 --- /dev/null +++ b/projects/packages/my-jetpack/changelog/update-my-jetpack-get-product-classes @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +My Jetpack: Enhanced `get_products()` to optionally accept parameter whether to return all or visible_only products. Non user-facing enhancement. diff --git a/projects/packages/my-jetpack/src/class-initializer.php b/projects/packages/my-jetpack/src/class-initializer.php index 6ad805d4697d3..6bdcbc11b9e3b 100644 --- a/projects/packages/my-jetpack/src/class-initializer.php +++ b/projects/packages/my-jetpack/src/class-initializer.php @@ -984,25 +984,11 @@ public static function alert_if_paid_plan_expiring( array $red_bubble_slugs ) { if ( ! $connection->is_connected() ) { return $red_bubble_slugs; } - $product_classes = Products::get_products_classes(); - $not_shown_products = array( - 'scan', - 'extras', - 'ai', - 'newsletter', - 'site-accelerator', - 'related-posts', - ); + // Passing `true` argument in order to only get visible products that are displayed as product cards on the My Jetpack page. + $displayed_products_classes = Products::get_products_classes( true ); $products_included_in_expiring_plan = array(); - foreach ( $product_classes as $key => $product ) { - // Skip these- we don't show them in My Jetpack. - // ('ai' is a duplicate class of 'jetpack-ai', and therefore not needed). - // See `get_product_classes() in projects/packages/my-jetpack/src/class-products.php for more info. - if ( in_array( $key, $not_shown_products, true ) ) { - continue; - } - + foreach ( $displayed_products_classes as $product ) { if ( $product::has_paid_plan_for_product() ) { $purchase = $product::get_paid_plan_purchase_for_product(); if ( $purchase ) { diff --git a/projects/packages/my-jetpack/src/class-products.php b/projects/packages/my-jetpack/src/class-products.php index b6a96e4304ce1..75894885c159e 100644 --- a/projects/packages/my-jetpack/src/class-products.php +++ b/projects/packages/my-jetpack/src/class-products.php @@ -114,13 +114,14 @@ class Products { /** * Get the list of Products classes - * * Here's where all the existing Products are registered * + * @param bool $visible_products_only If true, only products that are visible product cards on the My Jetpack page are returned. + * * @throws \Exception If the result of a filter has invalid classes. * @return array List of class names */ - public static function get_products_classes() { + public static function get_products_classes( $visible_products_only = false ) { $classes = array( 'anti-spam' => Products\Anti_Spam::class, 'backup' => Products\Backup::class, @@ -130,7 +131,8 @@ public static function get_products_classes() { 'extras' => Products\Extras::class, 'jetpack-ai' => Products\Jetpack_Ai::class, // TODO: Remove this duplicate class ('ai')? See: https://github.com/Automattic/jetpack/pull/35910#pullrequestreview-2456462227 - 'ai' => Products\Jetpack_Ai::class, + // Jan 2, 2025 - Removing this 'ai' slug now. But only commenting it out for now for debugging purposes, just in case it causes an error somewhere. + // 'ai' => Products\Jetpack_Ai::class, 'scan' => Products\Scan::class, 'search' => Products\Search::class, 'social' => Products\Social::class, @@ -170,17 +172,41 @@ public static function get_products_classes() { } } + if ( $visible_products_only ) { + // Filter out products that are not shown as visible product cards on the main My Jetpack page. + return array_filter( + $final_classes, + function ( $class ) { + return $class::$is_visible_product_card; + } + ); + } + return $final_classes; } + /** + * Get the list of product slugs that are NOT shown on the main My Jetpack screen. + * + * @return array An array of product slugs that do not get displayed as product cards on the My Jetpack screen. + */ + public static function get_products_not_shown_in_ui() { + $all_products = self::get_products_classes(); + $visible_products = self::get_products_classes( true ); + + return array_keys( array_diff( $all_products, $visible_products ) ); + } + /** * Product data * + * @param bool $visible_products_only If true, only products that are visible product cards on the My Jetpack page are returned. + * * @return array Jetpack products on the site and their availability. */ - public static function get_products() { + public static function get_products( $visible_products_only = false ) { $products = array(); - foreach ( self::get_products_classes() as $class ) { + foreach ( self::get_products_classes( $visible_products_only ) as $class ) { $product_slug = $class::$slug; $products[ $product_slug ] = $class::get_info(); } diff --git a/projects/packages/my-jetpack/src/class-rest-products.php b/projects/packages/my-jetpack/src/class-rest-products.php index dfa60108b7c56..6a0a27dead261 100644 --- a/projects/packages/my-jetpack/src/class-rest-products.php +++ b/projects/packages/my-jetpack/src/class-rest-products.php @@ -25,6 +25,12 @@ public function __construct() { 'methods' => \WP_REST_Server::READABLE, 'callback' => __CLASS__ . '::get_products', 'permission_callback' => __CLASS__ . '::permissions_callback', + 'args' => array( + 'visible_only' => array( + 'required' => false, + 'type' => 'boolean', + ), + ), ), 'schema' => array( $this, 'get_products_schema' ), ) @@ -144,10 +150,12 @@ public static function check_product_argument( $value ) { /** * Site products endpoint. * + * @param \WP_REST_Request $request The request object. * @return array of site products list. */ - public static function get_products() { - $response = Products::get_products(); + public static function get_products( $request ) { + $visible_products_only = $request->get_param( 'visible_only' ) ?? false; + $response = Products::get_products( $visible_products_only ); return rest_ensure_response( $response, 200 ); } diff --git a/projects/packages/my-jetpack/src/products/class-anti-spam.php b/projects/packages/my-jetpack/src/products/class-anti-spam.php index c341a9f88d32d..10d6d1e01dde1 100644 --- a/projects/packages/my-jetpack/src/products/class-anti-spam.php +++ b/projects/packages/my-jetpack/src/products/class-anti-spam.php @@ -64,6 +64,13 @@ class Anti_Spam extends Product { */ public static $has_standalone_plugin = true; + /** + * Whether the Product is a visible product card shown on the main My Jetpack screen + * + * @var bool + */ + public static $is_visible_product_card = true; + /** * Get the product name * diff --git a/projects/packages/my-jetpack/src/products/class-backup.php b/projects/packages/my-jetpack/src/products/class-backup.php index 829044b11d5ec..5826c52b02415 100644 --- a/projects/packages/my-jetpack/src/products/class-backup.php +++ b/projects/packages/my-jetpack/src/products/class-backup.php @@ -71,6 +71,13 @@ class Backup extends Hybrid_Product { */ public static $feature_identifying_paid_plan = 'backups'; + /** + * Whether the Product is a visible product card shown on the main My Jetpack screen + * + * @var bool + */ + public static $is_visible_product_card = true; + /** * Get the product name * diff --git a/projects/packages/my-jetpack/src/products/class-boost.php b/projects/packages/my-jetpack/src/products/class-boost.php index ca2b18f034763..ffe1c6cdc9f67 100644 --- a/projects/packages/my-jetpack/src/products/class-boost.php +++ b/projects/packages/my-jetpack/src/products/class-boost.php @@ -72,6 +72,13 @@ class Boost extends Product { */ public static $feature_identifying_paid_plan = 'cloud-critical-css'; + /** + * Whether the Product is a visible product card shown on the main My Jetpack screen + * + * @var bool + */ + public static $is_visible_product_card = true; + /** * Get the product name * diff --git a/projects/packages/my-jetpack/src/products/class-crm.php b/projects/packages/my-jetpack/src/products/class-crm.php index dceeb0b2ae889..4de87aa2ba599 100644 --- a/projects/packages/my-jetpack/src/products/class-crm.php +++ b/projects/packages/my-jetpack/src/products/class-crm.php @@ -60,6 +60,13 @@ class Crm extends Product { */ public static $has_standalone_plugin = true; + /** + * Whether the Product is a visible product card shown on the main My Jetpack screen + * + * @var bool + */ + public static $is_visible_product_card = true; + /** * Get the product name * 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 1e4e0c9b206b5..c00c05ecbaa1b 100644 --- a/projects/packages/my-jetpack/src/products/class-jetpack-ai.php +++ b/projects/packages/my-jetpack/src/products/class-jetpack-ai.php @@ -42,6 +42,13 @@ class Jetpack_Ai extends Product { */ public static $feature_identifying_paid_plan = 'ai-assistant'; + /** + * Whether the Product is a visible product card shown on the main My Jetpack screen + * + * @var bool + */ + public static $is_visible_product_card = true; + /** * Get the Product info for the API * diff --git a/projects/packages/my-jetpack/src/products/class-product.php b/projects/packages/my-jetpack/src/products/class-product.php index b2262eddd6b4a..9c5c660a7c718 100644 --- a/projects/packages/my-jetpack/src/products/class-product.php +++ b/projects/packages/my-jetpack/src/products/class-product.php @@ -116,6 +116,13 @@ abstract class Product { */ public static $feature_identifying_paid_plan = ''; + /** + * Whether the Product is a visible product card shown on the main My Jetpack screen + * + * @var bool + */ + public static $is_visible_product_card = false; + /** * Get the plugin slug * diff --git a/projects/packages/my-jetpack/src/products/class-protect.php b/projects/packages/my-jetpack/src/products/class-protect.php index b3e14499ddda9..c5dbda9dc8207 100644 --- a/projects/packages/my-jetpack/src/products/class-protect.php +++ b/projects/packages/my-jetpack/src/products/class-protect.php @@ -79,6 +79,13 @@ class Protect extends Hybrid_Product { */ public static $feature_identifying_paid_plan = 'scan'; + /** + * Whether the Product is a visible product card shown on the main My Jetpack screen + * + * @var bool + */ + public static $is_visible_product_card = true; + /** * Get the product name * diff --git a/projects/packages/my-jetpack/src/products/class-search.php b/projects/packages/my-jetpack/src/products/class-search.php index bb1bc87045743..07252edb5ae0f 100644 --- a/projects/packages/my-jetpack/src/products/class-search.php +++ b/projects/packages/my-jetpack/src/products/class-search.php @@ -87,6 +87,13 @@ class Search extends Hybrid_Product { */ public static $feature_identifying_paid_plan = 'search'; + /** + * Whether the Product is a visible product card shown on the main My Jetpack screen + * + * @var bool + */ + public static $is_visible_product_card = true; + /** * Get the product name * diff --git a/projects/packages/my-jetpack/src/products/class-social.php b/projects/packages/my-jetpack/src/products/class-social.php index 8ba40dc889e4f..a71dfc4ffc45b 100644 --- a/projects/packages/my-jetpack/src/products/class-social.php +++ b/projects/packages/my-jetpack/src/products/class-social.php @@ -70,6 +70,13 @@ class Social extends Hybrid_Product { */ public static $feature_identifying_paid_plan = 'social-enhanced-publishing'; + /** + * Whether the Product is a visible product card shown on the main My Jetpack screen + * + * @var bool + */ + public static $is_visible_product_card = true; + /** * Get the product name * diff --git a/projects/packages/my-jetpack/src/products/class-stats.php b/projects/packages/my-jetpack/src/products/class-stats.php index 4ae6f485b981a..9b8184a1006fb 100644 --- a/projects/packages/my-jetpack/src/products/class-stats.php +++ b/projects/packages/my-jetpack/src/products/class-stats.php @@ -74,6 +74,13 @@ class Stats extends Module_Product { */ public static $feature_identifying_paid_plan = 'stats-paid'; + /** + * Whether the Product is a visible product card shown on the main My Jetpack screen + * + * @var bool + */ + public static $is_visible_product_card = true; + /** * Get the product name * diff --git a/projects/packages/my-jetpack/src/products/class-videopress.php b/projects/packages/my-jetpack/src/products/class-videopress.php index f3eab30b844fc..b6f0d625c32f7 100644 --- a/projects/packages/my-jetpack/src/products/class-videopress.php +++ b/projects/packages/my-jetpack/src/products/class-videopress.php @@ -75,6 +75,13 @@ class Videopress extends Hybrid_Product { */ public static $feature_identifying_paid_plan = 'videopress'; + /** + * Whether the Product is a visible product card shown on the main My Jetpack screen + * + * @var bool + */ + public static $is_visible_product_card = true; + /** * Get the product name *