Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Make Fediverse preview template filterable #1098

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* Add a filter to allow modifying the ActivityPub preview template
* `@mentions` in the JSON representation of the reply

### Improved
Expand Down
11 changes: 8 additions & 3 deletions includes/class-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,15 @@ public static function render_activitypub_template( $template ) {
} elseif ( is_comment() ) {
$activitypub_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php';
} elseif ( \is_singular() && ! is_post_disabled( \get_the_ID() ) ) {
$preview = \get_query_var( 'preview' );
if ( $preview ) {
if ( \get_query_var( 'preview' ) ) {
\define( 'ACTIVITYPUB_PREVIEW', true );
$activitypub_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-preview.php';

/**
* Filter the template used for the ActivityPub preview.
*
* @param string $activitypub_template Absolute path to the template file.
*/
$activitypub_template = apply_filters( 'activitypub_preview_template', ACTIVITYPUB_PLUGIN_DIR . '/templates/post-preview.php' );
} else {
$activitypub_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php';
}
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ For reasons of data protection, it is not possible to see the followers of other

= Unreleased =

* Added: A filter to allow modifying the ActivityPub preview template
* Added: `@mentions` in the JSON representation of the reply
* Improved: HTML to e-mail text conversion

Expand Down
30 changes: 30 additions & 0 deletions tests/includes/class-test-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,34 @@ public function test_post_type_support() {
$this->assertContains( 'post', \get_post_types_by_support( 'activitypub' ) );
$this->assertContains( 'page', \get_post_types_by_support( 'activitypub' ) );
}

/**
* Test activitypub_preview_template filter.
*
* @covers ::render_activitypub_template
*/
public function test_preview_template_filter() {
// Create a test post.
$post_id = self::factory()->post->create();
$this->go_to( get_permalink( $post_id ) );

// Simulate ActivityPub request and preview mode.
$_SERVER['HTTP_ACCEPT'] = 'application/activity+json';
\set_query_var( 'preview', true );

// Add filter before testing.
\add_filter(
'activitypub_preview_template',
function () {
return '/custom/template.php';
}
);

// Test that the filter is applied.
$template = \Activitypub\Activitypub::render_activitypub_template( 'original.php' );
$this->assertEquals( '/custom/template.php', $template, 'Custom preview template should be used when filter is applied.' );

// Clean up.
unset( $_SERVER['HTTP_ACCEPT'] );
}
}
Loading