| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress; |
| 13 |
|
| 14 |
use WindPressDeps\EDD_SL\PluginUpdater; |
| 15 |
use Exception; |
| 16 |
use WIND_PRESS; |
| 17 |
use WindPress\WindPress\Admin\AdminPage; |
| 18 |
use WindPress\WindPress\Api\Router as ApiRouter; |
| 19 |
use WindPress\WindPress\Abilities\Loader as AbilitiesLoader; |
| 20 |
use WindPress\WindPress\Core\Runtime; |
| 21 |
use WindPress\WindPress\Integration\Loader as IntegrationLoader; |
| 22 |
use WindPress\WindPress\Upgrade\UpgradeManager; |
| 23 |
use WindPress\WindPress\Utils\Cache as UtilsCache; |
| 24 |
use WindPress\WindPress\Utils\Common; |
| 25 |
use WindPress\WindPress\Utils\Notice; |
| 26 |
use WP_Upgrader; |
| 27 |
/** |
| 28 |
* Manage the plugin lifecycle and provides a single point of entry to the plugin. |
| 29 |
* |
| 30 |
* @since 3.0.0 |
| 31 |
*/ |
| 32 |
final class Plugin |
| 33 |
{ |
| 34 |
/** |
| 35 |
* Easy Digital Downloads Software Licensing integration wrapper. |
| 36 |
* Pro version only. |
| 37 |
* |
| 38 |
* @var PluginUpdater|null |
| 39 |
*/ |
| 40 |
public $plugin_updater = null; |
| 41 |
/** |
| 42 |
* Stores the instance, implementing a Singleton pattern. |
| 43 |
*/ |
| 44 |
private static self $instance; |
| 45 |
/** |
| 46 |
* The Singleton's constructor should always be private to prevent direct |
| 47 |
* construction calls with the `new` operator. |
| 48 |
*/ |
| 49 |
private function __construct() |
| 50 |
{ |
| 51 |
} |
| 52 |
/** |
| 53 |
* Singletons should not be cloneable. |
| 54 |
*/ |
| 55 |
private function __clone() |
| 56 |
{ |
| 57 |
} |
| 58 |
/** |
| 59 |
* Singletons should not be restorable from strings. |
| 60 |
* |
| 61 |
* @throws Exception Cannot unserialize a singleton. |
| 62 |
*/ |
| 63 |
public function __wakeup() |
| 64 |
{ |
| 65 |
throw new Exception('Cannot unserialize a singleton.'); |
| 66 |
} |
| 67 |
/** |
| 68 |
* This is the static method that controls the access to the singleton |
| 69 |
* instance. On the first run, it creates a singleton object and places it |
| 70 |
* into the static property. On subsequent runs, it returns the client existing |
| 71 |
* object stored in the static property. |
| 72 |
*/ |
| 73 |
public static function get_instance(): self |
| 74 |
{ |
| 75 |
if (!isset(self::$instance)) { |
| 76 |
self::$instance = new self(); |
| 77 |
} |
| 78 |
return self::$instance; |
| 79 |
} |
| 80 |
/** |
| 81 |
* Boot the plugin. |
| 82 |
* |
| 83 |
* @link https://codex.wordpress.org/Plugin_API/Action_Reference |
| 84 |
*/ |
| 85 |
public function boot(): void |
| 86 |
{ |
| 87 |
do_action('a!windpress/plugin:boot.start'); |
| 88 |
// (de)activation hooks. |
| 89 |
register_activation_hook(WIND_PRESS::FILE, fn() => $this->activate_plugin()); |
| 90 |
register_deactivation_hook(WIND_PRESS::FILE, fn() => $this->deactivate_plugin()); |
| 91 |
// upgrade hooks. |
| 92 |
add_action('upgrader_process_complete', function (WP_Upgrader $wpUpgrader, array $options): void { |
| 93 |
if ($options['action'] === 'update' && $options['type'] === 'plugin') { |
| 94 |
foreach ($options['plugins'] as $plugin) { |
| 95 |
if ($plugin === plugin_basename(WIND_PRESS::FILE)) { |
| 96 |
$this->upgrade_plugin(); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
}, 10, 2); |
| 101 |
$this->maybe_update_plugin(); |
| 102 |
add_action('plugins_loaded', fn() => $this->plugins_loaded(), 9); |
| 103 |
add_action('init', fn() => $this->init_plugin()); |
| 104 |
do_action('a!windpress/plugin:boot.end'); |
| 105 |
} |
| 106 |
/** |
| 107 |
* Handle the plugin's activation by (maybe) running database migrations |
| 108 |
* and initializing the plugin configuration. |
| 109 |
*/ |
| 110 |
private function activate_plugin(): void |
| 111 |
{ |
| 112 |
do_action('a!windpress/plugin:activate_plugin.start'); |
| 113 |
update_option(WIND_PRESS::WP_OPTION . '_version', WIND_PRESS::VERSION); |
| 114 |
$this->maybe_new_plugin_version(); |
| 115 |
$this->maybe_embedded_license(); |
| 116 |
do_action('a!windpress/plugin:activate_plugin.end'); |
| 117 |
} |
| 118 |
/** |
| 119 |
* Handle plugin's deactivation by (maybe) cleaning up after ourselves. |
| 120 |
*/ |
| 121 |
private function deactivate_plugin(): void |
| 122 |
{ |
| 123 |
do_action('a!windpress/plugin:deactivate_plugin.start'); |
| 124 |
do_action('a!windpress/plugin:deactivate_plugin.end'); |
| 125 |
} |
| 126 |
/** |
| 127 |
* Handle the plugin's upgrade by (maybe) running database migrations. |
| 128 |
*/ |
| 129 |
private function upgrade_plugin(): void |
| 130 |
{ |
| 131 |
do_action('a!windpress/plugin:upgrade_plugin.start'); |
| 132 |
UpgradeManager::get_instance()->run(); |
| 133 |
do_action('a!windpress/plugin:upgrade_plugin.end'); |
| 134 |
} |
| 135 |
/** |
| 136 |
* Initialize the plugin on WordPress' `init` hook. |
| 137 |
*/ |
| 138 |
private function init_plugin(): void |
| 139 |
{ |
| 140 |
do_action('a!windpress/plugin:init_plugin.start'); |
| 141 |
// Load translations. |
| 142 |
if (defined('WindPressDeps\WIND_PRESS_LOAD_TEXT_DOMAIN') && constant('WIND_PRESS_LOAD_TEXT_DOMAIN')) { |
| 143 |
load_plugin_textdomain(WIND_PRESS::TEXT_DOMAIN, \false, dirname(plugin_basename(WIND_PRESS::FILE)) . '/languages/'); |
| 144 |
} |
| 145 |
new ApiRouter(); |
| 146 |
Runtime::get_instance()->init(); |
| 147 |
// Instantiate the AdminPage class. |
| 148 |
new AdminPage(); |
| 149 |
UtilsCache::exclude_optimization(); |
| 150 |
// Initialize Abilities API integration |
| 151 |
AbilitiesLoader::get_instance()->init(); |
| 152 |
do_action('a!windpress/plugin:init_plugin.end'); |
| 153 |
} |
| 154 |
/** |
| 155 |
* Initialize the plugin on WordPress' `plugins_loaded` hook. |
| 156 |
*/ |
| 157 |
private function plugins_loaded(): void |
| 158 |
{ |
| 159 |
do_action('a!windpress/plugin:plugins_loaded.start'); |
| 160 |
IntegrationLoader::get_instance()->register_integrations(); |
| 161 |
if (is_admin()) { |
| 162 |
add_action('admin_notices', static fn() => Notice::admin_notices()); |
| 163 |
add_filter('plugin_action_links_' . plugin_basename(WIND_PRESS::FILE), fn($links) => $this->plugin_action_links($links)); |
| 164 |
} |
| 165 |
do_action('a!windpress/plugin:plugins_loaded.end'); |
| 166 |
} |
| 167 |
/** |
| 168 |
* Add plugin action links. |
| 169 |
* |
| 170 |
* @param array $links Plugin action links. |
| 171 |
* @return array Plugin action links. |
| 172 |
* |
| 173 |
* @todo Add settings link when the settings page is implemented. |
| 174 |
*/ |
| 175 |
private function plugin_action_links(array $links): array |
| 176 |
{ |
| 177 |
$base_url = AdminPage::get_page_url(); |
| 178 |
array_unshift($links, sprintf('<a href="%s">%s</a>', esc_url(sprintf('%s#/settings', $base_url)), esc_html__('Settings', 'windpress'))); |
| 179 |
if (!Common::is_updater_library_available()) { |
| 180 |
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'))); |
| 181 |
} |
| 182 |
return $links; |
| 183 |
} |
| 184 |
/** |
| 185 |
* Initialize the plugin updater. |
| 186 |
* Pro version only. |
| 187 |
* |
| 188 |
* @return PluginUpdater |
| 189 |
*/ |
| 190 |
private function maybe_update_plugin() |
| 191 |
{ |
| 192 |
if (!class_exists(PluginUpdater::class)) { |
| 193 |
return null; |
| 194 |
} |
| 195 |
if ($this->plugin_updater instanceof \WindPressDeps\EDD_SL\PluginUpdater) { |
| 196 |
return $this->plugin_updater; |
| 197 |
} |
| 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 |
} |
| 202 |
/** |
| 203 |
* Check if the plugin has a new version by clearing the cache. |
| 204 |
* Pro version only. |
| 205 |
*/ |
| 206 |
private function maybe_new_plugin_version(): void |
| 207 |
{ |
| 208 |
if (!class_exists(PluginUpdater::class)) { |
| 209 |
return; |
| 210 |
} |
| 211 |
$this->maybe_update_plugin()->clear_cache(); |
| 212 |
} |
| 213 |
/** |
| 214 |
* Check if the plugin distributed with an embedded license and activate the license. |
| 215 |
* Pro version only. |
| 216 |
*/ |
| 217 |
private function maybe_embedded_license(): void |
| 218 |
{ |
| 219 |
if (!class_exists(PluginUpdater::class)) { |
| 220 |
return; |
| 221 |
} |
| 222 |
$license_file = dirname(WIND_PRESS::FILE) . '/license-data.php'; |
| 223 |
if (!file_exists($license_file)) { |
| 224 |
return; |
| 225 |
} |
| 226 |
require_once $license_file; |
| 227 |
$const_name = 'ROSUA_EMBEDDED_LICENSE_KEY_' . WIND_PRESS::EDD_STORE['item_id']; |
| 228 |
if (!defined($const_name)) { |
| 229 |
return; |
| 230 |
} |
| 231 |
$license_key = constant($const_name); |
| 232 |
update_option(WIND_PRESS::WP_OPTION . '_license', ['key' => $license_key, 'opt_in_pre_release' => \false]); |
| 233 |
wp_delete_file($license_file); |
| 234 |
// activate the license. |
| 235 |
$this->maybe_update_plugin()->activate($license_key); |
| 236 |
} |
| 237 |
} |
| 238 |
|