Skip to content

Instantly share code, notes, and snippets.

@yorkshiretwist
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorkshiretwist/9407798 to your computer and use it in GitHub Desktop.
Save yorkshiretwist/9407798 to your computer and use it in GitHub Desktop.
WP_Plugin
<?php
/**
* WordPress Plugin class.
*
* A base class from which WordPress plugins can be extended. Provides a
* robust set of convenience methods and properties which make plugin
* development easier and more standardised.
*
* @package WP_Plugin
* @since 0.1
*/
class WP_Plugin {
/**
* The (string) name of the plugin
*
* @since 0.1
* @access public
*/
public $name = 'WP_Plugin';
/**
* A convenience method for adding actions
*
* @since 0.1
* @access public
*
* @return boolean Always true
*/
public function hook ( $hook ) {
// set the default priority as 10
$priority = 10;
// get the name of the method to run
$method_name = $this->sanitize_method( $hook );
// get all the arguments passed to this method
$arguments = func_get_args();
// remove the first argument; this is the name of the action
unset( $arguments[0] );
// loop the remaining arguments
foreach ( ( array ) $arguments as $arg ) {
// if this argument is an integer we use it as the priority
if ( is_int( $arg ) ) {
$priority = $arg;
// otherwise we use it as the method name
} else {
$method_name = $arg;
}
}
// add the action, calling the given method in the current object, allowing up to 999 accepted arguments
// reference: https://codex.wordpress.org/Function_Reference/add_action
return add_action( $hook, array( $this, $method_name ) , $priority, 999 );
}
/**
* Sanitizes the given method name
* - Replaces . characters with _DOT_ and - characters with _DASH_
*
* @since 0.1
* @access private
*
* @return string The sanitized method name
*/
private function sanitize_method( $method_name ) {
return str_replace( array( '.' ,'-' ), array( '_DOT_', '_DASH_' ), $method_name);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment