Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract single plugin activation to seperate method #375

Merged
merged 2 commits into from
Apr 28, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 55 additions & 33 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
'<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
return true; // End it here if there is an error with automatic activation

} else {
echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
if( false === $this->activate_single_plugin( $plugin_activate, $slug, 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.
Expand All @@ -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 '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>';
echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
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 '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
}
}
} else {
echo '<div id="message" class="error"><p>',
sprintf(
esc_html__( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ),
'<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
),
'</p></div>';
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
}
}

Expand Down Expand Up @@ -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 False if an error was encountered, true 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 '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
'<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
return false; // 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 '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
}
}
else {
// Simpler message layout for use on the plugin install page
echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of hard-coding <p>, just use wpautop()?

(Same for other instances?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch, no - massive overhead which we can do without
See: https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-includes/formatting.php#L373

}
}
} else {
// No simpler message format provided as this message should never be encountered
// on the plugin install page
echo '<div id="message" class="error"><p>',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if others are also outputting admin notices? Will there be multiple elements with the same ID?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These messages are only displayed on our pages and in the case of the ones with the 'message' id - on the actual plugin installation page used by TGMPA. No other messages will (should) display there.

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

return true;
}

/**
* Echoes required plugin notice.
*
Expand Down