]> git.mxchange.org Git - friendica.git/commitdiff
Create events for prepare_body hooks
authorArt4 <art4@wlabs.de>
Mon, 17 Mar 2025 08:26:45 +0000 (08:26 +0000)
committerArt4 <art4@wlabs.de>
Mon, 17 Mar 2025 08:26:45 +0000 (08:26 +0000)
src/Core/Hooks/HookEventBridge.php
src/Event/ArrayFilterEvent.php
src/Model/Item.php
tests/Unit/Core/Hooks/HookEventBridgeTest.php
tests/Unit/Event/ArrayFilterEventTest.php

index 251c462929e20ef811e91c01a3b4924065e6a323..8d53df417fb28883ff4b0f046587be3e2bd47cd8 100644 (file)
@@ -48,6 +48,10 @@ final class HookEventBridge
                ArrayFilterEvent::INSERT_POST_LOCAL_END           => 'post_local_end',
                ArrayFilterEvent::INSERT_POST_REMOTE              => 'post_remote',
                ArrayFilterEvent::INSERT_POST_REMOTE_END          => 'post_remote_end',
+               ArrayFilterEvent::PREPARE_POST_START              => 'prepare_body_init',
+               ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT     => 'prepare_body_content_filter',
+               ArrayFilterEvent::PREPARE_POST                    => 'prepare_body',
+               ArrayFilterEvent::PREPARE_POST_END                => 'prepare_body_final',
                ArrayFilterEvent::PHOTO_UPLOAD_FORM               => 'photo_upload_form',
                ArrayFilterEvent::NETWORK_TO_NAME                 => 'network_to_name',
                ArrayFilterEvent::CONVERSATION_START              => 'conversation_start',
@@ -106,6 +110,10 @@ final class HookEventBridge
                        ArrayFilterEvent::INSERT_POST_LOCAL_END           => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE              => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE_END          => 'onArrayFilterEvent',
+                       ArrayFilterEvent::PREPARE_POST_START              => 'onPreparePostStartEvent',
+                       ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT     => 'onArrayFilterEvent',
+                       ArrayFilterEvent::PREPARE_POST                    => 'onArrayFilterEvent',
+                       ArrayFilterEvent::PREPARE_POST_END                => 'onArrayFilterEvent',
                        ArrayFilterEvent::PHOTO_UPLOAD_FORM               => 'onArrayFilterEvent',
                        ArrayFilterEvent::NETWORK_TO_NAME                 => 'onArrayFilterEvent',
                        ArrayFilterEvent::CONVERSATION_START              => 'onArrayFilterEvent',
@@ -163,6 +171,20 @@ final class HookEventBridge
                );
        }
 
+       /**
+        * Map the PREPARE_POST_START event to `prepare_body_init` hook
+        */
+       public static function onPreparePostStartEvent(ArrayFilterEvent $event): void
+       {
+               $data = $event->getArray();
+
+               $item = (array) $data['item'] ?? [];
+
+               $data['item'] = static::callHook($event->getName(), $item);
+
+               $event->setArray($data);
+       }
+
        /**
         * Map the OEMBED_FETCH_END event to `oembed_fetch_url` hook
         */
index 51719e7de9e15d3654ad625545ad7360edde9e59..f29acad95297940ecb8cdae0a5f123f38d61abb1 100644 (file)
@@ -34,6 +34,26 @@ final class ArrayFilterEvent extends Event
 
        public const INSERT_POST_REMOTE_END = 'friendica.data.insert_post_remote_end';
 
