| 1 |
<?php namespace Premmerce\Premmerce\Api; |
| 2 |
|
| 3 |
use Automatic_Upgrader_Skin; |
| 4 |
use Plugin_Upgrader; |
| 5 |
use Premmerce\Premmerce\Data\Plugins; |
| 6 |
use Premmerce\SDK\V2\FileManager\FileManager; |
| 7 |
|
| 8 |
class PluginApi |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var array |
| 12 |
*/ |
| 13 |
private $plugins = array(); |
| 14 |
|
| 15 |
/** |
| 16 |
* @var FileManager |
| 17 |
*/ |
| 18 |
private $fileManager; |
| 19 |
|
| 20 |
public function __construct(FileManager $fileManager) |
| 21 |
{ |
| 22 |
$this->fileManager = $fileManager; |
| 23 |
$this->init(); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @param $slug |
| 28 |
* |
| 29 |
* @return array|object|\WP_Error |
| 30 |
*/ |
| 31 |
public function getInfoPlugin($slug) |
| 32 |
{ |
| 33 |
if (!function_exists('plugins_api')) { |
| 34 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 35 |
} |
| 36 |
|
| 37 |
return plugins_api('plugin_information', array('slug' => $slug)); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @return array |
| 42 |
*/ |
| 43 |
public function getExtra() |
| 44 |
{ |
| 45 |
$plugins = Plugins::getAll(); |
| 46 |
foreach ($plugins as $slug => &$plugin) { |
| 47 |
if (isset($plugin['link'])) { |
| 48 |
$install = array( |
| 49 |
"handler" => "premmerce.plugins", |
| 50 |
"action" => "premmerce_actions", |
| 51 |
"value" => $slug, |
| 52 |
"option" => "install", |
| 53 |
); |
| 54 |
} else { |
| 55 |
$install = array( |
| 56 |
"handler" => "wp.updates", |
| 57 |
'action' => 'install-plugin', |
| 58 |
'slug' => $slug, |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
$plugin['install_action'] = $install; |
| 63 |
$plugin['activate_action'] = array( |
| 64 |
"handler" => "premmerce.plugins", |
| 65 |
"action" => "premmerce_actions", |
| 66 |
"value" => $slug, |
| 67 |
"option" => "activate", |
| 68 |
); |
| 69 |
}; |
| 70 |
|
| 71 |
return $plugins; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
public function getInstalledPlugins() |
| 78 |
{ |
| 79 |
return $this->plugins; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param $slug |
| 84 |
* |
| 85 |
* @return mixed |
| 86 |
*/ |
| 87 |
public function getInstalledPlugin($slug) |
| 88 |
{ |
| 89 |
return $this->plugins[ $slug ]; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @param string $slug |
| 94 |
* |
| 95 |
* @return bool |
| 96 |
*/ |
| 97 |
public function isInstalledPlugin($slug) |
| 98 |
{ |
| 99 |
return array_key_exists($slug, $this->plugins); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @param string $slug |
| 104 |
* |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
public function isActivePlugin($slug) |
| 108 |
{ |
| 109 |
if ($this->isInstalledPlugin($slug)) { |
| 110 |
$name = $this->plugins[ $slug ]['plugin']; |
| 111 |
|
| 112 |
return is_plugin_active($name); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @param string $slug |
| 118 |
* |
| 119 |
* @return null|bool|\WP_Error |
| 120 |
*/ |
| 121 |
public function activatePlugin($slug) |
| 122 |
{ |
| 123 |
if (!current_user_can('activate_plugins')) { |
| 124 |
return new \WP_Error('permission_denied', __('Sorry, you are not allowed to activate this plugin.')); |
| 125 |
} |
| 126 |
if (!$this->isActivePlugin($slug)) { |
| 127 |
$name = $this->plugins[ $slug ]['plugin']; |
| 128 |
$result = activate_plugin($name); |
| 129 |
if (is_null($result)) { |
| 130 |
return true; |
| 131 |
} else { |
| 132 |
return $result; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* @param $slug |
| 139 |
* |
| 140 |
* @return \WP_Error|mixed |
| 141 |
*/ |
| 142 |
public function deactivatePlugin($slug) |
| 143 |
{ |
| 144 |
if (!current_user_can('deactivate_plugins')) { |
| 145 |
return new \WP_Error('permission_denied', __('Sorry, you are not allowed to deactivate plugins for this site.')); |
| 146 |
} |
| 147 |
if ($this->isActivePlugin($slug)) { |
| 148 |
$name = $this->plugins[ $slug ]['plugin']; |
| 149 |
|
| 150 |
return deactivate_plugins($name); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Reload plugins |
| 156 |
*/ |
| 157 |
public function reloadPlugins() |
| 158 |
{ |
| 159 |
wp_clean_plugins_cache(); |
| 160 |
$this->init(); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Load plugins |
| 165 |
*/ |
| 166 |
private function init() |
| 167 |
{ |
| 168 |
if (!function_exists('get_plugins')) { |
| 169 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 170 |
} |
| 171 |
$plugins = get_plugins(); |
| 172 |
|
| 173 |
foreach ($plugins as $name => $plugin) { |
| 174 |
$parts = explode('/', $name); |
| 175 |
$slug = array_shift($parts); |
| 176 |
$this->plugins[ $slug ] = $plugin; |
| 177 |
$this->plugins[ $slug ]['plugin'] = $name; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* @param $slug |
| 183 |
* |
| 184 |
* @return bool |
| 185 |
*/ |
| 186 |
public function activateTheme($slug) |
| 187 |
{ |
| 188 |
switch_theme($slug); |
| 189 |
|
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* @param $slug |
| 195 |
* |
| 196 |
* @return bool |
| 197 |
*/ |
| 198 |
public function isActiveTheme($slug) |
| 199 |
{ |
| 200 |
return wp_get_theme()->get_template() == $slug; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* @param $slug |
| 205 |
* |
| 206 |
* @return bool |
| 207 |
*/ |
| 208 |
public function isInstalledTheme($slug) |
| 209 |
{ |
| 210 |
$themes = $this->getThemes(); |
| 211 |
|
| 212 |
return array_key_exists($slug, $themes); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* @return array |
| 217 |
*/ |
| 218 |
public function getThemes() |
| 219 |
{ |
| 220 |
return wp_get_themes(); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* @param string $slug |
| 225 |
* |
| 226 |
* @return bool|\WP_Error |
| 227 |
*/ |
| 228 |
public function upgrade($slug) |
| 229 |
{ |
| 230 |
return $this->getPluginUpgrader()->upgrade($slug); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* @param string $slug |
| 235 |
* |
| 236 |
* @return bool|\WP_Error |
| 237 |
*/ |
| 238 |
public function installPlugin($slug) |
| 239 |
{ |
| 240 |
if (!current_user_can('install_plugins')) { |
| 241 |
return new \WP_Error('permission_denied', __('Sorry, you are not allowed to install plugins on this site.')); |
| 242 |
} |
| 243 |
if (!$this->isInstalledPlugin($slug)) { |
| 244 |
if ($path = Plugins::getPath($slug)) { |
| 245 |
$link = $this->fileManager->getPluginDirectory() . $path; |
| 246 |
} else { |
| 247 |
$result = $this->getInfoPlugin($slug); |
| 248 |
$link = $result->download_link; |
| 249 |
} |
| 250 |
|
| 251 |
$result = $this->getPluginUpgrader()->install($link); |
| 252 |
|
| 253 |
$this->reloadPlugins(); |
| 254 |
|
| 255 |
return $result; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* @return Plugin_Upgrader |
| 261 |
*/ |
| 262 |
private function getPluginUpgrader() |
| 263 |
{ |
| 264 |
$includes = ABSPATH . 'wp-admin/includes/'; |
| 265 |
if (!class_exists('WP_Upgrader')) { |
| 266 |
include_once $includes . 'class-wp-upgrader.php'; |
| 267 |
} |
| 268 |
if (!class_exists('Plugin_Upgrader')) { |
| 269 |
include_once $includes . 'class-plugin-upgrader.php'; |
| 270 |
} |
| 271 |
|
| 272 |
return new Plugin_Upgrader(new Automatic_Upgrader_Skin()); |
| 273 |
} |
| 274 |
} |
| 275 |
|