Skip to content

Commit 347b232

Browse files
authored
Use short ternary for post content template fallback (#2417)
1 parent e202205 commit 347b232

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Fixed an issue with post content templates to ensure the correct fallback is always applied.

includes/transformer/class-post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ protected function get_context() {
891891
*/
892892
protected function get_post_content_template() {
893893
$content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
894-
$template = $content ?? ACTIVITYPUB_CUSTOM_POST_CONTENT;
894+
$template = $content ?: ACTIVITYPUB_CUSTOM_POST_CONTENT; // phpcs:ignore Universal.Operators.DisallowShortTernary.Found
895895

896896
$post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
897897

tests/phpunit/tests/includes/transformer/class-test-post.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,4 +1032,126 @@ public function test_get_interaction_policy_invalid_value_returns_null() {
10321032
'Invalid permission should fall back to anyone (public) policy.'
10331033
);
10341034
}
1035+
1036+
/**
1037+
* Test that get_post_content_template falls back to constant when option is empty.
1038+
*
1039+
* @covers ::get_post_content_template
1040+
*/
1041+
public function test_get_post_content_template_fallback_with_empty_option() {
1042+
$post = $this->create_test_post();
1043+
1044+
// Set object type to something other than wordpress-post-format.
1045+
\update_option( 'activitypub_object_type', 'Article' );
1046+
1047+
// Test with empty string option - should fall back to constant.
1048+
\update_option( 'activitypub_custom_post_content', '' );
1049+
1050+
$transformer = new Post( $post );
1051+
$reflection = new \ReflectionClass( Post::class );
1052+
$method = $reflection->getMethod( 'get_post_content_template' );
1053+
$method->setAccessible( true );
1054+
1055+
$template = $method->invoke( $transformer );
1056+
1057+
$this->assertSame( ACTIVITYPUB_CUSTOM_POST_CONTENT, $template, 'Empty option should fall back to ACTIVITYPUB_CUSTOM_POST_CONTENT constant.' );
1058+
}
1059+
1060+
/**
1061+
* Test that get_post_content_template uses option value when set.
1062+
*
1063+
* @covers ::get_post_content_template
1064+
*/
1065+
public function test_get_post_content_template_uses_option_when_set() {
1066+
$post = $this->create_test_post();
1067+
1068+
// Set object type to something other than wordpress-post-format.
1069+
\update_option( 'activitypub_object_type', 'Article' );
1070+
1071+
// Test with custom template option.
1072+
$custom_template = '[ap_title]\n\n[ap_content]\n\n[ap_hashtags]';
1073+
\update_option( 'activitypub_custom_post_content', $custom_template );
1074+
1075+
$transformer = new Post( $post );
1076+
$reflection = new \ReflectionClass( Post::class );
1077+
$method = $reflection->getMethod( 'get_post_content_template' );
1078+
$method->setAccessible( true );
1079+
1080+
$template = $method->invoke( $transformer );
1081+
1082+
$this->assertSame( $custom_template, $template, 'Should use custom template option when set.' );
1083+
}
1084+
1085+
/**
1086+
* Test that get_post_content_template falls back with null option.
1087+
*
1088+
* @covers ::get_post_content_template
1089+
*/
1090+
public function test_get_post_content_template_fallback_with_false_option() {
1091+
$post = $this->create_test_post();
1092+
1093+
// Set object type to something other than wordpress-post-format.
1094+
\update_option( 'activitypub_object_type', 'Article' );
1095+
1096+
// Test with false option (not set) - should fall back to constant.
1097+
\delete_option( 'activitypub_custom_post_content' );
1098+
1099+
$transformer = new Post( $post );
1100+
$reflection = new \ReflectionClass( Post::class );
1101+
$method = $reflection->getMethod( 'get_post_content_template' );
1102+
$method->setAccessible( true );
1103+
1104+
$template = $method->invoke( $transformer );
1105+
1106+
$this->assertSame( ACTIVITYPUB_CUSTOM_POST_CONTENT, $template, 'False option should fall back to ACTIVITYPUB_CUSTOM_POST_CONTENT constant.' );
1107+
}
1108+
1109+
/**
1110+
* Test that get_post_content_template respects wordpress-post-format setting.
1111+
*
1112+
* @covers ::get_post_content_template
1113+
*/
1114+
public function test_get_post_content_template_wordpress_post_format() {
1115+
// Create a post with long content and title (will be Article type).
1116+
$article_post = self::factory()->post->create_and_get(
1117+
array(
1118+
'post_title' => 'Test Article',
1119+
'post_content' => str_repeat( 'Long content. ', 100 ),
1120+
'post_status' => 'publish',
1121+
)
1122+
);
1123+
1124+
// Set custom content template.
1125+
\update_option( 'activitypub_custom_post_content', '[ap_title]\n\n[ap_content]' );
1126+
1127+
// Set post format setting to wordpress-post-format.
1128+
\update_option( 'activitypub_object_type', 'wordpress-post-format' );
1129+
1130+
$transformer = new Post( $article_post );
1131+
$reflection = new \ReflectionClass( Post::class );
1132+
$method = $reflection->getMethod( 'get_post_content_template' );
1133+
$method->setAccessible( true );
1134+
1135+
$template = $method->invoke( $transformer );
1136+
1137+
// When wordpress-post-format is set, template should be generated based on post type.
1138+
// For an Article (long content with title), it should just be [ap_content].
1139+
$this->assertSame( '[ap_content]', $template, 'wordpress-post-format should override custom template for Article type.' );
1140+
1141+
// Test with a Note type (no title or short content).
1142+
$note_post = self::factory()->post->create_and_get(
1143+
array(
1144+
'post_title' => '',
1145+
'post_content' => 'Short note',
1146+
'post_status' => 'publish',
1147+
)
1148+
);
1149+
1150+
$note_transformer = new Post( $note_post );
1151+
$note_template = $method->invoke( $note_transformer );
1152+
1153+
// For a Note, the template should include the title.
1154+
$this->assertStringContainsString( '[ap_title type="html"]', $note_template, 'wordpress-post-format should add title for Note type.' );
1155+
$this->assertStringContainsString( '[ap_content]', $note_template, 'wordpress-post-format should include content for Note type.' );
1156+
}
10351157
}

0 commit comments

Comments
 (0)