+       /**
+        * item array before any work
+        */
+       public const PREPARE_POST_START = 'friendica.data.prepare_post_start';
+
+       /**
+        * before first bbcode to html
+        */
+       public const PREPARE_POST_FILTER_CONTENT = 'friendica.data.prepare_post_filter_content';
+
+       /**
+        * after first bbcode to html
+        */
+       public const PREPARE_POST = 'friendica.data.prepare_post';
+
+       /**
+        * after attach icons and blockquote special case handling (spoiler, author)
+        */
+       public const PREPARE_POST_END = 'friendica.data.prepare_post_end';
+
        public const PHOTO_UPLOAD_FORM = 'friendica.data.photo_upload_form';
 
        public const NETWORK_TO_NAME = 'friendica.data.network_to_name';
index d2169d31569b2279a26e013d4ef86bef4eabe9c4..6c8be7528e659d1eb20a1f70b2756c1db342f134 100644 (file)
@@ -3058,16 +3058,22 @@ class Item
         * @return string item body html
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
-        * @hook  prepare_body_init item array before any work
-        * @hook  prepare_body_content_filter ('item'=>item array, 'filter_reasons'=>string array) before first bbcode to html
-        * @hook  prepare_body ('item'=>item array, 'html'=>body string, 'is_preview'=>boolean, 'filter_reasons'=>string array) after first bbcode to html
-        * @hook  prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author)
         */
        public static function prepareBody(array &$item, bool $attach = false, bool $is_preview = false, bool $only_cache = false): string
        {
-               $appHelper = DI::appHelper();
-               $uid       = DI::userSession()->getLocalUserId();
-               Hook::callAll('prepare_body_init', $item);
+               $appHelper       = DI::appHelper();
+               $uid             = DI::userSession()->getLocalUserId();
+               $eventDispatcher = DI::eventDispatcher();
+
+               $hook_data = [
+                       'item' => $item,
+               ];
+
+               $hook_data = $eventDispatcher->dispatch(
+                       new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST_START, $hook_data),
+               )->getArray();
+
+               $item = $hook_data['item'] ?? $item;
 
                // In order to provide theme developers more possibilities, event items
                // are treated differently.
@@ -3186,7 +3192,11 @@ class Item
                                'item'           => $item,
                                'filter_reasons' => $filter_reasons
                        ];
-                       Hook::callAll('prepare_body_content_filter', $hook_data);
+
+                       $hook_data = $eventDispatcher->dispatch(
+                               new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT, $hook_data),
+                       )->getArray();
+
                        $filter_reasons = $hook_data['filter_reasons'];
                        unset($hook_data);
                }
@@ -3205,7 +3215,11 @@ class Item
                        'preview'        => $is_preview,
                        'filter_reasons' => $filter_reasons
                ];
-               Hook::callAll('prepare_body', $hook_data);
+
+               $hook_data = $eventDispatcher->dispatch(
+                       new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST, $hook_data),
+               )->getArray();
+
                $s = $hook_data['html'];
 
                unset($hook_data);
@@ -3257,9 +3271,16 @@ class Item
 
                $s = HTML::applyContentFilter($s, $filter_reasons);
 
