2024-09-19 20:46:45 +09:00
|
|
|
<?php
|
|
|
|
|
if (!defined('_GNUBOARD_'))
|
|
|
|
|
exit;
|
|
|
|
|
|
|
|
|
|
class GML_Hook extends Hook
|
|
|
|
|
{
|
2024-09-29 09:17:16 +09:00
|
|
|
protected $filters = ['count' => 0];
|
|
|
|
|
protected $callback_filters = [];
|
2024-09-19 20:46:45 +09:00
|
|
|
protected static $current_filter = false;
|
|
|
|
|
|
|
|
|
|
protected function runAction($action, $args)
|
|
|
|
|
{
|
|
|
|
|
$function = $action['function'];
|
|
|
|
|
$argsNumber = $action['arguments'];
|
|
|
|
|
|
|
|
|
|
$args = $this->getArguments($argsNumber, $args);
|
|
|
|
|
|
2024-09-29 09:17:16 +09:00
|
|
|
if (is_callable($function)) {
|
2024-09-19 20:46:45 +09:00
|
|
|
return call_user_func_array($function, $args);
|
2024-09-29 09:17:16 +09:00
|
|
|
} elseif (is_array($function) && isset($function[0], $function[1])) {
|
|
|
|
|
$class = $function[0];
|
|
|
|
|
$method = $function[1];
|
|
|
|
|
|
|
|
|
|
if ($obj = call_user_func([$class, $this->singleton])) {
|
|
|
|
|
return $obj !== false ? call_user_func_array([$obj, $method], $args) : false;
|
|
|
|
|
} elseif (class_exists($class)) {
|
|
|
|
|
$instance = new $class();
|
|
|
|
|
return call_user_func_array([$instance, $method], $args);
|
2024-09-19 20:46:45 +09:00
|
|
|
}
|
|
|
|
|
}
|
2024-09-29 09:17:16 +09:00
|
|
|
|
|
|
|
|
return false;
|
2024-09-19 20:46:45 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getFilters($tag, $remove)
|
|
|
|
|
{
|
2024-09-29 09:17:16 +09:00
|
|
|
$filters = isset($this->callback_filters[$tag]) ? $this->callback_filters[$tag] : [];
|
|
|
|
|
if ($remove) {
|
|
|
|
|
unset($this->callback_filters[$tag]);
|
2024-09-19 20:46:45 +09:00
|
|
|
}
|
2024-09-29 09:17:16 +09:00
|
|
|
return $filters;
|
2024-09-19 20:46:45 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2024-09-29 09:17:16 +09:00
|
|
|
$that->callback_filters[$tag][$priority][] = [
|
2024-09-19 20:46:45 +09:00
|
|
|
'function' => $func,
|
|
|
|
|
'arguments' => $args,
|
2024-09-29 09:17:16 +09:00
|
|
|
];
|
2024-09-19 20:46:45 +09:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 09:17:16 +09:00
|
|
|
public static function apply_filters($tag, $args = [], $remove = true)
|
2024-09-19 20:46:45 +09:00
|
|
|
{
|
|
|
|
|
$that = self::getInstance(self::$id);
|
|
|
|
|
self::$current_filter = $tag;
|
|
|
|
|
$that->filters['count']++;
|
|
|
|
|
|
2024-09-29 09:17:16 +09:00
|
|
|
if (!isset($that->filters[$tag])) {
|
2024-09-19 20:46:45 +09:00
|
|
|
$that->filters[$tag] = 0;
|
|
|
|
|
}
|
|
|
|
|
$that->filters[$tag]++;
|
2024-09-29 09:17:16 +09:00
|
|
|
|
2024-09-19 20:46:45 +09:00
|
|
|
$filters = $that->getFilters($tag, $remove);
|
|
|
|
|
ksort($filters);
|
|
|
|
|
|
2024-09-29 09:17:16 +09:00
|
|
|
$value = isset($args[0]) ? $args[0] : null;
|
2024-09-19 20:46:45 +09:00
|
|
|
|
|
|
|
|
foreach ($filters as $priority) {
|
|
|
|
|
foreach ($priority as $filter) {
|
|
|
|
|
if (isset($args[0])) {
|
|
|
|
|
$args[0] = $value;
|
|
|
|
|
}
|
|
|
|
|
$replace = $that->runAction($filter, $args);
|
2024-09-29 09:17:16 +09:00
|
|
|
if ($replace !== false) {
|
2024-09-19 20:46:45 +09:00
|
|
|
$value = $replace;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::$current_filter = false;
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getArguments($argsNumber, $arguments)
|
|
|
|
|
{
|
2024-09-29 09:17:16 +09:00
|
|
|
if ($argsNumber === 1 && is_string($arguments)) {
|
|
|
|
|
return [$arguments];
|
2024-09-19 20:46:45 +09:00
|
|
|
}
|
|
|
|
|
|
2024-09-29 09:17:16 +09:00
|
|
|
if (is_array($arguments) && $argsNumber === count($arguments)) {
|
|
|
|
|
return $arguments;
|
2024-09-19 20:46:45 +09:00
|
|
|
}
|
|
|
|
|
|
2024-09-29 09:17:16 +09:00
|
|
|
return array_slice((array) $arguments, 0, $argsNumber);
|
2024-09-19 20:46:45 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function remove_filter($tag, $func, $priority)
|
|
|
|
|
{
|
|
|
|
|
$that = self::getInstance(self::$id);
|
2024-09-29 09:17:16 +09:00
|
|
|
return self::remove_callback($that->callback_filters, $tag, $func, $priority);
|
2024-09-19 20:46:45 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function remove_action($tag, $func, $priority)
|
|
|
|
|
{
|
|
|
|
|
$that = self::getInstance(self::$id);
|
2024-09-29 09:17:16 +09:00
|
|
|
return self::remove_callback($that->callbacks, $tag, $func, $priority);
|
|
|
|
|
}
|
2024-09-19 20:46:45 +09:00
|
|
|
|
2024-09-29 09:17:16 +09:00
|
|
|
private static function remove_callback(&$array, $tag, $func, $priority)
|
|
|
|
|
{
|
|
|
|
|
if (!isset($array[$tag][$priority])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-09-19 20:46:45 +09:00
|
|
|
|
2024-09-29 09:22:07 +09:00
|
|
|
$is_remove = false;
|
|
|
|
|
|
2024-09-29 09:17:16 +09:00
|
|
|
foreach ($array[$tag][$priority] as $key => $value) {
|
|
|
|
|
if (isset($value['function']) && $value['function'] === $func) {
|
|
|
|
|
unset($array[$tag][$priority][$key]);
|
2024-09-29 09:22:07 +09:00
|
|
|
$is_remove = true;
|
2024-09-19 20:46:45 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 09:22:07 +09:00
|
|
|
return $is_remove;
|
2024-09-19 20:46:45 +09:00
|
|
|
}
|
|
|
|
|
}
|