freemius = $freemius ?? static function (): ?object { return function_exists( 'adaf_fs' ) ? adaf_fs() : null; }; $this->debug = $debug ?? ( defined( 'WP_DEBUG' ) && WP_DEBUG ); } /** * The site's current tier: one of FeatureMap::TIER_ORDER. */ public function get_tier(): string { if ( $this->has_dev_override() ) { return $this->tier_from_option(); } $fs = ( $this->freemius )(); if ( $fs instanceof \Freemius && $fs->is_registered() ) { return $this->tier_from_freemius( $fs ); } return self::FREE; } /** * Whether a feature is available on the current tier. A key FeatureMap * does not declare, or one whose min_tier is off the ladder, is * unavailable (fail closed) - tier_rank() would rank a typo as free. * * @param string $feature Feature key. */ public function is_feature_available( string $feature ): bool { $min_tier = FeatureMap::min_tier( $feature ); if ( null === $min_tier || ! in_array( $min_tier, FeatureMap::TIER_ORDER, true ) ) { return false; } return FeatureMap::tier_rank( $this->get_tier() ) >= FeatureMap::tier_rank( $min_tier ); } /** * Whether the dev override is on. */ private function has_dev_override(): bool { return $this->debug && in_array( wp_get_environment_type(), self::DEV_ENVIRONMENTS, true ) && 'yes' === get_option( self::DEV_OVERRIDE_OPTION, 'no' ); } /** * Tier from a registered Freemius site. * * Freemius' is_paying() is false for the whole of a trial, and during a * no-card trial the licensed plan can still be free - so a trial is read * from is_trial_plan(), never from is_plan(). Both checks are exact: the * SDK's non-exact is_trial_plan() is true for any plan missing from its * plan list, and it appends a freshly fetched trial plan after the * others, so non-exact ordering could promote a Pro trial to Agency. A * paying or trialing site on a plan this build does not know gets the * lowest paid tier. * * @param \Freemius $fs Registered Freemius instance. */ private function tier_from_freemius( \Freemius $fs ): string { if ( $fs->is_paying() ) { return $this->highest_tier( static function ( string $tier ) use ( $fs ): bool { return (bool) $fs->is_plan( $tier, true ); } ); } if ( $fs->is_trial() ) { return $this->highest_tier( static function ( string $tier ) use ( $fs ): bool { return (bool) $fs->is_trial_plan( $tier, true ); } ); } return self::FREE; } /** * The highest paid tier the site holds, or the lowest paid tier when it * holds none this build knows. * * @param Closure(string): bool $holds Whether the site holds a tier's plan. */ private function highest_tier( Closure $holds ): string { foreach ( self::PAID_TIERS_DESCENDING as $tier ) { if ( $holds( $tier ) ) { return $tier; } } return self::PRO; } /** * Tier from the stored option, under the dev override only; anything off * the ladder is free. */ private function tier_from_option(): string { $value = get_option( self::TIER_OPTION, self::FREE ); return is_string( $value ) && in_array( $value, FeatureMap::TIER_ORDER, true ) ? $value : self::FREE; } }