From 5f5352d2b840b894e8291f474e906c749f8376c4 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Tue, 3 Sep 2024 15:56:05 +0800 Subject: [PATCH] Add is_trial_plan() - Update trial plan checks --- class-wc-calypso-bridge-dotcom-features.php | 65 +++++++++++++++++++ ...class-wc-calypso-bridge-admin-settings.php | 6 +- .../class-wc-calypso-bridge-coming-soon.php | 2 +- .../class-wc-calypso-bridge-setup-tasks.php | 4 +- 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/class-wc-calypso-bridge-dotcom-features.php b/class-wc-calypso-bridge-dotcom-features.php index 68a99555..c0890ef1 100644 --- a/class-wc-calypso-bridge-dotcom-features.php +++ b/class-wc-calypso-bridge-dotcom-features.php @@ -120,6 +120,19 @@ function wc_calypso_bridge_is_woo_express_plan() { } } +if ( ! function_exists( 'wc_calypso_bridge_is_trial_plan' ) ) { + /** + * Returns if a site is a trial plan site or not. + * + * @since x.x.x + * + * @return bool True if the site is a trial plan. + */ + function wc_calypso_bridge_is_trial_plan() { + return WC_Calypso_Bridge_DotCom_Features::is_trial_plan(); + } +} + /** * WC Calypso Bridge DotCom Features class. */ @@ -182,6 +195,13 @@ class WC_Calypso_Bridge_DotCom_Features { */ protected static $is_business_plan = null; + /** + * Is Trial plan. + * + * @var bool + */ + protected static $is_trial_plan = null; + /** * Determine if site has a WordPress.com eCommerce plan and cache the result. * @@ -340,4 +360,49 @@ public static function is_business_plan() { return self::$is_business_plan; } + + public static function is_trial_plan() { + if ( is_null( self::$is_trial_plan ) ) { + self::$is_trial_plan = self::is_ecommerce_trial_plan() + // Business trial plans + || self::has_plan( 'wp-bundle-hosting-trial', true ) + || self::has_plan( 'wp-bundle-migration-trial', true ); + } + + return self::$is_trial_plan; + } + + /** + * Check if the site has a specific plan. + * + * @param string $plan The plan to check for. + * @param bool $exact_one_plan If true, the site must have exactly one plan. + * + * @return bool True if the site has the plan, false otherwise. + */ + private static function has_plan( $plan, $exact_one_plan = false ) { + if ( ! function_exists( 'wpcom_get_site_purchases' ) ) { + return false; + } + + $all_site_purchases = wpcom_get_site_purchases(); + if ( ! is_array( $all_site_purchases ) ) { + return false; + } + + $bundles = wp_list_filter( $all_site_purchases, array( 'product_type' => 'bundle' ) ); + + if ( $exact_one_plan && count( $bundles ) !== 1 ) { + return false; + } + + foreach ( $bundles as $bundle ) { + if ( isset( $bundle->billing_product_slug ) && $bundle->billing_product_slug === $plan ) { + return true; + } + } + + return false; + } + } diff --git a/includes/class-wc-calypso-bridge-admin-settings.php b/includes/class-wc-calypso-bridge-admin-settings.php index 62c21084..c1d79509 100644 --- a/includes/class-wc-calypso-bridge-admin-settings.php +++ b/includes/class-wc-calypso-bridge-admin-settings.php @@ -35,6 +35,10 @@ final public static function get_instance() { * Constructor. */ public function __construct() { + if ( ! is_admin() ) { + return; + } + add_filter( 'woocommerce_settings_tabs_array', array( $this, 'replace_settings_tab' ), PHP_INT_MAX ); } @@ -47,7 +51,7 @@ public function __construct() { public function replace_settings_tab( $tabs ) { global $current_tab; - if ( wc_calypso_bridge_is_ecommerce_trial_plan() ) { + if ( wc_calypso_bridge_is_trial_plan() ) { // Remove Site Visibility setting for the free trial plan. unset( $tabs['site-visibility'] ); diff --git a/includes/class-wc-calypso-bridge-coming-soon.php b/includes/class-wc-calypso-bridge-coming-soon.php index b2da5cb6..a51d2a52 100644 --- a/includes/class-wc-calypso-bridge-coming-soon.php +++ b/includes/class-wc-calypso-bridge-coming-soon.php @@ -86,7 +86,7 @@ public function should_exclude_lys_coming_soon( $exclude ) { */ public function override_option_woocommerce_coming_soon( $current_value ) { // Turn off coming soon mode for trial plan. - if ( wc_calypso_bridge_is_ecommerce_trial_plan() ) { + if ( wc_calypso_bridge_is_trial_plan() ) { return 'no'; } diff --git a/includes/class-wc-calypso-bridge-setup-tasks.php b/includes/class-wc-calypso-bridge-setup-tasks.php index 5bf7011e..72d62403 100644 --- a/includes/class-wc-calypso-bridge-setup-tasks.php +++ b/includes/class-wc-calypso-bridge-setup-tasks.php @@ -142,8 +142,8 @@ public function replace_tasks( $lists ) { unset( $lists['setup']->tasks[$index] ); break; case 'launch-your-store': - if ( wc_calypso_bridge_is_ecommerce_trial_plan() ){ - // Remove launch your store task. + if ( wc_calypso_bridge_is_trial_plan() ) { + // Don't show launch your store task for trial sites. unset( $lists['setup']->tasks[$index] ); } break;