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',
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',
);
}
+ /**
+ * 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
*/
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';
* @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.
'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);
}
'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);
$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;
}
/**
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',
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']);
[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'],
[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'],