Skip to content

Instantly share code, notes, and snippets.

@markjaquith
Created January 20, 2012 05:44
Show Gist options
  • Save markjaquith/1645537 to your computer and use it in GitHub Desktop.
Save markjaquith/1645537 to your computer and use it in GitHub Desktop.
CWS_Plugin — Exploratory WordPress plugin parent class
<?php
if ( !class_exists( 'CWS_Plugin_v2' ) ) :
class CWS_Plugin_v2 {
public function hook( $hook ) {
$priority = 10;
$method = $this->sanitize_method( $hook );
$args = func_get_args();
unset( $args[0] );
foreach ( (array) $args as $arg ) {
if ( is_int( $arg ) )
$priority = $arg;
else
$method = $arg;
}
return add_action( $hook, array( $this, $method ), $priority, 999 );
}
private function sanitize_method( $method ) {
return str_replace( array( '.', '-' ), array( '_DOT_', '_DASH_' ), $method );
}
}
endif;
class My_Plugin extends CWS_Plugin_v2 {
public static $instance;
public function __construct() {
self::$instance = $this;
$this->hook( 'init' );
$this->hook( 'init', 'init_b' );
$this->hook( 'init', 'init_c', 20 );
$this->hook( 'init', 30, 'init_d' );
$this->hook( 'plugins_loaded', 20 );
}
public function init() {
// Fires on init:10
}
public function init_b() {
// Fires on init:10
}
public function init_c() {
// Fires on init:20
}
public function init_d() {
// Fires on init:30
}
public function plugins_loaded() {
// Fires on plugins_loaded:20
}
}
new My_Plugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment