-
Notifications
You must be signed in to change notification settings - Fork 238
/
hook.extends.class.php
164 lines (121 loc) · 4.55 KB
/
hook.extends.class.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
if (!defined('_GNUBOARD_')) exit;
Class GML_Hook extends Hook {
protected $filters = array('count' => 0);
protected $callback_filters = array();
protected static $current_filter = false;
protected function runAction($action, $args)
{
$function = $action['function'];
$argsNumber = $action['arguments'];
$class = (is_array($function) && isset($function[0])) ? $function[0] : false;
$method = (is_array($function) && isset($function[1])) ? $function[1] : false;
$args = $this->getArguments($argsNumber, $args);
if (! ($class && $method) && function_exists($function)) {
return call_user_func_array($function, $args);
} elseif ($obj = call_user_func(array($class, $this->singleton))) {
if ($obj !== false) {
return call_user_func_array(array($obj, $method), $args);
}
} elseif (class_exists($class)) {
$instance = new $class;
return call_user_func_array(array($instance, $method), $args);
}
}
protected function getFilters($tag, $remove)
{
if (isset($this->callback_filters[$tag])) {
$filters = $this->callback_filters[$tag];
if ($remove) {
unset($this->callback_filters[$tag]);
}
}
return (isset($filters)) ? $filters : array();
}
public static function get_properties($type, $is_callback=false){
$that = self::getInstance(self::$id);
if( $type === 'event' ){
return $is_callback ? $that->callbacks : $that->actions;
}
return $is_callback ? $that->callback_filters : $that->filters;
}
public static function addFilter($tag, $func, $priority = 8, $args = 0)
{
$that = self::getInstance(self::$id);
$that->callback_filters[$tag][$priority][] = array(
'function' => $func,
'arguments' => $args,
);
return true;
}
public static function apply_filters($tag, $args = array(), $remove = true)
{
$that = self::getInstance(self::$id);
self::$current_filter = $tag;
$that->filters['count']++;
if (! array_key_exists($tag, $that->filters)) {
$that->filters[$tag] = 0;
}
$that->filters[$tag]++;
$filters = $that->getFilters($tag, $remove);
ksort($filters);
$value = $args[0];
foreach ($filters as $priority) {
foreach ($priority as $filter) {
if( isset($args[0]) ){
$args[0] = $value;
}
$replace = $that->runAction($filter, $args);
if( ! is_null($replace) ) {
$value = $replace;
}
}
}
self::$current_filter = false;
return $value;
}
protected function getArguments($argsNumber, $arguments)
{
if ($argsNumber == 1 && is_string($arguments)) {
return array($arguments);
} elseif ($argsNumber === count($arguments)) {
return $arguments;
}
$args = array();
for ($i = 0; $i < $argsNumber; $i++) {
if (is_array($arguments) && array_key_exists($i, $arguments)) {
$args[] = $arguments[$i];
}
}
return $args;
}
public static function remove_filter($tag, $func, $priority)
{
$that = self::getInstance(self::$id);
$is_remove = false;
if (isset($that->callback_filters[$tag]) && isset($that->callback_filters[$tag][$priority]) ) {
foreach((array) $that->callback_filters[$tag][$priority] as $key=>$value){
if(isset($value['function']) && $value['function'] === $func) {
unset($that->callback_filters[$tag][$priority][$key]);
$is_remove = true;
}
}
}
return $is_remove;
}
public static function remove_action($tag, $func, $priority)
{
$that = self::getInstance(self::$id);
$is_remove = false;
if (isset($that->callbacks[$tag]) && isset($that->callbacks[$tag][$priority]) ) {
foreach((array) $that->callbacks[$tag][$priority] as $key=>$value){
if(isset($value['function']) && $value['function'] === $func) {
unset($that->callbacks[$tag][$priority][$key]);
$is_remove = true;
}
}
}
return $is_remove;
}
}
// end Hook Class;