From 39dfabccc31db417a04d759e5acbc8f6340e420b Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 29 Jan 2025 16:09:54 +0100 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + includes/class-hashtag.php | 11 +++++++++++ readme.txt | 1 + tests/includes/class-test-hashtag.php | 23 ++++++++++++++++++++++- 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb2a104fd..8c730bfc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed * Handle deletes from remote servers that leave behind an accessible Tombstone object. +* No longer parses tags for post types that don't support Activitypub. ## [4.7.3] - 2025-01-21 diff --git a/includes/class-hashtag.php b/includes/class-hashtag.php index 586261b06..8012f1df2 100644 --- a/includes/class-hashtag.php +++ b/includes/class-hashtag.php @@ -53,6 +53,17 @@ public static function filter_activity_object( $activity ) { * @param \WP_Post $post Post object. */ public static function insert_post( $post_id, $post ) { + // Check if the post supports ActivityPub. + if ( ! \post_type_supports( \get_post_type( $post ), 'activitypub' ) ) { + return; + } + + // Check if the (custom) post supports tags. + $taxonomies = \get_object_taxonomies( $post ); + if ( ! in_array( 'post_tag', $taxonomies, true ) ) { + return; + } + $tags = array(); // Skip hashtags in HTML attributes, like hex colors. diff --git a/readme.txt b/readme.txt index f33b35bd8..3a11ea7ac 100644 --- a/readme.txt +++ b/readme.txt @@ -135,6 +135,7 @@ For reasons of data protection, it is not possible to see the followers of other * Changed: Improved content negotiation and AUTHORIZED_FETCH support for third-party plugins * Fixed: Handle deletes from remote servers that leave behind an accessible Tombstone object. +* Fixed: No longer parses tags for post types that don't support Activitypub. = 4.7.3 = diff --git a/tests/includes/class-test-hashtag.php b/tests/includes/class-test-hashtag.php index dd671fe70..4bf9503c4 100644 --- a/tests/includes/class-test-hashtag.php +++ b/tests/includes/class-test-hashtag.php @@ -7,6 +7,8 @@ namespace Activitypub\Tests; +use Activitypub\Collection\Followers; + /** * Test class for Activitypub Hashtag. * @@ -89,7 +91,7 @@ public function the_content_provider() { * @param string $message The error message. */ public function test_hashtag_conversion( $content, $excerpt, $expected_tags, $message ) { - $post_id = $this->factory->post->create( + $post_id = self::factory()->post->create( array( 'post_content' => $content, 'post_excerpt' => $excerpt, @@ -104,6 +106,25 @@ public function test_hashtag_conversion( $content, $excerpt, $expected_tags, $me } } + /** + * Test no hashtags for unsupported post types. + * + * @covers ::insert_post + */ + public function test_no_hashtags_for_unsupported_post_types() { + $post_id = self::factory()->post->create( + array( + 'post_content' => 'Testing #php and #programming', + 'post_type' => Followers::POST_TYPE, + ) + ); + + \Activitypub\Hashtag::insert_post( $post_id, get_post( $post_id ) ); + $tags = wp_get_post_tags( $post_id, array( 'fields' => 'names' ) ); + + $this->assertEmpty( $tags, 'Should not add hashtags to unsupported post types' ); + } + /** * Data provider for hashtag tests. *