PluginProbe
WP-Stateless – Google Cloud Storage / trunk
WP-Stateless – Google Cloud Storage vtrunk
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / class-addons.php

class-addons.php in WP-Stateless – Google Cloud Storage trunk, at lib/classes/class-addons.php

382 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Addons manager.
5 *
6 * Checks if current theme or any of the installed plugins require WP-Stateless addon.
7 * Shows message to the user if addon is recommended.
8 * Responsible for showing the list of addons on the settings page.
9 *
10 * @since 3.3.0
11 */
12
13 namespace wpCloud\StatelessMedia {
14
15 class Addons {
16 use Singleton;
17
18 /**
19 * Addons list.
20 *
21 * @var array
22 */
23 protected $addons = [];
24
25 /**
26 * Addons slugs list.
27 *
28 * @var array
29 */
30 protected $addon_ids = [];
31
32 /**
33 * Recommended addons count.
34 *
35 * @var int
36 */
37 protected $recommended = 0;
38
39 /**
40 * Active addons count.
41 *
42 * @var int
43 */
44 protected $active = 0;
45
46 protected function __construct() {
47 $addons_file = ud_get_stateless_media()->path('static/data/addons.php', 'dir');
48
49 if ( file_exists($addons_file) ) {
50 $this->addons = require_once($addons_file);
51 $this->addon_ids = array_keys($this->addons);
52 }
53
54 add_action('sm::module::init', array($this, 'check_addons'));
55 add_action('sm::module::messages', array($this, 'show_messages'), 10, 3);
56 add_action('wp_stateless_addons_tab_content', array($this, 'tab_content'));
57 add_filter('wp_stateless_addons_tab_visible', array($this, 'addons_tab_visible'), 10, 1);
58 add_filter('wp_stateless_restrict_compatibility', array($this, 'restrict_compatibility'), 10, 3);
59 add_filter('wp_stateless_addon_files_root', array($this, 'get_addon_files_root'), 10);
60 add_filter('wp_stateless_addon_sync_files_path', array($this, 'get_addon_sync_files_path'), 10, 2);
61 add_filter('wp_stateless_addon_files_url', array($this, 'get_addon_files_url'), 10, 2);
62 }
63
64 /**
65 * Check current theme and installed plugins for recommended addons.
66 * Fires hok to display messages if recommended addons are not active.
67 *
68 */
69 public function check_addons() {
70 $active_plugins = Helper::get_active_plugins();
71
72 foreach ($this->addons as $id => $addon) {
73 // Recommended theme addons
74 if ( isset($addon['theme_name']) ) {
75 if ( Helper::is_theme_name($addon['theme_name']) ) {
76 $this->addons[$id]['recommended'] = true;
77 $this->recommended++;
78 }
79 }
80
81 // Recommended plugin addons
82 if ( isset($addon['plugin_files']) && is_array($addon['plugin_files']) ) {
83 foreach ($addon['plugin_files'] as $file) {
84 if ( in_array($file, $active_plugins) ) {
85 $this->addons[$id]['recommended'] = true;
86 $this->recommended++;
87
88 break;
89 }
90 }
91 }
92
93 // Active addons
94 if ( isset($addon['addon_file']) ) {
95 if ( in_array($addon['addon_file'], $active_plugins) ) {
96 $this->addons[$id]['active'] = true;
97 $this->active++;
98 }
99 }
100
101 // Installed addons
102 if ( isset($addon['addon_file']) ) {
103 if ( file_exists( WP_PLUGIN_DIR . '/' . $addon['addon_file'] ) ) {
104 $this->addons[$id]['installed'] = true;
105 $this->addons[$id]['activate_link'] = wp_nonce_url( 'plugins.php?action=activate&amp;plugin=' . $addon['addon_file'], 'activate-plugin_' . $addon['addon_file'] );
106 }
107 }
108 }
109
110 // Show message if recommended addons are not active
111 do_action('sm::module::messages', $this->addons);
112 }
113
114 /**
115 * Add messages for recommended addons.
116 *
117 * @param array $addons
118 * @param array $recommended
119 * @param array $active
120 *
121 */
122 public function show_messages($addons) {
123 foreach ($addons as $id => $addon) {
124 if ( !isset($addon['recommended']) || isset($addon['active']) ) {
125 continue;
126 }
127
128 $title = $addon['title'];
129 $button = __('Download Addon', ud_get_stateless_media()->domain);
130 $button_link = admin_url('upload.php?page=stateless-settings&tab=stless_addons_tab');
131 $message = sprintf(__('Download and activate the WP-Stateless addon for %s to ensure compatibility.', ud_get_stateless_media()->domain), $title);
132
133 if ( isset($addon['activate_link']) ) {
134 $button = __('Activate Addon', ud_get_stateless_media()->domain);
135 $button_link = $addon['activate_link'];
136 $message = sprintf(__('Activate the WP-Stateless addon for %s to ensure compatibility.', ud_get_stateless_media()->domain), $title);
137 }
138
139 $plugin_activate_url = wp_nonce_url( 'plugins.php?action=activate&amp;plugin=classic-widgets/classic-widgets.php', 'activate-plugin_classic-widgets/classic-widgets.php' );
140
141 ud_get_stateless_media()->errors->add([
142 'key' => $id,
143 'title' => sprintf(__('WP-Stateless: Install the %s Addon', ud_get_stateless_media()->domain), $title),
144 'button' => $button,
145 'button_link' => $button_link,
146 'message' => $message,
147 ], 'notice');
148 }
149 }
150
151 /**
152 * Outputs 'Addons' tab content on the settings page.
153 *
154 */
155 public function tab_content() {
156 // Prepare filters
157 $current_filter = isset($_GET['filter']) && !empty($_GET['filter']) ? $_GET['filter'] : 'all';
158
159 $url = ud_get_stateless_media()->get_settings_page_url('?page=stateless-settings') . '&tab=stless_addons_tab&filter=%s';
160
161 $filters = [
162 'all' => [
163 'title' => __('All <span class="count">(%d)</span>', ud_get_stateless_media()->domain),
164 'count' => count($this->addons),
165 ],
166 'recommended' => [
167 'title' => __('Recommended <span class="count">(%d)</span>', ud_get_stateless_media()->domain),
168 'count' => $this->recommended,
169 ],
170 'active' => [
171 'title' => __('Active <span class="count">(%d)</span>', ud_get_stateless_media()->domain),
172 'count' => $this->active,
173 ],
174 'inactive' => [
175 'title' => __('Inactive <span class="count">(%d)</span>', ud_get_stateless_media()->domain),
176 'count' => count($this->addons) - $this->active,
177 ],
178 ];
179
180 $filters = Helper::array_of_objects($filters);
181
182 // Filter addons
183 switch ($current_filter) {
184 case 'recommended':
185 $addons = array_filter($this->addons, function($addon) {
186 return isset($addon['recommended']);
187 });
188 break;
189 case 'active':
190 $addons = array_filter($this->addons, function($addon) {
191 return isset($addon['active']);
192 });
193 break;
194 case 'inactive':
195 $addons = array_filter($this->addons, function($addon) {
196 return !isset($addon['active']);
197 });
198 break;
199 default:
200 $addons = $this->addons;
201 break;
202 }
203
204 $addons = $this->sort_addons($addons);
205 $addons = $this->get_addons_view($addons);
206 $addons = Helper::array_of_objects($addons);
207
208 include ud_get_stateless_media()->path('static/views/addons-tab.php', 'dir');
209 }
210
211 /**
212 * Prepare addon data for output.
213 *
214 * @param array $addons
215 * @return array
216 */
217 private function get_addons_view($addons) {
218 $plugin_desc = __('Provides compatibility between the %s and the WP-Stateless plugins.', ud_get_stateless_media()->domain);
219 $theme_desc = __('Provides compatibility between the %s theme and the WP-Stateless plugin.', ud_get_stateless_media()->domain);
220
221 $link = ud_get_stateless_media()->get_docs_page_url('addons/%s');
222
223 $defaults = [
224 'title' => '',
225 'icon' => '',
226 'description' => '',
227 'recommended' => false,
228 'active' => false,
229 'installed' => false,
230 'activate_link' => '',
231 'hubspot_id' => '',
232 'hubspot_link' => '',
233 'repo' => '',
234 'docs' => '',
235 'link' => '',
236 'wp' => '',
237 'card_class' => '',
238 'status' => '',
239 ];
240
241 foreach ($addons as $id => $addon) {
242 if ( isset($addon['theme_name']) ) {
243 $addon['description'] = sprintf($theme_desc, $addon['title']);
244 } else {
245 $addon['description'] = sprintf($plugin_desc, $addon['title']);
246 }
247
248 if ( isset($addon['active']) && $addon['active']) {
249 $addon['card_class'] = 'active';
250 $addon['status'] = __('active', ud_get_stateless_media()->domain);
251 } elseif ( isset($addon['recommended']) && $addon['recommended']) {
252 $addon['card_class'] = 'recommended';
253 $addon['status'] = __('recommended', ud_get_stateless_media()->domain);
254 }
255
256 $addon['docs'] = sprintf($link, $id);
257 $addon['link'] = $addon['docs'];
258
259 $addons[$id] = wp_parse_args($addon, $defaults);
260 }
261
262 return $addons;
263 }
264
265 /**
266 * Sort addons:
267 * - recommended not active
268 * - other
269 * - active
270 *
271 * @param array $addons
272 * @return array
273 */
274 private function sort_addons($addons) {
275 uasort($addons, function($a1, $a2) {
276 $c1 = isset($a1['recommended']) ? -1 : 0;
277 $c1 += isset($a1['active']) ? 5 : 0;
278
279 $c2 = isset($a2['recommended']) ? -1 : 0;
280 $c2 += isset($a2['active']) ? 5 : 0;
281
282 if ( $c1 !== $c2 ) {
283 return $c1 > $c2 ? 1 : -1;
284 }
285
286 return strcasecmp( $a1['title'], $a2['title'] );
287 });
288
289 return $addons;
290 }
291
292 /**
293 * Check if 'Addons' tab should be visible.
294 */
295 public function addons_tab_visible($visible) {
296 return count($this->addons) > 0;
297 }
298
299 /**
300 * Restrict internal compatibility.
301 */
302 public function restrict_compatibility($restrict, $id, $is_internal) {
303 // If we have a plugin with the same ID as internal compatibility then disable internal compatibility
304 if ($is_internal) {
305 if ( in_array($id, $this->addon_ids) ) {
306 return true;
307 }
308 }
309
310 return $restrict;
311 }
312
313 /**
314 * Get the root for saving addons files.
315 * In Stateless Mode this will be the root of the bucket.
316 * In other modes this will be the uploads base directory.
317 */
318 public function get_addon_files_root($root_path) {
319 if ( ud_get_stateless_media()->is_mode('stateless') ) {
320 $root_path = ud_get_stateless_media()->get_gs_path();
321 } else {
322 $upload_dir = wp_get_upload_dir();
323 $root_path = $upload_dir['basedir'];
324 }
325
326 return $root_path;
327 }
328
329 /**
330 * Get the root path for syncing addons files.
331 * In Stateless and Ephemeral Modes this will be the root of the bucket.
332 * In other modes this will be the uploads base directory.
333 */
334 public function get_addon_sync_files_path($root_path, $addon_folder = '') {
335 if ( ud_get_stateless_media()->is_mode( ['stateless', 'ephemeral'] ) ) {
336 $root_path = ud_get_stateless_media()->get_gs_path();
337 } else {
338 $upload_dir = wp_get_upload_dir();
339 $root_path = $upload_dir['basedir'];
340 }
341
342 if ( !empty($addon_folder) ) {
343 $root_path = [
344 rtrim($root_path, '/'),
345 ltrim($addon_folder, '/'),
346 ];
347
348 $root_path = implode('/', $root_path);
349 }
350
351 return $root_path;
352 }
353
354 /**
355 * Get the root path for syncing addons files.
356 * In Stateless and Ephemeral Modes this will be the root of the bucket.
357 * In other modes this will be the uploads base directory.
358 */
359 public function get_addon_files_url($root_url, $addon_folder = '') {
360 $root_url = '';
361
362 if ( ud_get_stateless_media()->is_mode( ['disabled', 'backup'] ) ) {
363 $upload_dir = wp_get_upload_dir();
364 $root_url = $upload_dir['baseurl'];
365 } else {
366 $root_url = ud_get_stateless_media()->get_gs_host();
367 }
368
369 if ( !empty($addon_folder) ) {
370 $root_url = [
371 rtrim($root_url, '/'),
372 ltrim($addon_folder, '/'),
373 ];
374
375 $root_url = implode('/', $root_url);
376 }
377
378 return $root_url;
379 }
380 }
381 }
382