]> git.mxchange.org Git - friendica.git/commitdiff
Create hook for profile_sidebar hooks
authorArt4 <art4@wlabs.de>
Mon, 17 Mar 2025 12:20:25 +0000 (12:20 +0000)
committerArt4 <art4@wlabs.de>
Mon, 17 Mar 2025 12:20:25 +0000 (12:20 +0000)
src/Core/Hooks/HookEventBridge.php
src/Event/ArrayFilterEvent.php
src/Model/Profile.php
tests/Unit/Core/Hooks/HookEventBridgeTest.php
tests/Unit/Event/ArrayFilterEventTest.php

index 83a075c7d531498469436a015848a46273ed2b1e..e287cdd76768c5758491ae50205d293ed8f503bb 100644 (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
         */
index ee36b2d983f59768f0b7b9dcf14a13f7a8d7721f..12105decc9408f1b4eda01834c868c6822b9193f 100644 (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';
index 8217b2a96a50779d1385109a8728c3f856b04b05..7a900b9bc471691f29c4a2ff75d96d15dec3352d 100644 (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_data = $eventDispatcher->dispatch(
+                       new ArrayFilterEvent(ArrayFilterEvent::PROFILE_SIDEBAR, $hook_data),
+               )->getArray();
 
-               Hook::callAll('profile_sidebar', $arr);
+               $profile = $hook_data['profile'] ?? $profile;
+               $o = $hook_data['entry'] ?? $o;
 
                return $o;
        }
index c6b7ac7a5cde0d527902d4529785494640b2b43c..1a28e09161409b4c6ce66bc77bd53b20cf39fe2e 100644 (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'],
index 7a72faf758f0d95db84ae7711f3df9d0affbe05a..85d90ba1323e913dd82d83f5d25dbf36ec61b9d7 100644 (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'],