Created
January 18, 2012 07:11
-
-
Save thefuxia/1631723 to your computer and use it in GitHub Desktop.
T5_Autohook Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) <[email protected]> | |
* | |
*/ | |
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; | |
} | |
} |
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
What's your view on use of
getDocComment
based on this comment from the PHP docs?Are you aware of this?
Cheers.