From 21b4c4f00c460e8314ba58c7bdbbd702f1342bae Mon Sep 17 00:00:00 2001 From: Ian Ramos <5714212+IanRamosC@users.noreply.github.com> Date: Wed, 18 Dec 2024 15:54:14 -0300 Subject: [PATCH] My Jetpack: support features as recommendations (#40492) * My Jetpack: add feature as possible module recommendation * changelog * Add the module classes for the 3 features * Add config for feature modules * Refactor module product class * Fix PHAN errors --- .../changelog/add-feature-recommendations | 4 + .../my-jetpack/src/class-products.php | 38 ++-- .../src/products/class-module-product.php | 38 ++++ .../src/products/class-newsletter.php | 179 ++++++++++++++++++ .../src/products/class-related-posts.php | 179 ++++++++++++++++++ .../src/products/class-site-accelerator.php | 179 ++++++++++++++++++ 6 files changed, 600 insertions(+), 17 deletions(-) create mode 100644 projects/packages/my-jetpack/changelog/add-feature-recommendations create mode 100644 projects/packages/my-jetpack/src/products/class-newsletter.php create mode 100644 projects/packages/my-jetpack/src/products/class-related-posts.php create mode 100644 projects/packages/my-jetpack/src/products/class-site-accelerator.php diff --git a/projects/packages/my-jetpack/changelog/add-feature-recommendations b/projects/packages/my-jetpack/changelog/add-feature-recommendations new file mode 100644 index 0000000000000..700e01f03c9a4 --- /dev/null +++ b/projects/packages/my-jetpack/changelog/add-feature-recommendations @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +My Jetpack: add features as possible modules to the recommendations list. diff --git a/projects/packages/my-jetpack/src/class-products.php b/projects/packages/my-jetpack/src/class-products.php index 87e277dcae26a..8f8681fdb2ab8 100644 --- a/projects/packages/my-jetpack/src/class-products.php +++ b/projects/packages/my-jetpack/src/class-products.php @@ -119,24 +119,28 @@ class Products { */ public static function get_products_classes() { $classes = array( - 'anti-spam' => Products\Anti_Spam::class, - 'backup' => Products\Backup::class, - 'boost' => Products\Boost::class, - 'crm' => Products\Crm::class, - 'creator' => Products\Creator::class, - 'extras' => Products\Extras::class, - 'jetpack-ai' => Products\Jetpack_Ai::class, + 'anti-spam' => Products\Anti_Spam::class, + 'backup' => Products\Backup::class, + 'boost' => Products\Boost::class, + 'crm' => Products\Crm::class, + 'creator' => Products\Creator::class, + '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, - 'scan' => Products\Scan::class, - 'search' => Products\Search::class, - 'social' => Products\Social::class, - 'security' => Products\Security::class, - 'protect' => Products\Protect::class, - 'videopress' => Products\Videopress::class, - 'stats' => Products\Stats::class, - 'growth' => Products\Growth::class, - 'complete' => Products\Complete::class, + 'ai' => Products\Jetpack_Ai::class, + 'scan' => Products\Scan::class, + 'search' => Products\Search::class, + 'social' => Products\Social::class, + 'security' => Products\Security::class, + 'protect' => Products\Protect::class, + 'videopress' => Products\Videopress::class, + 'stats' => Products\Stats::class, + 'growth' => Products\Growth::class, + 'complete' => Products\Complete::class, + // Features + 'newsletter' => Products\Newsletter::class, + 'site-accelerator' => Products\Site_Accelerator::class, + 'related-posts' => Products\Related_Posts::class, ); /** diff --git a/projects/packages/my-jetpack/src/products/class-module-product.php b/projects/packages/my-jetpack/src/products/class-module-product.php index 5472cfec02e26..9d1a14f0fee86 100644 --- a/projects/packages/my-jetpack/src/products/class-module-product.php +++ b/projects/packages/my-jetpack/src/products/class-module-product.php @@ -7,6 +7,7 @@ namespace Automattic\Jetpack\My_Jetpack; +use Automattic\Jetpack\Connection\Manager as Connection_Manager; use Jetpack; use WP_Error; @@ -26,6 +27,13 @@ abstract class Module_Product extends Product { */ public static $module_name = null; + /** + * Whether this module is a Jetpack feature + * + * @var boolean + */ + public static $is_feature = false; + /** * Get the plugin slug - ovewrite it ans return Jetpack's * @@ -79,12 +87,42 @@ public static function is_module_active() { return Jetpack::is_module_active( static::$module_name ); } + /** + * Get the product status. + * We don't use parent::get_status() to avoid complexity. + * + * @return string Product status. + */ + private static function get_feature_status() { + if ( ! static::is_plugin_installed() ) { + return Products::STATUS_PLUGIN_ABSENT; + } + + if ( ! static::is_plugin_active() ) { + return Products::STATUS_INACTIVE; + } + + if ( static::$requires_user_connection && ! ( new Connection_Manager() )->has_connected_owner() ) { + return Products::STATUS_USER_CONNECTION_ERROR; + } + + if ( ! static::is_module_active() ) { + return Products::STATUS_MODULE_DISABLED; + } + + return Products::STATUS_ACTIVE; + } + /** * Gets the current status of the product * * @return string */ public static function get_status() { + if ( static::$is_feature ) { + return static::get_feature_status(); + } + $status = parent::get_status(); if ( Products::STATUS_INACTIVE === $status && ! static::is_module_active() ) { $status = Products::STATUS_MODULE_DISABLED; diff --git a/projects/packages/my-jetpack/src/products/class-newsletter.php b/projects/packages/my-jetpack/src/products/class-newsletter.php new file mode 100644 index 0000000000000..66d806d5ea6ca --- /dev/null +++ b/projects/packages/my-jetpack/src/products/class-newsletter.php @@ -0,0 +1,179 @@ + true, + 'is_free' => true, + ); + } + + /** + * Checks whether the Product is active. + * + * @return boolean + */ + public static function is_active() { + return static::is_jetpack_plugin_active(); + } + + /** + * Checks whether the plugin is installed + * + * @return boolean + */ + public static function is_plugin_installed() { + return static::is_jetpack_plugin_installed(); + } + + /** + * Get the URL where the user manages the product + * + * @return ?string + */ + public static function get_manage_url() { + return admin_url( 'admin.php?page=jetpack#/settings?term=newsletter' ); + } + + /** + * Activates the Jetpack plugin + * + * @return null|WP_Error Null on success, WP_Error on invalid file. + */ + public static function activate_plugin() { + $plugin_filename = static::get_installed_plugin_filename( self::JETPACK_PLUGIN_SLUG ); + + if ( $plugin_filename ) { + return activate_plugin( $plugin_filename ); + } + } +} diff --git a/projects/packages/my-jetpack/src/products/class-related-posts.php b/projects/packages/my-jetpack/src/products/class-related-posts.php new file mode 100644 index 0000000000000..e5b10ec989931 --- /dev/null +++ b/projects/packages/my-jetpack/src/products/class-related-posts.php @@ -0,0 +1,179 @@ + true, + 'is_free' => true, + ); + } + + /** + * Checks whether the Product is active. + * + * @return boolean + */ + public static function is_active() { + return static::is_jetpack_plugin_active(); + } + + /** + * Checks whether the plugin is installed + * + * @return boolean + */ + public static function is_plugin_installed() { + return static::is_jetpack_plugin_installed(); + } + + /** + * Get the URL where the user manages the product + * + * @return ?string + */ + public static function get_manage_url() { + return admin_url( 'admin.php?page=jetpack#/traffic?term=related%20posts' ); + } + + /** + * Activates the Jetpack plugin + * + * @return null|WP_Error Null on success, WP_Error on invalid file. + */ + public static function activate_plugin() { + $plugin_filename = static::get_installed_plugin_filename( self::JETPACK_PLUGIN_SLUG ); + + if ( $plugin_filename ) { + return activate_plugin( $plugin_filename ); + } + } +} diff --git a/projects/packages/my-jetpack/src/products/class-site-accelerator.php b/projects/packages/my-jetpack/src/products/class-site-accelerator.php new file mode 100644 index 0000000000000..42cda9bc8a798 --- /dev/null +++ b/projects/packages/my-jetpack/src/products/class-site-accelerator.php @@ -0,0 +1,179 @@ + true, + 'is_free' => true, + ); + } + + /** + * Checks whether the Product is active. + * + * @return boolean + */ + public static function is_active() { + return static::is_jetpack_plugin_active(); + } + + /** + * Checks whether the plugin is installed + * + * @return boolean + */ + public static function is_plugin_installed() { + return static::is_jetpack_plugin_installed(); + } + + /** + * Get the URL where the user manages the product + * + * @return ?string + */ + public static function get_manage_url() { + return admin_url( 'admin.php?page=jetpack#/settings?term=site%20accelerator' ); + } + + /** + * Activates the Jetpack plugin + * + * @return null|WP_Error Null on success, WP_Error on invalid file. + */ + public static function activate_plugin() { + $plugin_filename = static::get_installed_plugin_filename( self::JETPACK_PLUGIN_SLUG ); + + if ( $plugin_filename ) { + return activate_plugin( $plugin_filename ); + } + } +}