PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.3
WindPress – Tailwind CSS integration for WordPress v3.0.3
3.2.89 3.2.88 3.2.87 3.2.86 3.2.85 3.2.84 3.2.83 3.2.82 3.2.81 trunk 3.0.0 3.0.1 3.0.10 3.0.11 3.0.12 3.0.13 3.0.14 3.0.15 3.0.16 3.0.17 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 All 143 releases
windpress / src / Plugin.php

Plugin.php in WindPress – Tailwind CSS integration for WordPress 3.0.3, at src/Plugin.php

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