Skip to content

Commit

Permalink
Improve tags regex
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Jun 1, 2023
1 parent 7135cb6 commit cc8b2e6
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions includes/class-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function register_hooks() {
add_filter( 'pre_option_rss_use_excerpt', array( $this, 'feed_use_excerpt' ), 90 );
add_filter( 'friends_early_modify_feed_item', array( $this, 'apply_early_feed_rules' ), 10, 3 );
add_filter( 'friends_modify_feed_item', array( $this, 'apply_feed_rules' ), 10, 3 );
add_filter( 'friends_modify_feed_item', array( $this, 'extract_tags' ), 10, 3 );
add_filter( 'friends_modify_feed_item', array( $this, 'extract_tags' ) );

add_action( 'rss_item', array( $this, 'feed_additional_fields' ) );
add_action( 'rss2_item', array( $this, 'feed_additional_fields' ) );
Expand Down Expand Up @@ -442,11 +442,16 @@ public function validate_feed_catch_all( $catch_all ) {
return $catch_all;
}

public function extract_tags( $item, User_Feed $feed = null, User $friend_user = null ) {
// This is a variation of the ACTIVITYPUB_HASHTAGS_REGEXP.
if ( \preg_match_all( '/(?:(?<=\s)|(?<=<p>)|(?<=<br>)|^)#(\w+)(?:(?=\s|[[:punct:]]|$))/ui', \wp_strip_all_tags( $item->post_content ), $match ) ) {
public function extract_tags( $item ) {
// (?<!&) avoid to match HTML entities like &#039;.
// [\w-] to allow tags with dashes in them.
if ( \preg_match_all( '/(?<!&|\w)#([\w-]+)/ui', \wp_strip_all_tags( $item->post_content ), $match ) ) {
$tags = \implode( ', ', $match[1] );
$item->tags = $tags;
if ( is_array( $item->tags ) ) {
$item->tags = array_merge( $item->tags, $tags );
} else {
$item->tags = $tags;
}
}

return $item;
Expand Down Expand Up @@ -633,6 +638,7 @@ public function process_incoming_feed_items( array $items, User_Feed $user_feed
$modified_posts[] = $post_id;
}
}
wp_set_post_tags( $post_id, $item->tags );
} else {
$post_data['post_type'] = Friends::CPT;
$post_data['post_date_gmt'] = $item->date;
Expand Down

0 comments on commit cc8b2e6

Please sign in to comment.