Skip to content

Commit

Permalink
Rewrite tag links to local friend links
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Dec 20, 2024
1 parent b45c049 commit fe40f92
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions feed-parsers/class-feed-parser-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="mention hashtag"> 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;
}

Expand Down

0 comments on commit fe40f92

Please sign in to comment.