Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrisguitarguy/3719649 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/3719649 to your computer and use it in GitHub Desktop.
Collect & display data from current admin screens contextual hooks and from which hook on some globals are available.
<?php
! defined( 'ABSPATH' ) AND exit;
/**
* Plugin Name: Current admin screen info
* Description: Show information about contextual hooks and availability of globals in the admin UI.
* Version: 2012-09-13.0317
* Author: Franz Josef Kaiser <wecodemore@gmail.com>
* Author URI: http://unserkaiser.com
* License: The MIT License (MIT)
*
* Copyright: © Franz Josef Kaiser 2011-2012
*/
if ( ! class_exists( 'current_screen_data' ) )
{
/**
* @package Current Screen Data
* @subpackage Abstract/Base
*/
abstract class current_screen_data
{
protected $data = array();
public function __construct()
{
// Add a subarray for each sub class with the class name as key
if ( ! array_key_exists( get_class( $this ), $this->data ) )
$this->data[ get_class( $this ) ] = array();
add_action( 'all', array( $this, 'collect' ) );
add_filter( 'contextual_help', array( $this, 'show' ), 10, 3 );
}
public function show( $help, $screen_id, $screen )
{
if (
! method_exists( $screen, 'add_help_tab' )
OR ! current_user_can( 'manage_options' )
OR ! defined( 'WP_DEBUG' )
OR ! WP_DEBUG
)
return $help;
foreach ( $this->data as $title => $set )
{
$screen->add_help_tab( array(
'id' => "{$title}-screen-help"
,'title' => ucwords( str_replace( '_', ' ', $title ) )
,'content' => $this->markup( $set )
) );
}
return $help;
}
// Data Collector: Must get defined in child/extending class
abstract protected function collect();
protected function markup( $set )
{
sort( $set );
return sprintf(
"<ul><li>%s</li></ul>"
,implode( "</li><li>", $set )
);
}
} // END Class current_screen_data
} // endif;
if ( ! class_exists( 'current_admin_hooks' ) )
{
add_action( 'plugins_loaded', array( 'current_admin_hooks', 'init' ) );
/**
* @package Current Screen Data
* @subpackage Contextual Hooks
*/
final class current_admin_hooks extends current_screen_data
{
private static $instance;
public static function init()
{
null === self :: $instance AND self :: $instance = new self;
return self :: $instance;
}
public function collect()
{
global $hook_suffix;
// We're done: Remove callback
if ( 'contextual_help' === current_filter() )
remove_filter( current_filter(), __FUNCTION__ );
// Only add when we got context and the current filter has context
if (
! empty( $hook_suffix )
AND strstr( current_filter(), $hook_suffix )
)
$this->data[ get_class() ][] = current_filter();
}
} // END Class current_admin_hooks
} // endif;
if ( ! class_exists( 'current_admin_globals' ) )
{
add_action( 'plugins_loaded', array( 'current_admin_globals', 'init' ) );
/**
* @package Current Screen Data
* @subpackage Globals/Availability
*/
final class current_admin_globals extends current_screen_data
{
private static $instance;
public $globals = array(
'current_user'
,'hook_suffix'
,'menu'
,'page'
,'pagenow'
,'parent_file'
,'self'
,'submenu_file'
,'submenu'
,'taxnow'
,'typenow'
,'userdata'
);
public static function init()
{
null === self :: $instance AND self :: $instance = new self;
return self :: $instance;
}
public function collect()
{
foreach ( $this->globals as $key => $var )
{
// Check availability in global context
if ( ! empty( $GLOBALS[ $var ] ) )
{
$global = $GLOBALS[ $var ];
// Add to result
$this->data[ get_class() ][] = sprintf(
"<td><code>$%s</code></td><td><pre>%s</pre></td><td>%s</td>"
,$var
,var_export($global, true)
,current_filter()
);
// Don't loop this one the next time
unset( $this->globals[ $key ] );
}
// We're done: Remove callback & Return
if (
empty( $this->globals )
OR 'contextual_help' === current_filter()
)
return remove_filter( current_filter(), __FUNCTION__ );
}
}
protected function markup( $set )
{
sort( $set );
return sprintf(
"<p>Filters where globals are already available.</p><table class='widefat'>%s<tr>%s</tr></table>"
,'<thead><tr><th>Name</th><th>Data</th><th>Hook</th></tr></thead>'
,implode( "</tr><tr>", $set )
);
}
} // END Class current_admin_globals
} // endif;
@franz-josef-kaiser
Copy link

Now moved it to a repo. Please feel free to fork this one instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment