Skip to content

Commit 5eeac74

Browse files
committed
Merge pull request #346 from thomasgriffin/feature/fix-code-quality
Improve code quality
2 parents 9ae52ad + e9cc2f5 commit 5eeac74

File tree

1 file changed

+63
-59
lines changed

1 file changed

+63
-59
lines changed

class-tgm-plugin-activation.php

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -249,41 +249,43 @@ public function init() {
249249
// After this point, the plugins should be registered and the configuration set.
250250

251251
// Proceed only if we have plugins to handle.
252-
if ( $this->plugins ) {
253-
$sorted = array();
252+
if ( ! is_array( $this->plugins ) || empty( $this->plugins ) ) {
253+
return;
254+
}
254255

255-
foreach ( $this->plugins as $plugin ) {
256-
$sorted[] = $plugin['name'];
257-
}
256+
$sorted = array();
258257

259-
array_multisort( $sorted, SORT_ASC, $this->plugins );
258+
foreach ( $this->plugins as $plugin ) {
259+
$sorted[] = $plugin['name'];
260+
}
260261

261-
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
262-
add_action( 'admin_head', array( $this, 'dismiss' ) );
263-
add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) );
264-
add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) );
262+
array_multisort( $sorted, SORT_ASC, $this->plugins );
265263

266-
if ( $this->has_notices ) {
267-
add_action( 'admin_notices', array( $this, 'notices' ) );
268-
add_action( 'admin_init', array( $this, 'admin_init' ), 1 );
269-
add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) );
270-
add_action( 'switch_theme', array( $this, 'update_dismiss' ) );
271-
}
264+
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
265+
add_action( 'admin_head', array( $this, 'dismiss' ) );
266+
add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) );
267+
add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) );
272268

273-
// Setup the force activation hook.
274-
foreach ( $this->plugins as $plugin ) {
275-
if ( isset( $plugin['force_activation'] ) && true === $plugin['force_activation'] ) {
276-
add_action( 'admin_init', array( $this, 'force_activation' ) );
277-
break;
278-
}
269+
if ( $this->has_notices ) {
270+
add_action( 'admin_notices', array( $this, 'notices' ) );
271+
add_action( 'admin_init', array( $this, 'admin_init' ), 1 );
272+
add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) );
273+
add_action( 'switch_theme', array( $this, 'update_dismiss' ) );
274+
}
275+
276+
// Setup the force activation hook.
277+
foreach ( $this->plugins as $plugin ) {
278+
if ( isset( $plugin['force_activation'] ) && true === $plugin['force_activation'] ) {
279+
add_action( 'admin_init', array( $this, 'force_activation' ) );
280+
break;
279281
}
282+
}
280283

281-
// Setup the force deactivation hook.
282-
foreach ( $this->plugins as $plugin ) {
283-
if ( isset( $plugin['force_deactivation'] ) && true === $plugin['force_deactivation'] ) {
284-
add_action( 'switch_theme', array( $this, 'force_deactivation' ) );
285-
break;
286-
}
284+
// Setup the force deactivation hook.
285+
foreach ( $this->plugins as $plugin ) {
286+
if ( isset( $plugin['force_deactivation'] ) && true === $plugin['force_deactivation'] ) {
287+
add_action( 'switch_theme', array( $this, 'force_deactivation' ) );
288+
break;
287289
}
288290
}
289291

@@ -321,7 +323,8 @@ public function admin_init() {
321323
wp_enqueue_style( 'plugin-install' );
322324

323325
global $tab, $body_id;
324-
$body_id = $tab = 'plugin-information';
326+
$body_id = 'plugin-information';
327+
$tab = 'plugin-information';
325328

326329
install_plugin_information();
327330

@@ -480,9 +483,9 @@ protected function do_plugin_install() {
480483
if ( isset( $_GET['plugin'] ) && ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) ) {
481484
check_admin_referer( 'tgmpa-install' );
482485

483-
$plugin['name'] = $_GET['plugin_name']; // Plugin name. // @todo needs sanitizing, figure out how
484-
$plugin['slug'] = sanitize_title( $_GET['plugin'] ); // Plugin slug.
485-
$plugin['source'] = $_GET['plugin_source']; // Plugin source. // @todo needs sanitizing, figure out how
486+
$plugin['name'] = $_GET['plugin_name'];
487+
$plugin['slug'] = sanitize_title( $_GET['plugin'] );
488+
$plugin['source'] = $_GET['plugin_source'];
486489

487490
// Pass all necessary information via URL if WP_Filesystem is needed.
488491
$url = wp_nonce_url(
@@ -498,6 +501,7 @@ protected function do_plugin_install() {
498501
),
499502
'tgmpa-install'
500503
);
504+
501505
$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
502506
$fields = array( 'tgmpa-install' ); // Extra fields to pass to WP_Filesystem.
503507

@@ -518,7 +522,7 @@ protected function do_plugin_install() {
518522
$api = plugins_api( 'plugin_information', array( 'slug' => $plugin['slug'], 'fields' => array( 'sections' => false ) ) );
519523

520524
if ( is_wp_error( $api ) ) {
521-
if ( WP_DEBUG === true ) {
525+
if ( true === WP_DEBUG ) {
522526
wp_die( esc_html( $this->strings['oops'] ) . var_dump( $api ) ); // wpcs: xss ok
523527
}
524528
else {
@@ -549,7 +553,7 @@ protected function do_plugin_install() {
549553
$source = ( 'upload' === $type ) ? $this->default_path . $plugin['source'] : $plugin['source'];
550554

551555
// Create a new instance of Plugin_Upgrader.
552-
$upgrader = new Plugin_Upgrader( $skin = new Plugin_Installer_Skin( compact( 'type', 'title', 'url', 'nonce', 'plugin', 'api' ) ) );
556+
$upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin( compact( 'type', 'title', 'url', 'nonce', 'plugin', 'api' ) ) );
553557

554558
// Perform the action and install the plugin from the $source urldecode().
555559
add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
@@ -605,14 +609,14 @@ protected function do_plugin_install() {
605609
check_admin_referer( 'tgmpa-activate', 'tgmpa-activate-nonce' );
606610

607611
// Populate $plugin array with necessary information.
608-
$plugin['name'] = $_GET['plugin_name']; // @todo needs sanitizing, figure out how
612+
$plugin['name'] = $_GET['plugin_name'];
609613
$plugin['slug'] = sanitize_title( $_GET['plugin'] );
610-
$plugin['source'] = $_GET['plugin_source']; // @todo needs sanitizing, figure out how
614+
$plugin['source'] = $_GET['plugin_source'];
611615

612-
$plugin_data = get_plugins( '/' . $plugin['slug'] ); // Retrieve all plugins.
613-
$plugin_file = array_keys( $plugin_data ); // Retrieve all plugin files from installed plugins.
616+
$plugin_data = get_plugins( '/' . $plugin['slug'] ); // Retrieve all plugins.
617+
$plugin_file = array_keys( $plugin_data ); // Retrieve all plugin files from installed plugins.
614618
$plugin_to_activate = $plugin['slug'] . '/' . $plugin_file[0]; // Match plugin slug with appropriate plugin file.
615-
$activate = activate_plugin( $plugin_to_activate ); // Activate the plugin.
619+
$activate = activate_plugin( $plugin_to_activate ); // Activate the plugin.
616620

617621
if ( is_wp_error( $activate ) ) {
618622
echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>';
@@ -862,12 +866,12 @@ public function notices() {
862866
array(
863867
'install' => ( current_user_can( 'install_plugins' ) ) ? $show_install_link : '',
864868
'activate' => ( current_user_can( 'activate_plugins' ) ) ? $show_activate_link : '',
865-
'dismiss' => $this->dismissable ? '<a class="dismiss-notice" href="' . esc_url( add_query_arg( 'tgmpa-dismiss', 'dismiss_admin_notices' ) ) . '" target="_parent">' . esc_html( $this->strings['dismiss'] ) . '</a>' : '',
869+
'dismiss' => $this->dismissable ? '<a href="' . esc_url( add_query_arg( 'tgmpa-dismiss', 'dismiss_admin_notices' ) ) . '" class="dismiss-notice" target="_parent">' . esc_html( $this->strings['dismiss'] ) . '</a>' : '',
866870
)
867871
);
868872

869-
$action_links = array_filter( $action_links ); // Remove any empty array items.
870-
if ( $action_links ) {
873+
$action_links = array_filter( (array) $action_links ); // Remove any empty array items.
874+
if ( ! empty( $action_links ) ) {
871875
$rendered .= apply_filters( 'tgmpa_notice_rendered_action_links', '<p>' . implode( ' | ', $action_links ) . '</p>' );
872876
}
873877

@@ -1085,11 +1089,7 @@ public function _get_plugin_data_from_name( $name, $data = 'slug' ) {
10851089
*/
10861090
protected function is_tgmpa_page() {
10871091

1088-
if ( isset( $_GET['page'] ) && $this->menu === $_GET['page'] ) {
1089-
return true;
1090-
}
1091-
1092-
return false;
1092+
return isset( $_GET['page'] ) && $this->menu === $_GET['page'];
10931093

10941094
}
10951095

@@ -1220,7 +1220,7 @@ function tgmpa( $plugins, $config = array() ) {
12201220
call_user_func( array( $instance, 'register' ), $plugin );
12211221
}
12221222

1223-
if ( $config ) {
1223+
if ( ! empty( $config ) && is_array( $config ) ) {
12241224
call_user_func( array( $instance, 'config' ), $config );
12251225
}
12261226
}
@@ -1434,6 +1434,8 @@ protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
14341434
*
14351435
* @param array $item Array of item data.
14361436
* @param string $column_name The name of the column.
1437+
*
1438+
* @return string
14371439
*/
14381440
public function column_default( $item, $column_name ) {
14391441

@@ -1619,7 +1621,7 @@ public function process_bulk_actions() {
16191621
}
16201622
// Looks like the user can use the direct method, take from $_POST.
16211623
elseif ( isset( $_POST['plugin'] ) ) {
1622-
$plugins = (array) $_POST['plugin']; // @todo needs sanitizing, figure out how
1624+
$plugins = (array) $_POST['plugin'];
16231625
}
16241626
// Nothing has been submitted.
16251627
else {
@@ -1724,6 +1726,7 @@ public function process_bulk_actions() {
17241726
),
17251727
'bulk-plugins'
17261728
);
1729+
17271730
$method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
17281731
$fields = array( 'action', '_wp_http_referer', '_wpnonce' ); // Extra fields to pass to WP_Filesystem.
17291732

@@ -1740,8 +1743,8 @@ public function process_bulk_actions() {
17401743
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // Need for upgrade classes
17411744

17421745
// Store all information in arrays since we are processing a bulk installation.
1743-
$api = array();
1744-
$sources = array();
1746+
$api = array();
1747+
$sources = array();
17451748

17461749
// Loop through each plugin to install and try to grab information from WordPress API, if not create 'tgmpa-empty' scalar.
17471750
$i = 0;
@@ -1757,7 +1760,7 @@ public function process_bulk_actions() {
17571760
unset( $plugin, $temp );
17581761

17591762
if ( is_wp_error( $api ) ) {
1760-
if ( WP_DEBUG === true ) {
1763+
if ( true === WP_DEBUG ) {
17611764
wp_die( esc_html( $this->tgmpa->strings['oops'] ) . var_dump( $api ) ); // wpcs: xss ok
17621765
}
17631766
else {
@@ -1778,7 +1781,7 @@ public function process_bulk_actions() {
17781781
$names = $plugin_names;
17791782

17801783
// Create a new instance of TGM_Bulk_Installer.
1781-
$installer = new TGM_Bulk_Installer( $skin = new TGM_Bulk_Installer_Skin( compact( 'url', 'nonce', 'names' ) ) );
1784+
$installer = new TGM_Bulk_Installer( TGM_Bulk_Installer_Skin( compact( 'url', 'nonce', 'names' ) ) );
17821785

17831786
// Wrap the install process with the appropriate HTML.
17841787
echo '<div class="tgmpa wrap">',
@@ -1800,7 +1803,7 @@ public function process_bulk_actions() {
18001803
check_admin_referer( 'bulk-' . $this->_args['plural'] );
18011804

18021805
// Grab plugin data from $_POST.
1803-
$plugins = isset( $_POST['plugin'] ) ? (array) $_POST['plugin'] : array(); // @todo needs sanitizing, figure out how
1806+
$plugins = isset( $_POST['plugin'] ) ? (array) $_POST['plugin'] : array();
18041807
$plugins_to_activate = array();
18051808

18061809
// Split plugin value into array with plugin file path, plugin source and plugin name.
@@ -2041,7 +2044,8 @@ public function bulk_install( $packages ) {
20412044
* @since 2.2.0
20422045
*
20432046
* @param array $options The installation config options
2044-
* @return null/array Return early if error, array of installation data on success
2047+
*
2048+
* @return null|array Return early if error, array of installation data on success
20452049
*/
20462050
public function run( $options ) {
20472051

@@ -2303,20 +2307,20 @@ public function __construct( $args = array() ) {
23032307
*/
23042308
public function add_strings() {
23052309

2306-
$this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'tgmpa' );
2307-
$this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', 'tgmpa' );
2310+
$this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'tgmpa' );
2311+
$this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', 'tgmpa' );
23082312

23092313
// Automatic activation strings.
23102314
if ( $this->tgmpa->is_automatic ) {
23112315
$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' );
2312-
$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>';
2316+
$this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', 'tgmpa' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'tgmpa' ) . '</span>.</a>';
23132317
$this->upgrader->strings['skin_upgrade_end'] = __( 'All installations and activations have been completed.', 'tgmpa' );
23142318
$this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
23152319
}
23162320
// Default installation strings.
23172321
else {
23182322
$this->upgrader->strings['skin_upgrade_start'] = __( 'The installation process is starting. This process may take a while on some hosts, so please be patient.', 'tgmpa' );
2319-
$this->upgrader->strings['skin_update_successful'] = esc_html__( '%1$s installed successfully.', 'tgmpa' ) . ' <a onclick="%2$s" href="#" class="hide-if-no-js"><span>' . esc_html__( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'tgmpa' ) . '</span>.</a>';
2323+
$this->upgrader->strings['skin_update_successful'] = esc_html__( '%1$s installed successfully.', 'tgmpa' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'tgmpa' ) . '</span>.</a>';
23202324
$this->upgrader->strings['skin_upgrade_end'] = __( 'All installations have been completed.', 'tgmpa' );
23212325
$this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
23222326
}

0 commit comments

Comments
 (0)