PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / trunk
Kubio AI Page Builder vtrunk
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
CLI 1 year ago Core 1 month ago DemoSites 1 month ago AssetsDependencyInjector.php 1 year ago Config.php 1 year ago FileLog.php 1 year ago Flags.php 1 year ago GoogleFontsLocalLoader.php 1 year ago GutenbergControls.php 1 year ago Migrations.php 1 year ago NotificationsManager.php 1 month ago PluginsManager.php 2 years ago
PluginsManager.php
130 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 if ( ! function_exists( 'request_filesystem_credentials' ) ) {
23 require_once ABSPATH . 'wp-admin/includes/file.php';
24 }
25 }
26
27 /**
28 * @return PluginsManager
29 */
30 public static function getInstance() {
31 if ( ! static::$instance ) {
32 static::$instance = new static();
33 }
34
35 return static::$instance;
36 }
37
38 /**
39 * @param $slug
40 *
41 * @return bool|\WP_Error
42 */
43 public function installPlugin( $slug ) {
44 if ( $this->isPluginInstalled( $slug ) ) {
45 return true;
46 }
47
48 $plugin_api = plugins_api(
49 'plugin_information',
50 array(
51 'slug' => $slug,
52 'fields' => array( 'sections' => false ),
53 )
54 );
55
56 if ( is_wp_error( $plugin_api ) ) {
57 return $plugin_api;
58 }
59
60 if ( ! class_exists( '\Plugin_Upgrader', false ) ) {
61 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
62 }
63
64 $upgrader = new \Plugin_Upgrader( new \Automatic_Upgrader_Skin() );
65 $result = $upgrader->install( $plugin_api->download_link );
66
67 if ( $result !== true ) {
68 // translators: %s - plugin slug
69 return is_wp_error( $result ) ? $result : new \WP_Error( 'failed_install', sprintf( __( 'Failed to install plugin: %s', 'kubio' ), $slug ) );
70 }
71
72 return true;
73 }
74
75 public function isPluginInstalled( $slug ) {
76 return ! empty( $this->getPluginBaseName( $slug ) );
77 }
78
79 public function getPluginBaseName( $slug ) {
80 $plugins = get_plugins();
81
82 foreach ( array_keys( $plugins ) as $key ) {
83 if ( preg_match( '/^' . $slug . '\//', $key ) ) {
84 return $key;
85 }
86 }
87
88 return false;
89 }
90
91 /**
92 * Attempts activation of a plugin
93 *
94 * @param string $slug The plugin slug.
95 * @param boolean $silent Optional. Whether to prevent calling activation hooks. Default false.
96 * @return void
97 */
98 public function activatePlugin( $slug, $silent = false ) {
99 $result = activate_plugin( $this->getPluginBaseName( $slug ), '', false, $silent );
100
101 if ( is_wp_error( $result ) ) {
102 return $result;
103 }
104
105 return true;
106 }
107
108 public function getPluginStatus( $slug ) {
109 if ( $this->isPluginActive( $slug ) ) {
110 return static::ACTIVE;
111 }
112
113 if ( $this->isPluginInstalled( $slug ) ) {
114 return static::INSTALLED;
115 }
116
117 return static::NOT_INSTALLED;
118 }
119
120 public function isPluginActive( $slug ) {
121 $plugin_path = $this->getPluginBaseName( $slug );
122
123 if ( empty( $plugin_path ) ) {
124 return false;
125 }
126
127 return is_plugin_active( $plugin_path );
128 }
129 }
130