Skip to content

Commit 39dfabc

Browse files
pfefferleobenland
andauthored
Fix: Importing tags from the Fediverse (#1234)
* Fix: Importing tags from the Fediverse Fix #1167 This PR adds a check, so that only supported posts will be parsed for tags. This will prevent, that followers and other custom post types will be parsed for Hashtags. * Add test * Add changelog * Check if the (custom) post supports tags. --------- Co-authored-by: Konstantin Obenland <[email protected]>
1 parent e0c3b67 commit 39dfabc

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

CHANGELOG.md

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

1616
* Handle deletes from remote servers that leave behind an accessible Tombstone object.
17+
* No longer parses tags for post types that don't support Activitypub.
1718

1819
## [4.7.3] - 2025-01-21
1920

includes/class-hashtag.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ public static function filter_activity_object( $activity ) {
5353
* @param \WP_Post $post Post object.
5454
*/
5555
public static function insert_post( $post_id, $post ) {
56+
// Check if the post supports ActivityPub.
57+
if ( ! \post_type_supports( \get_post_type( $post ), 'activitypub' ) ) {
58+
return;
59+
}
60+
61+
// Check if the (custom) post supports tags.
62+
$taxonomies = \get_object_taxonomies( $post );
63+
if ( ! in_array( 'post_tag', $taxonomies, true ) ) {
64+
return;
65+
}
66+
5667
$tags = array();
5768

5869
// Skip hashtags in HTML attributes, like hex colors.

readme.txt

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

136136
* Changed: Improved content negotiation and AUTHORIZED_FETCH support for third-party plugins
137137
* Fixed: Handle deletes from remote servers that leave behind an accessible Tombstone object.
138+
* Fixed: No longer parses tags for post types that don't support Activitypub.
138139

139140
= 4.7.3 =
140141

tests/includes/class-test-hashtag.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Activitypub\Tests;
99

10+
use Activitypub\Collection\Followers;
11+
1012
/**
1113
* Test class for Activitypub Hashtag.
1214
*
@@ -89,7 +91,7 @@ public function the_content_provider() {
8991
* @param string $message The error message.
9092
*/
9193
public function test_hashtag_conversion( $content, $excerpt, $expected_tags, $message ) {
92-
$post_id = $this->factory->post->create(
94+
$post_id = self::factory()->post->create(
9395
array(
9496
'post_content' => $content,
9597
'post_excerpt' => $excerpt,
@@ -104,6 +106,25 @@ public function test_hashtag_conversion( $content, $excerpt, $expected_tags, $me
104106
}
105107
}
106108

109+
/**
110+
* Test no hashtags for unsupported post types.
111+
*
112+
* @covers ::insert_post
113+
*/
114+
public function test_no_hashtags_for_unsupported_post_types() {
115+
$post_id = self::factory()->post->create(
116+
array(
117+
'post_content' => 'Testing #php and #programming',
118+
'post_type' => Followers::POST_TYPE,
119+
)
120+
);
121+
122+
\Activitypub\Hashtag::insert_post( $post_id, get_post( $post_id ) );
123+
$tags = wp_get_post_tags( $post_id, array( 'fields' => 'names' ) );
124+
125+
$this->assertEmpty( $tags, 'Should not add hashtags to unsupported post types' );
126+
}
127+
107128
/**
108129
* Data provider for hashtag tests.
109130
*

0 commit comments

Comments
 (0)