PluginProbe ʕ •ᴥ•ʔ
Disable Admin Notices – Hide Dashboard Notifications / trunk
Disable Admin Notices – Hide Dashboard Notifications vtrunk
1.4.5 trunk 1.0.0 1.0.2 1.0.3 1.0.5 1.0.6 1.1.1 1.1.3 1.1.4 1.2.0 1.2.2 1.2.3 1.2.4 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4
disable-admin-notices / libs / factory / core / includes / components / class-install-component-button.php
disable-admin-notices / libs / factory / core / includes / components Last commit date
class-delete-component-button.php 1 year ago class-install-component-button.php 1 year ago
class-install-component-button.php
467 lines
1 <?php
2
3 namespace WBCR\Factory_480\Components;
4
5 /**
6 * This file groups the settings for quick setup
7 *
8 * @author Alex Kovalev <alex.kovalevv@gmail.com>
9 * @copyright (c) 16.09.2017, Webcraftic
10 * @version 1.0
11 */
12
13 // Exit if accessed directly
14 if( !defined('ABSPATH') ) {
15 exit;
16 }
17
18 class Install_Button {
19
20 public $plugin;
21 protected $type;
22 protected $plugin_slug;
23
24 protected $classes = [
25 'button',
26 'wfactory-480-process-button'
27 ];
28 protected $data = [];
29 protected $base_path;
30
31 protected $action;
32
33 protected $url;
34
35 /**
36 * @param string $group_name
37 *
38 * @throws \Exception
39 * @since 4.3.3
40 */
41 public function __construct(\Wbcr_Factory480_Plugin $plugin, $type, $plugin_slug)
42 {
43 if( empty($type) || !is_string($plugin_slug) ) {
44 throw new \Exception('Empty type or plugin_slug attribute.');
45 }
46
47 $this->plugin = $plugin;
48 $this->type = $type;
49 $this->plugin_slug = $plugin_slug;
50
51 if( $this->type == 'wordpress' || $this->type == 'creativemotion' ) {
52 if( strpos(rtrim(trim($this->plugin_slug)), '/') !== false ) {
53 $this->base_path = $this->plugin_slug;
54 $base_path_parts = explode('/', $this->base_path);
55 if( sizeof($base_path_parts) === 2 ) {
56 $this->plugin_slug = $base_path_parts[0];
57 }
58 } else {
59 $this->base_path = $this->get_plugin_base_path_by_slug($this->plugin_slug);
60 }
61
62 $this->build_wordpress();
63 } else if( $this->type == 'internal' ) {
64 $this->build_internal();
65 } else {
66 throw new \Exception('Invalid button type.');
67 }
68
69 // Set default data
70 $this->add_data('storage', $this->type);
71 $this->add_data('i18n', \WBCR\Factory_Templates_134\Helpers::getEscapeJson($this->get_i18n()));
72 $this->add_data('wpnonce', wp_create_nonce('updates'));
73 }
74
75 // Удалить, через несколько версий!
76 // Добавлено в 4.3.3
77 //--------------------------------------------------------
78
79 /**
80 * todo: для совместимости со старыми плагинами
81 *
82 * @return string
83 * @throws \Exception
84 * @since 4.3.3
85 */
86 public function getLink()
87 {
88 return $this->get_link();
89 }
90
91 /**
92 * todo: для совместимости со старыми плагинами
93 *
94 * @return string
95 * @since 4.3.3
96 */
97 public function getButton()
98 {
99 return $this->get_button();
100 }
101
102 /**
103 * Print an install a link
104 * todo: для совместимости со старыми плагинами
105 *
106 * @throws \Exception
107 * @since 4.3.3
108 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
109 */
110 public function renderLink()
111 {
112 echo esc_html($this->get_link());
113 }
114
115 /**
116 * Print an install button
117 * todo: для совместимости со старыми плагинами
118 *
119 * @throws \Exception
120 * @since 4.3.3
121 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
122 */
123 public function renderButton()
124 {
125 $this->render_button();
126 }
127
128 /**
129 * todo: для совместимости со старыми плагинами
130 *
131 * @param $class
132 *
133 * @throws \Exception
134 * @since 4.3.3
135 */
136 public function addClass($class)
137 {
138 $this->add_class($class);
139 }
140
141 /**
142 * todo: для совместимости со старыми плагинами
143 *
144 * @param $class
145 *
146 * @return bool
147 * @throws \Exception
148 * @since 4.3.3
149 */
150 public function removeClass($class)
151 {
152 return $this->remove_class($class);
153 }
154
155 //--------------------------------------------------------
156
157
158 /**
159 * @return bool
160 * @since 4.3.3
161 */
162 public function is_plugin_activate()
163 {
164 if( ($this->type == 'wordpress' || $this->type == 'creativemotion') && $this->is_plugin_install() ) {
165 require_once ABSPATH . '/wp-admin/includes/plugin.php';
166
167 return is_plugin_active($this->base_path);
168 } else if( $this->type == 'internal' ) {
169 $preinsatall_components = $this->plugin->getPopulateOption('deactive_preinstall_components', []);
170
171 return !in_array($this->plugin_slug, $preinsatall_components);
172 }
173
174 return false;
175 }
176
177 /**
178 * @return bool
179 * @since 4.3.3
180 */
181 public function is_plugin_install()
182 {
183 if( $this->type == 'wordpress' || $this->type == 'creativemotion' ) {
184 if( empty($this->base_path) ) {
185 return false;
186 }
187
188 // Check if the function get_plugins() is registered. It is necessary for the front-end
189 // usually get_plugins() only works in the admin panel.
190 if( !function_exists('get_plugins') ) {
191 require_once ABSPATH . 'wp-admin/includes/plugin.php';
192 }
193
194 $plugins = get_plugins();
195
196 if( isset($plugins[$this->base_path]) ) {
197 return true;
198 }
199 } else if( $this->type == 'internal' ) {
200 return true;
201 }
202
203 return false;
204 }
205
206 /**
207 * @param $class
208 *
209 * @throws \Exception
210 * @since 4.3.3
211 */
212 public function add_class($class)
213 {
214 if( !is_string($class) ) {
215 throw new \Exception('Attribute class must be a string.');
216 }
217 $this->classes[] = $class;
218 }
219
220 /**
221 * @param $class
222 *
223 * @return bool
224 * @throws \Exception
225 * @since 4.3.3
226 */
227 public function remove_class($class)
228 {
229 if( !is_string($class) ) {
230 throw new \Exception('Attribute class must be a string.');
231 }
232 $key = array_search($class, $this->classes);
233 if( isset($this->classes[$key]) ) {
234 unset($this->classes[$key]);
235
236 return true;
237 }
238
239 return false;
240 }
241
242 /**
243 * @param $name
244 * @param $value
245 *
246 * @throws \Exception
247 * @since 4.3.3
248 */
249 public function add_data($name, $value)
250 {
251 if( !is_string($name) || !is_string($value) ) {
252 throw new \Exception('Attributes name and value must be a string.');
253 }
254
255 $this->data[$name] = $value;
256 }
257
258 /**
259 * @param $name
260 *
261 * @return bool
262 * @throws \Exception
263 * @since 4.3.3
264 */
265 public function remove_data($name)
266 {
267 if( !is_string($name) ) {
268 throw new \Exception('Attribute name must be a string.');
269 }
270
271 if( isset($this->data[$name]) ) {
272 unset($this->data[$name]);
273
274 return true;
275 }
276
277 return false;
278 }
279
280 /**
281 * Print an install button
282 *
283 * @throws \Exception
284 * @since 4.3.3
285 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
286 */
287 public function render_button()
288 {
289 echo $this->get_button();
290 }
291
292 /**
293 * @return string
294 * @since 4.3.3
295 */
296 public function get_button()
297 {
298 $i18n = $this->get_i18n();
299
300 $button = '<a href="#" class="' . implode(' ', $this->get_classes()) . '" ' . implode(' ', $this->get_data()) . '>' . $i18n[$this->action] . '</a>';
301
302 return $button;
303 }
304
305 /**
306 * @return string
307 * @throws \Exception
308 * @since 4.3.3
309 */
310 public function get_link()
311 {
312 $this->remove_class('button');
313 $this->remove_class('button-default');
314 $this->remove_class('button-primary');
315
316 //$this->add_class('link');
317 $this->add_class('button-link');
318
319 return $this->get_button();
320 }
321
322 /**
323 * Print an install a link
324 *
325 * @throws \Exception
326 * @since 4.3.3
327 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
328 */
329 public function render_link()
330 {
331 echo $this->get_link();
332 }
333
334 /**
335 * @return array
336 * @since 4.3.3
337 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
338 */
339 protected function get_data()
340 {
341 $data_to_print = [];
342
343 foreach((array)$this->data as $key => $value) {
344 $data_to_print[$key] = 'data-' . esc_attr($key) . '="' . esc_attr($value) . '"';
345 }
346
347 return $data_to_print;
348 }
349
350 /**
351 * @return array
352 * @since 4.3.3
353 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
354 */
355 protected function get_classes()
356 {
357 return array_map('esc_attr', $this->classes);
358 }
359
360 /**
361 * @throws \Exception
362 * @since 4.3.3
363 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
364 */
365 protected function build_wordpress()
366 {
367 if( ('wordpress' === $this->type || 'creativemotion' === $this->type) && !empty($this->base_path) ) {
368
369 $this->action = 'install';
370
371 if( $this->is_plugin_install() ) {
372 $this->action = 'deactivate';
373 if( !$this->is_plugin_activate() ) {
374 $this->action = 'activate';
375 }
376 }
377
378 $this->add_data('plugin-action', $this->action);
379 $this->add_data('slug', $this->plugin_slug);
380 $this->add_data('plugin', $this->base_path);
381
382 if( $this->action == 'activate' ) {
383 $this->add_class('button-primary');
384 } else {
385 $this->add_class('button-default');
386 }
387 }
388 }
389
390 /**
391 * Configurate button of internal components
392 *
393 * @throws \Exception
394 * @since 4.3.3
395 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
396 */
397 protected function build_internal()
398 {
399 if( $this->type != 'internal' ) {
400 return;
401 }
402
403 $this->action = 'activate';
404
405 if( $this->is_plugin_activate() ) {
406 $this->action = 'deactivate';
407 }
408
409 $this->add_data('plugin-action', $this->action);
410 $this->add_data('plugin', $this->plugin_slug);
411
412 if( $this->action == 'activate' ) {
413 $this->add_class('button-primary');
414 } else {
415 $this->add_class('button-default');
416 }
417 }
418
419 /**
420 * Internalization for action buttons
421 *
422 * @return array
423 * @since 4.3.3
424 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
425 */
426 protected function get_i18n()
427 {
428 return [
429 'activate' => __('Activate', 'wbcr_factory_480'),
430 'install' => __('Install', 'wbcr_factory_480'),
431 'deactivate' => __('Deactivate', 'wbcr_factory_480'),
432 'delete' => __('Delete', 'wbcr_factory_480'),
433 'loading' => __('Please wait...', 'wbcr_factory_480'),
434 'preparation' => __('Preparation...', 'wbcr_factory_480'),
435 'read' => __('Read more', 'wbcr_factory_480')
436 ];
437 }
438
439
440 /**
441 * Allows you to get the base path to the plugin in the directory wp-content/plugins/
442 *
443 * @param $slug - slug for example "clearfy", "hide-login-page"
444 *
445 * @return int|null|string - "clearfy/clearfy.php"
446 */
447 protected function get_plugin_base_path_by_slug($slug)
448 {
449 // Check if the function get_plugins() is registered. It is necessary for the front-end
450 // usually get_plugins() only works in the admin panel.
451 if( !function_exists('get_plugins') ) {
452 require_once ABSPATH . 'wp-admin/includes/plugin.php';
453 }
454
455 $plugins = get_plugins();
456
457 foreach($plugins as $base_path => $plugin) {
458 if( strpos($base_path, rtrim(trim($slug))) !== false ) {
459 return $base_path;
460 }
461 }
462
463 return null;
464 }
465 }
466
467