3 * @file src/Core/Hook.php
5 namespace Friendica\Core;
8 use Friendica\BaseObject;
9 use Friendica\Database\DBA;
11 require_once 'include/dba.php';
14 * Some functions to handle hooks
16 class Hook extends BaseObject
19 * Array of registered hooks
23 * ["<hook name>"] => [
25 * 1 => "<hook function name>"
32 private static $hooks = [];
37 public static function loadHooks()
40 $stmt = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
42 while ($hook = DBA::fetch($stmt)) {
43 if (!array_key_exists($hook['hook'], self::$hooks)) {
44 self::$hooks[$hook['hook']] = [];
46 self::$hooks[$hook['hook']][] = [$hook['file'], $hook['function']];
54 * @param string $hook the name of the hook
55 * @param string $file the name of the file that hooks into
56 * @param string $function the name of the function that the hook will call
57 * @param int $priority A priority (defaults to 0)
60 public static function register($hook, $file, $function, $priority = 0)
62 $file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
64 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
65 if (DBA::exists('hook', $condition)) {
69 $result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
77 * @param string $hook the name of the hook
78 * @param string $file the name of the file that hooks into
79 * @param string $function the name of the function that the hook called
82 public static function unregister($hook, $file, $function)
84 $relative_file = str_replace(self::getApp()->getBasePath() . DIRECTORY_SEPARATOR, '', $file);
86 // This here is only needed for fixing a problem that existed on the develop branch
87 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
88 DBA::delete('hook', $condition);
90 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
91 $result = DBA::delete('hook', $condition);
96 * Returns the list of callbacks for a single hook
101 public static function getByName($name)
105 if (isset(self::$hooks[$name])) {
106 $return = self::$hooks[$name];
113 * @brief Forks a hook.
115 * Use this function when you want to fork a hook via the worker.
117 * @param string $name of the hook to call
118 * @param string|array $data to transmit to the callback handler
120 public static function fork($priority, $name, $data = null)
122 if (array_key_exists($name, self::$hooks)) {
123 foreach (self::$hooks[$name] as $hook) {
124 Worker::add($priority, 'ForkHook', $name, $hook, $data);
130 * @brief Calls a hook.
132 * Use this function when you want to be able to allow a hook to manipulate
135 * @param string $name of the hook to call
136 * @param string|array &$data to transmit to the callback handler
138 public static function callAll($name, &$data = null)
140 if (array_key_exists($name, self::$hooks)) {
141 foreach (self::$hooks[$name] as $hook) {
142 self::callSingle(self::getApp(), $name, $hook, $data);
148 * @brief Calls a single hook.
151 * @param string $name of the hook to call
152 * @param array $hook Hook data
153 * @param string|array &$data to transmit to the callback handler
155 public static function callSingle(App $a, $name, $hook, &$data = null)
157 // Don't run a theme's hook if the user isn't using the theme
158 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
162 @include_once($hook[0]);
163 if (function_exists($hook[1])) {
167 // remove orphan hooks
168 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
169 DBA::delete('hook', $condition, ['cascade' => false]);
174 * check if an app_menu hook exist for addon $name.
175 * Return true if the addon is an app
177 public static function isAddonApp($name)
179 if (array_key_exists('app_menu', self::$hooks)) {
180 foreach (self::$hooks['app_menu'] as $hook) {
181 if ($hook[0] == 'addon/' . $name . '/' . $name . '.php') {