From 3814796a248ddef604eced625bccdb07962d5f63 Mon Sep 17 00:00:00 2001 From: Paul Dechov Date: Mon, 16 Apr 2018 07:16:59 -0400 Subject: [PATCH 1/4] Enhance plugin installer to accept plugin 'file' 'file' string as part of plugin information is used for activating plugin in combination with slug, or instead of slug in the case of plugins already installed. Defaults to [slug].php. --- includes/class-wc-install.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index 76541cf99ff..e46053c9134 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -1058,13 +1058,12 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta ( /** * Get slug from path and associate it with the path. * - * @param array $plugins Associative array of plugin slugs to paths. + * @param array $plugins Associative array of plugin files to paths. * @param string $key Plugin relative path. Example: woocommerce/woocommerce.php. */ - private static function associate_plugin_slug( $plugins, $key ) { - $slug = explode( '/', $key ); - $slug = explode( '.', end( $slug ) ); - $plugins[ $slug[0] ] = $key; + private static function associate_plugin_file( $plugins, $key ) { + $path = explode( '/', $key ); + $plugins[ $path[1] ] = $key; return $plugins; } @@ -1091,15 +1090,16 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta ( $skin = new Automatic_Upgrader_Skin(); $upgrader = new WP_Upgrader( $skin ); - $installed_plugins = array_reduce( array_keys( get_plugins() ), array( __CLASS__, 'associate_plugin_slug' ), array() ); + $installed_plugins = array_reduce( array_keys( get_plugins() ), array( __CLASS__, 'associate_plugin_file' ), array() ); $plugin_slug = $plugin_to_install['repo-slug']; + $plugin_file = isset( $plugin_to_install['file'] ) ? $plugin_to_install['file'] : $plugin_slug . '.php'; $installed = false; $activate = false; // See if the plugin is installed already. - if ( isset( $installed_plugins[ $plugin_slug ] ) ) { + if ( isset( $installed_plugins[ $plugin_file ] ) ) { $installed = true; - $activate = ! is_plugin_active( $installed_plugins[ $plugin_slug ] ); + $activate = ! is_plugin_active( $installed_plugins[ $plugin_file ] ); } // Install this thing! @@ -1111,7 +1111,7 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta ( $plugin_information = plugins_api( 'plugin_information', array( - 'slug' => $plugin_to_install['repo-slug'], + 'slug' => $plugin_slug, 'fields' => array( 'short_description' => false, 'sections' => false, @@ -1175,7 +1175,7 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta ( __( '%1$s could not be installed (%2$s). Please install it manually by clicking here.', 'woocommerce' ), $plugin_to_install['name'], $e->getMessage(), - esc_url( admin_url( 'index.php?wc-install-plugin-redirect=' . $plugin_to_install['repo-slug'] ) ) + esc_url( admin_url( 'index.php?wc-install-plugin-redirect=' . $plugin_slug ) ) ) ); } @@ -1189,7 +1189,7 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta ( // Activate this thing. if ( $activate ) { try { - $result = activate_plugin( $installed_plugins[ $plugin_slug ] ); + $result = activate_plugin( $installed_plugins[ $plugin_file ] ); if ( is_wp_error( $result ) ) { throw new Exception( $result->get_error_message() ); From 2b118c8ba88c89ecc50bee3a89ba9cdfef3dbc16 Mon Sep 17 00:00:00 2001 From: Paul Dechov Date: Mon, 16 Apr 2018 07:20:42 -0400 Subject: [PATCH 2/4] Always check for active plugin in wizard before scheduling install Check for active plugin independent of whether 'file' is present. Also omits directory from 'file' string and prepends [slug]/ to it. --- includes/admin/class-wc-admin-setup-wizard.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/admin/class-wc-admin-setup-wizard.php b/includes/admin/class-wc-admin-setup-wizard.php index d8f80974c07..9ed55b955b3 100644 --- a/includes/admin/class-wc-admin-setup-wizard.php +++ b/includes/admin/class-wc-admin-setup-wizard.php @@ -593,7 +593,7 @@ class WC_Admin_Setup_Wizard { * Helper method to queue the background install of a plugin. * * @param string $plugin_id Plugin id used for background install. - * @param array $plugin_info Plugin info array containing at least main file and repo slug. + * @param array $plugin_info Plugin info array containing name and repo-slug, and optionally file if different from [repo-slug].php. */ protected function install_plugin( $plugin_id, $plugin_info ) { // Make sure we don't trigger multiple simultaneous installs. @@ -601,7 +601,8 @@ class WC_Admin_Setup_Wizard { return; } - if ( ! empty( $plugin_info['file'] ) && is_plugin_active( $plugin_info['file'] ) ) { + $plugin_file = isset( $plugin_info['file'] ) ? $plugin_info['file'] : $plugin_info['repo-slug'] . '.php'; + if ( is_plugin_active( $plugin_info['repo-slug'] . '/' . $plugin_file ) ) { return; } From 73884400f1676da2770e6b9703e7b091ab8d3541 Mon Sep 17 00:00:00 2001 From: Paul Dechov Date: Mon, 16 Apr 2018 07:21:04 -0400 Subject: [PATCH 3/4] Remove unnecessary 'file' value for existing plugins --- includes/admin/class-wc-admin-setup-wizard.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/includes/admin/class-wc-admin-setup-wizard.php b/includes/admin/class-wc-admin-setup-wizard.php index 9ed55b955b3..84d2cbddef0 100644 --- a/includes/admin/class-wc-admin-setup-wizard.php +++ b/includes/admin/class-wc-admin-setup-wizard.php @@ -648,7 +648,6 @@ class WC_Admin_Setup_Wizard { $this->install_plugin( 'jetpack', array( - 'file' => 'jetpack/jetpack.php', 'name' => __( 'Jetpack', 'woocommerce' ), 'repo-slug' => 'jetpack', ) @@ -663,7 +662,6 @@ class WC_Admin_Setup_Wizard { $this->install_plugin( 'woocommerce-services', array( - 'file' => 'woocommerce-services/woocommerce-services.php', 'name' => __( 'WooCommerce Services', 'woocommerce' ), 'repo-slug' => 'woocommerce-services', ) @@ -1892,7 +1890,6 @@ class WC_Admin_Setup_Wizard { } WC_Install::background_installer( 'jetpack', array( - 'file' => 'jetpack/jetpack.php', 'name' => __( 'Jetpack', 'woocommerce' ), 'repo-slug' => 'jetpack', ) ); From a4f76e3529d046ee4284b6611df9929890c5f656 Mon Sep 17 00:00:00 2001 From: Paul Dechov Date: Mon, 16 Apr 2018 07:22:15 -0400 Subject: [PATCH 4/4] Fix broken activation when done immediately after installation --- includes/class-wc-install.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index e46053c9134..8a2c9115a3c 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -1189,7 +1189,7 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta ( // Activate this thing. if ( $activate ) { try { - $result = activate_plugin( $installed_plugins[ $plugin_file ] ); + $result = activate_plugin( $installed ? $installed_plugins[ $plugin_file ] : $plugin_slug . '/' . $plugin_file ); if ( is_wp_error( $result ) ) { throw new Exception( $result->get_error_message() );