| @@ -10,18 +10,23 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | declare (strict_types=1); |
| 12 | 12 | namespace WindPress\WindPress; |
| 13 | 13 | |
| 14 | -use WindPressPackages\EDD_SL\PluginUpdater; | |
| 14 | +use EasyDigitalDownloads\Updater\Registry; | |
| 15 | 15 | use Exception; |
| 16 | 16 | use WIND_PRESS; |
| 17 | -use WP_Upgrader; | |
| 17 | +use WindPress\WindPress\Abilities\Loader as AbilitiesLoader; | |
| 18 | 18 | use WindPress\WindPress\Admin\AdminPage; |
| 19 | 19 | use WindPress\WindPress\Api\Router as ApiRouter; |
| 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; | |
| 24 | +use WindPress\WindPress\Upgrade\UpgradeManager; | |
| 25 | +use WindPress\WindPress\Utils\Cache as UtilsCache; | |
| 22 | 26 | use WindPress\WindPress\Utils\Common; |
| 23 | 27 | use WindPress\WindPress\Utils\Notice; |
| 28 | +use WP_Upgrader; | |
| 24 | 29 | /** |
| 25 | 30 | * Manage the plugin lifecycle and provides a single point of entry to the plugin. |
| 26 | 31 | * |
| 27 | 32 | * @since 3.0.0 |
| @@ -28,12 +33,12 @@ | ||
| 28 | 33 | */ |
| 29 | 34 | final class Plugin |
| 30 | 35 | { |
| 31 | 36 | /** |
| 32 | - * Easy Digital Downloads Software Licensing integration wrapper. | |
| 37 | + * Easy Digital Downloads Software Licensing integration manager. | |
| 33 | 38 | * Pro version only. |
| 34 | 39 | * |
| 35 | - * @var PluginUpdater|null | |
| 40 | + * @var LicenseManager|null | |
| 36 | 41 | */ |
| 37 | 42 | public $plugin_updater = null; |
| 38 | 43 | /** |
| 39 | 44 | * Stores the instance, implementing a Singleton pattern. |
| @@ -66,9 +71,9 @@ | ||
| 66 | 71 | * instance. On the first run, it creates a singleton object and places it |
| 67 | 72 | * into the static property. On subsequent runs, it returns the client existing |
| 68 | 73 | * object stored in the static property. |
| 69 | 74 | */ |
| 70 | - public static function get_instance() : self | |
| 75 | + public static function get_instance(): self | |
| 71 | 76 | { |
| 72 | 77 | if (!isset(self::$instance)) { |
| 73 | 78 | self::$instance = new self(); |
| 74 | 79 | } |
| @@ -78,82 +83,104 @@ | ||
| 78 | 83 | * Boot the plugin. |
| 79 | 84 | * |
| 80 | 85 | * @link https://codex.wordpress.org/Plugin_API/Action_Reference |
| 81 | 86 | */ |
| 82 | - public function boot() : void | |
| 87 | + public function boot(): void | |
| 83 | 88 | { |
| 84 | - \do_action('a!windpress/plugin:boot.start'); | |
| 89 | + do_action('a!windpress/plugin:boot.start'); | |
| 90 | + SourceRevision::register_hooks(); | |
| 91 | + $this->boot_license_sdk(); | |
| 85 | 92 | // (de)activation hooks. |
| 86 | - \register_activation_hook(WIND_PRESS::FILE, fn() => $this->activate_plugin()); | |
| 87 | - \register_deactivation_hook(WIND_PRESS::FILE, fn() => $this->deactivate_plugin()); | |
| 93 | + register_activation_hook(WIND_PRESS::FILE, fn() => $this->activate_plugin()); | |
| 94 | + register_deactivation_hook(WIND_PRESS::FILE, fn() => $this->deactivate_plugin()); | |
| 88 | 95 | // upgrade hooks. |
| 89 | - \add_action('upgrader_process_complete', function (WP_Upgrader $wpUpgrader, array $options) : void { | |
| 96 | + add_action('upgrader_process_complete', function (WP_Upgrader $wpUpgrader, array $options): void { | |
| 90 | 97 | if ($options['action'] === 'update' && $options['type'] === 'plugin') { |
| 91 | 98 | foreach ($options['plugins'] as $plugin) { |
| 92 | - if ($plugin === \plugin_basename(WIND_PRESS::FILE)) { | |
| 99 | + if ($plugin === plugin_basename(WIND_PRESS::FILE)) { | |
| 93 | 100 | $this->upgrade_plugin(); |
| 94 | 101 | } |
| 95 | 102 | } |
| 96 | 103 | } |
| 97 | 104 | }, 10, 2); |
| 98 | - $this->maybe_update_plugin(); | |
| 99 | - \add_action('plugins_loaded', fn() => $this->plugins_loaded(), 9); | |
| 100 | - \add_action('init', fn() => $this->init_plugin()); | |
| 101 | - \do_action('a!windpress/plugin:boot.end'); | |
| 105 | + add_action('plugins_loaded', fn() => $this->plugins_loaded(), 9); | |
| 106 | + add_action('init', fn() => $this->init_plugin()); | |
| 107 | + do_action('a!windpress/plugin:boot.end'); | |
| 102 | 108 | } |
| 103 | 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 | + /** | |
| 104 | 122 | * Handle the plugin's activation by (maybe) running database migrations |
| 105 | 123 | * and initializing the plugin configuration. |
| 106 | 124 | */ |
| 107 | - private function activate_plugin() : void | |
| 125 | + private function activate_plugin(): void | |
| 108 | 126 | { |
| 109 | - \do_action('a!windpress/plugin:activate_plugin.start'); | |
| 110 | - \update_option(WIND_PRESS::WP_OPTION . '_version', WIND_PRESS::VERSION); | |
| 111 | - $this->maybe_embedded_license(); | |
| 112 | - \do_action('a!windpress/plugin:activate_plugin.end'); | |
| 127 | + do_action('a!windpress/plugin:activate_plugin.start'); | |
| 128 | + update_option(WIND_PRESS::WP_OPTION . '_version', WIND_PRESS::VERSION); | |
| 129 | + if ($this->plugin_updater instanceof LicenseManager) { | |
| 130 | + $this->maybe_embedded_license(); | |
| 131 | + $this->plugin_updater->clear_cache(); | |
| 132 | + } | |
| 133 | + do_action('a!windpress/plugin:activate_plugin.end'); | |
| 113 | 134 | } |
| 114 | 135 | /** |
| 115 | 136 | * Handle plugin's deactivation by (maybe) cleaning up after ourselves. |
| 116 | 137 | */ |
| 117 | - private function deactivate_plugin() : void | |
| 138 | + private function deactivate_plugin(): void | |
| 118 | 139 | { |
| 119 | - \do_action('a!windpress/plugin:deactivate_plugin.start'); | |
| 120 | - \do_action('a!windpress/plugin:deactivate_plugin.end'); | |
| 140 | + do_action('a!windpress/plugin:deactivate_plugin.start'); | |
| 141 | + do_action('a!windpress/plugin:deactivate_plugin.end'); | |
| 121 | 142 | } |
| 122 | 143 | /** |
| 123 | 144 | * Handle the plugin's upgrade by (maybe) running database migrations. |
| 124 | 145 | */ |
| 125 | - private function upgrade_plugin() : void | |
| 146 | + private function upgrade_plugin(): void | |
| 126 | 147 | { |
| 127 | - \do_action('a!windpress/plugin:upgrade_plugin.start'); | |
| 128 | - \do_action('a!windpress/plugin:upgrade_plugin.end'); | |
| 148 | + do_action('a!windpress/plugin:upgrade_plugin.start'); | |
| 149 | + UpgradeManager::get_instance()->run(); | |
| 150 | + do_action('a!windpress/plugin:upgrade_plugin.end'); | |
| 129 | 151 | } |
| 130 | 152 | /** |
| 131 | 153 | * Initialize the plugin on WordPress' `init` hook. |
| 132 | 154 | */ |
| 133 | - private function init_plugin() : void | |
| 155 | + private function init_plugin(): void | |
| 134 | 156 | { |
| 135 | - \do_action('a!windpress/plugin:init_plugin.start'); | |
| 157 | + do_action('a!windpress/plugin:init_plugin.start'); | |
| 136 | 158 | // Load translations. |
| 137 | - // load_plugin_textdomain(WIND_PRESS::TEXT_DOMAIN, false, dirname(plugin_basename(WIND_PRESS::FILE)) . '/languages/'); | |
| 159 | + if (defined('WindPressDeps\WIND_PRESS_LOAD_TEXT_DOMAIN') && constant('WIND_PRESS_LOAD_TEXT_DOMAIN')) { | |
| 160 | + load_plugin_textdomain(WIND_PRESS::TEXT_DOMAIN, \false, dirname(plugin_basename(WIND_PRESS::FILE)) . '/languages/'); | |
| 161 | + } | |
| 138 | 162 | new ApiRouter(); |
| 139 | 163 | Runtime::get_instance()->init(); |
| 140 | 164 | // Instantiate the AdminPage class. |
| 141 | 165 | new AdminPage(); |
| 142 | - \do_action('a!windpress/plugin:init_plugin.end'); | |
| 166 | + UtilsCache::exclude_optimization(); | |
| 167 | + // Initialize Abilities API integration | |
| 168 | + AbilitiesLoader::get_instance()->init(); | |
| 169 | + do_action('a!windpress/plugin:init_plugin.end'); | |
| 143 | 170 | } |
| 144 | 171 | /** |
| 145 | 172 | * Initialize the plugin on WordPress' `plugins_loaded` hook. |
| 146 | 173 | */ |
| 147 | - private function plugins_loaded() : void | |
| 174 | + private function plugins_loaded(): void | |
| 148 | 175 | { |
| 149 | - \do_action('a!windpress/plugin:plugins_loaded.start'); | |
| 176 | + do_action('a!windpress/plugin:plugins_loaded.start'); | |
| 150 | 177 | IntegrationLoader::get_instance()->register_integrations(); |
| 151 | - if (\is_admin()) { | |
| 152 | - \add_action('admin_notices', static fn() => Notice::admin_notices()); | |
| 153 | - \add_filter('plugin_action_links_' . \plugin_basename(WIND_PRESS::FILE), fn($links) => $this->plugin_action_links($links)); | |
| 178 | + if (is_admin()) { | |
| 179 | + add_action('admin_notices', static fn() => Notice::admin_notices()); | |
| 180 | + add_filter('plugin_action_links_' . plugin_basename(WIND_PRESS::FILE), fn($links) => $this->plugin_action_links($links)); | |
| 154 | 181 | } |
| 155 | - \do_action('a!windpress/plugin:plugins_loaded.end'); | |
| 182 | + do_action('a!windpress/plugin:plugins_loaded.end'); | |
| 156 | 183 | } |
| 157 | 184 | /** |
| 158 | 185 | * Add plugin action links. |
| 159 | 186 | * |
| @@ -161,56 +188,92 @@ | ||
| 161 | 188 | * @return array Plugin action links. |
| 162 | 189 | * |
| 163 | 190 | * @todo Add settings link when the settings page is implemented. |
| 164 | 191 | */ |
| 165 | - private function plugin_action_links(array $links) : array | |
| 192 | + private function plugin_action_links(array $links): array | |
| 166 | 193 | { |
| 167 | 194 | $base_url = AdminPage::get_page_url(); |
| 168 | - \array_unshift($links, \sprintf('<a href="%s">%s</a>', \esc_url(\sprintf('%s#/settings', $base_url)), \esc_html__('Settings', 'windpress'))); | |
| 195 | + array_unshift($links, sprintf('<a href="%s">%s</a>', esc_url(sprintf('%s#/settings', $base_url)), esc_html__('Settings', 'windpress'))); | |
| 169 | 196 | if (!Common::is_updater_library_available()) { |
| 170 | - \array_unshift($links, \sprintf('<a href="%s" style="color:#067b34;font-weight:600;" target="_blank">%s</a>', \esc_url(Common::plugin_data('PluginURI') . '?utm_source=WordPress&utm_campaign=liteplugin&utm_medium=plugin-action-links&utm_content=Upgrade#pricing'), \esc_html__('Upgrade to Pro', 'windpress'))); | |
| 197 | + array_unshift($links, sprintf('<a href="%s" style="color:#067b34;font-weight:600;" target="_blank">%s</a>', esc_url(Common::plugin_data('PluginURI') . '?utm_source=WordPress&utm_campaign=liteplugin&utm_medium=plugin-action-links&utm_content=Upgrade#pricing'), esc_html__('Upgrade to Pro', 'windpress'))); | |
| 171 | 198 | } |
| 172 | 199 | return $links; |
| 173 | 200 | } |
| 174 | 201 | /** |
| 175 | - * Initialize the plugin updater. | |
| 176 | - * Pro version only. | |
| 177 | - * | |
| 178 | - * @return PluginUpdater | |
| 202 | + * Register the plugin with EDD's official Software Licensing SDK. | |
| 179 | 203 | */ |
| 180 | - public function maybe_update_plugin() | |
| 204 | + private function boot_license_sdk(): void | |
| 181 | 205 | { |
| 182 | - if (!\class_exists(PluginUpdater::class)) { | |
| 183 | - return null; | |
| 206 | + $sdk_path = self::get_license_sdk_path(); | |
| 207 | + if (!is_file($sdk_path)) { | |
| 208 | + return; | |
| 184 | 209 | } |
| 185 | - if ($this->plugin_updater instanceof \WindPressPackages\EDD_SL\PluginUpdater) { | |
| 186 | - 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()); | |
| 187 | 219 | } |
| 188 | - $license = \get_option(WIND_PRESS::WP_OPTION . '_license', ['key' => '', 'opt_in_pre_release' => \false]); | |
| 189 | - $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']]); | |
| 190 | - return $this->plugin_updater; | |
| 191 | 220 | } |
| 192 | 221 | /** |
| 222 | + * @param mixed $registry | |
| 223 | + */ | |
| 224 | + private function register_license_integration($registry): void | |
| 225 | + { | |
| 226 | + if (!$registry instanceof Registry) { | |
| 227 | + return; | |
| 228 | + } | |
| 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 | + } | |
| 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 | + } | |
| 243 | + /** | |
| 193 | 244 | * Check if the plugin distributed with an embedded license and activate the license. |
| 194 | 245 | * Pro version only. |
| 195 | 246 | */ |
| 196 | - private function maybe_embedded_license() : void | |
| 247 | + private function maybe_embedded_license(): void | |
| 197 | 248 | { |
| 198 | - if (!\class_exists(PluginUpdater::class)) { | |
| 249 | + if (!$this->plugin_updater instanceof LicenseManager) { | |
| 199 | 250 | return; |
| 200 | 251 | } |
| 201 | - $license_file = \dirname(WIND_PRESS::FILE) . '/license-data.php'; | |
| 202 | - if (!\file_exists($license_file)) { | |
| 252 | + $license_file = dirname(WIND_PRESS::FILE) . '/license-data.php'; | |
| 253 | + if (!file_exists($license_file)) { | |
| 203 | 254 | return; |
| 204 | 255 | } |
| 205 | 256 | require_once $license_file; |
| 206 | - $const_name = 'ROSUA_EMBEDDED_LICENSE_KEY_' . WIND_PRESS::EDD_STORE['item_id']; | |
| 207 | - 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) { | |
| 208 | 271 | return; |
| 209 | 272 | } |
| 210 | - $license_key = \constant($const_name); | |
| 211 | - \update_option(WIND_PRESS::WP_OPTION . '_license', ['key' => $license_key, 'opt_in_pre_release' => \false]); | |
| 212 | - \wp_delete_file($license_file); | |
| 273 | + $license_key = constant($const_name); | |
| 274 | + update_option(WIND_PRESS::WP_OPTION . '_license', ['key' => $license_key, 'opt_in_pre_release' => \false]); | |
| 275 | + wp_delete_file($license_file); | |
| 213 | 276 | // activate the license. |
| 214 | - $this->maybe_update_plugin()->activate($license_key); | |
| 277 | + $this->plugin_updater->activate((string) $license_key); | |
| 215 | 278 | } |
| 216 | 279 | } |