| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tier registry — single source of truth for whether a module is |
| 4 |
* available on the current install (Free always available; Pro available |
| 5 |
* only when xspeed-pro is active and reports a compatible API version). |
| 6 |
* |
| 7 |
* Pro features are loaded by the xspeed-pro plugin by hooking |
| 8 |
* `xspeed_register_modules` on `plugins_loaded` with priority later than |
| 9 |
* core (which registers Free modules at priority 10). Pro modules pass |
| 10 |
* through the same Module_Registry — there is no separate Pro registry. |
| 11 |
* |
| 12 |
* The `xspeed_module_tier` filter lets a site override a module's |
| 13 |
* declared tier (e.g., promote a Pro feature to Free for a custom build, |
| 14 |
* or beta-test a Pro feature without moving files). In stock installs no |
| 15 |
* filter fires, so the declared TIER constant wins. |
| 16 |
* |
| 17 |
* @package XSpeed |
| 18 |
*/ |
| 19 |
|
| 20 |
namespace XSpeed; |
| 21 |
|
| 22 |
defined( 'ABSPATH' ) || exit; |
| 23 |
|
| 24 |
final class Tier_Registry { |
| 25 |
|
| 26 |
/** |
| 27 |
* Pro plugin signals it's loaded + compatible by defining this constant |
| 28 |
* at its main entry point. The integer is the minimum Free-API version |
| 29 |
* the Pro plugin expects. If we ever break the Module/Registry contract |
| 30 |
* we bump XSPEED_API_VERSION; older Pro installs short-circuit safely. |
| 31 |
*/ |
| 32 |
public const PRO_LOADED_CONSTANT = 'XSPEED_PRO_API'; |
| 33 |
public const API_VERSION = 1; |
| 34 |
|
| 35 |
/** |
| 36 |
* Return the effective tier of a module — what the registry sees after |
| 37 |
* the `xspeed_module_tier` filter runs. Falls back to the module's |
| 38 |
* declared TIER if no filter overrides. |
| 39 |
*/ |
| 40 |
public static function tier_of( Module $module ): string { |
| 41 |
$declared = $module->tier(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Filter: xspeed_module_tier |
| 45 |
* |
| 46 |
* @param string $tier Currently declared tier ('free' | 'pro'). |
| 47 |
* @param string $slug Module slug. |
| 48 |
* @param Module $module The module instance. |
| 49 |
*/ |
| 50 |
$filtered = apply_filters( 'xspeed_module_tier', $declared, $module->slug(), $module ); |
| 51 |
|
| 52 |
return in_array( $filtered, array( Module::TIER_FREE, Module::TIER_PRO ), true ) |
| 53 |
? $filtered |
| 54 |
: $declared; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* A module is available on the current install iff: |
| 59 |
* - Its effective tier is Free, OR |
| 60 |
* - Its effective tier is Pro AND the Pro plugin is active + compatible. |
| 61 |
*/ |
| 62 |
public static function is_available( Module $module ): bool { |
| 63 |
$tier = self::tier_of( $module ); |
| 64 |
if ( Module::TIER_FREE === $tier ) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
return self::pro_active(); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Is xspeed-pro loaded and reporting a compatible API version? |
| 72 |
* |
| 73 |
* Pro is expected to `define( 'XSPEED_PRO_API', N )` at bootstrap. We |
| 74 |
* accept any constant whose value is >= our API_VERSION (forward |
| 75 |
* compat — newer Pro on older Free is fine) and >= 1 (sanity). |
| 76 |
*/ |
| 77 |
public static function pro_active(): bool { |
| 78 |
if ( ! defined( self::PRO_LOADED_CONSTANT ) ) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
$pro_api = (int) constant( self::PRO_LOADED_CONSTANT ); |
| 82 |
return $pro_api >= self::API_VERSION; |
| 83 |
} |
| 84 |
} |
| 85 |
|