Skip to content

Commit

Permalink
Incoming Feed Items: Fix in-article hash links (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk authored Jan 2, 2025
1 parent 2336ac2 commit 06fdf72
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
7 changes: 5 additions & 2 deletions feed-parsers/class-feed-parser-simplepie.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public function process_items( $items, $url ) {
array(
'permalink' => $item->get_permalink(),
'title' => $title,
'content' => $this->convert_relative_urls_to_absolute_urls( $item->get_content(), $url ),
'content' => $this->convert_relative_urls_to_absolute_urls( $item->get_content(), $item->get_permalink() ),
)
);

Expand Down Expand Up @@ -347,7 +347,10 @@ public function process_items( $items, $url ) {
}

if ( is_object( $item->get_author() ) ) {
$feed_item->author = \wp_strip_all_tags( $item->get_author()->name );
$feed_item->author = $item->get_author()->name;
// Strip HTML tags, even if they are escaped.
$feed_item->author = htmlspecialchars_decode( $feed_item->author, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 );
$feed_item->author = \wp_strip_all_tags( $feed_item->author );
}

$feed_item->date = $item->get_gmdate( 'U' );
Expand Down
23 changes: 19 additions & 4 deletions feed-parsers/class-feed-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,34 @@ public function discover_available_feeds( $content, $url ) { // phpcs:ignore Gen
* Convert relative URLs to absolute ones in incoming content.
*
* @param string $html The html.
* @param string $url The url of the feed.
* @param string $permalink The permalink of the feed.
*
* @return string The HTML with URLs replaced to their absolute represenation.
*/
public function convert_relative_urls_to_absolute_urls( $html, $url ) {
public function convert_relative_urls_to_absolute_urls( $html, $permalink ) {
if ( ! $html ) {
$html = '';
}

// Strip off the hash.
$permalink = strtok( $permalink, '#' );

// For now this only converts links and image srcs.
return preg_replace_callback(
'~(src|href)=(?:"([^"]+)|\'([^\']+))~i',
function ( $m ) use ( $url ) {
return str_replace( $m[2], Mf2\resolveUrl( $url, $m[2] ), $m[0] );
function ( $m ) use ( $permalink ) {
if ( str_starts_with( $m[2], '#' ) ) {
// Don't update hash-only links.
return $m[0];
}

if ( str_starts_with( $m[2], $permalink . '#' ) ) {
// Remove absolute URL from hashes.
return str_replace( $permalink, '', $m[0] );
}

// Convert relative URLs to absolute ones.
return str_replace( $m[2], Mf2\resolveUrl( $permalink, $m[2] ), $m[0] );
},
$html
);
Expand Down

0 comments on commit 06fdf72

Please sign in to comment.