Created
April 24, 2013 23:12
-
-
Save geilt/5456354 to your computer and use it in GitHub Desktop.
Add Action and Do Action for any system using Globals. Inspired by Wordpress but custom Coded.
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 | |
// Queue: Queues a function or object method to be run during do_action | |
function add_action($action, $function, $priority = 10, $args = array()){ | |
$GLOBALS['actions'][$action][$priority][] = array('function' => $function, 'args' => $args); | |
} | |
// Hook: Runs a function or a object method based on add_action | |
function do_action($action){ | |
$done = ''; | |
if(!empty($GLOBALS['actions'][$action])): | |
foreach($GLOBALS['actions'][$action] as $priorities): | |
foreach($priorities as $do): | |
if(is_array($do['function'])): | |
$object = $do['function'][0]; | |
$method = $do['function'][1]; | |
if(method_exists($object, $method)): | |
if(!empty($do['args'])): | |
$done .= call_user_func_array( array($object, $method), $do['args'] ); | |
else: | |
$done .= $object->$method(); | |
endif; | |
if(!empty($done)) $done .= PHP_EOL; | |
endif; | |
else: | |
$function = $do['function']; | |
if(function_exists($function)): | |
if(!empty($do['args'])): | |
$done .= call_user_func_array( $function , $do['args'] ); | |
else: | |
$done .= $function(); | |
endif; | |
if(!empty($done)) $done .= PHP_EOL; | |
endif; | |
endif; | |
endforeach; | |
endforeach; | |
endif; | |
if(!empty($done)) echo $done; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment