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