-
-
Save ravinGit/4094029 to your computer and use it in GitHub Desktop.
Improved class concept
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 | |
/***** | |
All new versions will be posted at | |
https://github.com/rmccue/Rotor_WPPlugin | |
Please use that repository instead of this Gist. | |
******/ | |
/* | |
Plugin Name: WP_Plugin class test | |
Description: A better test! | |
Author: Ryan McCue | |
Version: 1.0 | |
Author URI: http://ryanmccue.info/ | |
*/ | |
class WP_Plugin { | |
public function __construct() { | |
$self = new ReflectionClass($this); | |
foreach ($self->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { | |
$params = $method->getNumberOfParameters(); | |
$hooks = array('filter' => array(), 'action' => array()); | |
if (strpos($method->name, 'filter_') === 0) { | |
$hook = substr($method->name, 7); | |
$hooks['action'][$hook] = 10; | |
} | |
elseif (strpos($method->name, 'action_') === 0) { | |
$hook = substr($method->name, 7); | |
$hooks['action'][$hook] = 10; | |
} | |
else { | |
$doc = $method->getDocComment(); | |
if (empty($doc) || (strpos($doc, '@wordpress-filter') === false && strpos($doc, '@wordpress-action') === false)) { | |
continue; | |
} | |
preg_match_all('#^\s+\*\s*@wordpress-(action|filter)\s+([\w-]+)(\s*\d+)?#im', $doc, $matches, PREG_SET_ORDER); | |
var_dump($matches); | |
if (empty($matches)) { | |
continue; | |
} | |
foreach ($matches as $match) { | |
$type = $match[1]; | |
$hook = $match[2]; | |
$priority = 10; | |
if (!empty($match[3])) { | |
$priority = (int) $match[3]; | |
} | |
$hooks[$type][$hook] = $priority; | |
} | |
} | |
var_dump(__LINE__, $hooks); | |
foreach ($hooks['filter'] as $hook => $priority) { | |
add_filter($hook, array($this, $method->name), $priority, $params); | |
} | |
foreach ($hooks['action'] as $hook => $priority) { | |
add_action($hook, array($this, $method->name), $priority, $params); | |
} | |
} | |
} | |
} | |
// Here's an example of a plugin that would benefit from the above | |
class Some_Plugin extends WP_Plugin { | |
// Will filter the_title | |
public function filter_the_title( $title ) { | |
return $title . '123'; | |
} | |
// Will filter the_content | |
public function filter_the_content( $content ) { | |
return $content . ' Add more content.'; | |
} | |
// Will run during wp_footer | |
public function action_wp_footer() { | |
echo "I'm in the footer!"; | |
} | |
/** | |
* @wordpress-action init | |
* @wordpress-action admin_init 25 | |
*/ | |
public function my_init() { | |
echo "I'm in the footer!"; | |
} | |
} | |
$some_plugin = new Some_Plugin(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment