forked from scribu/wp-scb-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hooks.php
119 lines (99 loc) · 2.24 KB
/
Hooks.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Automatic filter binding.
*/
class scbHooks {
private static $mangle_name;
/**
* Adds.
*
* @param string $class
*
* @return void
*/
public static function add( $class ) {
self::_do( 'add_filter', $class );
}
/**
* Removes.
*
* @param string $class
*
* @return void
*/
public static function remove( $class ) {
self::_do( 'remove_filter', $class );
}
/**
* Prints debug.
*
* @param string $class
* @param string $mangle_name (optional)
*
* @return void
*/
public static function debug( $class, $mangle_name = false ) {
self::$mangle_name = $mangle_name;
echo "<pre>";
self::_do( array( __CLASS__, '_print' ), $class );
echo "</pre>";
}
/**
* Prints.
*
* @param string $tag
* @param array $callback
* @param int $prio
* @param int $argc
*
* @return void
*/
private static function _print( $tag, $callback, $prio, $argc ) {
$static = ! is_object( $callback[0] );
if ( self::$mangle_name ) {
$class = $static ? '__CLASS__' : '$this';
} else if ( $static ) {
$class = "'" . $callback[0] . "'";
} else {
$class = '$' . get_class( $callback[0] );
}
$func = "array( $class, '$callback[1]' )";
echo "add_filter( '$tag', $func";
if ( $prio != 10 || $argc > 1 ) {
echo ", $prio";
if ( $argc > 1 ) {
echo ", $argc";
}
}
echo " );\n";
}
/**
* Processes.
*
* @param string $action
* @param string $class
*
* @return void
*/
private static function _do( $action, $class ) {
$reflection = new ReflectionClass( $class );
foreach ( $reflection->getMethods() as $method ) {
if ( $method->isPublic() && ! $method->isConstructor() ) {
$comment = $method->getDocComment();
if ( preg_match( '/@nohook[ \t\*\n]+/', $comment ) ) {
continue;
}
preg_match_all( '/@hook:?\s+([^\s]+)/', $comment, $matches ) ? $matches[1] : $method->name;
if ( empty( $matches[1] ) ) {
$hooks = array( $method->name );
} else {
$hooks = $matches[1];
}
$priority = preg_match( '/@priority:?\s+(\d+)/', $comment, $matches ) ? $matches[1] : 10;
foreach ( $hooks as $hook ) {
call_user_func( $action, $hook, array( $class, $method->name ), $priority, $method->getNumberOfParameters() );
}
}
}
}
}