diff --git a/feed-parsers/class-feed-parser-activitypub.php b/feed-parsers/class-feed-parser-activitypub.php index 7bbbcfb2..826178f7 100644 --- a/feed-parsers/class-feed-parser-activitypub.php +++ b/feed-parsers/class-feed-parser-activitypub.php @@ -1577,6 +1577,25 @@ function ( $m ) use ( &$protected_tags ) { $the_content = str_replace( array_keys( $protected_tags ), array_values( $protected_tags ), $the_content ); + // replace all links in with /friends/tag/tagname using the WP_HTML_Tag_Processor. + $processor = new \WP_HTML_Tag_Processor( $the_content ); + while ( $processor->next_tag( array( 'tag_name' => 'a' ) ) ) { + if ( ! $processor->get_attribute( 'href' ) ) { + continue; + } + if ( ! $processor->get_attribute( 'class' ) || false === strpos( $processor->get_attribute( 'class' ), 'hashtag' ) ) { + // Also consider URLs that contain the word hashtag like https://twitter.com/hashtag/WordPress. + if ( false === strpos( $processor->get_attribute( 'href' ), '/hashtag/' ) ) { + continue; + } + } + $path_parts = explode( '/', wp_parse_url( $processor->get_attribute( 'href' ), PHP_URL_PATH ) ); + $tag = array_pop( $path_parts ); + $processor->set_attribute( 'href', '/friends/tag/' . sanitize_title_with_dashes( $tag ) . '/' ); + } + + $the_content = $processor->get_updated_html(); + return $the_content; }