Skip to content
Merged
Show file tree
Hide file tree
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
93 changes: 50 additions & 43 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public function init() {
/* After this point, the plugins should be registered and the configuration set. */

// Proceed only if we have plugins to handle.
if ( ! is_array( $this->plugins ) || empty( $this->plugins ) ) {
if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) {
return;
}

Expand Down Expand Up @@ -534,7 +534,9 @@ public function admin_init() {

global $tab, $body_id;
$body_id = 'plugin-information';
// @codingStandardsIgnoreStart
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why does this bit need to be ignored?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is an alternative way to get round that one issue where we were considering excluding the rule or lowering the severity.
Now the rule is still being used, just not for that one line.

$tab = 'plugin-information';
// @codingStandardsIgnoreEnd

install_plugin_information();

Expand Down Expand Up @@ -577,7 +579,7 @@ public function thickbox() {
*/
public function admin_menu() {

// Make sure priviledges are correct to see the page.
// Make sure privileges are correct to see the page.
if ( ! current_user_can( 'install_plugins' ) ) {
return;
}
Expand Down Expand Up @@ -692,14 +694,13 @@ protected function do_plugin_install() {
}

// All plugin information will be stored in an array for processing.
$plugin = array();
$slug = sanitize_key( urldecode( $_GET['plugin'] ) );
$slug = sanitize_key( urldecode( $_GET['plugin'] ) );

if ( ! isset( $this->plugins[ $slug ] ) ) {
return false;
}

// Checks for actions from action links to process the installation.
// Was an install or upgrade action link clicked?
if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) {

$install_type = 'install';
Expand Down Expand Up @@ -736,6 +737,7 @@ protected function do_plugin_install() {
/* If we arrive here, we have the filesystem. */

// Prep variables for Plugin_Installer_Skin class.
$extra = array();
$extra['slug'] = $slug; // Needed for potentially renaming of directory name.
$source = $this->get_download_url( $slug );
$api = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null;
Expand Down Expand Up @@ -813,10 +815,9 @@ protected function do_plugin_install() {
}

return true;
}

// Checks for actions from link-clicks to process the activation.
elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) {
} elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) {
// Activate action link was clicked.
check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' );

if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) {
Expand Down Expand Up @@ -883,7 +884,7 @@ public function inject_update_info( $plugins ) {
*/
public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {

if ( ! $this->is_tgmpa_page() ) {
if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) {
return $source;
}

Expand Down Expand Up @@ -1166,7 +1167,7 @@ public function notices() {
$action_links = apply_filters( 'tgmpa_notice_action_links', $action_links );

$action_links = array_filter( (array) $action_links ); // Remove any empty array items.
if ( is_array( $action_links ) && ! empty( $action_links ) ) {
if ( ! empty( $action_links ) && is_array( $action_links ) ) {
$action_links = sprintf( $line_template, implode( ' | ', $action_links ) );
$rendered .= apply_filters( 'tgmpa_notice_rendered_action_links', $action_links );
}
Expand Down Expand Up @@ -1235,7 +1236,7 @@ public function register( $plugin ) {
return;
}

if ( ! is_string( $plugin['slug'] ) || empty( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) {
if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) {
return;
}

Expand All @@ -1248,7 +1249,7 @@ public function register( $plugin ) {
'force_activation' => false, // Boolean
'force_deactivation' => false, // Boolean
'external_url' => '', // String
'is_callable' => '', // String|Array
'is_callable' => '', // String|Array.
);

// Prepare the received data.
Expand Down Expand Up @@ -1381,9 +1382,9 @@ public function flush_plugins_cache( $clear_update_cache = true ) {
* @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin.
* Parameter added in v2.5.0.
*/
public function populate_file_path( $plugin_slug = null ) {
public function populate_file_path( $plugin_slug = '' ) {

if ( is_string( $plugin_slug ) && ! empty( $plugin_slug ) ) {
if ( ! empty( $plugin_slug ) && is_string( $plugin_slug ) && isset( $this->plugins[ $plugin_slug ] ) ) {
$this->plugins[ $plugin_slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $plugin_slug );

} else {
Expand Down Expand Up @@ -1541,7 +1542,6 @@ protected function get_plugins_api( $slug ) {
* or the plugin name if not.
*/
public function get_info_link( $slug ) {
$link = '';

if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) {
$link = sprintf(
Expand Down Expand Up @@ -1677,7 +1677,7 @@ public function is_plugin_installed( $slug ) {
* @return bool True if active, false otherwise.
*/
public function is_plugin_active( $slug ) {
return ( is_plugin_active( $this->plugins[ $slug ]['file_path'] ) || ( ! empty( $plugin['is_callable'] ) && is_callable( $plugin['is_callable'] ) ) );
return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) );
}

/**
Expand Down Expand Up @@ -2298,23 +2298,33 @@ public function get_views() {
case 'all':
$text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins', 'tgmpa' );
break;

case 'install':
$text = _n( 'To Install <span class="count">(%s)</span>', 'To Install <span class="count">(%s)</span>', $count, 'tgmpa' );
break;

case 'update':
$text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count, 'tgmpa' );
break;

case 'activate':
$text = _n( 'To Activate <span class="count">(%s)</span>', 'To Activate <span class="count">(%s)</span>', $count, 'tgmpa' );
break;

default:
$text = '';
break;
}

$status_links[ $type ] = sprintf(
'<a href="%s"%s>%s</a>',
esc_url( $this->tgmpa->get_tgmpa_status_url( $type ) ),
( $type === $this->view_context ) ? ' class="current"' : '',
sprintf( $text, number_format_i18n( $count ) )
);
if ( ! empty( $text ) ) {

$status_links[ $type ] = sprintf(
'<a href="%s"%s>%s</a>',
esc_url( $this->tgmpa->get_tgmpa_status_url( $type ) ),
( $type === $this->view_context ) ? ' class="current"' : '',
sprintf( $text, number_format_i18n( $count ) )
);
}
}

return $status_links;
Expand Down Expand Up @@ -2466,10 +2476,7 @@ public function get_columns() {

if ( 'all' === $this->view_context || 'update' === $this->view_context ) {
$columns['version'] = __( 'Version', 'tgmpa' );
}

if ( 'all' === $this->view_context || 'update' === $this->view_context ) {
$columns['status'] = __( 'Status', 'tgmpa' );
$columns['status'] = __( 'Status', 'tgmpa' );
}

return apply_filters( 'tgmpa_table_columns', $columns );
Expand Down Expand Up @@ -2640,7 +2647,7 @@ public function process_bulk_actions() {
$plugins_to_install = (array) $_POST['plugin'];

} elseif ( is_string( $_POST['plugin'] ) ) {
// Received via Filesystem page - unflatten array (WP bug #19643).
// Received via Filesystem page - un-flatten array (WP bug #19643).
$plugins_to_install = explode( ',', $_POST['plugin'] );
}

Expand All @@ -2653,9 +2660,11 @@ public function process_bulk_actions() {
// Check if the plugin was registered with TGMPA and remove if not.
if ( ! isset( $this->tgmpa->plugins[ $slug ] ) ) {
unset( $plugins_to_install[ $key ] );
continue;
}

// For updates: make sure this is a plugin we *can* update (update available and WP version ok).
elseif ( 'update' === $install_type && ( $this->tgmpa->is_plugin_installed( $slug ) && ( false === $this->tgmpa->does_plugin_have_update( $slug ) || ! $this->tgmpa->can_plugin_update( $slug ) ) ) ) {
if ( 'update' === $install_type && ( $this->tgmpa->is_plugin_installed( $slug ) && ( false === $this->tgmpa->does_plugin_have_update( $slug ) || ! $this->tgmpa->can_plugin_update( $slug ) ) ) ) {
unset( $plugins_to_install[ $key ] );
}
}
Expand Down Expand Up @@ -2805,10 +2814,10 @@ public function process_bulk_actions() {
$last_plugin = array_pop( $plugin_names ); // Pop off last name to prep for readability.
$imploded = empty( $plugin_names ) ? $last_plugin : ( implode( ', ', $plugin_names ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );

printf(
printf( // WPCS: xss ok.
'<div id="message" class="updated"><p>%1$s %2$s.</p></div>',
esc_html( _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, 'tgmpa' ) ),
$imploded // WPCS: xss ok.
$imploded
);

// Update recently activated plugins option.
Expand Down Expand Up @@ -2959,7 +2968,7 @@ class TGM_Bulk_Installer extends Plugin_Upgrader {
*
* @since 2.2.0
*
* @param \Bulk_Upgrader_Skin $skin Installer skin.
* @param \Bulk_Upgrader_Skin|null $skin Installer skin.
*/
public function __construct( $skin = null ) {
// Get TGMPA class instance.
Expand Down Expand Up @@ -3036,9 +3045,9 @@ public function run( $options ) {
public function bulk_install( $plugins, $args = array() ) {

// [TGMPA + ] Hook auto-activation in
add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10, 3 );
add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );

$defaults = array(
$defaults = array(
'clear_update_cache' => true,
);
$parsed_args = wp_parse_args( $args, $defaults );
Expand Down Expand Up @@ -3153,7 +3162,7 @@ public function bulk_install( $plugins, $args = array() ) {
/* [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */

// [TGMPA + ] Remove our auto-activation hook.
remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10, 3 );
remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );

// Force refresh of plugin update information.
wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
Expand All @@ -3175,11 +3184,11 @@ public function bulk_install( $plugins, $args = array() ) {
*/
public function bulk_upgrade( $plugins, $args = array() ) {

add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10, 3 );
add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );

$result = parent::bulk_upgrade( $plugins, $args );

remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10, 3 );
remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );

return $result;
}
Expand All @@ -3191,13 +3200,11 @@ public function bulk_upgrade( $plugins, $args = array() ) {
*
* @since 2.5.0
*
* @param bool $bool The value we need to give back (true).
* @param array $hook_extra Any extra info we passed to the actual run method.
* @param array $result Result of the current plugin install/update run.
* @param bool $bool The value we need to give back (true).
*
* @return bool
*/
public function auto_activate( $bool, $hook_extra, $result ) {
public function auto_activate( $bool ) {

// Only process the activation of installed plugins if the automatic flag is set to true.
if ( $this->tgmpa->is_automatic ) {
Expand Down Expand Up @@ -3433,7 +3440,7 @@ public function bulk_footer() {
*/
public function before_flush_output() {
_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
parent::flush_output();
$this->flush_output();
}

/**
Expand All @@ -3446,7 +3453,7 @@ public function before_flush_output() {
*/
public function after_flush_output() {
_deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
parent::flush_output();
$this->flush_output();
$this->i++;
}
}
Expand All @@ -3462,7 +3469,7 @@ public function after_flush_output() {
/**
* Generic utilities for TGMPA.
*
* All methods are static, poor-dev namespacing class wrapper.
* All methods are static, poor-dev name-spacing class wrapper.
*
* @since 2.5.0
*
Expand Down
4 changes: 2 additions & 2 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ function my_theme_register_required_plugins() {
'slug' => 'buddypress',
'required' => false,
),
// This is an example of the use of 'is_callable' functionality. A user could - for instance -

// This is an example of the use of 'is_callable' functionality. A user could - for instance -
// have WPSEO installed *or* WPSEO Premium. The slug would in that last case be different, i.e.
// 'wordpress-seo-premium'.
// By setting 'is_callable' to either a function from that plugin or a class method
Expand Down