Skip to content

Commit 9d9ec68

Browse files
obenlandpfefferle
andauthored
Editor: Make Fediverse preview template filterable (#1098)
Fixes #967. Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent afcbc6e commit 9d9ec68

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

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

1415
### Improved

includes/class-activitypub.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,15 @@ public static function render_activitypub_template( $template ) {
102102
} elseif ( is_comment() ) {
103103
$activitypub_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php';
104104
} elseif ( \is_singular() && ! is_post_disabled( \get_the_ID() ) ) {
105-
$preview = \get_query_var( 'preview' );
106-
if ( $preview ) {
105+
if ( \get_query_var( 'preview' ) ) {
107106
\define( 'ACTIVITYPUB_PREVIEW', true );
108-
$activitypub_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-preview.php';
107+
108+
/**
109+
* Filter the template used for the ActivityPub preview.
110+
*
111+
* @param string $activitypub_template Absolute path to the template file.
112+
*/
113+
$activitypub_template = apply_filters( 'activitypub_preview_template', ACTIVITYPUB_PLUGIN_DIR . '/templates/post-preview.php' );
109114
} else {
110115
$activitypub_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php';
111116
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ For reasons of data protection, it is not possible to see the followers of other
134134

135135
= Unreleased =
136136

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

tests/includes/class-test-activitypub.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,34 @@ public function test_post_type_support() {
2626
$this->assertContains( 'post', \get_post_types_by_support( 'activitypub' ) );
2727
$this->assertContains( 'page', \get_post_types_by_support( 'activitypub' ) );
2828
}
29+
30+
/**
31+
* Test activitypub_preview_template filter.
32+
*
33+
* @covers ::render_activitypub_template
34+
*/
35+
public function test_preview_template_filter() {
36+
// Create a test post.
37+
$post_id = self::factory()->post->create();
38+
$this->go_to( get_permalink( $post_id ) );
39+
40+
// Simulate ActivityPub request and preview mode.
41+
$_SERVER['HTTP_ACCEPT'] = 'application/activity+json';
42+
\set_query_var( 'preview', true );
43+
44+
// Add filter before testing.
45+
\add_filter(
46+
'activitypub_preview_template',
47+
function () {
48+
return '/custom/template.php';
49+
}
50+
);
51+
52+
// Test that the filter is applied.
53+
$template = \Activitypub\Activitypub::render_activitypub_template( 'original.php' );
54+
$this->assertEquals( '/custom/template.php', $template, 'Custom preview template should be used when filter is applied.' );
55+
56+
// Clean up.
57+
unset( $_SERVER['HTTP_ACCEPT'] );
58+
}
2959
}

0 commit comments

Comments
 (0)