Created
July 5, 2015 22:20
-
-
Save jacobsantos/456e1db776a9b0b4780e to your computer and use it in GitHub Desktop.
WordPress Plugin Refactoring Example
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 | |
interface StorageInterface { | |
public function listen( $name, $listener, $priority = 10, $limitArguments = 1 ); | |
public function hasListener( $name, $listener = null ); | |
public function silence( $name, $listener, $priority = 10 ); | |
public function silenceAll( $name, $priority = false ); | |
public function firing(); | |
public function listened( $name = null ); | |
public function fired( $name ); | |
public function apply( $name, $value, array $arguments ); | |
public function call( $name, array $arguments ); | |
} |
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 | |
function event_registry(EventStorageInterface $storage = null) { | |
static $_storage = null; | |
if ( is_object($storage) ) { | |
$_storage = $storage; | |
} | |
if ( !is_object($_storage) ) { | |
$_storage = new Default_Event_Storage; | |
} | |
return $_storage; | |
} |
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 | |
function add_filter( $name, $listener, $priority = 10, $limitArguments = 1 ) { | |
return event_registry()->listen( $name, $listener, $priority, $limitArguments ); | |
} | |
function has_filter( $name, $listener = null ) { | |
return event_registry()->hasListener( $name, $listener ); | |
} | |
function apply_filters( $name, $value ) { | |
$arguments = array(); | |
if (func_num_args() > 2) { | |
$arguments = array_slice(func_get_args(), 2); | |
} | |
return event_registry()->apply( $name, $value, $arguments ); | |
} | |
function apply_filters_ref_array( $name, $args ) { | |
$default = array_shift($args); | |
return event_registry()->apply( $name, $default, $args ); | |
} | |
function remove_filter( $name, $listener, $priority = 10 ) { | |
return event_registry()->silence( $name, $listener, $priority ); | |
} | |
function remove_all_filters( $name, $priority = false ) { | |
return event_registry()->silenceAll( $name, $priority ); | |
} | |
function current_filter() { | |
return event_registry()->firing(); | |
} | |
function current_action() { | |
return event_registry()->firing(); | |
} | |
function doing_filter( $filter = null ) { | |
return event_registry()->listened( $filter ); | |
} | |
function doing_action( $action = null ) { | |
return event_registry()->listened( $action ); | |
} | |
function add_action( $name, $function_to_add, $priority = 10, $accepted_args = 1 ) { | |
return event_registry()->listen( $name, $function_to_add, $priority, $accepted_args ); | |
} | |
function do_action($name, $arg = '') { | |
$args = array(); | |
if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) { // array(&$this) | |
$args[] =& $arg[0]; | |
} else { | |
$args[] = $arg; | |
} | |
if ( func_num_args() > 2 ) { | |
$args = array_merge($args, array_slice(func_get_args(), 2)); | |
} | |
event_registry()->call($name, $args); | |
} | |
function did_filter( $name ) { | |
return event_registry()->fired( $name ); | |
} | |
function did_action( $name ) { | |
return event_registry()->fired( $name ); | |
} | |
function do_action_ref_array( $name, $arguments ) { | |
event_registry()->call( $name, $arguments ); | |
} | |
function has_action($name, $listener = null) { | |
return event_registry()->hasListener( $name, $listener ); | |
} | |
function remove_action( $name, $listener, $priority = 10 ) { | |
return event_registry()->silence( $name, $listener, $priority ); | |
} | |
function remove_all_actions( $name, $priority = false ) { | |
return event_registry()->silenceAll( $name, $priority ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment