Skip to content

Commit

Permalink
Issue #231: Added notifications when plugins are force activated
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Kinney committed May 15, 2016
1 parent c2585ab commit 83cf630
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 27 deletions.
157 changes: 130 additions & 27 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ public function init() {
'The following recommended plugins are currently inactive: %1$s.',
'tgmpa'
),
'notice_force_activation' => _n_noop(
/* translators: 1: plugin name(s). */
'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',
'tgmpa'
),
'install_link' => _n_noop(
'Begin installing plugin',
'Begin installing plugins',
Expand Down Expand Up @@ -448,6 +454,11 @@ public function init() {
if ( true === $this->has_forced_deactivation ) {
add_action( 'switch_theme', array( $this, 'force_deactivation' ) );
}

// Display forced activation notice, if present.
if ( current_user_can( 'manage_options' ) && is_admin() ) {
add_action( 'admin_notices', array( $this, 'display_forced_activation_notice' ) );
}
}

/**
Expand Down Expand Up @@ -1178,7 +1189,6 @@ public function notices() {

// If we have notices to display, we move forward.
if ( ! empty( $message ) || $total_required_action_count > 0 ) {
krsort( $message ); // Sort messages.
$rendered = '';

// As add_settings_error() wraps the final message in a <p> and as the final message can't be
Expand All @@ -1195,32 +1205,7 @@ public function notices() {
$rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) );
}

// Render the individual message lines for the notice.
foreach ( $message as $type => $plugin_group ) {
$linked_plugins = array();

// Get the external info link for a plugin if one is available.
foreach ( $plugin_group as $plugin_slug ) {
$linked_plugins[] = $this->get_info_link( $plugin_slug );
}
unset( $plugin_slug );

$count = count( $plugin_group );
$linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins );
$last_plugin = array_pop( $linked_plugins ); // Pop off last name to prep for readability.
$imploded = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );

$rendered .= sprintf(
$line_template,
sprintf(
translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ),
$imploded,
$count
)
);

}
unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded );
$rendered .= $this->build_message( $message, $line_template );

$rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template );
}
Expand All @@ -1235,6 +1220,49 @@ public function notices() {
}
}

/**
* Build a message string that specifies what actions are needed and the plugins that need those actions.
*
* @since 2.x.x
*
* @param array $message Associative array of actions: each key is an action and each value is an array of plugin slugs that need that action.
* @param string $line_template String that sprintf is applied to in creating a line of html markup for each action.
* @return string Message to display.
*/
protected function build_message( $message, $line_template = '%s' ) {
$message_output = '';

krsort( $message );

// Render the individual message lines for the notice.
foreach ( $message as $type => $plugin_group ) {
$linked_plugins = array();

// Get the external info link for a plugin if one is available.
foreach ( $plugin_group as $plugin_slug ) {
$linked_plugins[] = $this->get_info_link( $plugin_slug );
}
unset( $plugin_slug );

$count = count( $plugin_group );
$linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins );
$last_plugin = array_pop( $linked_plugins ); // Pop off last name to prep for readability.
$imploded = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );

$message_output .= sprintf(
$line_template,
sprintf(
translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ),
$imploded,
$count
)
);

}

return $message_output;
}

/**
* Generate the user action links for the admin notice.
*
Expand Down Expand Up @@ -1336,6 +1364,50 @@ protected function display_settings_errors() {
}
}

/**
* Display admin notice if plugins have been force activated.
*
* @since 2.x.x
*/
public function display_forced_activation_notice() {
$force_activated_plugins = get_transient( 'tgmpa_force_activated_plugins' );

if ( $force_activated_plugins ) {
// Check to make sure all force activated plugins are still active since the transient was set.
$force_activated_and_active_plugins = $this->clean_inactive_plugins( $force_activated_plugins );

if ( $force_activated_and_active_plugins ) {
?>
<div class="notice <?php echo esc_attr( $this->get_admin_notice_class() ); ?> is-dismissible">
<p><strong><?php echo wp_kses_post( $this->build_message( array( 'notice_force_activation' => $force_activated_and_active_plugins ) ) ); ?></strong></p>
</div>
<?php
}

delete_transient( 'tgmpa_force_activated_plugins' );
}
}

/**
* Remove inactive plugins from an array of plugin slugs.
*
* @since 2.x.x
*
* @param array $plugins Slugs.
* @return array Slugs with inactive plugins removed.
*/
protected function clean_inactive_plugins( $plugins ) {
$active_plugins = array();

foreach ( $plugins as $slug ) {
if ( $this->is_plugin_active( $slug ) ) {
$active_plugins[] = $slug;
}
}

return $active_plugins;
}

/**
* Register dismissal of admin notices.
*
Expand Down Expand Up @@ -2023,11 +2095,42 @@ public function force_activation() {
} elseif ( $this->can_plugin_activate( $slug ) ) {
// There we go, activate the plugin.
activate_plugin( $plugin['file_path'] );
$this->append_transient( 'tgmpa_force_activated_plugins', $slug );
}
}
}
}

/**
* Set/update a transient, appending the new value if the transient already exists.
*
* @since 2.x.x
*
* @param string $transient_key Name of transient.
* @param string|array $data To set or add to transient.
*/
protected function append_transient( $transient_key, $data ) {
$transient_data = get_transient( $transient_key );

if ( false === $transient_data ) {
$transient_data = array();
}

if ( ! is_array( $transient_data ) ) {
$transient_data = (array) $transient_data;
}

if ( $data ) {
if ( is_string( $data ) ) {
$transient_data[] = $data;
} elseif ( is_array( $data ) ) {
$transient_data = $transient_data + $data;
}

set_transient( $transient_key, $transient_data );
}
}

/**
* Forces plugin deactivation if the parameter 'force_deactivation'
* is set to true and adds the plugin to the 'recently active' plugins list.
Expand Down
6 changes: 6 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ function my_theme_register_required_plugins() {
'The following recommended plugins are currently inactive: %1$s.',
'theme-slug'
),
'notice_force_activation' => _n_noop(
/* translators: 1: plugin name(s). * /
'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',
'theme-slug'
),
'install_link' => _n_noop(
'Begin installing plugin',
'Begin installing plugins',
Expand Down

0 comments on commit 83cf630

Please sign in to comment.