screens
3 years ago
action.php
3 years ago
api.php
3 years ago
base.php
3 years ago
plugin-status.php
3 years ago
plugin-status.php
179 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Core\Settings; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | class Plugin_Status |
| 8 | { |
| 9 | /** |
| 10 | * @var mixed |
| 11 | */ |
| 12 | private static $instance; |
| 13 | /** |
| 14 | * @var array |
| 15 | */ |
| 16 | private $installedPlugins = []; |
| 17 | /** |
| 18 | * @var array |
| 19 | */ |
| 20 | private $activatedPlugins = []; |
| 21 | |
| 22 | public function __construct() |
| 23 | { |
| 24 | $this->collect_installed_plugins(); |
| 25 | $this->collect_activated_plugins(); |
| 26 | } |
| 27 | |
| 28 | private function collect_installed_plugins() |
| 29 | { |
| 30 | foreach (get_plugins() as $key => $plugin) { |
| 31 | array_push($this->installedPlugins, $key); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | private function collect_activated_plugins() |
| 36 | { |
| 37 | foreach (apply_filters('active_plugins', get_option('active_plugins')) as $plugin) { |
| 38 | array_push($this->activatedPlugins, $plugin); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public static function instance() |
| 43 | { |
| 44 | if (!static::$instance) { |
| 45 | static::$instance = new static(); |
| 46 | } |
| 47 | |
| 48 | return static::$instance; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @return mixed |
| 53 | */ |
| 54 | public function get_installed_plugins() |
| 55 | { |
| 56 | return $this->installedPlugins; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return mixed |
| 61 | */ |
| 62 | public function get_activated_plugins() |
| 63 | { |
| 64 | return $this->activatedPlugins; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param $name |
| 69 | * @return mixed |
| 70 | */ |
| 71 | public function get_status($name) |
| 72 | { |
| 73 | $data = [ |
| 74 | "url" => "", |
| 75 | "activation_url" => "", |
| 76 | "installation_url" => "", |
| 77 | "title" => "", |
| 78 | "status" => "" |
| 79 | ]; |
| 80 | |
| 81 | if ($this->check_installed_plugin($name)) { |
| 82 | if ($this->check_activated_plugin($name)) { |
| 83 | $data['title'] = __('Activated', 'shopengine'); |
| 84 | $data['status'] = "activated"; |
| 85 | } else { |
| 86 | $data['title'] = __('Activate Now', 'shopengine'); |
| 87 | $data['status'] = 'installed'; |
| 88 | $data['activation_url'] = $this->activation_url($name); |
| 89 | } |
| 90 | } else { |
| 91 | $data['title'] = __('Install Now', 'shopengine'); |
| 92 | $data['status'] = 'not_installed'; |
| 93 | $data['installation_url'] = $this->installation_url($name); |
| 94 | $data['activation_url'] = $this->activation_url($name); |
| 95 | } |
| 96 | |
| 97 | return $data; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param $name |
| 102 | */ |
| 103 | public function check_installed_plugin($name) |
| 104 | { |
| 105 | return in_array($name, $this->installedPlugins); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param $name |
| 110 | */ |
| 111 | public function check_activated_plugin($name) |
| 112 | { |
| 113 | return in_array($name, $this->activatedPlugins); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @param $pluginName |
| 118 | */ |
| 119 | public function activation_url($pluginName) |
| 120 | { |
| 121 | |
| 122 | $url = wp_nonce_url(add_query_arg( |
| 123 | [ |
| 124 | 'action' => 'activate', |
| 125 | 'plugin' => $pluginName, |
| 126 | 'plugin_status' => 'all', |
| 127 | 'paged' => '1&s' |
| 128 | ], |
| 129 | admin_url('plugins.php') |
| 130 | ), 'activate-plugin_' . $pluginName); |
| 131 | |
| 132 | return str_replace('&', '&', $url); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @param $pluginName |
| 137 | */ |
| 138 | public function installation_url($pluginName) |
| 139 | { |
| 140 | $action = 'install-plugin'; |
| 141 | $pluginSlug = $this->get_plugin_slug($pluginName); |
| 142 | |
| 143 | $url = wp_nonce_url( |
| 144 | add_query_arg( |
| 145 | [ |
| 146 | 'action' => $action, |
| 147 | 'plugin' => $pluginSlug |
| 148 | ], |
| 149 | admin_url('update.php') |
| 150 | ), |
| 151 | $action . '_' . $pluginSlug |
| 152 | ); |
| 153 | |
| 154 | return str_replace('&', '&', $url); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @param $name |
| 159 | */ |
| 160 | public function get_plugin_slug($name) |
| 161 | { |
| 162 | $split = explode('/', $name); |
| 163 | |
| 164 | return isset($split[0]) ? $split[0] : null; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @param $pluginName |
| 169 | */ |
| 170 | public function activated_url($pluginName) |
| 171 | { |
| 172 | return add_query_arg( |
| 173 | [ |
| 174 | 'page' => $this->get_plugin_slug($pluginName) |
| 175 | ], |
| 176 | admin_url('admin.php')); |
| 177 | } |
| 178 | } |
| 179 |