-               $hook_data = ['item' => $item, 'html' => $s];
-               Hook::callAll('prepare_body_final', $hook_data);
-               return $hook_data['html'];
+               $hook_data = [
+                       'item' => $item,
+                       'html' => $s,
+               ];
+
+               $hook_data = $eventDispatcher->dispatch(
+                       new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST_END, $hook_data),
+               )->getArray();
+
+               return (string) $hook_data['html'] ?? $s;
        }
 
        /**
index 325aefb7421aae3acabdeceabe154d9231ef390b..17291539a76d4ee09093cb9784d5fab68acea474 100644 (file)
@@ -37,6 +37,10 @@ class HookEventBridgeTest extends TestCase
                        ArrayFilterEvent::INSERT_POST_LOCAL_END           => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE              => 'onArrayFilterEvent',
                        ArrayFilterEvent::INSERT_POST_REMOTE_END          => 'onArrayFilterEvent',
+                       ArrayFilterEvent::PREPARE_POST_START              => 'onPreparePostStartEvent',
+                       ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT     => 'onArrayFilterEvent',
+                       ArrayFilterEvent::PREPARE_POST                    => 'onArrayFilterEvent',
+                       ArrayFilterEvent::PREPARE_POST_END                => 'onArrayFilterEvent',
                        ArrayFilterEvent::PHOTO_UPLOAD_FORM               => 'onArrayFilterEvent',
                        ArrayFilterEvent::NETWORK_TO_NAME                 => 'onArrayFilterEvent',
                        ArrayFilterEvent::CONVERSATION_START              => 'onArrayFilterEvent',
@@ -183,6 +187,28 @@ class HookEventBridgeTest extends TestCase
                HookEventBridge::onCollectRoutesEvent($event);
        }
 
+       public function testOnPreparePostStartEventCallsHookWithCorrectValue(): void
+       {
+               $event = new ArrayFilterEvent(ArrayFilterEvent::PREPARE_POST_START, ['item' => ['id' => -1]]);
+
+               $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
+               $reflectionProperty->setAccessible(true);
+
+               $reflectionProperty->setValue(null, function (string $name, array $data): array {
+                       $this->assertSame('prepare_body_init', $name);
+                       $this->assertSame(['id' => -1], $data);
+
+                       return ['id' => 123];
+               });
+
+               HookEventBridge::onPreparePostStartEvent($event);
+
+               $this->assertSame(
+                       ['item' => ['id' => 123]],
+                       $event->getArray(),
+               );
+       }
+
        public function testOnOembedFetchEndEventCallsHookWithCorrectValue(): void
        {
                $event = new ArrayFilterEvent(ArrayFilterEvent::OEMBED_FETCH_END, ['url' => 'original_url']);
@@ -318,6 +344,9 @@ class HookEventBridgeTest extends TestCase
                        [ArrayFilterEvent::INSERT_POST_LOCAL_END, 'post_local_end'],
                        [ArrayFilterEvent::INSERT_POST_REMOTE, 'post_remote'],
                        [ArrayFilterEvent::INSERT_POST_REMOTE_END, 'post_remote_end'],
+                       [ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT, 'prepare_body_content_filter'],
+                       [ArrayFilterEvent::PREPARE_POST, 'prepare_body'],
+                       [ArrayFilterEvent::PREPARE_POST_END, 'prepare_body_final'],
                        [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'photo_upload_form'],
                        [ArrayFilterEvent::NETWORK_TO_NAME, 'network_to_name'],
                        [ArrayFilterEvent::CONVERSATION_START, 'conversation_start'],
index 7c124f0b33674dead288536627a57e9376c0e9ec..5af49e9fb89fd2283e5d2d64cf06510142765e52 100644 (file)
@@ -34,6 +34,10 @@ class ArrayFilterEventTest extends TestCase
                        [ArrayFilterEvent::INSERT_POST_LOCAL_END, 'friendica.data.insert_post_local_end'],
                        [ArrayFilterEvent::INSERT_POST_REMOTE, 'friendica.data.insert_post_remote'],
                        [ArrayFilterEvent::INSERT_POST_REMOTE_END, 'friendica.data.insert_post_remote_end'],
+                       [ArrayFilterEvent::PREPARE_POST_START, 'friendica.data.prepare_post_start'],
+                       [ArrayFilterEvent::PREPARE_POST_FILTER_CONTENT, 'friendica.data.prepare_post_filter_content'],
+                       [ArrayFilterEvent::PREPARE_POST, 'friendica.data.prepare_post'],
+                       [ArrayFilterEvent::PREPARE_POST_END, 'friendica.data.prepare_post_end'],
                        [ArrayFilterEvent::PHOTO_UPLOAD_FORM, 'friendica.data.photo_upload_form'],
                        [ArrayFilterEvent::NETWORK_TO_NAME, 'friendica.data.network_to_name'],
                        [ArrayFilterEvent::CONVERSATION_START, 'friendica.data.conversation_start'],