AddonBridge.php
4 months ago
AddonLicenseAdapter.php
4 months ago
LegacyBridge.php
4 months ago
LicenseRegistry.php
3 months ago
PluginLicenseInfo.php
4 months ago
LegacyBridge.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WhatsappScoped\YayCommerce\AdminShell\Registry; |
| 4 | |
| 5 | /** |
| 6 | * Reads the legacy yaycommerce_licensing_plugins filter at priority 999 |
| 7 | * (after all legacy plugins register at priority 100) and converts each |
| 8 | * legacy registration into a PluginLicenseInfo stub with is_legacy=true. |
| 9 | * |
| 10 | * This enables the unified Licenses page to display un-migrated plugins |
| 11 | * alongside migrated ones during incremental rollout. |
| 12 | * @internal |
| 13 | */ |
| 14 | class LegacyBridge |
| 15 | { |
| 16 | private LicenseRegistry $registry; |
| 17 | public function __construct(LicenseRegistry $registry) |
| 18 | { |
| 19 | $this->registry = $registry; |
| 20 | } |
| 21 | /** |
| 22 | * Hook into plugins_loaded at priority 9999 so the registry is populated |
| 23 | * before admin_menu fires. This guarantees the Licenses submenu renders |
| 24 | * even when only un-migrated (legacy) plugins are active. |
| 25 | * |
| 26 | * Priority 9999 runs AFTER VersionedLoader::elect() at 999 (which calls |
| 27 | * AdminShell::boot() and fires yaycommerce_admin_shell_booted) AND after |
| 28 | * all legacy plugins register via yaycommerce_licensing_plugins at 100. |
| 29 | * |
| 30 | * INVARIANT: if VersionedLoader priority changes, update this priority too. |
| 31 | */ |
| 32 | public function init() : void |
| 33 | { |
| 34 | add_action('plugins_loaded', [$this, 'load_legacy_plugins'], 9999); |
| 35 | } |
| 36 | /** |
| 37 | * Read legacy filter and convert entries to PluginLicenseInfo stubs. |
| 38 | * |
| 39 | * Legacy format (each entry): |
| 40 | * [ 'slug', 'basename', 'file', 'url', 'item_id', 'dir_path', 'name' ] |
| 41 | */ |
| 42 | public function load_legacy_plugins() : void |
| 43 | { |
| 44 | $legacy_plugins = \apply_filters('yaycommerce_licensing_plugins', []); |
| 45 | foreach ($legacy_plugins as $plugin) { |
| 46 | $slug = $plugin['slug'] ?? ''; |
| 47 | if (empty($slug)) { |
| 48 | continue; |
| 49 | } |
| 50 | // Skip if already registered by migrated code. |
| 51 | if ($this->registry->has($slug)) { |
| 52 | continue; |
| 53 | } |
| 54 | $info = $this->build_legacy_info($plugin); |
| 55 | $this->registry->register($info); |
| 56 | } |
| 57 | } |
| 58 | /** |
| 59 | * Build a PluginLicenseInfo from a legacy filter entry. |
| 60 | * Reads {slug}_license_info from wp_options to get current status. |
| 61 | */ |
| 62 | private function build_legacy_info(array $plugin) : PluginLicenseInfo |
| 63 | { |
| 64 | $slug = $plugin['slug'] ?? ''; |
| 65 | $raw_info = get_option($slug . '_license_info', []); |
| 66 | if (\is_string($raw_info)) { |
| 67 | $raw_info = \json_decode($raw_info, \true) ?: []; |
| 68 | } |
| 69 | $license_key = (string) get_option($slug . '_license_key', ''); |
| 70 | $expires_raw = $raw_info['expires'] ?? null; |
| 71 | $status = $this->derive_status($license_key, $raw_info); |
| 72 | $info = new PluginLicenseInfo(); |
| 73 | $info->slug = $slug; |
| 74 | $info->name = $plugin['name'] ?? $slug; |
| 75 | $info->version = ''; |
| 76 | // legacy plugins don't expose version here |
| 77 | $info->basename = $plugin['basename'] ?? ''; |
| 78 | $info->item_id = (int) ($plugin['item_id'] ?? 0); |
| 79 | $info->store_link = $plugin['url'] ?? ''; |
| 80 | $info->license_key = $license_key; |
| 81 | $info->status = $status; |
| 82 | $info->expires_at = $expires_raw && 'Not updated' !== $expires_raw ? $expires_raw : null; |
| 83 | $info->activations_used = (int) ($raw_info['site_count'] ?? 0); |
| 84 | $info->activations_limit = (int) ($raw_info['license_limit'] ?? 0); |
| 85 | $info->is_legacy = \true; |
| 86 | $info->raw_info = $raw_info; |
| 87 | return $info; |
| 88 | } |
| 89 | /** |
| 90 | * Derive a normalized status string from legacy option data. |
| 91 | */ |
| 92 | private function derive_status(string $license_key, array $raw_info) : string |
| 93 | { |
| 94 | if (empty($license_key)) { |
| 95 | return 'inactive'; |
| 96 | } |
| 97 | $expires = $raw_info['expires'] ?? null; |
| 98 | if ($expires && 'lifetime' !== $expires && 'Not updated' !== $expires) { |
| 99 | if (\strtotime($expires) < \time()) { |
| 100 | return 'expired'; |
| 101 | } |
| 102 | } |
| 103 | return 'valid'; |
| 104 | } |
| 105 | } |
| 106 |