| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Helpers; |
| 4 |
|
| 5 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 |
use Give\Log\Log; |
| 7 |
|
| 8 |
class Hooks |
| 9 |
{ |
| 10 |
/** |
| 11 |
* A function which extends the WordPress add_action method to handle the instantiation of a class |
| 12 |
* once the action is fired. This prevents the need to instantiate a class before adding it to hook. |
| 13 |
* |
| 14 |
* @since 2.8.0 |
| 15 |
* |
| 16 |
* @param string $tag |
| 17 |
* @param string $class |
| 18 |
* @param string $method |
| 19 |
* @param int $priority |
| 20 |
* @param int $acceptedArgs |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public static function addAction($tag, $class, $method = '__invoke', $priority = 10, $acceptedArgs = 1) |
| 25 |
{ |
| 26 |
if ( ! method_exists($class, $method)) { |
| 27 |
throw new InvalidArgumentException("The method $method does not exist on $class"); |
| 28 |
} |
| 29 |
|
| 30 |
add_action( |
| 31 |
$tag, |
| 32 |
static function () use ($tag, $class, $method) { |
| 33 |
// Provide a way of disabling the hook |
| 34 |
if (apply_filters("give_disable_hook-{$tag}", false) || apply_filters( |
| 35 |
"give_disable_hook-{$tag}:{$class}@{$method}", |
| 36 |
false |
| 37 |
)) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$instance = give($class); |
| 42 |
|
| 43 |
call_user_func_array([$instance, $method], func_get_args()); |
| 44 |
}, |
| 45 |
$priority, |
| 46 |
$acceptedArgs |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* A function which extends the WordPress add_filter method to handle the instantiation of a class |
| 52 |
* once the filter is fired. This prevents the need to instantiate a class before adding it to hook. |
| 53 |
* |
| 54 |
* @since 2.8.0 |
| 55 |
* |
| 56 |
* @param string $tag |
| 57 |
* @param string $class |
| 58 |
* @param string $method |
| 59 |
* @param int $priority |
| 60 |
* @param int $acceptedArgs |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public static function addFilter($tag, $class, $method = '__invoke', $priority = 10, $acceptedArgs = 1) |
| 65 |
{ |
| 66 |
if ( ! method_exists($class, $method)) { |
| 67 |
throw new InvalidArgumentException("The method $method does not exist on $class"); |
| 68 |
} |
| 69 |
|
| 70 |
add_filter( |
| 71 |
$tag, |
| 72 |
static function () use ($tag, $class, $method) { |
| 73 |
// Provide a way of disabling the hook |
| 74 |
if (apply_filters("give_disable_hook-{$tag}", false) || apply_filters( |
| 75 |
"give_disable_hook-{$tag}:{$class}@{$method}", |
| 76 |
false |
| 77 |
)) { |
| 78 |
return func_get_arg(0); |
| 79 |
} |
| 80 |
|
| 81 |
$instance = give($class); |
| 82 |
|
| 83 |
return call_user_func_array([$instance, $method], func_get_args()); |
| 84 |
}, |
| 85 |
$priority, |
| 86 |
$acceptedArgs |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Calls the WordPress do_action filter and logs the execution. |
| 92 |
* |
| 93 |
* @since 2.19.6 |
| 94 |
* |
| 95 |
* @param string $hookName The name of the action to be executed. |
| 96 |
* @param mixed ...$args Optional. Additional arguments which are passed on to the functions hooked to the action. Default empty. |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
public static function doAction($hookName, ...$args) |
| 100 |
{ |
| 101 |
do_action($hookName, ...$args); |
| 102 |
|
| 103 |
Log::debug( |
| 104 |
"Hook Dispatched: $hookName", |
| 105 |
compact('hookName', 'args') |
| 106 |
); |
| 107 |
} |
| 108 |
} |
| 109 |
|