Advertisement
Guest User

Untitled

a guest
Jan 21st, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 KB | None | 0 0
  1. <?php
  2. // Daniel Dvorkin
  3. // Based on code by Ryan McCue
  4.  
  5. class WPPlugin_Base {
  6.  
  7.     public static function _register_hooks($parent) {
  8.  
  9.         $prefixes = array('filter' => 'filter_', 'action' => 'action_');
  10.  
  11.         $self = new ReflectionClass($parent);
  12.        
  13.         foreach ($self->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
  14.             $params = $method->getNumberOfParameters();
  15.             $hooks = array('filter' => array(), 'action' => array());
  16.  
  17.             if ($prefixes['filter'] === '' || $prefixes['action'] === '') {
  18.                 $hooks['filter'][$method->name] = apply_filters($method->name . '_priority', 10);
  19.             }
  20.  
  21.             elseif ($enable_prefixes === true && strpos($method->name, $prefixes['filter']) === 0) {
  22.                 $hook = substr($method->name, strlen($prefixes['filter']));
  23.                 $hooks['action'][$hook] = apply_filters($method->name . '_priority', 10);
  24.             }
  25.  
  26.             elseif ($enable_prefixes === true && strpos($method->name, $prefixes['action']) === 0) {
  27.                 $hook = substr($method->name, strlen($prefixes['action']));
  28.                 $hooks['action'][$hook] = apply_filters($method->name . '_priority', 10);
  29.             }
  30.  
  31.             foreach ($hooks['filter'] as $hook => $priority) {
  32.                 call_user_func(array($parent, 'add_filter'), $hook, $method->name, $priority, $params, $parent);
  33.             }
  34.             foreach ($hooks['action'] as $hook => $priority) {
  35.                 call_user_func(array($parent, 'add_action'), $hook, $method->name, $priority, $params, $parent);
  36.             }
  37.         }
  38.     }
  39. }
  40.  
  41.  
  42. class WPPlugin extends WPPlugin_Base {
  43.  
  44.     protected function __construct() {
  45.         $this->_register_hooks( $this);
  46.     }
  47.  
  48.     protected function add_filter($hook, $method = null, $priority = 10, $params = null) {
  49.         if ($method === null) {
  50.             $method = $hook;
  51.         }
  52.         elseif (is_int($method)) {
  53.             $priority = $method;
  54.             $method = $hook;
  55.         }
  56.  
  57.         if (!method_exists($this, $method)) {
  58.             throw new InvalidArgumentException('Method does not exist');
  59.         }
  60.  
  61.         if ($params === null) {
  62.             $ref = new ReflectionMethod($this, $method);
  63.             $params = $ref->getNumberOfParameters();
  64.         }
  65.  
  66.         return add_filter($hook, array($this, $method), $priority, $params);
  67.     }
  68.  
  69.    
  70.     protected function add_action($hook, $method = null, $priority = 10, $params = null) {
  71.         if ($method === null) {
  72.             $method = $hook;
  73.         }
  74.         elseif (is_int($method)) {
  75.             $priority = $method;
  76.             $method = $hook;
  77.         }
  78.  
  79.         if (!method_exists($this, $method)) {
  80.             throw new InvalidArgumentException('Method does not exist');
  81.         }
  82.  
  83.         if ($params === null) {
  84.             $ref = new ReflectionMethod($this, $method);
  85.             $params = $ref->getNumberOfParameters();
  86.         }
  87.  
  88.         return add_action($hook, array($this, $method), $priority, $params);
  89.     }
  90. }
  91.  
  92.  
  93. class Example extends WPPlugin {
  94.     public function __construct() {
  95.         parent::__construct();
  96.     }
  97.  
  98.     public function action_init($a) {
  99.         echo 'init!';
  100.     }
  101.  
  102.     public function filter_the_content_priority(){ return 3; }
  103.     public function filter_the_content($content){
  104.         return $content;
  105.     }
  106.  
  107. }
  108.  
  109. $plugin = new Example();
  110.  
  111. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement