ReportLegacyLicences.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Give\VendorOverrides\Harbor\Actions; |
| 6 | |
| 7 | use Give\License\Repositories\LicenseRepository; |
| 8 | |
| 9 | /** |
| 10 | * Reports legacy Give licenses to Harbor by expanding each license's downloads |
| 11 | * into individual entries, so each plugin slug (e.g. "give-recurring") is |
| 12 | * associated with the correct license key. |
| 13 | * |
| 14 | * Hooked into the `stellarwp/harbor/legacy_licenses` filter. |
| 15 | * |
| 16 | * @since 4.15.0 |
| 17 | */ |
| 18 | class ReportLegacyLicences |
| 19 | { |
| 20 | /** |
| 21 | * @since 4.15.0 |
| 22 | */ |
| 23 | private LicenseRepository $licenseRepository; |
| 24 | |
| 25 | /** |
| 26 | * @since 4.15.0 |
| 27 | */ |
| 28 | public function __construct(LicenseRepository $licenseRepository) |
| 29 | { |
| 30 | $this->licenseRepository = $licenseRepository; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @since 4.15.0 |
| 35 | * @param array $licenses Existing licenses already added by other plugins. |
| 36 | * @return array |
| 37 | */ |
| 38 | public function __invoke(array $licenses): array |
| 39 | { |
| 40 | $storedLicenses = $this->licenseRepository->getLicenses(); |
| 41 | $pageUrl = admin_url('edit.php?post_type=give_forms&page=give-settings&tab=licenses'); |
| 42 | $legacyLicenses = []; |
| 43 | $coveredSlugs = []; |
| 44 | |
| 45 | foreach ($storedLicenses as $license) { |
| 46 | foreach ($license->downloads as $download) { |
| 47 | if (empty($download->pluginSlug)) { |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | $coveredSlugs[] = $download->pluginSlug; |
| 52 | |
| 53 | $entry = [ |
| 54 | 'key' => $license->licenseKey, |
| 55 | 'slug' => $download->pluginSlug, |
| 56 | 'name' => $download->name, |
| 57 | 'product' => 'give', |
| 58 | 'is_active' => $license->isActive, |
| 59 | 'page_url' => $pageUrl, |
| 60 | ]; |
| 61 | |
| 62 | if (!empty($license->expires)) { |
| 63 | $entry['expires_at'] = $license->expires; |
| 64 | } |
| 65 | |
| 66 | $legacyLicenses[] = $entry; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | foreach ($this->getUnlicensedPremiumAddons($coveredSlugs, $pageUrl) as $entry) { |
| 71 | $legacyLicenses[] = $entry; |
| 72 | } |
| 73 | |
| 74 | return array_merge($licenses, $legacyLicenses); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns entries for premium add-ons that are installed but have no license key in the database. |
| 79 | * |
| 80 | * @since 4.15.0 |
| 81 | */ |
| 82 | private function getUnlicensedPremiumAddons(array $coveredSlugs, string $pageUrl): array |
| 83 | { |
| 84 | $entries = []; |
| 85 | |
| 86 | foreach (give_get_plugins(['only_premium_add_ons' => true]) as $plugin) { |
| 87 | $slug = $plugin['Dir']; |
| 88 | |
| 89 | if (in_array($slug, $coveredSlugs, true)) { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | $entries[] = [ |
| 94 | 'key' => '', |
| 95 | 'slug' => $slug, |
| 96 | 'name' => $plugin['Name'], |
| 97 | 'product' => 'give', |
| 98 | 'is_active' => false, |
| 99 | 'page_url' => $pageUrl, |
| 100 | ]; |
| 101 | } |
| 102 | |
| 103 | return $entries; |
| 104 | } |
| 105 | } |
| 106 |