Create hook for profile_sidebar hooks

This commit is contained in:
Art4 2025-03-17 12:20:25 +00:00
parent ac8ca35b2b
commit 59359f7d9d
5 changed files with 72 additions and 8 deletions

View file

@ -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
*/

View file

@ -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';

View file

@ -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;
}

View file

@ -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'],

View file

@ -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'],