dashboard
5 months ago
extension
5 months ago
Assets.php
5 months ago
Dashboard.php
5 months ago
Module_Settings.php
5 months ago
Plugin_Installer.php
5 months ago
Plugin_Installer.php
201 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 $activated_plugins = []; |
| 16 | private $initialized = false; |
| 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 | // If this class is instantiated after `plugins_loaded` has already fired, |
| 39 | // the hook would never run and status checks would always report |
| 40 | // "not_installed". So we initialize immediately when possible. |
| 41 | if ( \did_action( 'plugins_loaded' ) ) { |
| 42 | $this->init(); |
| 43 | } else { |
| 44 | \add_action( 'plugins_loaded', [ $this, 'init' ] ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Initializes the class |
| 50 | */ |
| 51 | public function init(): void |
| 52 | { |
| 53 | $this->collect_activated_plugins(); |
| 54 | |
| 55 | $this->initialized = true; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Collects the list of activated plugins |
| 60 | */ |
| 61 | private function collect_activated_plugins(): void |
| 62 | { |
| 63 | $this->activated_plugins = \get_option( 'active_plugins', [] ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Check if a plugin is installed |
| 68 | * |
| 69 | * @param string $name Plugin name |
| 70 | * |
| 71 | * @return bool True if the plugin is installed, false otherwise |
| 72 | */ |
| 73 | public function check_installed_plugin(string $name): bool |
| 74 | { |
| 75 | if ( $this->check_activated_plugin( $name ) ) { |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | // Security: Prevent path traversal |
| 80 | if ( 0 !== validate_file( $name ) ) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | // Sentinel Fix: Use file check instead of heavy get_plugins() scan |
| 85 | return file_exists( WP_PLUGIN_DIR . '/' . $name ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Check if a plugin is activated |
| 90 | * |
| 91 | * @param string $name Plugin name |
| 92 | * |
| 93 | * @return bool True if the plugin is activated, false otherwise |
| 94 | */ |
| 95 | public function check_activated_plugin(string $name): bool |
| 96 | { |
| 97 | return in_array( $name, $this->activated_plugins, true ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get the status information for a plugin |
| 102 | * |
| 103 | * @param string $name Plugin name |
| 104 | * |
| 105 | * @return array Plugin status information |
| 106 | */ |
| 107 | public function get_status(string $name): array |
| 108 | { |
| 109 | if ( ! $this->initialized ) { |
| 110 | $this->init(); |
| 111 | } |
| 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', 'spider-elements' ); |
| 127 | $data['status'] = 'inactive'; |
| 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 | return $data; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get the activation URL for a plugin |
| 142 | * |
| 143 | * @param string $pluginName Plugin name |
| 144 | * |
| 145 | * @return string Activation URL |
| 146 | */ |
| 147 | public function activation_url(string $pluginName): string |
| 148 | { |
| 149 | return \wp_nonce_url( |
| 150 | \add_query_arg( |
| 151 | array( |
| 152 | 'action' => 'activate', |
| 153 | 'plugin' => $pluginName, |
| 154 | 'plugin_status' => 'all', |
| 155 | 'paged' => '1&s', |
| 156 | ), |
| 157 | \admin_url( 'plugins.php' ) |
| 158 | ), |
| 159 | 'activate-plugin_' . $pluginName |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get the installation URL for a plugin |
| 165 | * |
| 166 | * @param string $pluginName Plugin name |
| 167 | * |
| 168 | * @return string Installation URL |
| 169 | */ |
| 170 | public function installation_url(string $pluginName): string |
| 171 | { |
| 172 | $action = 'install-plugin'; |
| 173 | $pluginSlug = $this->get_plugin_slug($pluginName); |
| 174 | |
| 175 | return \wp_nonce_url( |
| 176 | \add_query_arg( |
| 177 | array( |
| 178 | 'action' => $action, |
| 179 | 'plugin' => $pluginSlug, |
| 180 | ), |
| 181 | \admin_url( 'update.php' ) |
| 182 | ), |
| 183 | $action . '_' . $pluginSlug |
| 184 | ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get the plugin slug from a plugin name |
| 189 | * |
| 190 | * @param string $name Plugin name |
| 191 | * |
| 192 | * @return string Plugin slug |
| 193 | */ |
| 194 | public function get_plugin_slug(string $name): string |
| 195 | { |
| 196 | $split = explode('/', $name); |
| 197 | |
| 198 | return $split[0] ?? ''; |
| 199 | } |
| 200 | } |
| 201 |