has_dev_override() ) { return $this->get_tier_from_options(); } // 2. Freemius active + registered → query plan if ( $this->is_freemius_active() ) { $fs = $this->get_freemius(); if ( $fs !== null && $fs->is_registered() ) { return $this->get_tier_from_freemius( $fs ); } } // 3. Fallback: wp_options return $this->get_tier_from_options(); } public function set_tier( string $tier ): void { if ( in_array( $tier, FeatureMap::TIER_ORDER, true ) ) { update_option( self::TIER_OPTION, $tier ); } } /** * Check if a feature is available in the current tier. * * @param string $feature Feature identifier. * @return bool */ public function is_feature_available( string $feature ): bool { $min_tier = FeatureMap::min_tier( $feature ); if ( null === $min_tier ) { // Undeclared keys are not gated. FeatureGatingSyncTest forbids // unknown keys at call sites, so this cannot hide a typo. return true; } return FeatureMap::tier_rank( $this->get_tier() ) >= FeatureMap::tier_rank( $min_tier ); } /** * Check if Freemius SDK is active and available. * * Override in test subclasses (or via Mockery partial mock) to simulate * the "Freemius not loaded" code path. The default implementation only * checks that the global helper function is defined; in tests the helper * is always defined via the stub in tests/phpstan-freemius-stub.php so * the marker-based pro code paths can be exercised. * * @return bool */ public function is_freemius_active(): bool { return $this->freemius_helper_available(); } /** * Indirection for is_freemius_active() so tests can override the * "is webcal_fs() defined?" check via Mockery partial mocks. * * @return bool */ protected function freemius_helper_available(): bool { return function_exists( 'webcal_fs' ); } /** * Get the Freemius instance, or null if unavailable. * * @return \Freemius|null */ public function get_freemius(): ?\Freemius { if ( ! $this->is_freemius_active() ) { return null; } return function_exists( 'webcal_fs' ) ? webcal_fs() : null; } /** * Check if the dev override is enabled. * * @return bool */ private function has_dev_override(): bool { if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { return false; } $value = get_option( self::DEV_OVERRIDE_OPTION, 'no' ); return is_scalar( $value ) && (string) $value === 'yes'; } /** * Determine tier from Freemius plan. * * @param \Freemius $fs Freemius instance. * @return string */ private function get_tier_from_freemius( \Freemius $fs ): string { if ( ! $fs->is_paying() ) { return self::FREE; } if ( $fs->is_plan( 'agency' ) ) { return self::AGENCY; } if ( $fs->is_plan( 'business' ) ) { return self::BUSINESS; } if ( $fs->is_plan( 'starter' ) ) { return self::STARTER; } // Paying but no recognized plan — treat as starter. return self::STARTER; } /** * Determine tier from wp_options. * * @return string */ private function get_tier_from_options(): string { $value = get_option( self::TIER_OPTION, self::FREE ); return is_scalar( $value ) ? (string) $value : self::FREE; } }