← All changes
|
freemius/includes/managers/class-fs-plan-manager.php
+193
-0
51.1.2
→
51.1.87
View file →
| @@ -1,0 +1,193 @@ | ||
| 1 | +<?php | |
| 2 | + /** | |
| 3 | + * @package Freemius | |
| 4 | + * @copyright Copyright (c) 2015, Freemius, Inc. | |
| 5 | + * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 | |
| 6 | + * @since 1.0.6 | |
| 7 | + */ | |
| 8 | + | |
| 9 | + if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit; | |
| 11 | + } | |
| 12 | + | |
| 13 | + class FS_Plan_Manager { | |
| 14 | + /** | |
| 15 | + * @var FS_Plan_Manager | |
| 16 | + */ | |
| 17 | + private static $_instance; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * @return FS_Plan_Manager | |
| 21 | + */ | |
| 22 | + static function instance() { | |
| 23 | + if ( ! isset( self::$_instance ) ) { | |
| 24 | + self::$_instance = new FS_Plan_Manager(); | |
| 25 | + } | |
| 26 | + | |
| 27 | + return self::$_instance; | |
| 28 | + } | |
| 29 | + | |
| 30 | + private function __construct() { | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * @param FS_Plugin_License[] $licenses | |
| 35 | + * | |
| 36 | + * @return bool | |
| 37 | + */ | |
| 38 | + function has_premium_license( $licenses ) { | |
| 39 | + if ( is_array( $licenses ) ) { | |
| 40 | + /** | |
| 41 | + * @var FS_Plugin_License[] $licenses | |
| 42 | + */ | |
| 43 | + foreach ( $licenses as $license ) { | |
| 44 | + if ( ! $license->is_utilized() && $license->is_features_enabled() ) { | |
| 45 | + return true; | |
| 46 | + } | |
| 47 | + } | |
| 48 | + } | |
| 49 | + | |
| 50 | + return false; | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Check if plugin has any paid plans. | |
| 55 | + * | |
| 56 | + * @author Vova Feldman (@svovaf) | |
| 57 | + * @since 1.0.7 | |
| 58 | + * | |
| 59 | + * @param FS_Plugin_Plan[] $plans | |
| 60 | + * | |
| 61 | + * @return bool | |
| 62 | + */ | |
| 63 | + function has_paid_plan( $plans ) { | |
| 64 | + if ( ! is_array( $plans ) || 0 === count( $plans ) ) { | |
| 65 | + return false; | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * @var FS_Plugin_Plan[] $plans | |
| 70 | + */ | |
| 71 | + for ( $i = 0, $len = count( $plans ); $i < $len; $i ++ ) { | |
| 72 | + if ( ! $plans[ $i ]->is_free() ) { | |
| 73 | + return true; | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + return false; | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * Check if plugin has any free plan, or is it premium only. | |
| 82 | + * | |
| 83 | + * Note: If no plans configured, assume plugin is free. | |
| 84 | + * | |
| 85 | + * @author Vova Feldman (@svovaf) | |
| 86 | + * @since 1.0.7 | |
| 87 | + * | |
| 88 | + * @param FS_Plugin_Plan[] $plans | |
| 89 | + * | |
| 90 | + * @return bool | |
| 91 | + */ | |
| 92 | + function has_free_plan( $plans ) { | |
| 93 | + if ( ! is_array( $plans ) || 0 === count( $plans ) ) { | |
| 94 | + return true; | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * @var FS_Plugin_Plan[] $plans | |
| 99 | + */ | |
| 100 | + for ( $i = 0, $len = count( $plans ); $i < $len; $i ++ ) { | |
| 101 | + if ( $plans[ $i ]->is_free() ) { | |
| 102 | + return true; | |
| 103 | + } | |
| 104 | + } | |
| 105 | + | |
| 106 | + return false; | |
| 107 | + } | |
| 108 | + | |
| 109 | + /** | |
| 110 | + * Find all plans that have trial. | |
| 111 | + * Since 2.6.2 call get_filtered_plan | |
| 112 | + * | |
| 113 | + * @author Vova Feldman (@svovaf) | |
| 114 | + * @since 1.0.9 | |
| 115 | + * | |
| 116 | + * @param FS_Plugin_Plan[] $plans | |
| 117 | + * | |
| 118 | + * @return FS_Plugin_Plan[] | |
| 119 | + */ | |
| 120 | + function get_trial_plans( $plans ) { | |
| 121 | + return $this->get_filtered_plans( $plans, true ); | |
| 122 | + } | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * Find all plans that are not hidden and have trial. | |
| 126 | + * | |
| 127 | + * @author Daniele Alessandra (@danielealessandra) | |
| 128 | + * | |
| 129 | + * @param FS_Plugin_Plan[] $plans | |
| 130 | + * | |
| 131 | + * @return FS_Plugin_Plan[] | |
| 132 | + * @since 2.6.3 | |
| 133 | + * | |
| 134 | + */ | |
| 135 | + function get_visible_trial_plans( $plans ) { | |
| 136 | + return $this->get_filtered_plans( $plans, true, true ); | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * Find all plans filtered by trial or visibility. | |
| 141 | + * | |
| 142 | + * @author Daniele Alessandra (@danielealessandra) | |
| 143 | + * | |
| 144 | + * @param FS_Plugin_Plan[] $plans | |
| 145 | + * @param boolean $should_have_trials | |
| 146 | + * @param boolean $should_be_visible | |
| 147 | + * | |
| 148 | + * @return FS_Plugin_Plan[] | |
| 149 | + * @since 2.6.3 | |
| 150 | + * | |
| 151 | + */ | |
| 152 | + function get_filtered_plans( $plans, $should_have_trials = false, $should_be_visible = false ) { | |
| 153 | + $filtered_plans = array(); | |
| 154 | + | |
| 155 | + if ( is_array( $plans ) && count( $plans ) > 0 ) { | |
| 156 | + foreach ( $plans as $plan ) { | |
| 157 | + if ( ( $should_have_trials && ! $plan->has_trial() ) || ( $should_be_visible && $plan->is_hidden ) ) { | |
| 158 | + continue; | |
| 159 | + } | |
| 160 | + $filtered_plans[] = $plan; | |
| 161 | + } | |
| 162 | + } | |
| 163 | + | |
| 164 | + return $filtered_plans; | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Check if plugin has any trial plan. | |
| 169 | + * | |
| 170 | + * @author Vova Feldman (@svovaf) | |
| 171 | + * @since 1.0.9 | |
| 172 | + * | |
| 173 | + * @param FS_Plugin_Plan[] $plans | |
| 174 | + * | |
| 175 | + * @return bool | |
| 176 | + */ | |
| 177 | + function has_trial_plan( $plans ) { | |
| 178 | + if ( ! is_array( $plans ) || 0 === count( $plans ) ) { | |
| 179 | + return true; | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * @var FS_Plugin_Plan[] $plans | |
| 184 | + */ | |
| 185 | + for ( $i = 0, $len = count( $plans ); $i < $len; $i ++ ) { | |
| 186 | + if ( $plans[ $i ]->has_trial() ) { | |
| 187 | + return true; | |
| 188 | + } | |
| 189 | + } | |
| 190 | + | |
| 191 | + return false; | |
| 192 | + } | |
| 193 | + } | |