PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 1.6.4
Kubio AI Page Builder v1.6.4
2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / PluginsManager.php
kubio / lib / src Last commit date
Core 3 years ago DemoSites 3 years ago Config.php 4 years ago Flags.php 4 years ago GoogleFontsLocalLoader.php 3 years ago Migrations.php 4 years ago NotificationsManager.php 4 years ago PluginsManager.php 4 years ago
PluginsManager.php
125 lines
1 <?php
2
3 namespace Kubio;
4
5 class PluginsManager {
6
7 const ACTIVE = 'ACTIVE';
8 const INSTALLED = 'INSTALLED';
9 const NOT_INSTALLED = 'NOT_INSTALLED';
10
11 private static $instance = null;
12
13 public function __construct() {
14 if ( ! function_exists( 'plugins_api' ) ) {
15 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
16 }
17
18 if ( ! function_exists( 'get_plugins' ) ) {
19 require_once ABSPATH . 'wp-admin/includes/plugin.php';
20 }
21 }
22
23 /**
24 * @return PluginsManager
25 */
26 public static function getInstance() {
27 if ( ! static::$instance ) {
28 static::$instance = new static();
29 }
30
31 return static::$instance;
32 }
33
34 /**
35 * @param $slug
36 *
37 * @return bool|\WP_Error
38 */
39 public function installPlugin( $slug ) {
40 if ( $this->isPluginInstalled( $slug ) ) {
41 return true;
42 }
43
44 $plugin_api = plugins_api(
45 'plugin_information',
46 array(
47 'slug' => $slug,
48 'fields' => array( 'sections' => false ),
49 )
50 );
51
52 if ( is_wp_error( $plugin_api ) ) {
53 return $plugin_api;
54 }
55
56 if ( ! class_exists( '\Plugin_Upgrader', false ) ) {
57 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
58 }
59
60 $upgrader = new \Plugin_Upgrader( new \Automatic_Upgrader_Skin() );
61 $result = $upgrader->install( $plugin_api->download_link );
62
63 if ( $result !== true ) {
64 return is_wp_error( $result ) ? $result : new \WP_Error( 'failed_install', sprintf( __( 'Failed to install plugin: %s', 'kubio' ), $slug ) );
65 }
66
67 return true;
68 }
69
70 public function isPluginInstalled( $slug ) {
71 return ! empty( $this->getPluginBaseName( $slug ) );
72 }
73
74 public function getPluginBaseName( $slug ) {
75 $plugins = get_plugins();
76
77 foreach ( array_keys( $plugins ) as $key ) {
78 if ( preg_match( '/^' . $slug . '\//', $key ) ) {
79 return $key;
80 }
81 }
82
83 return false;
84 }
85
86 /**
87 * Attempts activation of a plugin
88 *
89 * @param string $slug The plugin slug.
90 * @param boolean $silent Optional. Whether to prevent calling activation hooks. Default false.
91 * @return void
92 */
93 public function activatePlugin( $slug, $silent = false ) {
94 $result = activate_plugin( $this->getPluginBaseName( $slug ), '', false, $silent );
95
96 if ( is_wp_error( $result ) ) {
97 return $result;
98 }
99
100 return true;
101 }
102
103 public function getPluginStatus( $slug ) {
104 if ( $this->isPluginActive( $slug ) ) {
105 return static::ACTIVE;
106 }
107
108 if ( $this->isPluginInstalled( $slug ) ) {
109 return static::INSTALLED;
110 }
111
112 return static::NOT_INSTALLED;
113 }
114
115 public function isPluginActive( $slug ) {
116 $plugin_path = $this->getPluginBaseName( $slug );
117
118 if ( empty( $plugin_path ) ) {
119 return false;
120 }
121
122 return is_plugin_active( $plugin_path );
123 }
124 }
125