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 14, 2016
1 parent c2585ab commit 16241d1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 27 deletions.
114 changes: 87 additions & 27 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ public function init() {
'The following recommended plugins are currently inactive: %1$s.',
'tgmpa'
),
'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',
Expand Down Expand Up @@ -448,6 +451,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 +1186,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 +1202,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 +1217,49 @@ public function notices() {
}
}

/**
* Build a message string that specifies what actions are needed and the plugins that need those actions.
*
* @since 2.6.0
*
* @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
*/
protected function build_message( $message, $line_template = '%s' ) {
$message_output = '';

krsort( $message ); // Sort messages.

// 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 +1361,22 @@ protected function display_settings_errors() {
}
}

/**
* Display admin notice if plugins have been force activated.
*
* @since 2.6.0
*/
public function display_forced_activation_notice() {
if ( $force_activated_plugins = get_transient( 'tgmpa_force_activated_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_plugins ) ) ); ?></strong></p>
</div>
<?php
delete_transient( 'tgmpa_force_activated_plugins' );
}
}

/**
* Register dismissal of admin notices.
*
Expand Down Expand Up @@ -2023,11 +2064,30 @@ 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.6.0
*
* @param string $transient_key Name of transient.
* @param string|array $data To set or add to transient.
*/
protected function append_transient( $transient_key, $data ) {
if ( $transient_data = get_transient( $transient_key ) ) {
(array) $transient_data[] = $data;
} else {
$transient_data = (array) $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
3 changes: 3 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ function my_theme_register_required_plugins() {
'The following recommended plugins are currently inactive: %1$s.',
'theme-slug'
),
'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',
Expand Down

0 comments on commit 16241d1

Please sign in to comment.