| @@ -10,16 +10,18 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | declare (strict_types=1); |
| 12 | 12 | namespace WindPress\WindPress; |
| 13 | 13 | |
| 14 | -use WindPressDeps\EDD_SL\PluginUpdater; | |
| 14 | +use EasyDigitalDownloads\Updater\Registry; | |
| 15 | 15 | use Exception; |
| 16 | 16 | use WIND_PRESS; |
| 17 | +use WindPress\WindPress\Abilities\Loader as AbilitiesLoader; | |
| 17 | 18 | use WindPress\WindPress\Admin\AdminPage; |
| 18 | 19 | use WindPress\WindPress\Api\Router as ApiRouter; |
| 19 | -use WindPress\WindPress\Abilities\Loader as AbilitiesLoader; | |
| 20 | 20 | use WindPress\WindPress\Core\Runtime; |
| 21 | +use WindPress\WindPress\Core\Scanner\SourceRevision; | |
| 21 | 22 | use WindPress\WindPress\Integration\Loader as IntegrationLoader; |
| 23 | +use WindPress\WindPress\Licensing\Manager as LicenseManager; | |
| 22 | 24 | use WindPress\WindPress\Upgrade\UpgradeManager; |
| 23 | 25 | use WindPress\WindPress\Utils\Cache as UtilsCache; |
| 24 | 26 | use WindPress\WindPress\Utils\Common; |
| 25 | 27 | use WindPress\WindPress\Utils\Notice; |
| @@ -31,12 +33,12 @@ | ||
| 31 | 33 | */ |
| 32 | 34 | final class Plugin |
| 33 | 35 | { |
| 34 | 36 | /** |
| 35 | - * Easy Digital Downloads Software Licensing integration wrapper. | |
| 37 | + * Easy Digital Downloads Software Licensing integration manager. | |
| 36 | 38 | * Pro version only. |
| 37 | 39 | * |
| 38 | - * @var PluginUpdater|null | |
| 40 | + * @var LicenseManager|null | |
| 39 | 41 | */ |
| 40 | 42 | public $plugin_updater = null; |
| 41 | 43 | /** |
| 42 | 44 | * Stores the instance, implementing a Singleton pattern. |
| @@ -84,8 +86,10 @@ | ||
| 84 | 86 | */ |
| 85 | 87 | public function boot(): void |
| 86 | 88 | { |
| 87 | 89 | do_action('a!windpress/plugin:boot.start'); |
| 90 | + SourceRevision::register_hooks(); | |
| 91 | + $this->boot_license_sdk(); | |
| 88 | 92 | // (de)activation hooks. |
| 89 | 93 | register_activation_hook(WIND_PRESS::FILE, fn() => $this->activate_plugin()); |
| 90 | 94 | register_deactivation_hook(WIND_PRESS::FILE, fn() => $this->deactivate_plugin()); |
| 91 | 95 | // upgrade hooks. |
| @@ -97,14 +101,25 @@ | ||
| 97 | 101 | } |
| 98 | 102 | } |
| 99 | 103 | } |
| 100 | 104 | }, 10, 2); |
| 101 | - $this->maybe_update_plugin(); | |
| 102 | 105 | add_action('plugins_loaded', fn() => $this->plugins_loaded(), 9); |
| 103 | 106 | add_action('init', fn() => $this->init_plugin()); |
| 104 | 107 | do_action('a!windpress/plugin:boot.end'); |
| 105 | 108 | } |
| 106 | 109 | /** |
| 110 | + * Get the initialized license manager. | |
| 111 | + * Pro version only. | |
| 112 | + */ | |
| 113 | + public function maybe_update_plugin(): ?LicenseManager | |
| 114 | + { | |
| 115 | + return $this->plugin_updater instanceof LicenseManager ? $this->plugin_updater : null; | |
| 116 | + } | |
| 117 | + public static function is_pro_edition(): bool | |
| 118 | + { | |
| 119 | + return is_file(self::get_license_sdk_path()); | |
| 120 | + } | |
| 121 | + /** | |
| 107 | 122 | * Handle the plugin's activation by (maybe) running database migrations |
| 108 | 123 | * and initializing the plugin configuration. |
| 109 | 124 | */ |
| 110 | 125 | private function activate_plugin(): void |
| @@ -110,10 +125,12 @@ | ||
| 110 | 125 | private function activate_plugin(): void |
| 111 | 126 | { |
| 112 | 127 | do_action('a!windpress/plugin:activate_plugin.start'); |
| 113 | 128 | update_option(WIND_PRESS::WP_OPTION . '_version', WIND_PRESS::VERSION); |
| 114 | - $this->maybe_new_plugin_version(); | |
| 115 | - $this->maybe_embedded_license(); | |
| 129 | + if ($this->plugin_updater instanceof LicenseManager) { | |
| 130 | + $this->maybe_embedded_license(); | |
| 131 | + $this->plugin_updater->clear_cache(); | |
| 132 | + } | |
| 116 | 133 | do_action('a!windpress/plugin:activate_plugin.end'); |
| 117 | 134 | } |
| 118 | 135 | /** |
| 119 | 136 | * Handle plugin's deactivation by (maybe) cleaning up after ourselves. |
| @@ -181,36 +198,49 @@ | ||
| 181 | 198 | } |
| 182 | 199 | return $links; |
| 183 | 200 | } |
| 184 | 201 | /** |
| 185 | - * Initialize the plugin updater. | |
| 186 | - * Pro version only. | |
| 187 | - * | |
| 188 | - * @return PluginUpdater | |
| 202 | + * Register the plugin with EDD's official Software Licensing SDK. | |
| 189 | 203 | */ |
| 190 | - private function maybe_update_plugin() | |
| 204 | + private function boot_license_sdk(): void | |
| 191 | 205 | { |
| 192 | - if (!class_exists(PluginUpdater::class)) { | |
| 193 | - return null; | |
| 206 | + $sdk_path = self::get_license_sdk_path(); | |
| 207 | + if (!is_file($sdk_path)) { | |
| 208 | + return; | |
| 194 | 209 | } |
| 195 | - if ($this->plugin_updater instanceof \WindPressDeps\EDD_SL\PluginUpdater) { | |
| 196 | - return $this->plugin_updater; | |
| 210 | + $this->plugin_updater = new LicenseManager(); | |
| 211 | + add_action('init', [$this->plugin_updater, 'boot_updater']); | |
| 212 | + add_action('edd_sl_sdk_registry', function ($registry): void { | |
| 213 | + $this->register_license_integration($registry); | |
| 214 | + }); | |
| 215 | + require_once $sdk_path; | |
| 216 | + // Plugin activation can happen after the SDK initialization hooks ran. | |
| 217 | + if (did_action('after_setup_theme') && class_exists(Registry::class)) { | |
| 218 | + $this->register_license_integration(Registry::instance()); | |
| 197 | 219 | } |
| 198 | - $license = get_option(WIND_PRESS::WP_OPTION . '_license', ['key' => '', 'opt_in_pre_release' => \false]); | |
| 199 | - $this->plugin_updater = new PluginUpdater(WIND_PRESS::WP_OPTION, ['version' => WIND_PRESS::VERSION, 'license' => $license['key'] ? trim($license['key']) : \false, 'beta' => $license['opt_in_pre_release'], 'plugin_file' => WIND_PRESS::FILE, 'item_id' => WIND_PRESS::EDD_STORE['item_id'], 'store_url' => WIND_PRESS::EDD_STORE['store_url'], 'author' => WIND_PRESS::EDD_STORE['author']]); | |
| 200 | - return $this->plugin_updater; | |
| 201 | 220 | } |
| 202 | 221 | /** |
| 203 | - * Check if the plugin has a new version by clearing the cache. | |
| 204 | - * Pro version only. | |
| 222 | + * @param mixed $registry | |
| 205 | 223 | */ |
| 206 | - private function maybe_new_plugin_version(): void | |
| 224 | + private function register_license_integration($registry): void | |
| 207 | 225 | { |
| 208 | - if (!class_exists(PluginUpdater::class)) { | |
| 226 | + if (!$registry instanceof Registry) { | |
| 209 | 227 | return; |
| 210 | 228 | } |
| 211 | - $this->maybe_update_plugin()->clear_cache(); | |
| 229 | + if (!$registry->offsetExists(LicenseManager::INTEGRATION_ID)) { | |
| 230 | + $registry->register(LicenseManager::get_integration_args()); | |
| 231 | + } | |
| 232 | + $handler = $registry->offsetGet(LicenseManager::INTEGRATION_ID); | |
| 233 | + if (is_object($handler) && method_exists($handler, 'auto_updater')) { | |
| 234 | + // The SDK registry does not pass wp_override or beta through in | |
| 235 | + // version 1.0.3, so Manager boots the official updater directly. | |
| 236 | + remove_action('init', [$handler, 'auto_updater']); | |
| 237 | + } | |
| 212 | 238 | } |
| 239 | + private static function get_license_sdk_path(): string | |
| 240 | + { | |
| 241 | + return dirname(WIND_PRESS::FILE) . '/vendor/easy-digital-downloads/edd-sl-sdk/edd-sl-sdk.php'; | |
| 242 | + } | |
| 213 | 243 | /** |
| 214 | 244 | * Check if the plugin distributed with an embedded license and activate the license. |
| 215 | 245 | * Pro version only. |
| 216 | 246 | */ |
| @@ -215,9 +245,9 @@ | ||
| 215 | 245 | * Pro version only. |
| 216 | 246 | */ |
| 217 | 247 | private function maybe_embedded_license(): void |
| 218 | 248 | { |
| 219 | - if (!class_exists(PluginUpdater::class)) { | |
| 249 | + if (!$this->plugin_updater instanceof LicenseManager) { | |
| 220 | 250 | return; |
| 221 | 251 | } |
| 222 | 252 | $license_file = dirname(WIND_PRESS::FILE) . '/license-data.php'; |
| 223 | 253 | if (!file_exists($license_file)) { |
| @@ -223,10 +253,22 @@ | ||
| 223 | 253 | if (!file_exists($license_file)) { |
| 224 | 254 | return; |
| 225 | 255 | } |
| 226 | 256 | require_once $license_file; |
| 227 | - $const_name = 'ROSUA_EMBEDDED_LICENSE_KEY_' . WIND_PRESS::EDD_STORE['item_id']; | |
| 228 | - if (!defined($const_name)) { | |
| 257 | + $const_names = [ | |
| 258 | + 'WIND_PRESS_EMBEDDED_LICENSE_KEY_' . WIND_PRESS::EDD_STORE['item_id'], | |
| 259 | + 'JOOOSI_EMBEDDED_LICENSE_KEY_' . WIND_PRESS::EDD_STORE['item_id'], | |
| 260 | + // Preserve the embedded-license constant used by previous releases. | |
| 261 | + 'ROSUA_EMBEDDED_LICENSE_KEY_' . WIND_PRESS::EDD_STORE['item_id'], | |
| 262 | + ]; | |
| 263 | + $const_name = null; | |
| 264 | + foreach ($const_names as $candidate) { | |
| 265 | + if (defined($candidate)) { | |
| 266 | + $const_name = $candidate; | |
| 267 | + break; | |
| 268 | + } | |
| 269 | + } | |
| 270 | + if ($const_name === null) { | |
| 229 | 271 | return; |
| 230 | 272 | } |
| 231 | 273 | $license_key = constant($const_name); |
| 232 | 274 | update_option(WIND_PRESS::WP_OPTION . '_license', ['key' => $license_key, 'opt_in_pre_release' => \false]); |
| @@ -231,7 +273,7 @@ | ||
| 231 | 273 | $license_key = constant($const_name); |
| 232 | 274 | update_option(WIND_PRESS::WP_OPTION . '_license', ['key' => $license_key, 'opt_in_pre_release' => \false]); |
| 233 | 275 | wp_delete_file($license_file); |
| 234 | 276 | // activate the license. |
| 235 | - $this->maybe_update_plugin()->activate($license_key); | |
| 277 | + $this->plugin_updater->activate((string) $license_key); | |
| 236 | 278 | } |
| 237 | 279 | } |