-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hook.php
57 lines (41 loc) · 1.54 KB
/
Hook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
* Advanced wrapper for filters
*
* @package ThemePlate
* @since 0.1.0
*/
namespace ThemePlate;
use Error;
use ThemePlate\Hook\Handler;
/**
* @method static bool return( string $tag, $value, int $priority = 10 )
* @method static bool append( string $tag, $value, int $priority = 10 )
* @method static bool prepend( string $tag, $value, int $priority = 10 )
* @method static bool pluck( string $tag, $value, int $priority = 10 )
* @method static bool replace( string $tag, $old, $new, int $priority = 10 )
* @method static bool insert( string $tag, $value, int $position, int $priority = 10 )
* @method static bool once( string $tag, array $value, int $priority = 10 )
*/
class Hook {
public static function __callStatic( string $action, array $arguments ) {
if ( in_array( $action, array( ...Handler::ACTIONS, 'once' ), true ) ) {
$tag = $arguments[0];
$value = $arguments[1];
$priority = $arguments[2] ?? 10;
if ( 'once' === $action ) {
$value = compact( 'value', 'priority' );
}
if ( in_array( $action, array( 'insert', 'replace' ), true ) ) {
$value = array( $arguments[1], $arguments[2] );
$priority = $arguments[3] ?? 10;
}
return add_filter( $tag, array( new Handler( $value ), $action ), $priority );
}
throw new Error( 'Call to undefined method ' . __CLASS__ . '::' . $action . '()' );
}
public static function remove( string $tag, array $value, int $priority = 10 ): bool {
$handler = new Handler( compact( 'value', 'priority' ) );
return $handler->remove( $tag );
}
}