Skip to content

Commit

Permalink
Issue #231: Added notifications when plugins are forced activated
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Kinney committed Oct 3, 2014
1 parent 374285e commit 35c969f
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public function __construct() {
'notice_cannot_activate' => _n_noop( 'Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.', 'Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.' ),
'notice_ask_to_update' => _n_noop( 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.' ),
'notice_cannot_update' => _n_noop( 'Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.', 'Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.' ),
'notice_force_activation' => _n_noop( 'The following plugin has been automatically activated because it is required by the current theme: %s', 'The following plugins have been automatically activated because they are required by the current theme: %s' ),
'install_link' => _n_noop( 'Begin installing plugin', 'Begin installing plugins' ),
'activate_link' => _n_noop( 'Begin activating plugin', 'Begin activating plugins' ),
'return' => __( 'Return to Required Plugins Installer', 'tgmpa' ),
Expand Down Expand Up @@ -966,6 +967,7 @@ public function force_activation() {
$this->populate_file_path();

$installed_plugins = get_plugins();
$reactivated_plugins = array();

foreach ( $this->plugins as $plugin ) {
// Oops, plugin isn't there so iterate to next condition.
Expand All @@ -975,9 +977,12 @@ public function force_activation() {
// There we go, activate the plugin.
elseif ( isset( $plugin['force_activation'] ) && $plugin['force_activation'] && is_plugin_inactive( $plugin['file_path'] ) ) {
activate_plugin( $plugin['file_path'] );
$reactivated_plugins[] = $plugin['name'];
}
}

$this->force_notices( 'activation', $reactivated_plugins );

}

/**
Expand Down Expand Up @@ -1006,6 +1011,49 @@ public function force_deactivation() {

}

/**
* Displays a notice if plugins are automatically activated
*
* When plugins are forced activated, this overrides the
* default WordPress notice and replaces it with a custom notice,
* 'notice_force_activation'
*
* Issues:
* - When more than one plugin is activated, but not all of those plugins
* are being forced, the default WordPress notice is still unset
* - Could be extended to display notices on forced deactivation, but
* currently doesn't work because this is called on switch_theme, which
* redirects after firing
*
* @since 2.4.1
*
* @param string $action which action is being forced, 'activation'
* @param array $plugins plugins that have been forced activated
* @return void
*/
public function force_notices( $action, $plugins ) {

$count = count($plugins);
$string = $this->strings["notice_force_$action"];

// If no plugins have been forced, exit early
if ( $count < 1 ) return;

// Override default WordPress notifications
$notices = array(
'deactivate',
'deactivate-multi',
);
foreach( $notices as $notice ) {
if( isset($_GET[$notice]) ) unset($_GET[$notice]);
}

// Display admin notice about forced action
$message = sprintf( translate_nooped_plural($string, $count, 'tgmpa'), implode(", ", $plugins) );
add_settings_error( 'tgmpa', 'tgmpa', $message, 'error' );

}

/**
* Returns the singleton instance of the class.
*
Expand Down Expand Up @@ -2193,4 +2241,4 @@ public function after_flush_output() {

}
}
}
}

0 comments on commit 35c969f

Please sign in to comment.