Skip to content

Commit

Permalink
[fix] [hidden-plans] [notices] Added is_hidden property to `FS_Plug…
Browse files Browse the repository at this point in the history
…in_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.
  • Loading branch information
DanieleAlessandra committed Mar 11, 2024
1 parent e79b974 commit 3f26ef4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 27 deletions.
2 changes: 1 addition & 1 deletion includes/class-freemius.php
Original file line number Diff line number Diff line change
Expand Up @@ -24102,7 +24102,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 ) {
Expand Down
4 changes: 4 additions & 0 deletions includes/entities/class-fs-plugin-plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
79 changes: 53 additions & 26 deletions includes/managers/class-fs-plan-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,59 @@ function has_free_plan( $plans ) {
return false;
}

/**
* Find all plans that have trial.
*
* @author Vova Feldman (@svovaf)
* @since 1.0.9
*
* @param FS_Plugin_Plan[] $plans
*
* @return FS_Plugin_Plan[]
*/
function get_trial_plans( $plans ) {
$trial_plans = array();

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 ];
}
}
}

return $trial_plans;
}
/**
* Find all plans that have trial.
* Since 2.6.2 call get_filtered_plan
*
* @author Vova Feldman (@svovaf)
* @since 1.0.9
*
* @param FS_Plugin_Plan[] $plans
*
* @return FS_Plugin_Plan[]
*/
function get_trial_plans( $plans ) {
return $this->get_filtered_plans($plans, true);
}

/**
* Find all plans that are not hidden and have trial.
*
* @author Daniele Alessandra (@danielealessandra)
* @since 2.6.3
*
* @param FS_Plugin_Plan[] $plans
*
* @return FS_Plugin_Plan[]
*/
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)
* @since 2.6.3
*
* @param FS_Plugin_Plan[] $plans
*
* @return FS_Plugin_Plan[]
*/
function get_filtered_plans($plans, $filter_trial = false, $filter_hidden = false) {
$filtered_plans = array();

if (is_array($plans) && count($plans) > 0) {
foreach ($plans as $plan) {
if (($filter_trial && !$plan->has_trial()) || ($filter_hidden && $plan->is_hidden)) {
continue;
}
$filtered_plans[] = $plan;
}
}

return $filtered_plans;
}

/**
* Check if plugin has any trial plan.
Expand Down

0 comments on commit 3f26ef4

Please sign in to comment.