| @@ -330,107 +330,112 @@ | ||
| 330 | 330 | return new \WP_Error('plugin_install_error', $exception->getMessage()); |
| 331 | 331 | } |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | - /** | |
| 335 | - * Install and activate a plugin. Resolves the package from wordpress.org | |
| 336 | - * unless $downloadUrl is given, for plugins hosted outside the repo. | |
| 337 | - */ | |
| 338 | - public static function backgroundInstaller($plugin_to_install, $downloadUrl = null) | |
| 334 | + public static function backgroundInstaller($plugin_to_install) | |
| 339 | 335 | { |
| 340 | - if (empty($plugin_to_install['repo-slug'])) { | |
| 341 | - return; | |
| 342 | - } | |
| 336 | + if (!empty($plugin_to_install['repo-slug'])) { | |
| 337 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 338 | + require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; | |
| 339 | + require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
| 340 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 343 | 341 | |
| 344 | - require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 345 | - require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; | |
| 346 | - require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
| 347 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 342 | + WP_Filesystem(); | |
| 348 | 343 | |
| 349 | - WP_Filesystem(); | |
| 344 | + $skin = new \Automatic_Upgrader_Skin(); | |
| 345 | + $upgrader = new \WP_Upgrader($skin); | |
| 346 | + $installed_plugins = array_reduce(array_keys(\get_plugins()), array(self::class, 'associate_plugin_file'), array()); | |
| 347 | + $plugin_slug = $plugin_to_install['repo-slug']; | |
| 348 | + $plugin_file = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $plugin_slug . '.php'; | |
| 349 | + $installed = false; | |
| 350 | + $activate = false; | |
| 350 | 351 | |
| 351 | - $installedPlugins = array_reduce(array_keys(\get_plugins()), array(self::class, 'associate_plugin_file'), array()); | |
| 352 | + // See if the plugin is installed already. | |
| 353 | + if (isset($installed_plugins[$plugin_file])) { | |
| 354 | + $installed = true; | |
| 355 | + $activate = !is_plugin_active($installed_plugins[$plugin_file]); | |
| 356 | + } | |
| 352 | 357 | |
| 353 | - $pluginSlug = $plugin_to_install['repo-slug']; | |
| 354 | - $pluginFile = isset($plugin_to_install['file']) ? $plugin_to_install['file'] : $pluginSlug . '.php'; | |
| 355 | - $isInstalled = isset($installedPlugins[$pluginFile]); | |
| 358 | + // Install this thing! | |
| 359 | + if (!$installed) { | |
| 360 | + // Suppress feedback. | |
| 361 | + ob_start(); | |
| 356 | 362 | |
| 357 | - if ($isInstalled) { | |
| 358 | - $needsActivation = !is_plugin_active($installedPlugins[$pluginFile]); | |
| 359 | - } else { | |
| 360 | - $upgrader = new \WP_Upgrader(new \Automatic_Upgrader_Skin()); | |
| 363 | + try { | |
| 364 | + $plugin_information = plugins_api( | |
| 365 | + 'plugin_information', | |
| 366 | + array( | |
| 367 | + 'slug' => $plugin_slug, | |
| 368 | + 'fields' => array( | |
| 369 | + 'short_description' => false, | |
| 370 | + 'sections' => false, | |
| 371 | + 'requires' => false, | |
| 372 | + 'rating' => false, | |
| 373 | + 'ratings' => false, | |
| 374 | + 'downloaded' => false, | |
| 375 | + 'last_updated' => false, | |
| 376 | + 'added' => false, | |
| 377 | + 'tags' => false, | |
| 378 | + 'homepage' => false, | |
| 379 | + 'donate_link' => false, | |
| 380 | + 'author_profile' => false, | |
| 381 | + 'author' => false, | |
| 382 | + ), | |
| 383 | + ) | |
| 384 | + ); | |
| 361 | 385 | |
| 362 | - // The upgrader prints progress markup that would corrupt the response. | |
| 363 | - ob_start(); | |
| 386 | + if (is_wp_error($plugin_information)) { | |
| 387 | + throw new \Exception(wp_kses_post($plugin_information->get_error_message())); | |
| 388 | + } | |
| 364 | 389 | |
| 365 | - try { | |
| 366 | - $package = $downloadUrl; | |
| 390 | + $package = $plugin_information->download_link; | |
| 391 | + $download = $upgrader->download_package($package); | |
| 367 | 392 | |
| 368 | - if (!$package) { | |
| 369 | - $information = plugins_api('plugin_information', array( | |
| 370 | - 'slug' => $pluginSlug, | |
| 371 | - 'fields' => array( | |
| 372 | - 'short_description' => false, | |
| 373 | - 'sections' => false, | |
| 374 | - 'requires' => false, | |
| 375 | - 'rating' => false, | |
| 376 | - 'ratings' => false, | |
| 377 | - 'downloaded' => false, | |
| 378 | - 'last_updated' => false, | |
| 379 | - 'added' => false, | |
| 380 | - 'tags' => false, | |
| 381 | - 'homepage' => false, | |
| 382 | - 'donate_link' => false, | |
| 383 | - 'author_profile' => false, | |
| 384 | - 'author' => false, | |
| 385 | - ), | |
| 386 | - )); | |
| 387 | - if (is_wp_error($information)) { | |
| 388 | - throw new \Exception(wp_kses_post($information->get_error_message())); | |
| 393 | + if (is_wp_error($download)) { | |
| 394 | + throw new \Exception(wp_kses_post($download->get_error_message())); | |
| 389 | 395 | } |
| 390 | 396 | |
| 391 | - $package = $information->download_link; | |
| 392 | - } | |
| 397 | + $working_dir = $upgrader->unpack_package($download, true); | |
| 393 | 398 | |
| 394 | - $download = $upgrader->download_package($package); | |
| 395 | - if (is_wp_error($download)) { | |
| 396 | - throw new \Exception(wp_kses_post($download->get_error_message())); | |
| 397 | - } | |
| 399 | + if (is_wp_error($working_dir)) { | |
| 400 | + throw new \Exception(wp_kses_post($working_dir->get_error_message())); | |
| 401 | + } | |
| 398 | 402 | |
| 399 | - $workingDir = $upgrader->unpack_package($download, true); | |
| 400 | - if (is_wp_error($workingDir)) { | |
| 401 | - throw new \Exception(wp_kses_post($workingDir->get_error_message())); | |
| 402 | - } | |
| 403 | + $result = $upgrader->install_package( | |
| 404 | + array( | |
| 405 | + 'source' => $working_dir, | |
| 406 | + 'destination' => WP_PLUGIN_DIR, | |
| 407 | + 'clear_destination' => false, | |
| 408 | + 'abort_if_destination_exists' => false, | |
| 409 | + 'clear_working' => true, | |
| 410 | + 'hook_extra' => array( | |
| 411 | + 'type' => 'plugin', | |
| 412 | + 'action' => 'install', | |
| 413 | + ), | |
| 414 | + ) | |
| 415 | + ); | |
| 403 | 416 | |
| 404 | - $installed = $upgrader->install_package(array( | |
| 405 | - 'source' => $workingDir, | |
| 406 | - 'destination' => WP_PLUGIN_DIR, | |
| 407 | - 'clear_destination' => false, | |
| 408 | - 'abort_if_destination_exists' => false, | |
| 409 | - 'clear_working' => true, | |
| 410 | - 'hook_extra' => array( | |
| 411 | - 'type' => 'plugin', | |
| 412 | - 'action' => 'install', | |
| 413 | - ), | |
| 414 | - )); | |
| 415 | - if (is_wp_error($installed)) { | |
| 416 | - throw new \Exception(wp_kses_post($installed->get_error_message())); | |
| 417 | + if (is_wp_error($result)) { | |
| 418 | + throw new \Exception(wp_kses_post($result->get_error_message())); | |
| 419 | + } | |
| 420 | + | |
| 421 | + $activate = true; | |
| 422 | + } finally { | |
| 423 | + // Discard feedback. | |
| 424 | + ob_end_clean(); | |
| 417 | 425 | } |
| 418 | - } finally { | |
| 419 | - ob_end_clean(); | |
| 420 | 426 | } |
| 421 | 427 | |
| 422 | - $needsActivation = true; | |
| 423 | - } | |
| 428 | + wp_clean_plugins_cache(); | |
| 424 | 429 | |
| 425 | - wp_clean_plugins_cache(); | |
| 430 | + // Activate this thing. | |
| 431 | + if ($activate) { | |
| 432 | + $result = activate_plugin($installed ? $installed_plugins[$plugin_file] : $plugin_slug . '/' . $plugin_file); | |
| 426 | 433 | |
| 427 | - if (!$needsActivation) return; | |
| 428 | - | |
| 429 | - $activated = activate_plugin($isInstalled ? $installedPlugins[$pluginFile] : $pluginSlug . '/' . $pluginFile); | |
| 430 | - | |
| 431 | - if (is_wp_error($activated)) { | |
| 432 | - throw new \Exception(wp_kses_post($activated->get_error_message())); | |
| 434 | + if (is_wp_error($result)) { | |
| 435 | + throw new \Exception(wp_kses_post($result->get_error_message())); | |
| 436 | + } | |
| 437 | + } | |
| 433 | 438 | } |
| 434 | 439 | } |
| 435 | 440 | |
| 436 | 441 | private static function associate_plugin_file($plugins, $key) |