Form
4 years ago
Frontend
4 years ago
Gateways
4 years ago
ArrayDataSet.php
4 years ago
Call.php
4 years ago
Hooks.php
4 years ago
Html.php
4 years ago
Table.php
4 years ago
Utils.php
4 years ago
Hooks.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Helpers; |
| 4 | |
| 5 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 | |
| 7 | class Hooks |
| 8 | { |
| 9 | /** |
| 10 | * A function which extends the WordPress add_action method to handle the instantiation of a class |
| 11 | * once the action is fired. This prevents the need to instantiate a class before adding it to hook. |
| 12 | * |
| 13 | * @since 2.8.0 |
| 14 | * |
| 15 | * @param string $tag |
| 16 | * @param string $class |
| 17 | * @param string $method |
| 18 | * @param int $priority |
| 19 | * @param int $acceptedArgs |
| 20 | * |
| 21 | * @return void |
| 22 | */ |
| 23 | public static function addAction($tag, $class, $method = '__invoke', $priority = 10, $acceptedArgs = 1) |
| 24 | { |
| 25 | if ( ! method_exists($class, $method)) { |
| 26 | throw new InvalidArgumentException("The method $method does not exist on $class"); |
| 27 | } |
| 28 | |
| 29 | add_action( |
| 30 | $tag, |
| 31 | static function () use ($tag, $class, $method) { |
| 32 | // Provide a way of disabling the hook |
| 33 | if (apply_filters("give_disable_hook-{$tag}", false) || apply_filters( |
| 34 | "give_disable_hook-{$tag}:{$class}@{$method}", |
| 35 | false |
| 36 | )) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | $instance = give($class); |
| 41 | |
| 42 | call_user_func_array([$instance, $method], func_get_args()); |
| 43 | }, |
| 44 | $priority, |
| 45 | $acceptedArgs |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * A function which extends the WordPress add_filter method to handle the instantiation of a class |
| 51 | * once the filter is fired. This prevents the need to instantiate a class before adding it to hook. |
| 52 | * |
| 53 | * @since 2.8.0 |
| 54 | * |
| 55 | * @param string $tag |
| 56 | * @param string $class |
| 57 | * @param string $method |
| 58 | * @param int $priority |
| 59 | * @param int $acceptedArgs |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | public static function addFilter($tag, $class, $method = '__invoke', $priority = 10, $acceptedArgs = 1) |
| 64 | { |
| 65 | if ( ! method_exists($class, $method)) { |
| 66 | throw new InvalidArgumentException("The method $method does not exist on $class"); |
| 67 | } |
| 68 | |
| 69 | add_filter( |
| 70 | $tag, |
| 71 | static function () use ($tag, $class, $method) { |
| 72 | // Provide a way of disabling the hook |
| 73 | if (apply_filters("give_disable_hook-{$tag}", false) || apply_filters( |
| 74 | "give_disable_hook-{$tag}:{$class}@{$method}", |
| 75 | false |
| 76 | )) { |
| 77 | return func_get_arg(0); |
| 78 | } |
| 79 | |
| 80 | $instance = give($class); |
| 81 | |
| 82 | return call_user_func_array([$instance, $method], func_get_args()); |
| 83 | }, |
| 84 | $priority, |
| 85 | $acceptedArgs |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 |