CLI
2 years ago
Core
1 year ago
DemoSites
1 year ago
AssetsDependencyInjector.php
3 years ago
Config.php
3 years ago
FileLog.php
2 years ago
Flags.php
2 years ago
GoogleFontsLocalLoader.php
2 years ago
GutenbergControls.php
1 year ago
Migrations.php
4 years ago
NotificationsManager.php
1 year 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 |