Skip to content

Commit df1c645

Browse files
authored
Hashtag: Ignore tags within HTML attributes (like hex colors) (#1037)
Fixes #955.
1 parent fb0aa57 commit df1c645

File tree

4 files changed

+95
-8
lines changed

4 files changed

+95
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Fixed
11+
12+
* Prevent hex color codes in HTML attributes from being added as post tags.
13+
814
## [4.3.0] - 2024-12-02
915

1016
### Added

includes/class-hashtag.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,14 @@ public static function filter_activity_object( $activity ) {
5555
public static function insert_post( $post_id, $post ) {
5656
$tags = array();
5757

58-
if ( \preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $post->post_content, $match ) ) {
59-
$tags = array_merge( $tags, $match[1] );
60-
}
58+
// Skip hashtags in HTML attributes, like hex colors.
59+
$content = wp_strip_all_tags( $post->post_content . "\n" . $post->post_excerpt );
6160

62-
if ( \preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $post->post_excerpt, $match ) ) {
63-
$tags = array_merge( $tags, $match[1] );
61+
if ( \preg_match_all( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', $content, $match ) ) {
62+
$tags = array_unique( $match[1] );
6463
}
6564

66-
$tags = \implode( ', ', $tags );
67-
68-
\wp_add_post_tags( $post->ID, $tags );
65+
\wp_add_post_tags( $post->ID, \implode( ', ', $tags ) );
6966
}
7067

7168
/**

readme.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ For reasons of data protection, it is not possible to see the followers of other
132132

133133
== Changelog ==
134134

135+
= Unreleased =
136+
137+
* Fixed: Prevent hex color codes in HTML attributes from being added as post tags.
138+
135139
= 4.3.0 =
136140

137141
* Added: A `pre_activitypub_get_upload_baseurl` filter

tests/class-test-activitypub-hashtag.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,84 @@ public function the_content_provider() {
7373
array( $pre, $pre ),
7474
);
7575
}
76+
77+
/**
78+
* Tests auto-converting hashtags to tags.
79+
*
80+
* @see https://github.com/Automattic/wordpress-activitypub/issues/955
81+
* @dataProvider hashtag_provider
82+
* @covers ::insert_post
83+
*
84+
* @param string $content The post content.
85+
* @param string $excerpt The post excerpt.
86+
* @param string[] $expected_tags The expected tags.
87+
* @param string $message The error message.
88+
*/
89+
public function test_hashtag_conversion( $content, $excerpt, $expected_tags, $message ) {
90+
$post_id = $this->factory->post->create(
91+
array(
92+
'post_content' => $content,
93+
'post_excerpt' => $excerpt,
94+
)
95+
);
96+
97+
\Activitypub\Hashtag::insert_post( $post_id, get_post( $post_id ) );
98+
$tags = wp_get_post_tags( $post_id, array( 'fields' => 'names' ) );
99+
100+
foreach ( $expected_tags as $tag ) {
101+
$this->assertContains( $tag, $tags, $message );
102+
}
103+
}
104+
105+
/**
106+
* Data provider for hashtag tests.
107+
*
108+
* @return array[] The data.
109+
*/
110+
public function hashtag_provider() {
111+
return array(
112+
'basic_hashtags' => array(
113+
'Testing #php and #programming',
114+
'',
115+
array( 'php', 'programming' ),
116+
'Basic hashtags should be converted',
117+
),
118+
'hashtags_in_attributes' => array(
119+
'<div style="color: #fff">#validtag</div>',
120+
'',
121+
array( 'validtag' ),
122+
'Hashtags in HTML attributes should be ignored',
123+
),
124+
'mixed_content' => array(
125+
'Color is #red <span style="color: #ff0000">#valid</span> #blue',
126+
'',
127+
array( 'red', 'blue', 'valid' ),
128+
'Should handle mixed content correctly',
129+
),
130+
'hex_in_text' => array(
131+
'<span style="color: #ff0000">#f00</span> #fff #000000',
132+
'',
133+
array( 'f00', 'fff', '000000' ),
134+
'Hex colors in text should be converted',
135+
),
136+
'excerpt_tags' => array(
137+
'',
138+
'Testing #excerpt with #tags',
139+
array( 'excerpt', 'tags' ),
140+
'Should process excerpt hashtags',
141+
),
142+
'multiple_attributes' => array(
143+
'<div data-color="#123" style="border: 1px solid #456">#valid</div>',
144+
'',
145+
array( 'valid' ),
146+
'Should ignore multiple attribute hashtags',
147+
),
148+
'quotes_in_content' => array(
149+
'Here is a "#quoted" #tag',
150+
'',
151+
array( 'tag' ),
152+
'Should handle quotes in content correctly',
153+
),
154+
);
155+
}
76156
}

0 commit comments

Comments
 (0)