From c5d6acba8c3278cbc2624e5d9cbca9e187ec225f Mon Sep 17 00:00:00 2001 From: Daniele Alessandra Date: Wed, 27 Mar 2024 14:42:31 +0100 Subject: [PATCH] [fix] [hidden-plans] [notices] Added `is_hidden` property to `FS_Plugin_Plan`. Added `get_filtered_plans`and `get_visible_trial_plans` to `FS_Plan_Manager`. Modified `_add_trial_notice` in `Freemius` to use only visible plan with trials. --- includes/class-freemius.php | 2 +- includes/entities/class-fs-plugin-plan.php | 4 ++ includes/managers/class-fs-plan-manager.php | 49 +++++++++++++++++---- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/includes/class-freemius.php b/includes/class-freemius.php index 84a8337ec..020c97946 100755 --- a/includes/class-freemius.php +++ b/includes/class-freemius.php @@ -24110,7 +24110,7 @@ function _add_trial_notice() { if ( $this->is_registered() ) { // If opted-in, override trial with up to date data from API. - $trial_plans = FS_Plan_Manager::instance()->get_trial_plans( $this->_plans ); + $trial_plans = FS_Plan_Manager::instance()->get_visible_trial_plans( $this->_plans ); $trial_plans_count = count( $trial_plans ); if ( 0 === $trial_plans_count ) { diff --git a/includes/entities/class-fs-plugin-plan.php b/includes/entities/class-fs-plugin-plan.php index 00a0d747b..5bc6bc2d4 100755 --- a/includes/entities/class-fs-plugin-plan.php +++ b/includes/entities/class-fs-plugin-plan.php @@ -88,6 +88,10 @@ class FS_Plugin_Plan extends FS_Entity { * @var bool Is featured plan. */ public $is_featured; + /** + * @var bool Is hidden plan. + */ + public $is_hidden; #endregion Properties diff --git a/includes/managers/class-fs-plan-manager.php b/includes/managers/class-fs-plan-manager.php index 639de43e3..9becceaff 100755 --- a/includes/managers/class-fs-plan-manager.php +++ b/includes/managers/class-fs-plan-manager.php @@ -108,6 +108,7 @@ function has_free_plan( $plans ) { /** * Find all plans that have trial. + * Since 2.6.2 call get_filtered_plan * * @author Vova Feldman (@svovaf) * @since 1.0.9 @@ -117,20 +118,50 @@ function has_free_plan( $plans ) { * @return FS_Plugin_Plan[] */ function get_trial_plans( $plans ) { - $trial_plans = array(); + return $this->get_filtered_plans( $plans, true ); + } - if ( is_array( $plans ) && 0 < count( $plans ) ) { - /** - * @var FS_Plugin_Plan[] $plans - */ - for ( $i = 0, $len = count( $plans ); $i < $len; $i ++ ) { - if ( $plans[ $i ]->has_trial() ) { - $trial_plans[] = $plans[ $i ]; + /** + * Find all plans that are not hidden and have trial. + * + * @author Daniele Alessandra (@danielealessandra) + * + * @param FS_Plugin_Plan[] $plans + * + * @return FS_Plugin_Plan[] + * @since 2.6.3 + * + */ + function get_visible_trial_plans( $plans ) { + return $this->get_filtered_plans( $plans, true, true ); + } + + /** + * Find all plans filtered by trial or visibility. + * + * @author Daniele Alessandra (@danielealessandra) + * + * @param FS_Plugin_Plan[] $plans + * @param boolean $should_have_trials + * @param boolean $should_be_visible + * + * @return FS_Plugin_Plan[] + * @since 2.6.3 + * + */ + function get_filtered_plans( $plans, $should_have_trials = false, $should_be_visible = false ) { + $filtered_plans = array(); + + if ( is_array( $plans ) && count( $plans ) > 0 ) { + foreach ( $plans as $plan ) { + if ( ( $should_have_trials && ! $plan->has_trial() ) || ( $should_be_visible && $plan->is_hidden ) ) { + continue; } + $filtered_plans[] = $plan; } } - return $trial_plans; + return $filtered_plans; } /**