From 2c57da223702481aac9fe6e12a61b0032871cb2a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 28 Apr 2015 17:59:22 +0200 Subject: [PATCH 1/2] Abstract single plugin activation to seperate method --- class-tgm-plugin-activation.php | 90 ++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 34 deletions(-) diff --git a/class-tgm-plugin-activation.php b/class-tgm-plugin-activation.php index 3036c457..96e13747 100644 --- a/class-tgm-plugin-activation.php +++ b/class-tgm-plugin-activation.php @@ -573,17 +573,12 @@ protected function do_plugin_install() { // Only activate plugins if the config option is set to true. if ( $this->is_automatic ) { $plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method. - $activate = activate_plugin( $plugin_activate ); // Activate the plugin. - $this->populate_file_path( $slug ); // Re-populate the file path now that the plugin has been installed and activated. - - if ( is_wp_error( $activate ) ) { - echo '

', wp_kses_post( $activate->get_error_message() ), '

', - '

', esc_html( $this->strings['return'] ), '

'; - return true; // End it here if there is an error with automatic activation - - } else { - echo '

', esc_html( $this->strings['plugin_activated'] ), '

'; + if( $this->activate_single_plugin( $plugin_activate, $slug, true ) === true ) { + return true; // Finish execution of the function early as we encountered an error } + + // Re-populate the file path now that the plugin has been installed and activated. + $this->populate_file_path( $slug ); } // Display message based on if all plugins are now active or not. @@ -608,29 +603,8 @@ protected function do_plugin_install() { elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) { check_admin_referer( 'tgmpa-activate', 'tgmpa-activate-nonce' ); - $plugin_to_activate = $this->plugins[ $slug ]['file_path']; - - if ( ! is_plugin_active( $plugin_to_activate ) ) { - $activate = activate_plugin( $plugin_to_activate ); // Activate the plugin. - - if ( is_wp_error( $activate ) ) { - echo '

', wp_kses_post( $activate->get_error_message() ), '

'; - echo '

', esc_html( $this->strings['return'] ), '

'; - return true; // End it here if there is an error with activation. - - } else { - // Make sure message doesn't display again if bulk activation is performed immediately after a single activation. - if ( ! isset( $_POST['action'] ) ) { - echo '

', esc_html( $this->strings['activated_successfully'] ), ' ', esc_html( $this->plugins[ $slug ]['name'] ), '.

'; - } - } - } else { - echo '

', - sprintf( - esc_html__( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ), - '' . esc_html( $this->plugins[ $slug ]['name'] ) . '' - ), - '

'; + if( $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) === true ) { + return true; // Finish execution of the function early as we encountered an error } } @@ -705,6 +679,54 @@ public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) { return $source; } + /** + * Activate a single plugin and send feedback about the result to the screen. + * + * @since 2.5.0 + * + * @param string $file_path Path within wp-plugins/ to main plugin file + * @param string $slug Plugin slug + * @param bool $automatic Whether this is an automatic activation after an install. Defaults to false. + * This determines the styling of the output messages. + * + * @return bool True if an error was encountered, false otherwise. + */ + protected function activate_single_plugin( $file_path, $slug, $automatic = false ) { + + if ( ! is_plugin_active( $file_path ) ) { + $activate = activate_plugin( $file_path ); // Activate the plugin. + + if ( is_wp_error( $activate ) ) { + echo '

', wp_kses_post( $activate->get_error_message() ), '

', + '

', esc_html( $this->strings['return'] ), '

'; + return true; // End it here if there is an error with activation. + + } else { + if ( ! $automatic ) { + // Make sure message doesn't display again if bulk activation is performed immediately after a single activation. + if ( ! isset( $_POST['action'] ) ) { + echo '

', esc_html( $this->strings['activated_successfully'] ), ' ', esc_html( $this->plugins[ $slug ]['name'] ), '.

'; + } + } + else { + // Simpler message layout for use on the plugin install page + echo '

', esc_html( $this->strings['plugin_activated'] ), '

'; + } + } + } else { + // No simpler message format provided as this message should never be encountered + // on the plugin install page + echo '

', + sprintf( + esc_html__( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ), + '' . esc_html( $this->plugins[ $slug ]['name'] ) . '' + ), + '

'; + } + + return false; + } + /** * Echoes required plugin notice. * @@ -729,7 +751,7 @@ public function notices() { return; } - $installed_plugins = get_plugins(); // Retrieve a list of all the plugins + $installed_plugins = get_plugins(); // Retrieve a list of all the plugins $message = array(); // Store the messages in an array to be outputted after plugins have looped through. $install_link = false; // Set to false, change to true in loop if conditions exist, used for action link 'install'. $install_link_count = 0; // Used to determine plurality of install action link text. From 9d3f66adb6001ddc4a93cf94073e5530b44253c6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 28 Apr 2015 20:43:49 +0200 Subject: [PATCH 2/2] Minor adjustments based on code review. - reverse function true/false logic - now more logical from a function point of view, but completely incomprehensible on the receiving end of the function. - darn yoda (Seriously, if you use strict checking - as I do -, why the heck would I need it ? makes code so much less readable...) --- class-tgm-plugin-activation.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/class-tgm-plugin-activation.php b/class-tgm-plugin-activation.php index 96e13747..ac9318bd 100644 --- a/class-tgm-plugin-activation.php +++ b/class-tgm-plugin-activation.php @@ -573,7 +573,7 @@ protected function do_plugin_install() { // Only activate plugins if the config option is set to true. if ( $this->is_automatic ) { $plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method. - if( $this->activate_single_plugin( $plugin_activate, $slug, true ) === true ) { + if( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) { return true; // Finish execution of the function early as we encountered an error } @@ -603,7 +603,7 @@ protected function do_plugin_install() { elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) { check_admin_referer( 'tgmpa-activate', 'tgmpa-activate-nonce' ); - if( $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) === true ) { + if( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) { return true; // Finish execution of the function early as we encountered an error } } @@ -689,7 +689,7 @@ public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) { * @param bool $automatic Whether this is an automatic activation after an install. Defaults to false. * This determines the styling of the output messages. * - * @return bool True if an error was encountered, false otherwise. + * @return bool False if an error was encountered, true otherwise. */ protected function activate_single_plugin( $file_path, $slug, $automatic = false ) { @@ -699,7 +699,7 @@ protected function activate_single_plugin( $file_path, $slug, $automatic = false if ( is_wp_error( $activate ) ) { echo '

', wp_kses_post( $activate->get_error_message() ), '

', '

', esc_html( $this->strings['return'] ), '

'; - return true; // End it here if there is an error with activation. + return false; // End it here if there is an error with activation. } else { if ( ! $automatic ) { @@ -724,7 +724,7 @@ protected function activate_single_plugin( $file_path, $slug, $automatic = false '

'; } - return false; + return true; } /** @@ -751,7 +751,7 @@ public function notices() { return; } - $installed_plugins = get_plugins(); // Retrieve a list of all the plugins + $installed_plugins = get_plugins(); // Retrieve a list of all the plugins $message = array(); // Store the messages in an array to be outputted after plugins have looped through. $install_link = false; // Set to false, change to true in loop if conditions exist, used for action link 'install'. $install_link_count = 0; // Used to determine plurality of install action link text.