Skip to content

Commit

Permalink
Reply Block: Embed referenced post when possible
Browse files Browse the repository at this point in the history
Fixes #1027.
  • Loading branch information
obenland committed Dec 19, 2024
1 parent b2e14e9 commit 77ac01f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Improved

* Direct Messages: Improve HTML to e-mail text conversion
* The Reply block now shows an embedded version of the referenced post, if available

## [4.5.1] - 2024-12-18

Expand Down
14 changes: 13 additions & 1 deletion includes/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,19 @@ public static function render_reply_block( $attrs ) {
$html = '';

if ( ! empty( $attrs['url'] ) ) {
$html = sprintf(
wp_oembed_add_provider( '#https?://mastodon\.social/(@.+)/([0-9]+)#i', 'https://mastodon.social/api/oembed', true );

/**
* Fires to add an oEmbed provider for the reply block.
*/
do_action( 'activitypub_reply_block_add_oembed_provider' );

$embed = wp_oembed_get( $attrs['url'] );
if ( $embed ) {
$html .= $embed;
}

$html .= sprintf(
'<p><a title="%2$s" aria-label="%2$s" href="%1$s" class="u-in-reply-to" target="_blank">%3$s</a></p>',
esc_url( $attrs['url'] ),
esc_attr__( 'This post is a response to the referenced content.', 'activitypub' ),
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ For reasons of data protection, it is not possible to see the followers of other
= Unreleased =

* Improved: HTML to e-mail text conversion
* Improved: The Reply block now shows an embedded version of the referenced post, if available

= 4.5.1 =

Expand Down
38 changes: 38 additions & 0 deletions tests/includes/class-test-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,42 @@ public function test_render_reply_block_with_empty_url() {
$output = \Activitypub\Blocks::render_reply_block( $attrs );
$this->assertEmpty( $output );
}

/**
* Test the render_reply_block() method with a Mastodon URL that has an embed.
*
* @covers ::render_reply_block
*/
public function test_render_reply_block_with_mastodon_embed() {
$mock_embed = '<div class="mastodon-embed">Mock Embed Content</div>';
add_filter(
'pre_oembed_result',
function () use ( $mock_embed ) {
return $mock_embed;
}
);

$attrs = array( 'url' => 'https://mastodon.social/@user/123456' );
$output = \Activitypub\Blocks::render_reply_block( $attrs );

$this->assertStringContainsString( $mock_embed, $output, 'Output should contain the embedded content.' );
$this->assertStringContainsString( 'u-in-reply-to', $output, 'Output should still contain the reply link.' );
$this->assertStringContainsString( 'mastodon.social/@user/123456', $output, 'Output should contain the formatted URL.' );
}

/**
* Test the render_reply_block() method with a URL that has no available embed.
*
* @covers ::render_reply_block
*/
public function test_render_reply_block_with_no_embed() {
add_filter( 'pre_oembed_result', '__return_false' );

$attrs = array( 'url' => 'https://example.com/no-embed' );
$output = \Activitypub\Blocks::render_reply_block( $attrs );

$this->assertStringNotContainsString( '<blockquote', $output, 'Output should not contain any embedded content.' );
$this->assertStringContainsString( 'u-in-reply-to', $output, 'Output should contain the reply link.' );
$this->assertStringContainsString( 'example.com/no-embed', $output, 'Output should contain the formatted URL.' );
}
}

0 comments on commit 77ac01f

Please sign in to comment.