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

[Develop] Improve extensibility #219

Closed
wants to merge 3 commits into from
Closed
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
63 changes: 36 additions & 27 deletions class-tgm-plugin-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ class TGM_Plugin_Activation {
*
* @see TGM_Plugin_Activation::init()
*/
public function __construct() {

self::$instance = $this;
protected function __construct() {

// Set the current WordPress version.
global $wp_version;
Expand Down Expand Up @@ -1034,8 +1032,8 @@ public function force_deactivation() {
*/
public static function get_instance() {

if ( ! isset( self::$instance ) && ! ( self::$instance instanceof TGM_Plugin_Activation ) ) {
self::$instance = new TGM_Plugin_Activation();
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
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 having an untestable singleton class, how about we put a static into load_tgm_plugin_activation() instead?

}

return self::$instance;
Expand All @@ -1044,8 +1042,19 @@ public static function get_instance() {

}

// Ensure only one instance of the class is ever invoked.
$tgmpa = TGM_Plugin_Activation::get_instance();
if ( did_action( 'plugins_loaded' ) ) {
load_tgm_plugin_activation();
}
else {
add_action( 'plugins_loaded', 'load_tgm_plugin_activation' );
}

if ( ! function_exists( 'load_tgm_plugin_activation' ) ) {
// Ensure only one instance of the class is ever invoked.
function load_tgm_plugin_activation() {
$GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance();
}
}

}

Expand All @@ -1062,11 +1071,11 @@ public static function get_instance() {
function tgmpa( $plugins, $config = array() ) {

foreach ( $plugins as $plugin ) {
TGM_Plugin_Activation::$instance->register( $plugin );
$GLOBALS['tgmpa']::$instance->register( $plugin );
}

if ( $config ) {
TGM_Plugin_Activation::$instance->config( $config );
$GLOBALS['tgmpa']::$instance->config( $config );
}

}
Expand Down Expand Up @@ -1133,15 +1142,15 @@ public function __construct() {
protected function _gather_plugin_data() {

// Load thickbox for plugin links.
TGM_Plugin_Activation::$instance->admin_init();
TGM_Plugin_Activation::$instance->thickbox();
$GLOBALS['tgmpa']::$instance->admin_init();
$GLOBALS['tgmpa']::$instance->thickbox();

// Prep variables for use and grab list of all installed plugins.
$table_data = array();
$i = 0;
$installed_plugins = get_plugins();

foreach ( TGM_Plugin_Activation::$instance->plugins as $plugin ) {
foreach ( $GLOBALS['tgmpa']::$instance->plugins as $plugin ) {
if ( is_plugin_active( $plugin['file_path'] ) || ( isset( $plugin['is_callable'] ) && is_callable( $plugin['is_callable'] ) ) ) {
continue; // No need to display plugins if they are installed and activated.
}
Expand Down Expand Up @@ -1251,7 +1260,7 @@ protected function _gather_plugin_data() {
*/
protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {

foreach ( TGM_Plugin_Activation::$instance->plugins as $plugin => $values ) {
foreach ( $GLOBALS['tgmpa']::$instance->plugins as $plugin => $values ) {
if ( $name == $values['name'] && isset( $values[$data] ) ) {
return $values[$data];
}
Expand Down Expand Up @@ -1302,7 +1311,7 @@ public function column_plugin( $item ) {
wp_nonce_url(
add_query_arg(
array(
'page' => TGM_Plugin_Activation::$instance->menu,
'page' => $GLOBALS['tgmpa']::$instance->menu,
'plugin' => $item['slug'],
'plugin_name' => $item['sanitized_plugin'],
'plugin_source' => urlencode( $item['url'] ),
Expand All @@ -1323,7 +1332,7 @@ public function column_plugin( $item ) {
'<a href="%1$s" title="' . __( 'Activate', 'tgmpa' ) . ' %2$s">' . __( 'Activate', 'tgmpa' ) . '</a>',
add_query_arg(
array(
'page' => TGM_Plugin_Activation::$instance->menu,
'page' => $GLOBALS['tgmpa']::$instance->menu,
'plugin' => $item['slug'],
'plugin_name' => $item['sanitized_plugin'],
'plugin_source' => urlencode( $item['url'] ),
Expand Down Expand Up @@ -1534,7 +1543,7 @@ public function process_bulk_actions() {
$url = wp_nonce_url(
add_query_arg(
array(
'page' => TGM_Plugin_Activation::$instance->menu,
'page' => $GLOBALS['tgmpa']::$instance->menu,
'tgmpa-action' => 'install-selected',
'plugins' => urlencode( implode( ',', $plugins ) ),
'plugin_paths' => urlencode( implode( ',', $plugin_paths ) ),
Expand Down Expand Up @@ -1572,7 +1581,7 @@ public function process_bulk_actions() {
}

if ( is_wp_error( $api ) ) {
wp_die( TGM_Plugin_Activation::$instance->strings['oops'] . var_dump( $api ) );
wp_die( $GLOBALS['tgmpa']::$instance->strings['oops'] . var_dump( $api ) );
}

// Capture download links from $api or set install link to pre-packaged/private repo.
Expand All @@ -1583,7 +1592,7 @@ public function process_bulk_actions() {
}

// Finally, all the data is prepared to be sent to the installer.
$url = add_query_arg( array( 'page' => TGM_Plugin_Activation::$instance->menu ), admin_url( 'themes.php' ) );
$url = add_query_arg( array( 'page' => $GLOBALS['tgmpa']::$instance->menu ), admin_url( 'themes.php' ) );
$nonce = 'bulk-plugins';
$names = $plugin_names;

Expand All @@ -1592,7 +1601,7 @@ public function process_bulk_actions() {

// Wrap the install process with the appropriate HTML.
echo '<div class="tgmpa wrap">';
if ( version_compare( TGM_Plugin_Activation::$instance->wp_version, '3.8', '<' ) ) {
if ( version_compare( $GLOBALS['tgmpa']::$instance->wp_version, '3.8', '<' ) ) {
screen_icon( apply_filters( 'tgmpa_default_screen_icon', 'themes' ) );
}
echo '<h2>' . esc_html( get_admin_page_title() ) . '</h2>';
Expand Down Expand Up @@ -1703,7 +1712,7 @@ public function prepare_items() {
if ( ! function_exists( 'tgmpa_load_bulk_installer' ) ) {
function tgmpa_load_bulk_installer() {

if ( ! class_exists( 'WP_Upgrader' ) && ( isset( $_GET['page'] ) && TGM_Plugin_Activation::$instance->menu === $_GET['page'] ) ) {
if ( ! class_exists( 'WP_Upgrader' ) && ( isset( $_GET['page'] ) && $GLOBALS['tgmpa']::$instance->menu === $_GET['page'] ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

if ( ! class_exists( 'TGM_Bulk_Installer' ) ) {
Expand Down Expand Up @@ -1755,7 +1764,7 @@ public function bulk_install( $packages ) {

// Set install strings and automatic activation strings (if config option is set to true).
$this->install_strings();
if ( TGM_Plugin_Activation::$instance->is_automatic ) {
if ( $GLOBALS['tgmpa']::$instance->is_automatic ) {
$this->activate_strings();
}

Expand Down Expand Up @@ -1901,7 +1910,7 @@ public function run( $options ) {
}

// Only process the activation of installed plugins if the automatic flag is set to true.
if ( TGM_Plugin_Activation::$instance->is_automatic ) {
if ( $GLOBALS['tgmpa']::$instance->is_automatic ) {
// Flush plugins cache so we can make sure that the installed plugins list is always up to date.
wp_cache_flush();

Expand All @@ -1910,7 +1919,7 @@ public function run( $options ) {
$activate = activate_plugin( $plugin_info );

// Re-populate the file path now that the plugin has been installed and activated.
TGM_Plugin_Activation::$instance->populate_file_path();
$GLOBALS['tgmpa']::$instance->populate_file_path();

// Set correct strings based on results.
if ( is_wp_error( $activate ) ) {
Expand Down Expand Up @@ -2072,7 +2081,7 @@ public function __construct( $args = array() ) {
public function add_strings() {

// Automatic activation strings.
if ( TGM_Plugin_Activation::$instance->is_automatic ) {
if ( $GLOBALS['tgmpa']::$instance->is_automatic ) {
$this->upgrader->strings['skin_upgrade_start'] = __( 'The installation and activation process is starting. This process may take a while on some hosts, so please be patient.', 'tgmpa' );
$this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', 'tgmpa' ) . ' <a onclick="%2$s" href="#" class="hide-if-no-js"><span>' . __( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . __( 'Hide Details', 'tgmpa' ) . '</span>.</a>';
$this->upgrader->strings['skin_upgrade_end'] = __( 'All installations and activations have been completed.', 'tgmpa' );
Expand Down Expand Up @@ -2160,9 +2169,9 @@ public function bulk_footer() {

// Display message based on if all plugins are now active or not.
$complete = array();
foreach ( TGM_Plugin_Activation::$instance->plugins as $plugin ) {
foreach ( $GLOBALS['tgmpa']::$instance->plugins as $plugin ) {
if ( ! is_plugin_active( $plugin['file_path'] ) ) {
echo '<p><a href="' . add_query_arg( 'page', TGM_Plugin_Activation::$instance->menu, admin_url( 'themes.php' ) ) . '" title="' . esc_attr( TGM_Plugin_Activation::$instance->strings['return'] ) . '" target="_parent">' . TGM_Plugin_Activation::$instance->strings['return'] . '</a></p>';
echo '<p><a href="' . add_query_arg( 'page', $GLOBALS['tgmpa']::$instance->menu, admin_url('themes.php' ) ) . '" title="' . esc_attr( $GLOBALS['tgmpa']::$instance->strings['return'] ) . '" target="_parent">' . $GLOBALS['tgmpa']::$instance->strings['return'] . '</a></p>';
$complete[] = $plugin;
break;
}
Expand All @@ -2177,7 +2186,7 @@ public function bulk_footer() {

// All plugins are active, so we display the complete string and hide the menu to protect users.
if ( empty( $complete ) ) {
echo '<p>' . sprintf( TGM_Plugin_Activation::$instance->strings['complete'], '<a href="' . admin_url() . '" title="' . __( 'Return to the Dashboard', 'tgmpa' ) . '">' . __( 'Return to the Dashboard', 'tgmpa' ) . '</a>' ) . '</p>';
echo '<p>' . sprintf( $GLOBALS['tgmpa']::$instance->strings['complete'], '<a href="' . admin_url() . '" title="' . __( 'Return to the Dashboard', 'tgmpa' ) . '">' . __( 'Return to the Dashboard', 'tgmpa' ) . '</a>' ) . '</p>';
echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
}

Expand Down