diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index 83a075c7d5..e287cdd767 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -63,6 +63,8 @@ final class HookEventBridge ArrayFilterEvent::RENDER_LOCATION => 'render_location', ArrayFilterEvent::ITEM_PHOTO_MENU => 'item_photo_menu', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'contact_photo_menu', + ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'profile_sidebar_enter', + ArrayFilterEvent::PROFILE_SIDEBAR => 'profile_sidebar', ArrayFilterEvent::OEMBED_FETCH_END => 'oembed_fetch_url', ArrayFilterEvent::PAGE_INFO => 'page_info_data', ArrayFilterEvent::SMILEY_LIST => 'smilie', @@ -129,6 +131,8 @@ final class HookEventBridge ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', + ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', @@ -193,6 +197,20 @@ final class HookEventBridge $event->setArray($data); } + /** + * Map the PROFILE_SIDEBAR_ENTRY event to `profile_sidebar_enter` hook + */ + public static function onProfileSidebarEntryEvent(ArrayFilterEvent $event): void + { + $data = $event->getArray(); + + $profile = (array) $data['profile'] ?? []; + + $data['profile'] = static::callHook($event->getName(), $profile); + + $event->setArray($data); + } + /** * Map the OEMBED_FETCH_END event to `oembed_fetch_url` hook */ diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index ee36b2d983..12105decc9 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -76,6 +76,10 @@ final class ArrayFilterEvent extends Event public const CONTACT_PHOTO_MENU = 'friendica.data.contact_photo_menu'; + public const PROFILE_SIDEBAR_ENTRY = 'friendica.data.profile_sidebar_entry'; + + public const PROFILE_SIDEBAR = 'friendica.data.profile_sidebar'; + public const OEMBED_FETCH_END = 'friendica.data.oembed_fetch_end'; public const PAGE_INFO = 'friendica.data.page_info'; diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 8217b2a96a..7a900b9bc4 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -19,6 +19,7 @@ use Friendica\Core\Search; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Network\HTTPException; use Friendica\Protocol\Activity; use Friendica\Protocol\Diaspora; @@ -258,11 +259,6 @@ class Profile * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException * @note Returns empty string if passed $profile is wrong type or not populated - * - * @hooks 'profile_sidebar_enter' - * array $profile - profile data - * @hooks 'profile_sidebar' - * array $arr */ public static function getVCardHtml(array $profile, bool $block, bool $show_contacts): string { @@ -282,7 +278,17 @@ class Profile $profile['network_link'] = ''; - Hook::callAll('profile_sidebar_enter', $profile); + $eventDispatcher = DI::eventDispatcher(); + + $hook_data = [ + 'profile' => $profile, + ]; + + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, $hook_data), + )->getArray(); + + $profile = $hook_data['profile'] ?? $profile; $profile_url = $profile['url']; @@ -473,9 +479,17 @@ class Profile '$network_url' => $network_url, ]); - $arr = ['profile' => &$profile, 'entry' => &$o]; + $hook_data = [ + 'profile' => &$profile, + 'entry' => &$o, + ]; - Hook::callAll('profile_sidebar', $arr); + $hook_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::PROFILE_SIDEBAR, $hook_data), + )->getArray(); + + $profile = $hook_data['profile'] ?? $profile; + $o = $hook_data['entry'] ?? $o; return $o; } diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index c6b7ac7a5c..1a28e09161 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -52,6 +52,8 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::RENDER_LOCATION => 'onArrayFilterEvent', ArrayFilterEvent::ITEM_PHOTO_MENU => 'onArrayFilterEvent', ArrayFilterEvent::CONTACT_PHOTO_MENU => 'onArrayFilterEvent', + ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY => 'onProfileSidebarEntryEvent', + ArrayFilterEvent::PROFILE_SIDEBAR => 'onArrayFilterEvent', ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', @@ -213,6 +215,28 @@ class HookEventBridgeTest extends TestCase ); } + public function testOnProfileSidebarEntryEventCallsHookWithCorrectValue(): void + { + $event = new ArrayFilterEvent(ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, ['profile' => ['uid' => 0, 'name' => 'original']]); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, array $data): array { + $this->assertSame('profile_sidebar_enter', $name); + $this->assertSame(['uid' => 0, 'name' => 'original'], $data); + + return ['uid' => 0, 'name' => 'changed']; + }); + + HookEventBridge::onProfileSidebarEntryEvent($event); + + $this->assertSame( + ['profile' => ['uid' => 0, 'name' => 'changed']], + $event->getArray(), + ); + } + public function testOnOembedFetchEndEventCallsHookWithCorrectValue(): void { $event = new ArrayFilterEvent(ArrayFilterEvent::OEMBED_FETCH_END, ['url' => 'original_url']); @@ -362,6 +386,8 @@ class HookEventBridgeTest extends TestCase [ArrayFilterEvent::RENDER_LOCATION, 'render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'item_photo_menu'], [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'contact_photo_menu'], + [ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, 'profile_sidebar_enter'], + [ArrayFilterEvent::PROFILE_SIDEBAR, 'profile_sidebar'], [ArrayFilterEvent::PAGE_INFO, 'page_info_data'], [ArrayFilterEvent::SMILEY_LIST, 'smilie'], [ArrayFilterEvent::JOT_NETWORKS, 'jot_networks'], diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 7a72faf758..85d90ba132 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -49,6 +49,8 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::RENDER_LOCATION, 'friendica.data.render_location'], [ArrayFilterEvent::ITEM_PHOTO_MENU, 'friendica.data.item_photo_menu'], [ArrayFilterEvent::CONTACT_PHOTO_MENU, 'friendica.data.contact_photo_menu'], + [ArrayFilterEvent::PROFILE_SIDEBAR_ENTRY, 'friendica.data.profile_sidebar_entry'], + [ArrayFilterEvent::PROFILE_SIDEBAR, 'friendica.data.profile_sidebar'], [ArrayFilterEvent::OEMBED_FETCH_END, 'friendica.data.oembed_fetch_end'], [ArrayFilterEvent::PAGE_INFO, 'friendica.data.page_info'], [ArrayFilterEvent::SMILEY_LIST, 'friendica.data.smiley_list'],