Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My Jetpack: Improve Products::get_products(). Allow to get displayed products only. #40838

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 3 additions & 17 deletions projects/packages/my-jetpack/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
36 changes: 31 additions & 5 deletions projects/packages/my-jetpack/src/class-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}
Expand Down
12 changes: 10 additions & 2 deletions projects/packages/my-jetpack/src/class-rest-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
)
Expand Down Expand Up @@ -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 );
}

Expand Down
7 changes: 7 additions & 0 deletions projects/packages/my-jetpack/src/products/class-anti-spam.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 7 additions & 0 deletions projects/packages/my-jetpack/src/products/class-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 7 additions & 0 deletions projects/packages/my-jetpack/src/products/class-boost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 7 additions & 0 deletions projects/packages/my-jetpack/src/products/class-crm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 7 additions & 0 deletions projects/packages/my-jetpack/src/products/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 7 additions & 0 deletions projects/packages/my-jetpack/src/products/class-protect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 7 additions & 0 deletions projects/packages/my-jetpack/src/products/class-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 7 additions & 0 deletions projects/packages/my-jetpack/src/products/class-social.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 7 additions & 0 deletions projects/packages/my-jetpack/src/products/class-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Loading