From db112275c2a85f2c8ffd34cb3ccd6681eb865764 Mon Sep 17 00:00:00 2001 From: Bero Date: Fri, 29 Nov 2019 15:40:53 +0100 Subject: [PATCH 1/2] Return folder_exists error as success --- .../class-wc-wccom-site-installer.php | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/includes/wccom-site/class-wc-wccom-site-installer.php b/includes/wccom-site/class-wc-wccom-site-installer.php index 894f2b6f673..e3e83ac6430 100644 --- a/includes/wccom-site/class-wc-wccom-site-installer.php +++ b/includes/wccom-site/class-wc-wccom-site-installer.php @@ -15,6 +15,13 @@ defined( 'ABSPATH' ) || exit; */ class WC_WCCOM_Site_Installer { + /** + * Error message returned install_package if the folder already exists. + * + * @var string + */ + private static $folder_exists = 'folder_exists'; + /** * Default state. * @@ -249,6 +256,9 @@ class WC_WCCOM_Site_Installer { break; case 'move_product': $state_steps[ $product_id ]['installed_path'] = $result['destination']; + if ( $result[ self::$folder_exists ] ) { + $state_steps[ $product_id ]['warning'] = self::$folder_exists; + } break; } } @@ -369,7 +379,18 @@ class WC_WCCOM_Site_Installer { ), ); - return $upgrader->install_package( $package ); + $result = $upgrader->install_package( $package ); + + /** + * If install package returns error 'folder_exists' threat as success. + */ + if ( is_wp_error( $result ) && array_key_exists( self::$folder_exists, $result->errors ) ) { + return array( + self::$folder_exists => true, + 'destination' => $result->error_data[ self::$folder_exists ], + ); + } + return $result; } /** From 33d9f33fa35febb4dc217e567bbd05f28bd31e08 Mon Sep 17 00:00:00 2001 From: Bero Date: Mon, 2 Dec 2019 16:11:44 +0100 Subject: [PATCH 2/2] folder_exists plugin info added --- .../class-wc-wccom-site-installer.php | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/includes/wccom-site/class-wc-wccom-site-installer.php b/includes/wccom-site/class-wc-wccom-site-installer.php index e3e83ac6430..abe1b338500 100644 --- a/includes/wccom-site/class-wc-wccom-site-installer.php +++ b/includes/wccom-site/class-wc-wccom-site-installer.php @@ -247,6 +247,7 @@ class WC_WCCOM_Site_Installer { case 'get_product_info': $state_steps[ $product_id ]['download_url'] = $result['download_url']; $state_steps[ $product_id ]['product_type'] = $result['product_type']; + $state_steps[ $product_id ]['product_name'] = $result['product_name']; break; case 'download_product': $state_steps[ $product_id ]['download_path'] = $result; @@ -257,7 +258,10 @@ class WC_WCCOM_Site_Installer { case 'move_product': $state_steps[ $product_id ]['installed_path'] = $result['destination']; if ( $result[ self::$folder_exists ] ) { - $state_steps[ $product_id ]['warning'] = self::$folder_exists; + $state_steps[ $product_id ]['warning'] = array( + 'message' => self::$folder_exists, + 'plugin_info' => self::get_plugin_info( $state_steps[ $product_id ]['installed_path'] ), + ); } break; } @@ -297,6 +301,7 @@ class WC_WCCOM_Site_Installer { $result = json_decode( wp_remote_retrieve_body( $request ), true ); $product_info['product_type'] = $result['_product_type']; + $product_info['product_name'] = $result['name']; if ( ! empty( $result['_wporg_product'] ) && ! empty( $result['download_link'] ) ) { // For wporg product, download is set already from info response. @@ -532,4 +537,41 @@ class WC_WCCOM_Site_Installer { return false; } + + + /** + * Get plugin info + * + * @since 3.9.0 + * @param string $dir Directory name of the plugin. + * @return bool|array + */ + private static function get_plugin_info( $dir ) { + $plugin_folder = basename( $dir ); + + if ( ! function_exists( 'get_plugins' ) ) { + require_once ABSPATH . 'wp-admin/includes/plugin.php'; + } + + $plugins = get_plugins(); + + $related_plugins = array_filter( + $plugins, + function( $key ) use ( $plugin_folder ) { + return strpos( $key, $plugin_folder . '/' ) === 0; + }, + ARRAY_FILTER_USE_KEY + ); + + if ( 1 === count( $related_plugins ) ) { + $plugin_key = array_keys( $related_plugins )[0]; + $plugin_data = $plugins[ $plugin_key ]; + return array( + 'name' => $plugin_data['Name'], + 'version' => $plugin_data['Version'], + 'active' => is_plugin_active( $plugin_key ), + ); + } + return false; + } }