Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Created January 18, 2012 07:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefuxia/1631723 to your computer and use it in GitHub Desktop.
Save thefuxia/1631723 to your computer and use it in GitHub Desktop.
T5_Autohook Plugin
<?php # -*- coding: utf-8 -*-
declare( encoding = 'UTF-8' );
class Autohook_Demo
{
public static function init()
{
new self;
}
public function __construct()
{
new T5_Autohook( $this );
}
/**
* Append 123 to the title
*
* @wordpress-filter the_title 42
* @param string $title
* @return string
*/
public function add_123_to_title( $title )
{
return $title . ' 123';
}
/**
* Shortcode for blogname
*
* @wordpress-shortcode blogname
* @return string
*/
public function show_blogname()
{
return get_bloginfo( 'name' );
}
/**
* Says HELLO!
*
* @wordpress-action wp_footer
* @return void
*/
public function say_hello()
{
echo "HELLO!";
}
}
// set a predictable point for execution.
add_action( 'plugins_loaded', array ( 'Autohook_Demo', 'init' ), 99 );
<?php # -*- coding: utf-8 -*-
declare( encoding = 'UTF-8' );
/**
* Plugin Name: T5_Autohook
* Version: 2012.01.18
* Required: 3.3
* Author: Thomas Scholz
* Author URI: http://toscho.de
* License: GPL
*/
/**
* Runs functions from a class automatically.
*
* Usage: T5_Autohook::init( $class );
*
* The auto-hooked function MUST use a doc block with at least one of the
* following:
* @wordpress-action action name
* @wordpress-filter filter name
* @wordpress-shortcode shortcode name
*
* @author Thomas Scholz (toscho) <info@toscho.de>
*
*/
class T5_Autohook
{
/**
* Constructor.
* Does all the work.
*
* @param object|string $class Object or class name.
*/
public function __construct( $class )
{
$reflection = new ReflectionClass( $class );
$public_methods = $reflection->getMethods( ReflectionMethod::IS_PUBLIC );
if ( empty ( $public_methods ) )
return; // Someone called the class without thinking about.
foreach ( $public_methods as $method )
{
$this->handle_method( $method, $class );
}
}
/**
* Handler for singular method.
*
* @param object $method A ReflectionMethod object
* @param object|string $class Class name or object
* @return void
*/
protected function handle_method( ReflectionMethod $method, $class )
{
if ( ! $doc = $method->getDocComment() )
return; // no doc, no hook
if ( ! $meta = $this->parse_docblock( $doc ) )
return; // not a hook function
foreach ( $meta as $data )
{
list( $type, $hook, $priority ) = $data;
$function = "add_$type";
// All we can eat. A method may use variable parameter length.
$function( $hook, array ( $class, $method->name ), $priority, 99 );
}
}
/**
* Finds action or filter directives.
*
* @param string $doc
* @return array
*/
protected function parse_docblock( $doc )
{
$parts = explode( "\n", $doc );
$out = array ();
foreach ( $parts as $part )
{
preg_match( '~@wordpress-(action|filter|shortcode)\s+([^\s]+)\s*(\d*)~', $part, $m )
and $out[] = array ( $m[1], $m[2], ( '' == $m[3] ) ? 10 : $m[3] );
}
// Easier to check in handle_method() than an empty array.
empty ( $out ) and $out = FALSE;
return $out;
}
}
@DeanMarkTaylor
Copy link

What's your view on use of getDocComment based on this comment from the PHP docs?

Beware when you try to use the getDocComment() methods with eAccelerator turned on. eAccelerator »optimizes« your code in a way that it removes any comments from your source. So at runtime you're not able to get any comments and therefor getDocComments return false.

Are you aware of this?

Cheers.

@DeanMarkTaylor
Copy link

Not to worry - I have found the conversation you had already about this problem here:
https://gist.github.com/1626492#comment-76888

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