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