From 62d27de3b0816d0cb16ab38d545a0e468f62aba7 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 14 Feb 2025 15:03:29 +0000 Subject: [PATCH] Create events for bbcode hooks --- src/Content/Text/BBCode.php | 23 ++++++++-- src/Core/Hooks/HookEventBridge.php | 32 +++++++++++++ src/Event/ArrayFilterEvent.php | 4 ++ tests/Unit/Core/Hooks/HookEventBridgeTest.php | 46 +++++++++++++++++++ tests/Unit/Event/ArrayFilterEventTest.php | 2 + 5 files changed, 104 insertions(+), 3 deletions(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 2102b43a32..d111437dff 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -15,10 +15,10 @@ use Friendica\Content\Item; use Friendica\Content\OEmbed; use Friendica\Content\PageInfo; use Friendica\Content\Smilies; -use Friendica\Core\Hook; use Friendica\Core\Protocol; use Friendica\Core\Renderer; use Friendica\DI; +use Friendica\Event\ArrayFilterEvent; use Friendica\Model\Contact; use Friendica\Model\Event; use Friendica\Model\Post; @@ -1297,7 +1297,15 @@ class BBCode DI::profiler()->startRecording('rendering'); - Hook::callAll('bbcode', $text); + $eventDispatcher = DI::eventDispatcher(); + + $text_data = ['bbcode2html' => $text]; + + $text_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::BBCODE_TO_HTML_START, $text_data), + )->getArray(); + + $text = $text_data['bbcode2html'] ?? $text; $ev = Event::fromBBCode($text); @@ -2375,9 +2383,18 @@ class BBCode ); } - Hook::callAll('bb2diaspora', $text); + $eventDispatcher = DI::eventDispatcher(); + + $text_data = ['bbcode2markdown' => $text]; + + $text_data = $eventDispatcher->dispatch( + new ArrayFilterEvent(ArrayFilterEvent::BBCODE_TO_MARKDOWN_END, $text_data), + )->getArray(); + + $text = $text_data['bbcode2markdown'] ?? $text; DI::profiler()->stopRecording(); + return $text; } diff --git a/src/Core/Hooks/HookEventBridge.php b/src/Core/Hooks/HookEventBridge.php index ec79484f28..6daa53a54d 100644 --- a/src/Core/Hooks/HookEventBridge.php +++ b/src/Core/Hooks/HookEventBridge.php @@ -55,6 +55,8 @@ final class HookEventBridge ArrayFilterEvent::OEMBED_FETCH_END => 'oembed_fetch_url', ArrayFilterEvent::PAGE_INFO => 'page_info_data', ArrayFilterEvent::SMILEY_LIST => 'smilie', + ArrayFilterEvent::BBCODE_TO_HTML_START => 'bbcode', + ArrayFilterEvent::BBCODE_TO_MARKDOWN_END => 'bb2diaspora', HtmlFilterEvent::HEAD => 'head', HtmlFilterEvent::FOOTER => 'footer', HtmlFilterEvent::PAGE_HEADER => 'page_header', @@ -89,6 +91,8 @@ final class HookEventBridge ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', + ArrayFilterEvent::BBCODE_TO_HTML_START => 'onBbcodeToHtmlEvent', + ArrayFilterEvent::BBCODE_TO_MARKDOWN_END => 'onBbcodeToMarkdownEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', @@ -129,6 +133,34 @@ final class HookEventBridge $event->setArray($data); } + /** + * Map the BBCODE_TO_HTML_START event to `bbcode` hook + */ + public static function onBbcodeToHtmlEvent(ArrayFilterEvent $event): void + { + $data = $event->getArray(); + + $bbcode2html = (string) $data['bbcode2html'] ?? ''; + + $data['bbcode2html'] = static::callHook($event->getName(), $bbcode2html); + + $event->setArray($data); + } + + /** + * Map the BBCODE_TO_MARKDOWN_END event to `bb2diaspora` hook + */ + public static function onBbcodeToMarkdownEvent(ArrayFilterEvent $event): void + { + $data = $event->getArray(); + + $bbcode2markdown = (string) $data['bbcode2markdown'] ?? ''; + + $data['bbcode2markdown'] = static::callHook($event->getName(), $bbcode2markdown); + + $event->setArray($data); + } + public static function onArrayFilterEvent(ArrayFilterEvent $event): void { $event->setArray( diff --git a/src/Event/ArrayFilterEvent.php b/src/Event/ArrayFilterEvent.php index d5f9b4e96d..2541940802 100644 --- a/src/Event/ArrayFilterEvent.php +++ b/src/Event/ArrayFilterEvent.php @@ -48,6 +48,10 @@ final class ArrayFilterEvent extends Event public const SMILEY_LIST = 'friendica.data.smiley_list'; + public const BBCODE_TO_HTML_START = 'friendica.data.bbcode_to_html_start'; + + public const BBCODE_TO_MARKDOWN_END = 'friendica.data.bbcode_to_markdown_end'; + private array $array; public function __construct(string $name, array $array) diff --git a/tests/Unit/Core/Hooks/HookEventBridgeTest.php b/tests/Unit/Core/Hooks/HookEventBridgeTest.php index 58c9e29853..11f178451b 100644 --- a/tests/Unit/Core/Hooks/HookEventBridgeTest.php +++ b/tests/Unit/Core/Hooks/HookEventBridgeTest.php @@ -44,6 +44,8 @@ class HookEventBridgeTest extends TestCase ArrayFilterEvent::OEMBED_FETCH_END => 'onOembedFetchEndEvent', ArrayFilterEvent::PAGE_INFO => 'onArrayFilterEvent', ArrayFilterEvent::SMILEY_LIST => 'onArrayFilterEvent', + ArrayFilterEvent::BBCODE_TO_HTML_START => 'onBbcodeToHtmlEvent', + ArrayFilterEvent::BBCODE_TO_MARKDOWN_END => 'onBbcodeToMarkdownEvent', HtmlFilterEvent::HEAD => 'onHtmlFilterEvent', HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent', HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent', @@ -181,6 +183,50 @@ class HookEventBridgeTest extends TestCase ); } + public function testOnBbcodeToHtmlEventCallsHookWithCorrectValue(): void + { + $event = new ArrayFilterEvent(ArrayFilterEvent::BBCODE_TO_HTML_START, ['bbcode2html' => '[b]original[/b]']); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, string $data): string { + $this->assertSame('bbcode', $name); + $this->assertSame('[b]original[/b]', $data); + + return 'changed'; + }); + + HookEventBridge::onBbcodeToHtmlEvent($event); + + $this->assertSame( + ['bbcode2html' => 'changed'], + $event->getArray(), + ); + } + + public function testOnBbcodeToMarkdownEventCallsHookWithCorrectValue(): void + { + $event = new ArrayFilterEvent(ArrayFilterEvent::BBCODE_TO_MARKDOWN_END, ['bbcode2markdown' => '[b]original[/b]']); + + $reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue(null, function (string $name, string $data): string { + $this->assertSame('bb2diaspora', $name); + $this->assertSame('[b]original[/b]', $data); + + return '**changed**'; + }); + + HookEventBridge::onBbcodeToMarkdownEvent($event); + + $this->assertSame( + ['bbcode2markdown' => '**changed**'], + $event->getArray(), + ); + } + public static function getArrayFilterEventData(): array { return [ diff --git a/tests/Unit/Event/ArrayFilterEventTest.php b/tests/Unit/Event/ArrayFilterEventTest.php index 2fa8f78663..643f309a8b 100644 --- a/tests/Unit/Event/ArrayFilterEventTest.php +++ b/tests/Unit/Event/ArrayFilterEventTest.php @@ -41,6 +41,8 @@ class ArrayFilterEventTest extends TestCase [ArrayFilterEvent::OEMBED_FETCH_END, 'friendica.data.oembed_fetch_end'], [ArrayFilterEvent::PAGE_INFO, 'friendica.data.page_info'], [ArrayFilterEvent::SMILEY_LIST, 'friendica.data.smiley_list'], + [ArrayFilterEvent::BBCODE_TO_HTML_START, 'friendica.data.bbcode_to_html_start'], + [ArrayFilterEvent::BBCODE_TO_MARKDOWN_END, 'friendica.data.bbcode_to_markdown_end'], ]; }