Skip to content

Commit

Permalink
Merge pull request #21846 from Yoast/21354-prevnext-links-include-the…
Browse files Browse the repository at this point in the history
…-slug-twice-when-using-query-loop-block-on-non-homepage-content

fix adjacent_rel_url and tests
  • Loading branch information
pls78 authored Nov 26, 2024
2 parents ae05a9f + 66fd56b commit 74ea9e4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
47 changes: 35 additions & 12 deletions src/integrations/front-end-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ public function query_loop_next_prev( $html, $block ) {

/**
* Returns correct adjacent pages when Query loop block does not inherit query from template.
* Prioritizes existing prev and next links.
* Includes a safety check for full urls though it is not expected in the query pagination block.
*
* @param string $link The current link.
* @param string $rel Link relationship, prev or next.
Expand All @@ -306,21 +308,42 @@ public function query_loop_next_prev( $html, $block ) {
* @return string The correct link.
*/
public function adjacent_rel_url( $link, $rel, $presentation = null ) {
if ( $link === \home_url( '/' ) ) {
// Prioritize existing prev and next links.
if ( $link ) {
return $link;
}

if ( ( $rel === 'next' || $rel === 'prev' ) && ( ! \is_null( $this->$rel ) ) ) {
// Reconstruct url if it's relative.
if ( \class_exists( WP_HTML_Tag_Processor::class ) ) {
$processor = new WP_HTML_Tag_Processor( $this->$rel );
while ( $processor->next_tag( [ 'tag_name' => 'a' ] ) ) {
$href = $processor->get_attribute( 'href' );
if ( $href && \strpos( $href, '/' ) === 0 ) {
return $presentation->permalink . \substr( $href, 1 );
}
}
}
// Safety check for rel value.
if ( $rel !== 'next' && $rel !== 'prev' ) {
return $link;
}

// Check $this->next or $this->prev for existing links.
if ( \is_null( $this->$rel ) ) {
return $link;
}

$processor = new WP_HTML_Tag_Processor( $this->$rel );

if ( ! $processor->next_tag( [ 'tag_name' => 'a' ] ) ) {
return $link;
}

$href = $processor->get_attribute( 'href' );

if ( ! $href ) {
return $link;
}

// Safety check for full url, not expected.
if ( \strpos( $href, 'http' ) === 0 ) {
return $href;
}

// Check if $href is relative and append last part of the url to permalink.
if ( \strpos( $href, '/' ) === 0 ) {
$href_parts = \explode( '/', $href );
return $presentation->permalink . \end( $href_parts );
}

return $link;
Expand Down
26 changes: 13 additions & 13 deletions tests/WP/Frontend/Front_End_Integration_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,39 +124,39 @@ public static function data_provider_adjacent_rel_url() {
'next' => '<a href="/?query-1-page=4">Next</a>',
'expected' => '',
],
'next link' => [
'link' => 'https://example.org?query-1-page=4',
'query loop next link' => [
'link' => '',
'rel' => 'next',
'prev' => '<a href="/?query-1-page=2">Prev</a>',
'prev' => '',
'next' => '<a href="/?query-1-page=4">Next</a>',
'expected' => 'https://example.org/?query-1-page=4',
],
'prev link' => [
'link' => 'https://example.org?query-1-page=2',
'link' => '',
'rel' => 'prev',
'prev' => '<a href="/?query-1-page=2">Prev</a>',
'next' => '<a href="/?query-1-page=4">Next</a>',
'next' => '',
'expected' => 'https://example.org/?query-1-page=2',
],
'prev link is home' => [
'link has url' => [
'link' => 'https://example.org/',
'rel' => 'prev',
'prev' => '<a href="/?query-1-page=2">Prev</a>',
'next' => '<a href="/?query-1-page=4">Next</a>',
'expected' => 'https://example.org/?query-1-page=2',
'next' => '',
'expected' => 'https://example.org/',
],
'prev link is null' => [
'link' => 'https://example.org/',
'link' => '',
'rel' => 'prev',
'prev' => null,
'next' => '<a href="/?query-1-page=4">Next</a>',
'expected' => 'https://example.org/',
'next' => '',
'expected' => '',
],
'full url' => [
'link' => 'https://example.org/?query-1-page=2',
'link' => '',
'rel' => 'prev',
'prev' => '<a href="https://example.org/?query-1-page=2">Prev</a>',
'next' => '<a href="https://example.org/?query-1-page=4">Next</a>',
'next' => '',
'expected' => 'https://example.org/?query-1-page=2',
],
];
Expand Down

0 comments on commit 74ea9e4

Please sign in to comment.