diff options
author | ArthurHoaro <arthur@hoa.ro> | 2021-01-20 15:59:00 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2021-02-04 11:02:50 +0100 |
commit | bcba6bd353161fab456b423e93571ab027d5423c (patch) | |
tree | 97a618b77d327a5f963c91522988e24db5a9e158 /application/plugin/PluginManager.php | |
parent | 8997ae6c8e24286f7d47981eaf905e80d2481c10 (diff) | |
download | Shaarli-bcba6bd353161fab456b423e93571ab027d5423c.tar.gz Shaarli-bcba6bd353161fab456b423e93571ab027d5423c.tar.zst Shaarli-bcba6bd353161fab456b423e93571ab027d5423c.zip |
New plugin hook: ability to add custom filters to Shaarli search engine
A new plugin hook has been added: hook_test_filter_search_entry
This hook allows to filter out bookmark with custom plugin code when a search is performed.
Related to #143
Diffstat (limited to 'application/plugin/PluginManager.php')
-rw-r--r-- | application/plugin/PluginManager.php | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/application/plugin/PluginManager.php b/application/plugin/PluginManager.php index 7fc0cb04..939db1ea 100644 --- a/application/plugin/PluginManager.php +++ b/application/plugin/PluginManager.php | |||
@@ -2,6 +2,7 @@ | |||
2 | 2 | ||
3 | namespace Shaarli\Plugin; | 3 | namespace Shaarli\Plugin; |
4 | 4 | ||
5 | use Shaarli\Bookmark\Bookmark; | ||
5 | use Shaarli\Config\ConfigManager; | 6 | use Shaarli\Config\ConfigManager; |
6 | use Shaarli\Plugin\Exception\PluginFileNotFoundException; | 7 | use Shaarli\Plugin\Exception\PluginFileNotFoundException; |
7 | use Shaarli\Plugin\Exception\PluginInvalidRouteException; | 8 | use Shaarli\Plugin\Exception\PluginInvalidRouteException; |
@@ -45,6 +46,9 @@ class PluginManager | |||
45 | */ | 46 | */ |
46 | protected $errors; | 47 | protected $errors; |
47 | 48 | ||
49 | /** @var callable[]|null Preloaded list of hook function for filterSearchEntry() */ | ||
50 | protected $filterSearchEntryHooks = null; | ||
51 | |||
48 | /** | 52 | /** |
49 | * Plugins subdirectory. | 53 | * Plugins subdirectory. |
50 | * | 54 | * |
@@ -274,6 +278,14 @@ class PluginManager | |||
274 | } | 278 | } |
275 | 279 | ||
276 | /** | 280 | /** |
281 | * @return array List of registered filter_search_entry hooks | ||
282 | */ | ||
283 | public function getFilterSearchEntryHooks(): ?array | ||
284 | { | ||
285 | return $this->filterSearchEntryHooks; | ||
286 | } | ||
287 | |||
288 | /** | ||
277 | * Return the list of encountered errors. | 289 | * Return the list of encountered errors. |
278 | * | 290 | * |
279 | * @return array List of errors (empty array if none exists). | 291 | * @return array List of errors (empty array if none exists). |
@@ -284,6 +296,50 @@ class PluginManager | |||
284 | } | 296 | } |
285 | 297 | ||
286 | /** | 298 | /** |
299 | * Apply additional filter on every search result of BookmarkFilter calling plugins hooks. | ||
300 | * | ||
301 | * @param Bookmark $bookmark To check. | ||
302 | * @param array $context Additional info about search context, depends on the search source. | ||
303 | * | ||
304 | * @return bool True if the result must be kept in search results, false otherwise. | ||
305 | */ | ||
306 | public function filterSearchEntry(Bookmark $bookmark, array $context): bool | ||
307 | { | ||
308 | if ($this->filterSearchEntryHooks === null) { | ||
309 | $this->loadFilterSearchEntryHooks(); | ||
310 | } | ||
311 | |||
312 | if ($this->filterSearchEntryHooks === []) { | ||
313 | return true; | ||
314 | } | ||
315 | |||
316 | foreach ($this->filterSearchEntryHooks as $filterSearchEntryHook) { | ||
317 | if ($filterSearchEntryHook($bookmark, $context) === false) { | ||
318 | return false; | ||
319 | } | ||
320 | } | ||
321 | |||
322 | return true; | ||
323 | } | ||
324 | |||
325 | /** | ||
326 | * filterSearchEntry() method will be called for every search result, | ||
327 | * so for performances we preload existing functions to invoke them directly. | ||
328 | */ | ||
329 | protected function loadFilterSearchEntryHooks(): void | ||
330 | { | ||
331 | $this->filterSearchEntryHooks = []; | ||
332 | |||
333 | foreach ($this->loadedPlugins as $plugin) { | ||
334 | $hookFunction = $this->buildHookName('filter_search_entry', $plugin); | ||
335 | |||
336 | if (function_exists($hookFunction)) { | ||
337 | $this->filterSearchEntryHooks[] = $hookFunction; | ||
338 | } | ||
339 | } | ||
340 | } | ||
341 | |||
342 | /** | ||
287 | * Checks whether provided input is valid to register a new route. | 343 | * Checks whether provided input is valid to register a new route. |
288 | * It must contain keys `method`, `route`, `callable` (all strings). | 344 | * It must contain keys `method`, `route`, `callable` (all strings). |
289 | * | 345 | * |