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