API
3 weeks ago
BlockTemplates
2 years ago
Composer
2 years ago
DateTimeProvider
4 years ago
Features
1 week ago
Marketing
1 year ago
Notes
4 months ago
Overrides
4 weeks ago
PluginsInstallLoggers
1 year ago
PluginsProvider
4 years ago
RemoteInboxNotifications
4 weeks ago
RemoteSpecs
4 weeks ago
Schedulers
1 year ago
DataSourcePoller.php
2 years ago
DeprecatedClassFacade.php
1 year ago
FeaturePlugin.php
4 years ago
Loader.php
2 years ago
PageController.php
3 months ago
PluginsHelper.php
2 months ago
PluginsInstaller.php
4 years ago
ReportCSVEmail.php
1 year ago
ReportCSVExporter.php
1 year ago
ReportExporter.php
3 years ago
ReportsSync.php
3 months ago
WCAdminHelper.php
1 year ago
PluginsInstaller.php
120 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PluginsInstaller |
| 4 | * |
| 5 | * Installer to allow plugin installation via URL query. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\API\Plugins; |
| 13 | use Automattic\WooCommerce\Admin\Features\TransientNotices; |
| 14 | |
| 15 | /** |
| 16 | * Class PluginsInstaller |
| 17 | */ |
| 18 | class PluginsInstaller { |
| 19 | |
| 20 | /** |
| 21 | * Constructor |
| 22 | */ |
| 23 | public static function init() { |
| 24 | add_action( 'admin_init', array( __CLASS__, 'possibly_install_activate_plugins' ) ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Check if an install or activation is being requested via URL query. |
| 29 | */ |
| 30 | public static function possibly_install_activate_plugins() { |
| 31 | /* phpcs:disable WordPress.Security.NonceVerification.Recommended */ |
| 32 | if ( |
| 33 | ! isset( $_GET['plugin_action'] ) || |
| 34 | ! isset( $_GET['plugins'] ) || |
| 35 | ! current_user_can( 'install_plugins' ) || |
| 36 | ! isset( $_GET['nonce'] ) |
| 37 | ) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | $nonce = sanitize_text_field( wp_unslash( $_GET['nonce'] ) ); |
| 42 | |
| 43 | if ( ! wp_verify_nonce( $nonce, 'install-plugin' ) ) { |
| 44 | wp_nonce_ays( 'install-plugin' ); |
| 45 | } |
| 46 | |
| 47 | $plugins = sanitize_text_field( wp_unslash( $_GET['plugins'] ) ); |
| 48 | $plugin_action = sanitize_text_field( wp_unslash( $_GET['plugin_action'] ) ); |
| 49 | /* phpcs:enable WordPress.Security.NonceVerification.Recommended */ |
| 50 | |
| 51 | $plugins_api = new Plugins(); |
| 52 | $install_result = null; |
| 53 | $activate_result = null; |
| 54 | |
| 55 | switch ( $plugin_action ) { |
| 56 | case 'install': |
| 57 | $install_result = $plugins_api->install_plugins( array( 'plugins' => $plugins ) ); |
| 58 | break; |
| 59 | case 'activate': |
| 60 | $activate_result = $plugins_api->activate_plugins( array( 'plugins' => $plugins ) ); |
| 61 | break; |
| 62 | case 'install-activate': |
| 63 | $install_result = $plugins_api->install_plugins( array( 'plugins' => $plugins ) ); |
| 64 | $activate_result = $plugins_api->activate_plugins( array( 'plugins' => implode( ',', $install_result['data']['installed'] ) ) ); |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | self::cache_results( $plugins, $install_result, $activate_result ); |
| 69 | self::redirect_to_referer(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Display the results of installation and activation on the page. |
| 74 | * |
| 75 | * @param string $plugins Comma separated list of plugins. |
| 76 | * @param array $install_result Result of installation. |
| 77 | * @param array $activate_result Result of activation. |
| 78 | */ |
| 79 | public static function cache_results( $plugins, $install_result, $activate_result ) { |
| 80 | if ( ! $install_result && ! $activate_result ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | if ( is_wp_error( $install_result ) || is_wp_error( $activate_result ) ) { |
| 85 | $message = $activate_result ? $activate_result->get_error_message() : $install_result->get_error_message(); |
| 86 | } else { |
| 87 | $message = $activate_result ? $activate_result['message'] : $install_result['message']; |
| 88 | } |
| 89 | |
| 90 | TransientNotices::add( |
| 91 | array( |
| 92 | 'user_id' => get_current_user_id(), |
| 93 | 'id' => 'plugin-installer-' . str_replace( ',', '-', $plugins ), |
| 94 | 'status' => 'success', |
| 95 | 'content' => $message, |
| 96 | ) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Redirect back to the referring page if one exists. |
| 102 | */ |
| 103 | public static function redirect_to_referer() { |
| 104 | $referer = wp_get_referer(); |
| 105 | if ( $referer && 0 !== strpos( $referer, wp_login_url() ) ) { |
| 106 | wp_safe_redirect( $referer ); |
| 107 | exit(); |
| 108 | } |
| 109 | |
| 110 | if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | $url = remove_query_arg( 'plugin_action', wp_unslash( $_SERVER['REQUEST_URI'] ) ); // phpcs:ignore sanitization ok. |
| 115 | $url = remove_query_arg( 'plugins', $url ); |
| 116 | wp_safe_redirect( $url ); |
| 117 | exit(); |
| 118 | } |
| 119 | } |
| 120 |