PluginProbe
WindPress – Tailwind CSS integration for WordPress / 3.0.17
WindPress – Tailwind CSS integration for WordPress v3.0.17
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.17, 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 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\Core\Runtime;
20 use WindPress\WindPress\Integration\Loader as IntegrationLoader;
21 use WindPress\WindPress\Utils\Common;
22 use WindPress\WindPress\Utils\Notice;
23 use WP_Upgrader;
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 * Initialize the plugin updater.
105 * Pro version only.
106 *
107 * @return PluginUpdater
108 */
109 public function maybe_update_plugin()
110 {
111 if (!\class_exists(PluginUpdater::class)) {
112 return null;
113 }
114 if ($this->plugin_updater instanceof \WindPressDeps\EDD_SL\PluginUpdater) {
115 return $this->plugin_updater;
116 }
117 $license = \get_option(WIND_PRESS::WP_OPTION . '_license', ['key' => '', 'opt_in_pre_release' => \false]);
118 $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']]);
119 return $this->plugin_updater;
120 }
121 /**
122 * Handle the plugin's activation by (maybe) running database migrations
123 * and initializing the plugin configuration.
124 */
125 private function activate_plugin() : void
126 {
127 \do_action('a!windpress/plugin:activate_plugin.start');
128 \update_option(WIND_PRESS::WP_OPTION . '_version', WIND_PRESS::VERSION);
129 $this->maybe_embedded_license();
130 \do_action('a!windpress/plugin:activate_plugin.end');
131 }
132 /**
133 * Handle plugin's deactivation by (maybe) cleaning up after ourselves.
134 */
135 private function deactivate_plugin() : void
136 {
137 \do_action('a!windpress/plugin:deactivate_plugin.start');
138 \do_action('a!windpress/plugin:deactivate_plugin.end');
139 }
140 /**
141 * Handle the plugin's upgrade by (maybe) running database migrations.
142 */
143 private function upgrade_plugin() : void
144 {
145 \do_action('a!windpress/plugin:upgrade_plugin.start');
146 \do_action('a!windpress/plugin:upgrade_plugin.end');
147 }
148 /**
149 * Initialize the plugin on WordPress' `init` hook.
150 */
151 private function init_plugin() : void
152 {
153 \do_action('a!windpress/plugin:init_plugin.start');
154 // Load translations.
155 // load_plugin_textdomain(WIND_PRESS::TEXT_DOMAIN, false, dirname(plugin_basename(WIND_PRESS::FILE)) . '/languages/');
156 new ApiRouter();
157 Runtime::get_instance()->init();
158 // Instantiate the AdminPage class.
159 new AdminPage();
160 \do_action('a!windpress/plugin:init_plugin.end');
161 }
162 /**
163 * Initialize the plugin on WordPress' `plugins_loaded` hook.
164 */
165 private function plugins_loaded() : void
166 {
167 \do_action('a!windpress/plugin:plugins_loaded.start');
168 IntegrationLoader::get_instance()->register_integrations();
169 if (\is_admin()) {
170 \add_action('admin_notices', static fn() => Notice::admin_notices());
171 \add_filter('plugin_action_links_' . \plugin_basename(WIND_PRESS::FILE), fn($links) => $this->plugin_action_links($links));
172 }
173 \do_action('a!windpress/plugin:plugins_loaded.end');
174 }
175 /**
176 * Add plugin action links.
177 *
178 * @param array $links Plugin action links.
179 * @return array Plugin action links.
180 *
181 * @todo Add settings link when the settings page is implemented.
182 */
183 private function plugin_action_links(array $links) : array
184 {
185 $base_url = AdminPage::get_page_url();
186 \array_unshift($links, \sprintf('<a href="%s">%s</a>', \esc_url(\sprintf('%s#/settings', $base_url)), \esc_html__('Settings', 'windpress')));
187 if (!Common::is_updater_library_available()) {
188 \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')));
189 }
190 return $links;
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