-
-
Save thefuxia/1627700 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 # -*- coding: utf-8 -*- | |
declare( encoding = 'UTF-8' ); | |
/** | |
* Plugin Name: WP_Plugin class test | |
* Version: 2012.01.17 | |
* Required: 3.3 | |
* Author: Thomas Scholz | |
* Author URI: http://toscho.de | |
* License: GPL | |
*/ | |
! defined( 'ABSPATH' ) and exit; | |
class WP_Plugin | |
{ | |
public function __construct() | |
{ | |
$self = new ReflectionClass( $this ); | |
$public_methods = $self->getMethods( ReflectionMethod::IS_PUBLIC ); | |
if ( empty ( $public_methods ) ) | |
{ | |
return; | |
} | |
foreach ( $public_methods as $method ) | |
{ | |
$params = $method->getNumberOfParameters(); | |
if ( 0 === strpos( $method->name, 'filter_' ) ) | |
{ | |
// priority MUST be two numbers long, eg.: 04 or 98 | |
$priority = (int) substr( $method->name, 7, 2 ); | |
$name = substr( $method->name, 10 ); | |
add_filter( | |
$name, | |
array ( $this, $method->name ), | |
$priority, | |
$params | |
); | |
} | |
elseif ( 0 === strpos( $method->name, 'action_' ) ) | |
{ | |
$priority = (int) substr( $method->name, 7, 2 ); | |
$name = substr( $method->name, 10 ); | |
add_action( | |
$name, | |
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 | |
{ | |
public static function init() | |
{ | |
new self; | |
} | |
// Will filter the_title | |
public function filter_20_the_title( $title ) | |
{ | |
return $title . ' 123'; | |
} | |
// Will run during wp_footer | |
public function action_99_wp_footer() | |
{ | |
echo "I'm in the footer!"; | |
} | |
} | |
// set a predictable point for execution. | |
add_action( 'plugins_loaded', array ( 'Some_Plugin', 'init' ), 99 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Done. :)