Skip to content

Commit

Permalink
fix issue with multiple functions on hook
Browse files Browse the repository at this point in the history
  • Loading branch information
zevilz committed Jul 1, 2018
1 parent 754948f commit e190b7a
Showing 1 changed file with 47 additions and 50 deletions.
97 changes: 47 additions & 50 deletions zHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@
* URL: https://github.com/zevilz/zHook
* Author: Alexandr "zEvilz" Emshanov
* License: MIT
* Version: 1.0.0
* Version: 1.1.0
*/

/**
* Add filter to hooks array
*
* @param string $name Hook name
* @param string $function Function name for hook
* @param int $priority Function priority
* @param int $accepted_args Num of accepted args for function
* @param string $name Hook name
* @param string $function Function name for hook
* @param int $priority Function priority
* @param int $accepted_args Num of accepted args for function
*
* @return bool Result
*/
function add_filter($name, $function, $priority = 10, $accepted_args = 1) {
function add_filter( $name, $function, $priority = 10, $accepted_args = 1 ) {
global $hooks;
$function_isset = false;
if (isset($hooks[$name])) {
foreach ($hooks[$name] as $hook) {
if ($hook['function'] == $function) {
if ( isset( $hooks[$name] ) ) {
foreach ( $hooks[$name] as $hook ) {
if ( $hook['function'] == $function ) {
$function_isset = true;
}
}
}
if (!$function_isset) {
if (!preg_match("|^[\d]+$|", $priority)) {
if ( ! $function_isset ) {
if ( ! preg_match( "|^[\d]+$|", $priority ) ) {
$priority = 10;
}
if (!preg_match("|^[\d]+$|", $accepted_args)) {
if ( ! preg_match( "|^[\d]+$|", $accepted_args ) ) {
$accepted_args = 1;
}
$hooks[$name][] = [
'function' => $function,
'priority' => $priority,
'accepted_args' => $accepted_args
'accepted_args' => $accepted_args,
];
return true;
}
Expand All @@ -49,46 +49,43 @@ function add_filter($name, $function, $priority = 10, $accepted_args = 1) {
/**
* Add action to hooks array
*
* @param string $name Hook name
* @param string $function Function name for hook
* @param int $priority Function priority
* @param int $accepted_args Num of accepted args for function
* @param string $name Hook name
* @param string $function Function name for hook
* @param int $priority Function priority
* @param int $accepted_args Num of accepted args for function
*
* @return bool Result
*/
function add_action($name, $function, $priority = 10, $accepted_args = 0) {
return add_filter($name, $function, $priority, $accepted_args);
function add_action( $name, $function, $priority = 10, $accepted_args = 0 ) {
return add_filter( $name, $function, $priority, $accepted_args );
}

/**
* Apply hook filters
*
* @param string $name Hook name
* @param mixed $data Input data
* @param mixed $data Input data
*
* @return mixed Filtered input data
*/
function apply_filters($name, $data) {
function apply_filters( $name, $data ) {
global $hooks;
if (isset($hooks[$name])) {
usort($hooks[$name], function($a, $b){
return ($a['priority'] - $b['priority']);
});
if ( isset( $hooks[$name] ) ) {
usort( $hooks[$name], function( $a, $b ) {
return ( $a['priority'] - $b['priority'] );
} );
$args = func_get_args();
array_shift($args);
$num_args = count($args);
foreach ($hooks[$name] as $hook) {
if ($hook['accepted_args'] == 0) {
$data = call_user_func_array($hook['function'], []);
$num_args = count( $args );
foreach ( $hooks[$name] as $hook ) {
$args[0] = $data;
if ( $hook['accepted_args'] == 0 ) {
$data = call_user_func_array( $hook['function'], [] );
}
elseif ($hook['accepted_args'] >= $num_args) {
$data = call_user_func_array($hook['function'], $args);
elseif ( $hook['accepted_args'] >= $num_args ) {
$data = call_user_func_array( $hook['function'], $args );
}
else {
$data = call_user_func_array(
$hook['function'],
array_slice($args, 0, (int)$hook['accepted_args'])
);
$data = call_user_func_array( $hook['function'], array_slice( $args, 0, (int) $hook['accepted_args'] ) );
}
}
}
Expand All @@ -102,34 +99,34 @@ function apply_filters($name, $data) {
*
* @return mixed Action output
*/
function do_action($name) {
function do_action( $name ) {
$args = func_get_args();
if (count($args) == 1) {
if ( count( $args ) == 1 ) {
$args[] = '';
}
return call_user_func_array('apply_filters', $args);
return call_user_func_array( 'apply_filters', $args );
}

/**
* Remove filter by hook name and function
*
* @param string $name Hook name
* @param string $name Hook name
* @param string $function Function name for hook
*
* @return bool Result
*/
function remove_filter($name, $function) {
function remove_filter( $name, $function ) {
$result = false;
global $hooks;
if (isset($hooks[$name])) {
foreach ($hooks[$name] as $k=>$hook_item) {
if ($hook_item['function'] == $function) {
unset($hooks[$name][$k]);
if ( isset( $hooks[$name] ) ) {
foreach ( $hooks[$name] as $k => $hook_item ) {
if ( $hook_item['function'] == $function ) {
unset( $hooks[$name][$k] );
$result = true;
}
}
if (count($hooks[$name]) == 0) {
unset($hooks[$name]);
if ( count( $hooks[$name] ) == 0 ) {
unset( $hooks[$name] );
}
}
return $result;
Expand All @@ -138,11 +135,11 @@ function remove_filter($name, $function) {
/**
* Remove action by hook name and function
*
* @param string $name Hook name
* @param string $name Hook name
* @param string $function Function name for hook
*
* @return bool Result
*/
function remove_action($name, $function) {
return remove_filter($name, $function);
function remove_action( $name, $function ) {
return remove_filter( $name, $function );
}

0 comments on commit e190b7a

Please sign in to comment.