Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

minor readability clean ups

  • Loading branch information...
commit 333e6316a7456a40a1f2299fdb04e2b02ee62bf3 1 parent b819902
franz-josef-kaiser authored
Showing with 126 additions and 80 deletions.
  1. +68 −32 inc/admin.php
  2. +56 −46 inc/core.php
  3. +2 −2 plugin-dirs.php
View
100 inc/admin.php
@@ -63,17 +63,17 @@ public function init()
$this->handle_actions();
- add_filter( 'views_' . $screen->id, array( &$this, 'views' ) );
+ add_filter( "views_{$screen->id}", array( $this, 'views' ) );
// check to see if we're using one of our custom directories
if ( $this->get_plugin_status() )
{
- add_filter( 'views_' . $screen->id, array( &$this, 'views_again' ) );
- add_filter( 'all_plugins', array( &$this, 'filter_plugins' ) );
- // TODO: support bulk actions
- add_filter( 'bulk_actions-' . $screen->id, '__return_empty_array' );
- add_filter( 'plugin_action_links', array( &$this, 'action_links' ), 10, 2 );
- add_action( 'admin_enqueue_scripts', array( &$this, 'scripts' ) );
+ add_filter( "views_{$screen->id}", array( $this, 'views_again' ) );
+ add_filter( 'all_plugins', array( $this, 'filter_plugins' ) );
+ // @TODO: support bulk actions
+ add_filter( "bulk_actions-{$screen->id}", '__return_empty_array' );
+ add_filter( 'plugin_action_links', array( $this, 'action_links' ), 10, 2 );
+ add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
}
}
@@ -88,20 +88,24 @@ public function views( $views )
global $wp_plugin_directories;
// bail if we don't have any extra dirs
- if ( empty( $wp_plugin_directories ) ) return $views;
+ if ( empty( $wp_plugin_directories ) )
+ return $views;
// Add our directories to the action links
foreach ( $wp_plugin_directories as $key => $info )
{
- if ( ! count( $this->plugins[$key] ) ) continue;
- $class = $this->get_plugin_status() == $key ? ' class="current" ' : '';
- $views[$key] = sprintf(
- '<a href="%s"' . $class . '>%s <span class="count">(%d)</span></a>',
+ if ( ! count( $this->plugins[$key] ) )
+ continue;
+
+ $views[ $key ] = sprintf(
+ '<a href="%s"%s>%s <span class="count">(%d)</span></a>',
add_query_arg( 'plugin_status', $key, 'plugins.php' ),
+ $this->get_plugin_status() == $key ? ' class="current" ' : '',
esc_html( $info['label'] ),
- count( $this->plugins[$key] )
+ count( $this->plugins[ $key ] )
);
}
+
return $views;
}
@@ -111,7 +115,9 @@ public function views( $views )
*/
public function views_again( $views )
{
- if ( isset( $views['inactive'] ) ) unset( $views['inactive'] );
+ if ( isset( $views['inactive'] ) )
+ unset( $views['inactive'] );
+
return $views;
}
@@ -124,8 +130,9 @@ public function filter_plugins( $plugins )
if ( $key = $this->get_plugin_status() )
{
$this->all_count = count( $plugins );
- $plugins = $this->plugins[$key];
+ $plugins = $this->plugins[ $key] ;
}
+
return $plugins;
}
@@ -141,19 +148,26 @@ public function action_links( $links, $plugin_file )
$links = array();
$links['activate'] = sprintf(
'<a href="%s" title="Activate this plugin">%s</a>',
- wp_nonce_url( 'plugins.php?action=custom_activate&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . esc_attr( $context ), 'custom_activate-' . $plugin_file ),
+ wp_nonce_url(
+ "plugins.php?action=custom_activate&amp;plugin={$plugin_file}&amp;plugin_status=".esc_attr( $context ),
+ "custom_activate-{$plugin_file}"
+ ),
__( 'Activate' )
);
- $active = get_option( 'active_plugins_' . $context, array() );
+ $active = get_option( "active_plugins_{$context}", array() );
if ( in_array( $plugin_file, $active ) )
{
$links['deactivate'] = sprintf(
'<a href="%s" title="Deactivate this plugin" class="cd-apd-deactivate">%s</a>',
- wp_nonce_url( 'plugins.php?action=custom_deactivate&amp;plugin=' . $plugin_file . '&amp;plugin_status=' . esc_attr( $context ), 'custom_deactivate-' . $plugin_file ),
+ wp_nonce_url(
+ "plugins.php?action=custom_deactivate&amp;plugin={$plugin_file}&amp;plugin_status=".esc_attr( $context ),
+ "custom_deactivate-{$plugin_file}"
+ ),
__( 'Deactivate' )
);
}
+
return $links;
}
@@ -191,10 +205,12 @@ public function scripts()
public function get_plugins()
{
global $wp_plugin_directories;
- if ( empty( $wp_plugin_directories ) ) $wp_plugin_directories = array();
+
+ empty( $wp_plugin_directories ) AND $wp_plugin_directories = array();
+
foreach ( array_keys( $wp_plugin_directories ) as $key )
{
- $this->plugins[$key] = cd_apd_get_plugins( $key );
+ $this->plugins[ $key ] = cd_apd_get_plugins( $key );
}
}
@@ -214,7 +230,9 @@ public function handle_actions()
// Get the plugin we're going to activate
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : false;
- if ( ! $plugin ) return;
+
+ if ( ! $plugin )
+ return;
$context = $this->get_plugin_status();
@@ -224,15 +242,22 @@ public function handle_actions()
if ( ! current_user_can('activate_plugins') )
wp_die( __('You do not have sufficient permissions to manage plugins for this site.') );
- check_admin_referer( 'custom_activate-' . $plugin );
+ check_admin_referer( "custom_activate-{$plugin}" );
$result = cd_apd_activate_plugin( $plugin, $context );
if ( is_wp_error( $result ) )
{
if ( 'unexpected_output' == $result->get_error_code() )
{
- $redirect = add_query_arg( 'plugin_status', $context, self_admin_url( 'plugins.php' ) );
- wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) ) ;
+ wp_redirect( add_query_arg(
+ '_error_nonce',
+ wp_create_nonce( "plugin-activation-error_{$plugin}" ),
+ add_query_arg(
+ 'plugin_status',
+ $context,
+ self_admin_url( 'plugins.php' )
+ )
+ ) );
exit();
}
else
@@ -241,28 +266,33 @@ public function handle_actions()
}
}
- wp_redirect( add_query_arg( array( 'plugin_status' => $context, 'activate' => 'true' ), self_admin_url( 'plugins.php' ) ) );
+ wp_redirect( add_query_arg(
+ array( 'plugin_status' => $context, 'activate' => 'true' ),
+ self_admin_url( 'plugins.php' )
+ ) );
exit();
break;
case 'custom_deactivate':
if ( ! current_user_can( 'activate_plugins' ) )
- wp_die( __('You do not have sufficient permissions to deactivate plugins for this site.') );
+ wp_die( __( 'You do not have sufficient permissions to deactivate plugins for this site.' ) );
- check_admin_referer('custom_deactivate-' . $plugin);
+ check_admin_referer( "custom_deactivate-{$plugin}" );
cd_apd_deactivate_plugins( $plugin, $context );
if ( headers_sent() )
- echo "<meta http-equiv='refresh' content='" . esc_attr( "0;url=plugins.php?deactivate=true&plugin_status=$status&paged=$page&s=$s" ) . "' />";
+ printf(
+ "<meta http-equiv='refresh' content='%s' />",
+ esc_attr( "0;url=plugins.php?deactivate=true&plugin_status={$status}&paged={$page}&s={$s}" )
+ );
else
- wp_redirect( self_admin_url("plugins.php?deactivate=true&plugin_status=$context") );
+ wp_redirect( self_admin_url("plugins.php?deactivate=true&plugin_status={$context}") );
exit();
break;
default:
- do_action( 'custom_plugin_dir_' . $action );
+ do_action( "custom_plugin_dir_{$action}" );
break;
}
-
}
@@ -276,11 +306,17 @@ public function handle_actions()
public function get_plugin_status()
{
global $wp_plugin_directories;
+
$rv = false;
- if ( isset( $_GET['plugin_status'] ) && in_array( $_GET['plugin_status'], array_keys( $wp_plugin_directories ) ) )
+
+ if (
+ isset( $_GET['plugin_status'] )
+ AND in_array( $_GET['plugin_status'], array_keys( $wp_plugin_directories ) )
+ )
{
$rv = $_GET['plugin_status'];
}
+
return $rv;
}
} // end class
View
102 inc/core.php
@@ -1,5 +1,5 @@
<?php
-! defined( 'ABSPATH' ) && exit();
+! defined( 'ABSPATH' ) AND exit();
/**
@@ -13,14 +13,15 @@
function register_plugin_directory( $key, $dir, $label )
{
global $wp_plugin_directories;
- if ( empty( $wp_plugin_directories ) ) $wp_plugin_directories = array();
- if ( ! file_exists( $dir ) && file_exists( trailingslashit( WP_CONTENT_DIR ) . $dir ) )
+ empty( $wp_plugin_directories ) AND $wp_plugin_directories = array();
+
+ if ( ! file_exists( $dir ) AND file_exists( trailingslashit( WP_CONTENT_DIR ) . $dir ) )
{
$dir = trailingslashit( WP_CONTENT_DIR ) . $dir;
}
- $wp_plugin_directories[$key] = array(
+ $wp_plugin_directories[ $key ] = array(
'label' => $label,
'dir' => $dir
);
@@ -35,15 +36,17 @@ function register_plugin_directory( $key, $dir, $label )
function cd_apd_load_more()
{
global $wp_plugin_directories;
- if ( empty( $wp_plugin_directories ) ) $wp_plugin_directories = array();
- foreach ( array_keys( $wp_plugin_directories) as $key )
+
+ empty( $wp_plugin_directories ) AND $wp_plugin_directories = array();
+
+ foreach ( array_keys( $wp_plugin_directories ) as $key )
{
- $active = get_option( 'active_plugins_' . $key, array() );
+ $active = get_option( "active_plugins_{$key}", array() );
foreach( $active as $a )
{
- if( file_exists( $wp_plugin_directories[$key]['dir'] . '/' . $a ) )
+ if ( file_exists( "{$wp_plugin_directories[ $key ]['dir']}/{$a}" ) )
{
- include_once( $wp_plugin_directories[$key]['dir'] . '/' . $a );
+ include_once( "{$wp_plugin_directories[ $key ]['dir']}/{$a}" );
}
}
}
@@ -62,20 +65,16 @@ function cd_apd_get_plugins( $dir_key )
global $wp_plugin_directories;
// invalid dir key? bail
- if ( ! isset( $wp_plugin_directories[$dir_key] ) )
- {
+ if ( ! isset( $wp_plugin_directories[ $dir_key ] ) )
return array();
- }
- else
- {
- $plugin_root = $wp_plugin_directories[$dir_key]['dir'];
- }
+
+ $plugin_root = $wp_plugin_directories[ $dir_key ]['dir'];
if ( ! $cache_plugins = wp_cache_get( 'plugins', 'plugins') )
$cache_plugins = array();
- if ( isset( $cache_plugins[$dir_key] ) )
- return $cache_plugins[$dir_key];
+ if ( isset( $cache_plugins[ $dir_key ] ) )
+ return $cache_plugins[ $dir_key ];
$wp_plugins = array();
@@ -85,48 +84,49 @@ function cd_apd_get_plugins( $dir_key )
while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
if ( substr($file, 0, 1) == '.' )
continue;
- if ( is_dir( $plugin_root.'/'.$file ) ) {
- $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
+ if ( is_dir( "{$plugin_root}/{$file}" ) ) {
+ $plugins_subdir = @ opendir( "{$plugin_root}/{$file}" );
if ( $plugins_subdir ) {
- while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
+ while ( ( $subfile = readdir( $plugins_subdir ) ) !== false ) {
if ( substr($subfile, 0, 1) == '.' )
continue;
if ( substr($subfile, -4) == '.php' )
- $plugin_files[] = "$file/$subfile";
+ $plugin_files[] = "{$file}/{$subfile}";
}
closedir( $plugins_subdir );
}
} else {
- if ( substr($file, -4) == '.php' )
- $plugin_files[] = $file;
+ '.php' === substr( $file, -4 ) AND $plugin_files[] = $file;
}
}
closedir( $plugins_dir );
}
- if ( empty($plugin_files) )
+ if ( empty( $plugin_files ) )
return $wp_plugins;
foreach ( $plugin_files as $plugin_file ) {
- if ( !is_readable( "$plugin_root/$plugin_file" ) )
+ if ( ! is_readable( "{$plugin_root}/{$plugin_file}" ) )
continue;
- $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
+ // Do not apply markup/translate as it'll be cached.
+ $plugin_data = get_plugin_data( "{$plugin_root}/{$plugin_file}", false, false );
if ( empty ( $plugin_data['Name'] ) )
continue;
- $wp_plugins[trim( $plugin_file )] = $plugin_data;
+ $wp_plugins[ trim( $plugin_file ) ] = $plugin_data;
}
uasort( $wp_plugins, '_sort_uname_callback' );
- $cache_plugins[$dir_key] = $wp_plugins;
- wp_cache_set('plugins', $cache_plugins, 'plugins');
+ $cache_plugins[ $dir_key ] = $wp_plugins;
+ wp_cache_set( 'plugins', $cache_plugins, 'plugins' );
return $wp_plugins;
}
+
/**
* Custom plugin activation function.
*/
@@ -137,26 +137,35 @@ function cd_apd_activate_plugin( $plugin, $context, $silent = false )
$redirect = add_query_arg( 'plugin_status', $context, admin_url( 'plugins.php' ) );
$redirect = apply_filters( 'custom_plugin_redirect', $redirect );
- $current = get_option( 'active_plugins_' . $context, array() );
+ $current = get_option( "active_plugins_{$context}", array() );
$valid = cd_apd_validate_plugin( $plugin, $context );
+
if ( is_wp_error( $valid ) )
return $valid;
- if ( !in_array($plugin, $current) ) {
- if ( !empty($redirect) )
- wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
+ if ( ! in_array( $plugin, $current ) ) {
+ if ( ! empty( $redirect ) )
+ {
+ // we'll override this later if the plugin can be included without fatal error
+ wp_redirect( add_query_arg(
+ '_error_nonce'
+ ,wp_create_nonce( "plugin-activation-error_{$plugin}" )
+ ,$redirect
+ ) );
+ }
+
ob_start();
include_once( $valid );
if ( ! $silent ) {
do_action( 'custom_activate_plugin', $plugin, $context );
- do_action( 'custom_activate_' . $plugin, $context );
+ do_action( "custom_activate_{$plugin}", $context );
}
$current[] = $plugin;
sort( $current );
- update_option( 'active_plugins_' . $context, $current );
+ update_option( "active_plugins_{$context}", $current );
if ( ! $silent ) {
do_action( 'custom_activated_plugin', $plugin, $context );
@@ -164,7 +173,7 @@ function cd_apd_activate_plugin( $plugin, $context, $silent = false )
if ( ob_get_length() > 0 ) {
$output = ob_get_clean();
- return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);
+ return new WP_Error( 'unexpected_output', __( 'The plugin generated unexpected output.' ), $output );
}
ob_end_clean();
}
@@ -177,7 +186,7 @@ function cd_apd_activate_plugin( $plugin, $context, $silent = false )
* Deactivate custom plugins
*/
function cd_apd_deactivate_plugins( $plugins, $context, $silent = false ) {
- $current = get_option( 'active_plugins_' . $context, array() );
+ $current = get_option( "active_plugins_{$context}", array() );
foreach ( (array) $plugins as $plugin )
{
@@ -193,12 +202,12 @@ function cd_apd_deactivate_plugins( $plugins, $context, $silent = false ) {
}
if ( ! $silent ) {
- do_action( 'custom_deactivate_' . $plugin, $context );
+ do_action( "custom_deactivate_{$plugin}", $context );
do_action( 'custom_deactivated_plugin', $plugin, $context );
}
}
- update_option( 'active_plugins_' . $context, $current );
+ update_option( "active_plugins_{$context}", $current );
}
@@ -213,27 +222,28 @@ function cd_apd_validate_plugin( $plugin, $context )
$rv = true;
if ( validate_file( $plugin ) )
{
- $rv = new WP_Error('plugin_invalid', __('Invalid plugin path.'));
+ $rv = new WP_Error( 'plugin_invalid', __( 'Invalid plugin path.' ) );
}
global $wp_plugin_directories;
- if ( ! isset( $wp_plugin_directories[$context] ) )
+
+ if ( ! isset( $wp_plugin_directories[ $context ] ) )
{
$rv = new WP_Error( 'invalid_context', __( 'The context for this plugin does not exist' ) );
}
- $dir = $wp_plugin_directories[$context]['dir'];
- if ( ! file_exists( $dir . '/' . $plugin) )
+ $dir = $wp_plugin_directories[ $context ]['dir'];
+ if ( ! file_exists( "{$dir}/{$plugin}" ) )
{
$rv = new WP_Error( 'plugin_not_found', __( 'Plugin file does not exist.' ) );
}
$installed_plugins = cd_apd_get_plugins( $context );
- if ( ! isset($installed_plugins[$plugin]) )
+ if ( ! isset( $installed_plugins[ $plugin ] ) )
{
$rv = new WP_Error( 'no_plugin_header', __('The plugin does not have a valid header.') );
}
- $rv = $dir . '/' . $plugin;
+ $rv = "{$dir}/{$plugin}";
return $rv;
}
View
4 plugin-dirs.php
@@ -14,9 +14,9 @@
define( 'CD_APD_PATH', plugin_dir_path( __FILE__ ) );
define( 'CD_APD_URL', plugin_dir_url( __FILE__ ) );
-require_once( CD_APD_PATH . 'inc/core.php' );
+require_once( CD_APD_PATH.'inc/core.php' );
if( is_admin() )
{
- require_once( CD_APD_PATH . 'inc/admin.php' );
+ require_once( CD_APD_PATH.'inc/admin.php' );
}
Please sign in to comment.
Something went wrong with that request. Please try again.