Merge pull request #19728 from woocommerce/fix/plugin-installer-non-slug-filenames

Setup wizard: Bug fix and support for non-slug filenames in plugin installer
This commit is contained in:
Claudiu Lodromanean 2018-04-17 08:39:16 -07:00 committed by GitHub
commit 90dfbf0ccd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 16 deletions

View File

@ -593,7 +593,7 @@ class WC_Admin_Setup_Wizard {
* Helper method to queue the background install of a plugin. * Helper method to queue the background install of a plugin.
* *
* @param string $plugin_id Plugin id used for background install. * @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 ) { protected function install_plugin( $plugin_id, $plugin_info ) {
// Make sure we don't trigger multiple simultaneous installs. // Make sure we don't trigger multiple simultaneous installs.
@ -601,7 +601,8 @@ class WC_Admin_Setup_Wizard {
return; 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; return;
} }
@ -647,7 +648,6 @@ class WC_Admin_Setup_Wizard {
$this->install_plugin( $this->install_plugin(
'jetpack', 'jetpack',
array( array(
'file' => 'jetpack/jetpack.php',
'name' => __( 'Jetpack', 'woocommerce' ), 'name' => __( 'Jetpack', 'woocommerce' ),
'repo-slug' => 'jetpack', 'repo-slug' => 'jetpack',
) )
@ -662,7 +662,6 @@ class WC_Admin_Setup_Wizard {
$this->install_plugin( $this->install_plugin(
'woocommerce-services', 'woocommerce-services',
array( array(
'file' => 'woocommerce-services/woocommerce-services.php',
'name' => __( 'WooCommerce Services', 'woocommerce' ), 'name' => __( 'WooCommerce Services', 'woocommerce' ),
'repo-slug' => 'woocommerce-services', 'repo-slug' => 'woocommerce-services',
) )
@ -1891,7 +1890,6 @@ class WC_Admin_Setup_Wizard {
} }
WC_Install::background_installer( 'jetpack', array( WC_Install::background_installer( 'jetpack', array(
'file' => 'jetpack/jetpack.php',
'name' => __( 'Jetpack', 'woocommerce' ), 'name' => __( 'Jetpack', 'woocommerce' ),
'repo-slug' => 'jetpack', 'repo-slug' => 'jetpack',
) ); ) );

View File

@ -1058,13 +1058,12 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
/** /**
* Get slug from path and associate it with the path. * 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. * @param string $key Plugin relative path. Example: woocommerce/woocommerce.php.
*/ */
private static function associate_plugin_slug( $plugins, $key ) { private static function associate_plugin_file( $plugins, $key ) {
$slug = explode( '/', $key ); $path = explode( '/', $key );
$slug = explode( '.', end( $slug ) ); $plugins[ $path[1] ] = $key;
$plugins[ $slug[0] ] = $key;
return $plugins; return $plugins;
} }
@ -1091,15 +1090,16 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
$skin = new Automatic_Upgrader_Skin(); $skin = new Automatic_Upgrader_Skin();
$upgrader = new WP_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_slug = $plugin_to_install['repo-slug'];
$plugin_file = isset( $plugin_to_install['file'] ) ? $plugin_to_install['file'] : $plugin_slug . '.php';
$installed = false; $installed = false;
$activate = false; $activate = false;
// See if the plugin is installed already. // See if the plugin is installed already.
if ( isset( $installed_plugins[ $plugin_slug ] ) ) { if ( isset( $installed_plugins[ $plugin_file ] ) ) {
$installed = true; $installed = true;
$activate = ! is_plugin_active( $installed_plugins[ $plugin_slug ] ); $activate = ! is_plugin_active( $installed_plugins[ $plugin_file ] );
} }
// Install this thing! // Install this thing!
@ -1111,7 +1111,7 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
$plugin_information = plugins_api( $plugin_information = plugins_api(
'plugin_information', 'plugin_information',
array( array(
'slug' => $plugin_to_install['repo-slug'], 'slug' => $plugin_slug,
'fields' => array( 'fields' => array(
'short_description' => false, 'short_description' => false,
'sections' => false, 'sections' => false,
@ -1175,7 +1175,7 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
__( '%1$s could not be installed (%2$s). <a href="%3$s">Please install it manually by clicking here.</a>', 'woocommerce' ), __( '%1$s could not be installed (%2$s). <a href="%3$s">Please install it manually by clicking here.</a>', 'woocommerce' ),
$plugin_to_install['name'], $plugin_to_install['name'],
$e->getMessage(), $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. // Activate this thing.
if ( $activate ) { if ( $activate ) {
try { try {
$result = activate_plugin( $installed_plugins[ $plugin_slug ] ); $result = activate_plugin( $installed ? $installed_plugins[ $plugin_file ] : $plugin_slug . '/' . $plugin_file );
if ( is_wp_error( $result ) ) { if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message() ); throw new Exception( $result->get_error_message() );