diff --git a/Languages/en_US/Admin.php b/Languages/en_US/Admin.php
index 84416b1565b..33411c13ce1 100644
--- a/Languages/en_US/Admin.php
+++ b/Languages/en_US/Admin.php
@@ -757,8 +757,8 @@
$txt['hooks_field_hook_name'] = 'Hook Name';
$txt['hooks_field_function_name'] = 'Function Name';
$txt['hooks_field_function_method'] = 'Function is a method and its class is instantiated';
-$txt['hooks_field_function'] = 'Function: {real_function}';
-$txt['hooks_field_included_file'] = 'Included file: {included_file}';
+$txt['hooks_field_function'] = 'Function: {function}';
+$txt['hooks_field_included_file'] = 'Included file: {file}';
$txt['hooks_field_file_name'] = 'File Name';
$txt['hooks_field_hook_exists'] = 'Status';
$txt['hooks_active'] = 'Exists';
diff --git a/Sources/Actions/Admin/Maintenance.php b/Sources/Actions/Admin/Maintenance.php
index edc4f224ae6..c83b11f1bbc 100644
--- a/Sources/Actions/Admin/Maintenance.php
+++ b/Sources/Actions/Admin/Maintenance.php
@@ -1816,23 +1816,16 @@ public function hooks(): void
{
$filter_url = '';
$current_filter = '';
- $hooks = $this->getIntegrationHooks();
+ $defined_hooks = array_keys(array_filter(IntegrationHook::get(), fn($val) => \count($val) > 0));
+
$hooks_filters = [];
- if (isset($_GET['filter'], $hooks[$_GET['filter']])) {
+ if (isset($_GET['filter'], $defined_hooks[$_GET['filter']])) {
$filter_url = ';filter=' . $_GET['filter'];
$current_filter = $_GET['filter'];
}
- $filtered_hooks = array_filter(
- $hooks,
- function ($hook) use ($current_filter) {
- return $current_filter == '' || $current_filter == $hook;
- },
- ARRAY_FILTER_USE_KEY,
- );
- ksort($hooks);
- foreach ($hooks as $hook => $functions) {
+ foreach ($defined_hooks as $hook) {
$hooks_filters[] = '';
}
@@ -1863,21 +1856,16 @@ function ($hook) use ($current_filter) {
'base_href' => Config::$scripturl . '?action=admin;area=maintain;sa=hooks' . $filter_url . ';' . Utils::$context['session_var'] . '=' . Utils::$context['session_id'],
'default_sort_col' => 'hook_name',
'get_items' => [
- 'function' => __CLASS__ . '::getIntegrationHooksData',
+ 'function' => __CLASS__ . '::list_getHooks',
'params' => [
- $filtered_hooks,
- strtr(Config::$boarddir, '\\', '/'),
- strtr(Config::$sourcedir, '\\', '/'),
+ $current_filter,
],
],
'get_count' => [
- 'value' => array_reduce(
- $filtered_hooks,
- function ($accumulator, $functions) {
- return $accumulator + \count($functions);
- },
- 0,
- ),
+ 'function' => __CLASS__ . '::list_getNumHooks',
+ 'params' => [
+ $current_filter,
+ ],
],
'no_items_label' => Lang::getTxt('hooks_no_hooks', file: 'Admin'),
'columns' => [
@@ -1893,39 +1881,39 @@ function ($accumulator, $functions) {
'reverse' => 'hook_name DESC',
],
],
- 'function_name' => [
+ 'func' => [
'header' => [
'value' => Lang::getTxt('hooks_field_function_name', file: 'Admin'),
],
'data' => [
'function' => function ($data) {
// Show a nice icon to indicate this is an instance.
- $instance = (!empty($data['instance']) ? ' ' : '');
+ $instance = ($data['is_object'] ? ' ' : '');
- if (!empty($data['included_file']) && !empty($data['real_function'])) {
+ if (!empty($data['file']) && !empty($data['function'])) {
return $instance . Lang::getTxt('hooks_field_function', $data, file: 'Admin') . '
' . Lang::getTxt('hooks_field_included_file', $data, file: 'Admin');
}
- return $instance . $data['real_function'];
+ return $instance . $data['function'];
},
'class' => 'word_break',
],
'sort' => [
- 'default' => 'function_name',
- 'reverse' => 'function_name DESC',
+ 'default' => 'func',
+ 'reverse' => 'func DESC',
],
],
- 'file_name' => [
+ 'file' => [
'header' => [
'value' => Lang::getTxt('hooks_field_file_name', file: 'Admin'),
],
'data' => [
- 'db' => 'file_name',
+ 'db' => 'file',
'class' => 'word_break',
],
'sort' => [
- 'default' => 'file_name',
- 'reverse' => 'file_name DESC',
+ 'default' => 'file',
+ 'reverse' => 'file DESC',
],
],
'status' => [
@@ -1936,15 +1924,15 @@ function ($accumulator, $functions) {
'data' => [
'function' => function ($data) use ($filter_url) {
// Cannot update temp hooks in any way, really. Just show the appropriate icon.
- if ($data['status'] == 'temp') {
- return '';
+ if ($data['is_temp']) {
+ return '';
}
$change_status = ['before' => '', 'after' => ''];
// Can only enable/disable if it exists...
- if ($data['hook_exists']) {
- $change_status['before'] = '';
+ if ($data['exists']) {
+ $change_status['before'] = '';
$change_status['after'] = '';
}
@@ -1953,8 +1941,8 @@ function ($accumulator, $functions) {
'class' => 'centertext',
],
'sort' => [
- 'default' => 'status',
- 'reverse' => 'status DESC',
+ 'default' => 'is_enabled',
+ 'reverse' => 'is_enabled DESC',
],
],
],
@@ -1991,7 +1979,7 @@ function ($accumulator, $functions) {
'data' => [
'function' => function ($data) use ($filter_url) {
// Note: Cannot remove temp hooks via the UI...
- if (!$data['hook_exists'] && $data['status'] != 'temp') {
+ if (!$data['exists'] && $data['status'] != 'temp') {
return '
@@ -2024,64 +2012,109 @@ function ($accumulator, $functions) {
* @param int $start The item to start with (for pagination purposes)
* @param int $per_page How many items to display on each page
* @param string $sort A string indicating how to sort things
- * @param object|array $filtered_hooks
+ * @param string $filter hook name to filter by.
* @param string $normalized_boarddir
* @param string $normalized_sourcedir
* @return array An array of information about the integration hooks
*/
- public static function getIntegrationHooksData($start, $per_page, $sort, $filtered_hooks, $normalized_boarddir, $normalized_sourcedir): array
+ public static function list_getHooks(int $start, int $per_page, string $sort, string $filter = ''): array
{
- $function_list = $sort_array = $temp_data = [];
- $files = self::getFileRecursive($normalized_sourcedir);
-
- foreach ($files as $currentFile => $fileInfo) {
- $function_list += self::getDefinedFunctionsInFile($currentFile);
- }
-
- $sort_types = [
- 'hook_name' => ['hook_name', SORT_ASC],
- 'hook_name DESC' => ['hook_name', SORT_DESC],
- 'function_name' => ['function_name', SORT_ASC],
- 'function_name DESC' => ['function_name', SORT_DESC],
- 'file_name' => ['file_name', SORT_ASC],
- 'file_name DESC' => ['file_name', SORT_DESC],
- 'status' => ['status', SORT_ASC],
- 'status DESC' => ['status', SORT_DESC],
- ];
+ $ret = [];
+
+ $request = Db::$db->query(
+ 'SELECT id_hook, is_enabled, hook_name, func, file, class, is_object, package_id
+ FROM {db_prefix}hooks' . (!empty($filter) ? '
+ WHERE hook_name = {string:filter}' : '') . '
+ ORDER BY {raw:sort}
+ LIMIT {int:start}, {int:per_page}',
+ [
+ 'filter' => $filter,
+ 'sort' => $sort,
+ 'start' => $start,
+ 'per_page' => $per_page,
+ ],
+ );
- foreach ($filtered_hooks as $hook => $functions) {
- foreach ($functions as $rawFunc) {
- $hookParsedData = self::parseIntegrationHook($hook, $rawFunc);
+ foreach (Db::$db->fetch_all($request) as $row) {
+ $hooks[$row['hook_name']] ??= IntegrationHook::get($row['hook_name']);
+ $hook = array_find($hooks[$row['hook_name']], fn($val) => $val['id_hook'] == $row['id_hook']);
+
+ $ret[(int) $row['id_hook']] = [
+ 'id_hook' => (int) $row['id_hook'],
+ 'is_enabled' => $row['is_enabled'] === '1' ? true : false,
+ 'hook_name' => trim($row['hook_name'] ?? ''),
+ 'function' => trim($row['func'] ?? ''),
+ 'file' => trim($row['file'] ?? ''),
+ 'class' => trim($row['class'] ?? ''),
+ 'is_object' => $row['is_object'] == '1' ? true : false,
+ 'is_temp' => $hook !== null && $hook['is_temp'],
+ 'package_id' => $row['package_id'] ?? null,
+ 'exists' => $hook !== null,
+ 'status' => $hook !== null ? ($hook['is_enabled'] ? 'allow' : 'moderate') : 'deny',
+ 'img_text' => Lang::getTxt('hooks_' . ($hook !== null ? ($row['is_enabled'] === '1' ? 'active' : 'disabled') : 'missing'), file: 'Admin'),
+ ];
+ }
+ Db::$db->free_result($request);
- // Handle hooks pointing outside the sources directory.
- $absPath_clean = rtrim($hookParsedData['absPath'], '!');
+ // load up any temp hooks.
+ foreach (IntegrationHook::get() as $name => $hooks) {
+ foreach ($hooks as $id => $row) {
+ if (empty($row['is_temp'])) {
+ continue;
+ }
- if ($absPath_clean != '' && !isset($files[$absPath_clean]) && file_exists($absPath_clean)) {
- $function_list += self::getDefinedFunctionsInFile($absPath_clean);
+ if (!empty($filter) && $row['hook_name'] !== $filter) {
+ continue;
}
- $hook_exists = isset($function_list[$hookParsedData['call']]) || (str_ends_with($hook, '_include') && isset($files[$absPath_clean]));
- $hook_temp = !empty(Utils::$context['integration_hooks_temporary'][$hook][$hookParsedData['rawData']]);
- $temp = [
- 'hook_name' => $hook,
- 'function_name' => $hookParsedData['rawData'],
- 'real_function' => $hookParsedData['call'],
- 'included_file' => $hookParsedData['hookFile'],
- 'file_name' => strtr($hookParsedData['absPath'] ?: ($function_list[$hookParsedData['call']] ?? ''), [$normalized_boarddir => '.']),
- 'instance' => $hookParsedData['object'],
- 'hook_exists' => $hook_exists,
- 'status' => ($hook_temp ? 'temp' : ($hook_exists ? ($hookParsedData['enabled'] ? 'allow' : 'moderate') : 'deny')),
- 'img_text' => Lang::getTxt('hooks_' . ($hook_exists ? ($hook_temp ? 'temp' : ($hookParsedData['enabled'] ? 'active' : 'disabled')) : 'missing'), file: 'Admin'),
- 'enabled' => $hookParsedData['enabled'],
+ $ret[(int) $row['id_hook']] = [
+ 'id_hook' => (int) $row['id_hook'],
+ 'is_enabled' => $row['is_enabled'] === '1' ? true : false,
+ 'hook_name' => trim($row['hook_name'] ?? ''),
+ 'function' => trim($row['function'] ?? ''),
+ 'file' => trim($row['file'] ?? ''),
+ 'class' => trim($row['class'] ?? ''),
+ 'is_object' => $row['is_object'] == '1' ? true : false,
+ 'is_temp' => $row['is_temp'],
+ 'package_id' => $row['package_id'] ?? null,
+ 'exists' => $hook !== null,
+ 'status' => 'temp',
+ 'img_text' => Lang::getTxt('hooks_temp', file: 'Admin'),
];
- $sort_array[] = $temp[$sort_types[$sort][0]];
- $temp_data[] = $temp;
+
}
}
- array_multisort($sort_array, $sort_types[$sort][1], $temp_data);
+ return $ret;
+ }
+
+ /**
+ * Return the number of hooks of the specified type recorded in the database.
+ * (the specified type being attachments or avatars).
+ *
+ * @param string $filter hook name to filter by.
+ * @return int The number of hooks
+ */
+ public static function list_getNumHooks(string $filter = ''): int
+ {
+ $request = Db::$db->query(
+ 'SELECT COUNT(*)
+ FROM {db_prefix}hooks' . (!empty($filter) ? '
+ WHERE hook_name = {string:filter}' : ''),
+ [
+ 'filter' => $filter,
+ ],
+ );
+
+ list($num_hooks) = Db::$db->fetch_row($request);
+ Db::$db->free_result($request);
+ $num_hooks = (int) $num_hooks;
+
+ if (!empty($filter)) {
+ return $num_hooks + \count(IntegrationHook::get($filter));
+ }
- return \array_slice($temp_data, $start, $per_page, true);
+ return (int) $num_hooks + array_sum(array_map('count', IntegrationHook::get()));
}
/**
diff --git a/Sources/Config.php b/Sources/Config.php
index 1bf0d46ea70..1bc05390253 100644
--- a/Sources/Config.php
+++ b/Sources/Config.php
@@ -324,14 +324,6 @@ class Config
*/
public static string $scripturl;
- /**
- * @var \Composer\Autoload\ClassLoader
- *
- * Autoloader instance.
- * This is used to support the integrate_autoload hook.
- */
- public static \Composer\Autoload\ClassLoader $loader;
-
/****************************
* Internal static properties
****************************/
@@ -1151,18 +1143,8 @@ public static function reloadModSettings(): void
die('SMF file version (' . SMF_VERSION . ') does not match SMF database version (' . self::$modSettings['smfVersion'] . ').
Run the SMF upgrader to fix this.
More information.');
}
- // Any autoloader integrations to add?
- if (isset(self::$modSettings['integrate_autoload'])) {
- $class_map = [];
-
- IntegrationHook::call('integrate_autoload', [&$class_map]);
-
- foreach ($class_map as $prefix => $dirname) {
- self::$loader->addPsr4($prefix, self::$sourcedir . '/' . $dirname);
- }
- }
-
- // Ensure the cache_enable setting reflects reality.
+ // Load up any hooks.
+ IntegrationHook::load();
self::$modSettings['cache_enable'] = Cache\CacheApi::$enable;
// Used to force browsers to download fresh CSS and JavaScript when necessary
@@ -1242,22 +1224,24 @@ public static function reloadModSettings(): void
$integration_settings = Utils::jsonDecode(SMF_INTEGRATION_SETTINGS, true);
foreach ($integration_settings as $hook => $function) {
- IntegrationHook::add($hook, $function, false);
+ if (\is_string($function)) {
+ IntegrationHook::add($hook, $function, false);
+ } else {
+ IntegrationHook::register(
+ name: $hook,
+ function: $function['function'],
+ file: $function['file'],
+ class: $function['class'],
+ is_object: !empty($function['is_object']),
+ is_enabled: true,
+ permanent: false,
+ );
+ }
}
}
// Any files to pre include?
- if (!empty(self::$modSettings['integrate_pre_include'])) {
- $pre_includes = explode(',', self::$modSettings['integrate_pre_include']);
-
- foreach ($pre_includes as $include) {
- $include = strtr(trim($include), ['$boarddir' => self::$boarddir, '$sourcedir' => self::$sourcedir]);
-
- if (file_exists($include)) {
- require_once Sapi::canonicalPath($include);
- }
- }
- }
+ IntegrationHook::call('integrate_pre_include');
Utils::load();
@@ -1319,6 +1303,12 @@ public static function updateModSettings(array $change_array, bool $update = fal
// In some cases, this may be better and faster, but for large sets we don't want so many UPDATEs.
if ($update) {
foreach ($change_array as $variable => $value) {
+ // If this is a integration setting, we need to step in.
+ if (Config::$backward_compatibility && str_starts_with($variable, 'integrate_')) {
+ IntegrationHook::processUpdateModSettings($variable, $value);
+ continue;
+ }
+
Db\DatabaseApi::$db->query(
'UPDATE {db_prefix}settings
SET value = {' . (\is_bool($value) ? 'raw' : 'string') . ':value}
diff --git a/Sources/Db/Schema/v3_0/Hooks.php b/Sources/Db/Schema/v3_0/Hooks.php
new file mode 100644
index 00000000000..1a18a9ff115
--- /dev/null
+++ b/Sources/Db/Schema/v3_0/Hooks.php
@@ -0,0 +1,118 @@
+name = 'hooks';
+
+ $this->columns = [
+ 'id_hook' => new Column(
+ name: 'id_hook',
+ type: 'int',
+ unsigned: true,
+ not_null: true,
+ auto: true,
+ ),
+ 'is_enabled' => new Column(
+ name: 'is_enabled',
+ type: 'tinyint',
+ unsigned: true,
+ not_null: true,
+ default: 0,
+ ),
+ 'hook_name' => new Column(
+ name: 'hook_name',
+ type: 'varchar',
+ size: 255,
+ not_null: true,
+ default: '',
+ ),
+ 'func' => new Column(
+ name: 'func',
+ type: 'varchar',
+ size: 255,
+ not_null: true,
+ default: '',
+ ),
+ 'file' => new Column(
+ name: 'file',
+ type: 'varchar',
+ size: 255,
+ not_null: true,
+ default: '',
+ ),
+ 'class' => new Column(
+ name: 'class',
+ type: 'varchar',
+ size: 255,
+ not_null: false,
+ ),
+ 'is_object' => new Column(
+ name: 'is_object',
+ type: 'tinyint',
+ unsigned: true,
+ not_null: true,
+ default: 0,
+ ),
+ 'package_id' => new Column(
+ name: 'package_id',
+ type: 'varchar',
+ size: 255,
+ not_null: true,
+ default: '',
+ ),
+ ];
+
+ $this->indexes = [
+ 'primary' => new DbIndex(
+ type: 'primary',
+ columns: [
+ [
+ 'name' => 'id_hook',
+ ],
+ ],
+ ),
+ 'idx_hook_name' => new DbIndex(
+ name: 'idx_hook_name',
+ columns: [
+ [
+ 'name' => 'hook_name',
+ ],
+ [
+ 'name' => 'is_enabled',
+ ],
+ ],
+ ),
+ ];
+ }
+}
diff --git a/Sources/IntegrationHook.php b/Sources/IntegrationHook.php
index 524ce7d58b6..49519f5a20a 100644
--- a/Sources/IntegrationHook.php
+++ b/Sources/IntegrationHook.php
@@ -15,6 +15,7 @@
namespace SMF;
+use Exception;
use SMF\Db\DatabaseApi as Db;
use SMF\Debug\DebugUtils;
@@ -61,6 +62,14 @@ class IntegrationHook
*/
public static bool $enabled = true;
+ /**
+ * @var \Composer\Autoload\ClassLoader
+ *
+ * Autoloader instance.
+ * This is used to support the integrate_autoload hook.
+ */
+ public static \Composer\Autoload\ClassLoader $loader;
+
/*********************
* Internal properties
*********************/
@@ -72,6 +81,36 @@ class IntegrationHook
*/
private array $callables = [];
+ /****************************
+ * Internal static properties
+ ****************************/
+
+ /**
+ * Big array of hooks.
+ * Key holds the hook name.
+ * Value holds an array of Hook data.
+ *
+ * @var array
+ */
+ private static array $hooks = [];
+
+ /**
+ * These hooks did not use the intergate_ prefix.
+ * @var array
+ */
+ private static array $no_integrate_names = [
+ 'pre_cache_quick_get',
+ 'post_cache_quick_get',
+ 'cache_put_data',
+ 'cache_get_data',
+ 'mention_insert_quote',
+ 'mention_insert_msg',
+ 'before_profile_save_avatar',
+ 'after_profile_save_avatar',
+ 'who_allowed',
+ 'whos_online_after',
+ ];
+
/****************
* Public methods
****************/
@@ -97,24 +136,40 @@ public function __construct(string $name, ?bool $ignore_errors = null)
$this->ignore_errors = $ignore_errors ?? !empty(Utils::$context['ignore_hook_errors']);
+ if (!isset(Db::$db)) {
+ $this->ignore_errors = true;
+ }
+
if (DebugUtils::isDebugEnabled()) {
DebugUtils::addDebugSource('hooks', $this->name);
}
- if (empty(Config::$modSettings[$this->name])) {
+ if (empty(self::$hooks) || empty(self::$hooks[$name])) {
return;
}
- $func_strings = explode(',', Config::$modSettings[$this->name]);
-
// Loop through each one to get the callable for it.
- foreach ($func_strings as $func_string) {
+ foreach (self::$hooks[$name] as $hook) {
// Hook has been marked as disabled. Skip it!
- if (str_contains($func_string, '!')) {
+ if (!$hook['is_enabled']) {
continue;
}
- $this->callables[$func_string] = Utils::getCallable($func_string);
+ // Old "include" hooks would dump the content into the function list.
+ if (str_ends_with($name, '_include') && empty($hook['file']) && !empty($hook['function'])) {
+ $hook['file'] = $hook['function'];
+ $hook['function'] = '';
+ }
+
+ // Attempt to load the file, only if succesful do we attempt to prepare the callable.
+ if (!empty($hook['file']) && !self::loadFile($hook['file'], $name === 'pre_include')) {
+ continue;
+ }
+
+ // Special include hooks don't generate callables.
+ if (!str_ends_with($name, '_include')) {
+ $this->callables[$hook['id_hook']] = self::getCallable($hook['function'], $hook['class'], $hook['is_object'], $this->ignore_errors);
+ }
}
}
@@ -132,10 +187,10 @@ public function execute(array $parameters = []): array
}
// Loop through each callable.
- foreach ($this->callables as $func_string => $callable) {
+ foreach ($this->callables as $id_hook => $callable) {
// Is it valid?
if (\is_callable($callable)) {
- $this->results[$func_string] = \call_user_func_array($callable, $parameters);
+ $this->results[$id_hook] = \call_user_func_array($callable, $parameters);
}
// This failed, but we want to do so silently.
elseif ($this->ignore_errors) {
@@ -144,11 +199,15 @@ public function execute(array $parameters = []): array
}
// Whatever it was supposed to call, it failed :(
else {
- // Get a full path to show on error.
- if (str_contains($func_string, '|')) {
- list($file, $func) = explode('|', $func_string);
+ $hook = self::$hooks[$id_hook];
+ $hook_call = (!empty($hook['is_object']) ? '#' : '') . (!empty($hook['class']) ? $hook['class'] . ':' : '') . $hook['function'];
- $path = strtr($file, [
+ // Assume the file resides on Config::$boarddir somewhere...
+ $file = Config::$boarddir;
+
+ // Get a full path to show on error.
+ if (!empty($hook['file'])) {
+ $file = strtr($hook['file'], [
'$boarddir' => Config::$boarddir,
'$sourcedir' => Config::$sourcedir,
]);
@@ -158,13 +217,9 @@ public function execute(array $parameters = []): array
'$themedir' => Theme::$current->settings['theme_dir'],
]);
}
-
- ErrorHandler::log(Lang::getTxt('hook_fail_call_to', [$func, $path], file: 'Errors'), 'general');
- }
- // Assume the file resides on Config::$boarddir somewhere...
- else {
- ErrorHandler::log(Lang::getTxt('hook_fail_call_to', [$func_string, Config::$boarddir], file: 'Errors'), 'general');
}
+
+ ErrorHandler::log(Lang::getTxt('hook_fail_call_to', [$hook_call, $file], file: 'Errors'), 'general');
}
}
@@ -180,104 +235,356 @@ public function execute(array $parameters = []): array
*
* @param string $name The name of the integration hook.
* @param array $parameters Parameters to pass to the hooked callables.
+ * @param ?bool $silenceErrors If true, will not report any failures, false will report, null uses default.
* @return array The results returned by all the hooked callables.
*/
- public static function call(string $name, array $parameters = []): array
+ public static function call(string $name, array $parameters = [], ?bool $silenceErrors = null): array
{
- $hook = new self($name);
+ $name = self::cleanHookName($name);
+ $hook = new self($name, $silenceErrors);
return $hook->execute($parameters);
}
/**
- * Adds a function or method to an integration hook.
+ * Convenience method to fetch all hooks loaded.
*
- * Does nothing if the function is already added.
- * Cleans up enabled/disabled variants before taking requested action.
+ * @param ?string $name If provided, only returns hooks from a single hook
+ * @return array The results returned.
+ */
+ public static function get(?string $name = null): array
+ {
+ if (!empty($name)) {
+ return self::$hooks[$name];
+ }
+
+ return self::$hooks;
+ }
+
+ /**
+ * Load up all our hook data from the database.
+ * Sends off for the compatiblity layer with SMF 2.1 calls.
*
- * @param string $name The complete hook name.
- * @param string $function The function name. Can be a call to a method via
- * Class::method.
- * @param bool $permanent If true, updates the value in settings table.
- * @param string $file The filename. Must include one of the following
- * wildcards: $boarddir, $sourcedir, $themedir.
- * Example: $sourcedir/Test.php
- * @param bool $object Indicates if your class will be instantiated when its
- * respective hook is called. If true, your function must be a method.
*/
- public static function add(string $name, string $function, bool $permanent = true, string $file = '', bool $object = false): void
+ final public static function load()
{
- // Any objects?
- if ($object) {
- $function = $function . '#';
+ try {
+ $request = Db::$db->query(
+ 'SELECT id_hook, is_enabled, hook_name, func, file, class, is_object, package_id
+ FROM {db_prefix}hooks',
+ [
+ ],
+ );
+
+ foreach (Db::$db->fetch_all($request) as $row) {
+ self::$hooks[$row['hook_name']] ??= [];
+
+ self::$hooks[$row['hook_name']][] = [
+ 'id_hook' => (int) $row['id_hook'],
+ 'is_enabled' => $row['is_enabled'] === '1' ? true : false,
+ 'hook_name' => trim($row['hook_name'] ?? ''),
+ 'function' => trim($row['func'] ?? ''),
+ 'file' => trim($row['file'] ?? ''),
+ 'class' => trim($row['class'] ?? ''),
+ 'is_object' => $row['is_object'] == '1' ? true : false,
+ 'package_id' => $row['package_id'] ?? null,
+ 'is_temp' => false,
+ ];
+
+ if (Config::$backward_compatibility) {
+ self::updateModSettings($row['hook_name'], self::buildBcString($row));
+ }
+ }
+ Db::$db->free_result($request);
+ } catch (Exception $e) {
+ return;
}
- // Any files to load?
- if (!empty($file) && \is_string($file)) {
- $function = $file . (!empty($function) ? '|' . $function : '');
+ // Fire the autoloader.
+ $class_map = [];
+ self::call('autoload', [&$class_map]);
+
+ foreach ($class_map as $prefix => $dirname) {
+ self::$loader->addPsr4($prefix, Config::$sourcedir . '/' . $dirname);
}
+ }
- // Get the correct string.
- $integration_call = $function;
- $enabled_call = rtrim($function, '!');
- $disabled_call = $enabled_call . '!';
+ /**
+ * Parses the given input to determine if is a callable entity or can be
+ * turned into one, and then returns either a callable or false.
+ *
+ * If $input is already a callable entity, it will simply be returned.
+ * Otherwise, this method will attempt to turn it into one.
+ *
+ * @param string|callable $function Function to parse as a callable.
+ * @param string|null $class (Optional) Class we would be loading.
+ * @param ?bool $is_object (Optional) Initialize the object.
+ * @param ?bool $ignore_errors Optional. Whether to suppress errors if the
+ * callable is invalid. If null, falls back to the current value of
+ * Utils::$context['ignore_hook_errors']. Default: null.
+ * @return callable|false Either a valid callable or false on failure.
+ */
+ final public static function getCallable(string|callable $function, ?string $class = null, bool $is_object = false, ?bool $ignore_errors = null): callable|false
+ {
+ if (!\is_string($function)) {
+ return \is_callable($function) ? $function : false;
+ }
+
+ // Abort if file loading fails.
+ if (empty($function)) {
+ return false;
+ }
+
+ $callable = false;
+ $callable_name = (!empty($class) ? $class . '::' : '') . $function;
+
+ // Process the instances.
+ if ($is_object && \is_string($class)) {
+ Utils::$context['instances'] ??= [];
+
+ if (!isset(Utils::$context['instances'][$class]) || !(Utils::$context['instances'][$class] instanceof $class)) {
+ Utils::$context['instances'][$class] = new $class();
+
+ // Optionally track instance creation for debugging.
+ if (!empty(Config::$db_show_debug)) {
+ Utils::$context['debug']['instances'][$class] = $class;
+ }
+ }
+
+ $callable = [Utils::$context['instances'][$class], $function];
+ } elseif (!empty($class)) {
+ // Static method reference.
+ $callable = [$class, $function];
+ } else {
+ // Treat as a plain function.
+ $callable = $function;
+ }
+
+ // Validate the callable.
+ if ($callable !== false && !\is_callable($callable, false, $callable_name)) {
+ $ignore_errors ??= !empty(Utils::$context['ignore_hook_errors']);
+
+ if ($ignore_errors) {
+ return false;
+ }
+
+ // Log error for invalid callables.
+ ErrorHandler::log((string) Lang::getTxt('sub_action_fail', [$callable_name], file: 'Errors'), 'general');
+
+ return false;
+ }
+
+ return $callable;
+ }
+
+ /**
+ * Register a hook.
+ *
+ * @param string $name Calls self::cleanHookName($name) to remote integrate_
+ * @param string $function Name of the function to call
+ * @param string $file (Optional) Load a file prior to calling the function
+ * @param string $class (Optional) Class of the function, supports both static and object
+ * @param bool $is_object (Optional) If class should be intialized as an object
+ * @param bool $is_enabled (Optional) if False, the hook is added, but not called
+ * @param ?string $package_id (Optional) If defined, add the package_id that added this hook
+ * @param bool $permanent (Optional) if true, we will add to the database, otherwise just held in memory.
+ * @return int id of the hook.
+ */
+ public static function register(string $name, string $function, string $file = '', string $class = '', bool $is_object = false, bool $is_enabled = false, ?string $package_id = null, bool $permanent = true): int
+ {
+ $name = self::cleanHookName($name);
- // Is it going to be permanent?
if ($permanent) {
- $request = Db::$db->query(
- 'SELECT value
- FROM {db_prefix}settings
- WHERE variable = {string:variable}',
+ $id_hook = Db::$db->insert(
+ '',
+ '{db_prefix}hooks',
+ [
+ 'is_enabled' => 'int',
+ 'hook_name' => 'string-255',
+ 'func' => 'string-255',
+ 'file' => 'string-255',
+ 'class' => 'string-255',
+ 'is_object' => 'int',
+ 'package_id' => 'string-255',
+ ],
+ [
+ [
+ $is_enabled ? 1 : 0,
+ $name,
+ $function,
+ $file,
+ $class,
+ $is_object ? 1 : 0,
+ $package_id,
+ ],
+ ],
[
- 'variable' => $name,
+ 'id_hook',
],
+ 1,
);
- list($current_functions) = Db::$db->fetch_row($request);
- Db::$db->free_result($request);
+ } else {
+ $id_hook = rand(10000, 90000);
+ }
- if (!empty($current_functions)) {
- $current_functions = explode(',', $current_functions);
+ $hook = [
+ 'id_hook' => $id_hook,
+ 'is_enabled' => $is_enabled,
+ 'hook_name' => $name,
+ 'function' => $function,
+ 'file' => $file,
+ 'class' => $class,
+ 'is_object' => $is_object,
+ 'package_id' => $package_id,
+ 'is_temp' => !$permanent,
+ ];
+
+ self::$hooks[$name] ??= [];
+ self::$hooks[$name][] = $hook;
+
+ if (Config::$backward_compatibility) {
+ self::updateModSettings($name, self::buildBcString($hook));
+ }
- // Cleanup enabled/disabled variants before taking action.
- $current_functions = array_diff($current_functions, [$enabled_call, $disabled_call]);
+ return $id_hook;
+ }
- $permanent_functions = array_unique(array_merge($current_functions, [$integration_call]));
- } else {
- $permanent_functions = [$integration_call];
- }
+ /**
+ * Remove a hook from the system.
+ *
+ * @param string $name Calls self::cleanHookName($name) to remote integrate_
+ * @param int $id_hook Id of hook.
+ * @param string $function (Optional) Name of the function to call
+ * @param string $file (Optional) Load a file prior to calling the function
+ * @param string $class (Optional) Class of the function, supports both static and object
+ * @param bool $is_object (Optional) If class should be intialized as an object
+ * @param bool $is_enabled (Optional) if False, the hook is added, but not called
+ * @param bool $permanent If true, removes from database.
+ */
+ public static function unregister(string $name, int $id_hook, string $function = '', string $file = '', string $class = '', bool $is_object = false, bool $is_enabled = false, bool $permanent = true): void
+ {
+ $name = self::cleanHookName($name);
+ self::$hooks[$name] ??= [];
+
+ if (empty(self::$hooks[$name])) {
+ return;
+ }
- Config::updateModSettings([$name => implode(',', $permanent_functions)]);
+ // If we don't have a hook id, we have to search.
+ $key = null;
+
+ if (empty($id_hook)) {
+ $key = array_find_key(self::$hooks[$name], function ($val) use ($function, $file, $class, $is_object) {
+ return
+ $val['function'] === $function
+ && $val['class'] === $class
+ && $val['file'] === $file
+ && $val['is_object'] === $is_object;
+ });
+ } else {
+ $key = array_find_key(self::$hooks[$name], fn($val) => $val['id_hook'] === $id_hook);
}
- // Make current function list usable.
- $functions = empty(Config::$modSettings[$name]) ? [] : explode(',', Config::$modSettings[$name]);
+ if ($key !== null) {
+ if (Config::$backward_compatibility) {
+ self::updateModSettings($name, self::buildBcString(self::$hooks[$name][$key]));
+ }
- // Cleanup enabled/disabled variants before taking action.
- $functions = array_diff($functions, [$enabled_call, $disabled_call]);
- $functions = array_unique(array_merge($functions, [$integration_call]));
+ unset(self::$hooks[$name][$key]);
+ }
- Config::$modSettings[$name] = implode(',', $functions);
+ if ($permanent) {
+ Db::$db->query(
+ 'DELETE FROM {db_prefix}hooks
+ WHERE id_hook = {int:hook}',
+ [
+ 'hook' => $id_hook,
+ ],
+ );
+ }
+ }
- // It is handy to be able to know which hooks are temporary...
- if ($permanent !== true) {
- if (!isset(Utils::$context['integration_hooks_temporary'])) {
- Utils::$context['integration_hooks_temporary'] = [];
+ /**
+ * Find all instances of hooks related to the package id and remove them.
+ *
+ * @param string $package_id
+ */
+ public static function uninstallPackage(string $package_id): void
+ {
+ foreach (self::$hooks as $name => &$hooks) {
+ foreach ($hooks as $key => $calls) {
+ if ($calls['package_id'] !== $package_id) {
+ continue;
+ }
+
+ unset($hooks[$key]);
}
+ }
+ Db::$db->query(
+ 'DELETE FROM {db_prefix}hooks
+ WHERE package_id = {string:package_id}',
+ [
+ 'package_id' => $package_id,
+ ],
+ );
+ }
+
+ /**
+ * Adds a function or method to an integration hook.
+ *
+ * For use with SMF 2.1 compatbility layer.
+ *
+ * Does nothing if the function is already added.
+ * Cleans up enabled/disabled variants before taking requested action.
+ *
+ * @param string $name The complete hook name. Calls self::cleanHookName($name) to remote integrate_
+ * @param string $function The function name. Can be a call to a method via
+ * Class::method.
+ * @param bool $permanent If true, updates the value in settings table.
+ * @param string $file The filename. Must include one of the following
+ * wildcards: $boarddir, $sourcedir, $themedir.
+ * Example: $sourcedir/Test.php
+ * @param bool $object Indicates if your class will be instantiated when its
+ * respective hook is called. If true, your function must be a method.
+ */
+ public static function add(string $name, string $function, bool $permanent = true, string $file = '', bool $object = false): void
+ {
+ $name = self::cleanHookName($name);
+ $hook = self::parseBcString($name, $function, $permanent);
+
+ if ($permanent) {
+ self::register($name, $hook['function'], $hook['file'], $hook['class'], $hook['is_object'], $hook['is_enabled']);
+ } else {
+ self::$hooks[$hook['hook_name']] ??= [];
+ self::$hooks[$hook['hook_name']][] = $hook;
+
+ // It is handy to be able to know which hooks are temporary...
+ Utils::$context['integration_hooks_temporary'] ??= [];
Utils::$context['integration_hooks_temporary'][$name][$function] = true;
+
+ if (!empty($file)) {
+ self::loadFile($file);
+ }
+
+ if (!empty(Config::$backward_compatibility)) {
+ self::updateModSettings($name, $function);
+ }
}
}
/**
* Removes an integration hook function.
*
+ * For use with SMF 2.1 compatbility layer.
+ *
* Removes the given function from the given hook.
* Does nothing if the function is not available.
* Cleans up enabled/disabled variants before taking requested action.
*
* @see IntegrationHook::add
*
- * @param string $name The complete hook name.
+ * @param string $name The complete hook name. Calls self::cleanHookName($name) to remote integrate_
* @param string $function The function name. Can be a call to a method via
* Class::method.
* @param bool $permanent Irrelevant for the function itself but need to
@@ -290,50 +597,270 @@ public static function add(string $name, string $function, bool $permanent = tru
*/
public static function remove(string $name, string $function, bool $permanent = true, string $file = '', bool $object = false): void
{
- // Any objects?
- if ($object) {
- $function = $function . '#';
+ $name = self::cleanHookName($name);
+ $tmpHook = self::parseBcString($name, $function, $permanent);
+
+ $key = array_find(self::$hooks[$name], function ($val) use ($tmpHook) {
+ return
+ $val['function'] === $tmpHook['function']
+ && $val['class'] === $tmpHook['class']
+ && $val['file'] === $tmpHook['file']
+ && $val['is_object'] === $val['is_object'];
+ });
+
+ if ($permanent) {
+ self::unregister($name, $key['id_hook']);
+ } else {
+ self::$hooks[$name] ??= [];
+ unset(self::$hooks[$name][$key['id_hook']]);
+
+ if (!empty(Config::$backward_compatibility)) {
+ self::updateModSettings($name, $function, true);
+ }
}
+ }
- // Any files to load?
- if (!empty($file) && \is_string($file)) {
- $function = $file . '|' . $function;
+ /**
+ * Calls to Config::updateModSettings get interupted and redirected here.
+ * Process the incoming string of data, break out existing matching hooks
+ * and then determine if we added or removed a hook.
+ *
+ * For use with SMF 2.1 compatbility layer.
+ *
+ * Note, A danger exists (since hooks where added in 2.x), that we assume
+ * modSettings has the hook data we want, but we also added temporary hooks
+ * and if a call to a permanent hook is made after, it could result in the
+ * being saved to the database.
+ *
+ * @param mixed $name Calls self::cleanHookName($name) to remote integrate_
+ * @param mixed $hooks
+ */
+ final public static function processUpdateModSettings($name, $hooks): void
+ {
+ $name = self::cleanHookName($name);
+
+ // This is easy.
+ if (empty($hooks)) {
+ Config::$modSettings[$name] = '';
+
+ return;
}
- // Get the correct string.
- $integration_call = $function;
- $enabled_call = rtrim($function, '!');
- $disabled_call = $enabled_call . '!';
+ $tmp_data = self::$hooks[$name];
+ $tmp_hooks = explode(',', $hooks);
- if ($permanent) {
- // Get the permanent functions.
- $request = Db::$db->query(
- 'SELECT value
- FROM {db_prefix}settings
- WHERE variable = {string:variable}',
- [
- 'variable' => $name,
- ],
- );
- list($current_functions) = Db::$db->fetch_row($request);
- Db::$db->free_result($request);
+ foreach ($tmp_data as $key1 => $tmp) {
+ $hook_string = self::buildBcString($tmp);
- if (!empty($current_functions)) {
- $current_functions = explode(',', $current_functions);
+ $key2 = array_search($hook_string, $tmp_hooks);
- // Cleanup enabled and disabled variants.
- $current_functions = array_unique(array_diff($current_functions, [$enabled_call, $disabled_call]));
+ // We found the key, nothing to do.
+ if ($key2 !== false) {
+ unset($tmp_hooks[$key2], $tmp_data[$key1]);
- Config::updateModSettings([$name => implode(',', $current_functions)]);
}
+ // Hook needs deleted.
+ else {
+ self::remove($name, $hook_string);
+ }
+ }
+
+ // If the hook still exists, it needs added.
+ foreach ($tmp_hooks as $tmp) {
+ self::add($name, $tmp);
}
+ }
- // Turn the function list into something usable.
- $functions = empty(Config::$modSettings[$name]) ? [] : explode(',', Config::$modSettings[$name]);
+ /*************************
+ * Internal static methods
+ *************************/
+
+ /**
+ * Receives a filename and tries to loads the file.
+ *
+ * You can use the following wildcards in the path:
+ * - $boarddir
+ * - $sourcedir
+ * - $themedir (only works if SMF\Theme has already been initialized)
+ *
+ * @param string $file The string containing a valid format.
+ * @param bool $silent Should we be silent about the failure?
+ * @return bool False if we failed, true if we loaded.
+ */
+ final protected static function loadFile(string $file, bool $silent = false): bool
+ {
+ if (empty($file)) {
+ return false;
+ }
- // Cleanup enabled and disabled variants.
- $functions = array_unique(array_diff($functions, [$enabled_call, $disabled_call]));
+ $path = strtr($file, [
+ '$boarddir' => Config::$boarddir,
+ '$sourcedir' => Config::$sourcedir,
+ ]);
- Config::$modSettings[$name] = implode(',', $functions);
+ if (str_contains($path, '$themedir') && class_exists(Theme::class, false) && !empty(Theme::$current->settings['theme_dir'])) {
+ $path = strtr($path, [
+ '$themedir' => Theme::$current->settings['theme_dir'],
+ ]);
+ }
+
+ $path = Sapi::canonicalPath($path);
+
+ // Load the file if it can be loaded.
+ if (is_file($path)) {
+ require_once $path;
+ }
+ // No? Try a fallback to Config::$sourcedir.
+ else {
+ $path = Sapi::canonicalPath(Config::$sourcedir . '/' . $file);
+
+ if (is_file($path)) {
+ require_once $path;
+ }
+ // Sorry, can't do much for you at this point.
+ elseif (empty(Utils::$context['uninstalling'])) {
+ if (!$silent) {
+ ErrorHandler::log(Lang::getTxt('hook_fail_loading_file', [$path], file: 'Errors'), 'general');
+ }
+
+ // File couldn't be loaded.
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Parses the given input to determine and returns a compatible hook array.
+ *
+ * For use with SMF 2.1 compatbility layer.
+ *
+ * Two special syntaxes can be used with string input, as follows:
+ *
+ * - Instructions to load a specific file can be given by prepending a file
+ * path followed by a `|` character to the $input string. This amounts to
+ * a form of autoloading for callables that are not class-based. These
+ * file paths support the wildcards $boarddir, $sourcedir, and $themedir.
+ *
+ * Example: '$sourcedir/foo.php|func_name' will load ./Sources/foo.php
+ * and then return 'func_name'.
+ *
+ * - If a class method is specified with a "#" character appended to it, an
+ * instance of that class will be automatically created and added to
+ * Utils::$context['instances'], and the returned value from this method
+ * will be a callable array that will call the specified method on that
+ * instance. Note, however, that there is no way to pass arguments to the
+ * class's constructor when using this syntax. For that reason, it is
+ * usually better to construct the object directly rather than using this
+ * syntax to do the job for you.
+ *
+ * Example: 'SMF\Foo::methodName#' will create an instance of SMF\Foo and
+ * then return an array containing the instantiated object and the string
+ * 'methodName'.
+ *
+ * @param string $hook_name The name of the hook
+ * @param string|callable $function Function to parse as a callable.
+ * @param bool $permanent If true, updates the value in settings table.
+ * @return array Either a valid callable or false on failure.
+ */
+ private static function parseBcString(string $hook_name, string $function, bool $permanent = true): array
+ {
+ $file = '';
+ $class = '';
+
+ if (str_contains($function, '|')) {
+ [$file, $function] = explode('|', $function);
+ }
+
+ if (str_contains($function, '::')) {
+ [$class, $function] = explode('::', $function);
+ }
+
+ $hook = [
+ 'id_hook' => rand(100000, 900000),
+ 'is_enabled' => !str_starts_with('!', $function) ? true : false,
+ 'hook_name' => self::cleanHookName($hook_name),
+ 'function' => $function,
+ 'file' => $file,
+ 'class' => $class,
+ 'is_object' => false,
+ 'package_id' => null,
+ 'is_temp' => !$permanent,
+ ];
+
+ return $hook;
+ }
+
+ /**
+ * Builds a string compatible with SMF 2.1 hook system.
+ *
+ * @param array $hook Data from our hook system.
+ * @return string
+ */
+ private static function buildBcString(array $hook): string
+ {
+ return
+ (!empty($hook['is_enabled']) ? '!' : '')
+ . (!empty($hook['file']) ? $hook['file'] . '|' : '')
+ . (!empty($hook['class']) ? $hook['class'] . ':' : '')
+ . ($hook['function'] ?? '')
+ . (!empty($hook['is_object']) ? '#' : '');
+ }
+
+ /**
+ * Registers with modSettings our hook data.
+ *
+ * For use with SMF 2.1 compatbility layer.
+ *
+ * Note, A danger exists (since hooks where added in 2.x), that we assume
+ * modSettings has the hook data we want, but we also added temporary hooks
+ * and if a call to a permanent hook is made after, it could result in the
+ * being saved to the database.
+ *
+ * @param string $name Name of hook
+ * @param string $function Function we will call
+ * @param bool $remove True if we should remove, false to add
+ */
+ private static function updateModSettings(string $name, string $function, bool $remove = false)
+ {
+ $name = self::prepareLegacyName($name);
+ Config::$modSettings[$name] ??= '';
+
+ if ($remove) {
+ $tmps = explode(',', Config::$modSettings[$name]);
+
+ $key = array_search($function, $tmps);
+ unset($tmps[$key]);
+ Config::$modSettings[$name] = implode(',', $tmps);
+ } else {
+ Config::$modSettings[$name] .= (!empty(Config::$modSettings[$name]) ? ',' : '') . $function;
+ }
+ }
+
+ /**
+ * Wrapper to remove integrate_ from the hook name.
+ *
+ * For use with SMF 2.1 compatbility layer.
+ *
+ * @param string $name
+ * @return string Name cleansed of integrate_
+ */
+ private static function cleanHookName(string $name): string
+ {
+ return str_starts_with($name, 'integrate_') ? substr($name, 10) : $name;
+ }
+
+ /**
+ * Wrapper to prepare the right prefix of the legacy hook name.
+ *
+ * For use with SMF 2.1 compatbility layer.
+ *
+ * @param string $name
+ * @return string
+ */
+ private static function prepareLegacyName(string $name): string
+ {
+ return \in_array($name, self::$no_integrate_names) ? $name : 'integrate_' . $name;
}
}
diff --git a/Sources/Maintenance/Migration/v3_0/CleanHooks.php b/Sources/Maintenance/Migration/v3_0/CleanHooks.php
new file mode 100644
index 00000000000..eea80538926
--- /dev/null
+++ b/Sources/Maintenance/Migration/v3_0/CleanHooks.php
@@ -0,0 +1,63 @@
+query(
+ 'DELETE FROM {db_prefix}settings
+ WHERE variable LIKE {string:integration}',
+ [
+ 'integration' => 'integration_%',
+ ],
+ );
+
+ $tables = Db::$db->list_tables();
+
+ if (\in_array(Config::$db_prefix . 'hooks', $tables)) {
+ $this->query(
+ 'TRUNCATE TABLE {db_prefix}hooks',
+ );
+ }
+
+ return true;
+ }
+}
diff --git a/Sources/Maintenance/Tools/Upgrade.php b/Sources/Maintenance/Tools/Upgrade.php
index ac485c18ac8..b20416e88a8 100644
--- a/Sources/Maintenance/Tools/Upgrade.php
+++ b/Sources/Maintenance/Tools/Upgrade.php
@@ -190,6 +190,7 @@ class Upgrade extends ToolsBase implements ToolsInterface
Migration\v3_0\BoardPostsCount::class,
Migration\v3_0\ValidationCodeLength::class,
Migration\v3_0\ReportCommentLength::class,
+ Migration\v3_0\CleanHooks::class,
],
];
diff --git a/Sources/PackageManager/PackageManager.php b/Sources/PackageManager/PackageManager.php
index 747a004a9a4..fc8e95b5ee2 100644
--- a/Sources/PackageManager/PackageManager.php
+++ b/Sources/PackageManager/PackageManager.php
@@ -1146,15 +1146,22 @@ public function install(): void
'title' => $action['title'],
];
} elseif ($action['type'] == 'hook' && isset($action['hook'], $action['function'])) {
+ $action['is_enabled'] ??= str_starts_with($action['function'], '!');
+ $action['class'] ??= '';
+
+ if (empty($action['class']) && str_contains($action['function'], '::')) {
+ [$action['class'], $action['function']] = explode('::', $action['function']);
+ }
+
// Set the system to ignore hooks, but only if it wasn't changed before.
if (!isset(Utils::$context['ignore_hook_errors'])) {
Utils::$context['ignore_hook_errors'] = true;
}
if ($action['reverse']) {
- IntegrationHook::remove($action['hook'], $action['function'], true, $action['include_file'], $action['object']);
+ IntegrationHook::unregister($action['hook'], 0, $action['function'], true, $action['include_file'], $action['object']);
} else {
- IntegrationHook::add($action['hook'], $action['function'], true, $action['include_file'], $action['object']);
+ IntegrationHook::register($action['hook'], $action['function'], $action['include_file'], $action['class'], $action['object'], $action['is_enabled'], $packageInfo['id']);
}
}
// Only do the database changes on uninstall if requested.
@@ -1372,6 +1379,9 @@ public function install(): void
],
['id_install'],
);
+ } else {
+ // When uninstalling, ensure that we removed all hooks related to this.
+ IntegrationHook::uninstallPackage($packageInfo['id']);
}
Db::$db->free_result($request);
diff --git a/Sources/Services/ErrorHandlerService.php b/Sources/Services/ErrorHandlerService.php
index 1b73df7a744..04d68296265 100644
--- a/Sources/Services/ErrorHandlerService.php
+++ b/Sources/Services/ErrorHandlerService.php
@@ -142,7 +142,7 @@ public function handleError(int $error_level, string $error_string, string $file
$message = $this->log($error_level . ': ' . $error_string, $error_type, $file, $line);
// Let's give integrations a chance to output a bit differently
- IntegrationHook::call('integrate_output_error', [$message, $error_type, $error_level, $file, $line]);
+ IntegrationHook::call('integrate_output_error', [$message, $error_type, $error_level, $file, $line], true);
// Dying on these errors only causes MORE problems (blank pages!)
if ($file == 'Unknown') {
@@ -284,7 +284,7 @@ public function log(string $error_message, string|bool $error_type = 'general',
$tried_hook = true;
// Allow the hook to change the error_type and know about the error.
- IntegrationHook::call('integrate_error_types', [&$other_error_types, &$error_type, $error_message, $file, $line]);
+ IntegrationHook::call('integrate_error_types', [&$other_error_types, &$error_type, $error_message, $file, $line], true);
$this->known_error_types = array_merge($this->known_error_types, $other_error_types);
}
diff --git a/index.php b/index.php
index 319c3b5d9bf..c467b411327 100644
--- a/index.php
+++ b/index.php
@@ -127,10 +127,10 @@
SMF\Config::set(get_defined_vars());
// Start up the autoloader.
- SMF\Config::$loader = require SMF\Config::$vendordir . '/autoload.php';
+ SMF\IntegrationHook::$loader = require SMF\Config::$vendordir . '/autoload.php';
// Ensure the SMF namespace points to $sourcedir.
- SMF\Config::$loader->setPsr4('SMF\\', $sourcedir);
+ SMF\IntegrationHook::$loader->setPsr4('SMF\\', $sourcedir);
// Ensure $db_last_error is set, too.
SMF\Config::getDbLastError();
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index c24ec5f6030..f5b8e72fd9e 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -59,10 +59,10 @@
define('SMF_SETTINGS_FILE', TESTS_BOARDDIR . '/Settings.php');
define('SMF_SETTINGS_BACKUP_FILE', TESTS_BOARDDIR . '/Settings_bak.php');
-SMF\Config::$loader = require TESTS_BOARDDIR . '/vendor/autoload.php';
-SMF\Config::$loader->setPsr4('SMF\\', TESTS_BOARDDIR . '/Sources');
-SMF\Config::$loader->setPsr4('SMF\\Themes\\', TESTS_BOARDDIR . '/Themes');
-SMF\Config::$loader->setPsr4('SMF\\Tests\\', TESTS_BOARDDIR . '/tests');
+SMF\IntegrationHook::$loader = require TESTS_BOARDDIR . '/vendor/autoload.php';
+SMF\IntegrationHook::$loader->setPsr4('SMF\\', TESTS_BOARDDIR . '/Sources');
+SMF\IntegrationHook::$loader->setPsr4('SMF\\Themes\\', TESTS_BOARDDIR . '/Themes');
+SMF\IntegrationHook::$loader->setPsr4('SMF\\Tests\\', TESTS_BOARDDIR . '/tests');
/*
* Paths and the default language, which the Unicode and entity helpers need in