Plugin_Installer.php
194 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SPEL\includes\classes; |
| 4 | |
| 5 | if (!defined('ABSPATH')) { |
| 6 | exit; // Exit if accessed directly. |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * Class Plugin_Installer |
| 11 | */ |
| 12 | class Plugin_Installer |
| 13 | { |
| 14 | |
| 15 | private static $instance; |
| 16 | private $installedPlugins = array(); |
| 17 | private $activatedPlugins = array(); |
| 18 | |
| 19 | /** |
| 20 | * Get the single instance of the class |
| 21 | * |
| 22 | * @return Plugin_Installer Singleton instance of the class |
| 23 | */ |
| 24 | public static function instance () |
| 25 | { |
| 26 | if (!static::$instance) { |
| 27 | static::$instance = new static(); |
| 28 | } |
| 29 | |
| 30 | return static::$instance; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Constructor |
| 35 | * Initializes the class and collects installed and activated plugins |
| 36 | */ |
| 37 | public function __construct () |
| 38 | { |
| 39 | add_action('plugins_loaded', [ $this, 'init' ]); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Initializes the class |
| 44 | */ |
| 45 | public function init () |
| 46 | { |
| 47 | $this->collect_installed_plugins(); |
| 48 | $this->collect_activated_plugins(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Collects the list of installed plugins |
| 53 | */ |
| 54 | private function collect_installed_plugins () |
| 55 | { |
| 56 | |
| 57 | if (!function_exists('get_plugins')) { |
| 58 | foreach ( get_plugins() as $key => $plugin ) { |
| 59 | array_push($this->installedPlugins, $key); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Collects the list of activated plugins |
| 66 | */ |
| 67 | private function collect_activated_plugins () |
| 68 | { |
| 69 | foreach ( apply_filters('active_plugins', get_option('active_plugins')) as $plugin ) { |
| 70 | array_push($this->activatedPlugins, $plugin); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get the status information for a plugin |
| 76 | * |
| 77 | * @param string $name Plugin name |
| 78 | * |
| 79 | * @return array Plugin status information |
| 80 | */ |
| 81 | public function get_status ($name) |
| 82 | { |
| 83 | $data = array( |
| 84 | 'url' => '', |
| 85 | 'activation_url' => '', |
| 86 | 'installation_url' => '', |
| 87 | ); |
| 88 | |
| 89 | if ($this->check_installed_plugin($name)) { |
| 90 | if ($this->check_activated_plugin($name)) { |
| 91 | $data[ 'title' ] = __('Activated', 'spider-elements'); |
| 92 | $data[ 'status' ] = 'activated'; |
| 93 | } else { |
| 94 | $data[ 'title' ] = __('Activate Now', 'spider-elements'); |
| 95 | $data[ 'status' ] = 'installed'; |
| 96 | $data[ 'activation_url' ] = $this->activation_url($name); |
| 97 | } |
| 98 | } else { |
| 99 | $data[ 'title' ] = __('Install Now', 'spider-elements'); |
| 100 | $data[ 'status' ] = 'not_installed'; |
| 101 | $data[ 'installation_url' ] = $this->installation_url($name); |
| 102 | $data[ 'activation_url' ] = $this->activation_url($name); |
| 103 | } |
| 104 | |
| 105 | return $data; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Check if a plugin is installed |
| 110 | * |
| 111 | * @param string $name Plugin name |
| 112 | * |
| 113 | * @return bool True if the plugin is installed, false otherwise |
| 114 | */ |
| 115 | public function check_installed_plugin ($name) |
| 116 | { |
| 117 | return in_array($name, $this->installedPlugins); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Check if a plugin is activated |
| 122 | * |
| 123 | * @param string $name Plugin name |
| 124 | * |
| 125 | * @return bool True if the plugin is activated, false otherwise |
| 126 | */ |
| 127 | public function check_activated_plugin ($name) |
| 128 | { |
| 129 | return in_array($name, $this->activatedPlugins); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get the activation URL for a plugin |
| 134 | * |
| 135 | * @param string $pluginName Plugin name |
| 136 | * |
| 137 | * @return string Activation URL |
| 138 | */ |
| 139 | public function activation_url ($pluginName) |
| 140 | { |
| 141 | |
| 142 | return wp_nonce_url( |
| 143 | add_query_arg( |
| 144 | array( |
| 145 | 'action' => 'activate', |
| 146 | 'plugin' => $pluginName, |
| 147 | 'plugin_status' => 'all', |
| 148 | 'paged' => '1&s', |
| 149 | ), |
| 150 | admin_url('plugins.php') |
| 151 | ), |
| 152 | 'activate-plugin_' . $pluginName |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get the installation URL for a plugin |
| 158 | * |
| 159 | * @param string $pluginName Plugin name |
| 160 | * |
| 161 | * @return string Installation URL |
| 162 | */ |
| 163 | public function installation_url ($pluginName) |
| 164 | { |
| 165 | $action = 'install-plugin'; |
| 166 | $pluginSlug = $this->get_plugin_slug($pluginName); |
| 167 | |
| 168 | return wp_nonce_url( |
| 169 | add_query_arg( |
| 170 | array( |
| 171 | 'action' => $action, |
| 172 | 'plugin' => $pluginSlug, |
| 173 | ), |
| 174 | admin_url('update.php') |
| 175 | ), |
| 176 | $action . '_' . $pluginSlug |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Get the plugin slug from a plugin name |
| 182 | * |
| 183 | * @param string $name Plugin name |
| 184 | * |
| 185 | * @return string|null Plugin slug |
| 186 | */ |
| 187 | public function get_plugin_slug ($name) |
| 188 | { |
| 189 | $split = explode('/', $name); |
| 190 | |
| 191 | return isset($split[ 0 ]) ? $split[ 0 ] : null; |
| 192 | } |
| 193 | |
| 194 | } |