PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.6
WindPress – Tailwind CSS integration for WordPress v3.0.6
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 3.0.7 All 142 releases
windpress / src / Plugin.php

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

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