Skip to content

Commit

Permalink
fix(categories): fix pager urls (#2913)
Browse files Browse the repository at this point in the history
* fix(categories): fix pager urls

On a permalink structure like this /%category%/%postname%/ some fixes to
the pager was needed. Also guard against a post with the slug 'page' to
avoid confusion to routing.

* fix(categories): fix pager urls

Don't hardcode category base

* Don't hardcode category base and remove unneeded filter

* fix(categories): fix pager urls

* PR feedback adressed
* Use 'category' as default if base is empty

---------

Co-authored-by: Adam Boro <aborowski24@gmail.com>
  • Loading branch information
2 people authored and leogermani committed Feb 9, 2024
1 parent 4df9e9d commit bb7e534
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 deletions includes/class-category-pager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Newspack category pager fix.
*
* @package Newspack
*/

namespace Newspack;

defined( 'ABSPATH' ) || exit;

/**
* Newspack category pager.
*
* This class adds tweaks to sites where the permalink structure is "/%category%/%postname%/".
*
* See https://core.trac.wordpress.org/ticket/8905 and https://core.trac.wordpress.org/ticket/21209
*/
final class Category_Pager {
/**
* Initialize Hooks.
*/
public static function init() {
add_filter( 'paginate_links', [ __CLASS__, 'fix_category_pager' ] );
}

/**
* Filter callback for paginate_links.
*
* Filter the pager urls on category pages.
*
* For a category with the slug "kitten" for instance, the url /kitten will show the content that
* really should be on /category/kitten. On the /kitten page, WP will generate pager urls like "/kitten/page/2/"
* – this fixes the pager urls to be "/category/kitten/page/2/".
*
* @param string $link The pager link to filter.
*
* @return mixed|string
*/
public static function fix_category_pager( $link ) {
if ( ! is_category() || ! str_starts_with( get_option( 'permalink_structure' ), '/%category%/' ) ) {
return $link;
}

$category_base = get_option( 'category_base' );
if ( empty( $category_base ) ) {
$category_base = 'category';
}

$path = wp_parse_url( $link, PHP_URL_PATH );
if ( str_starts_with( $path, '/' . $category_base ) ) {
// No fixing needed.
return $link;
}

$new_path = '/' . $category_base . $path;

return str_replace( $path, $new_path, $link );
}
}

Category_Pager::init();
1 change: 1 addition & 0 deletions includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ private function includes() {

include_once NEWSPACK_ABSPATH . 'includes/class-handoff-banner.php';
include_once NEWSPACK_ABSPATH . 'includes/class-donations.php';
include_once NEWSPACK_ABSPATH . 'includes/class-category-pager.php';
include_once NEWSPACK_ABSPATH . 'includes/class-salesforce.php';
include_once NEWSPACK_ABSPATH . 'includes/class-pwa.php';
include_once NEWSPACK_ABSPATH . 'includes/class-starter-content.php';
Expand Down

0 comments on commit bb7e534

Please sign in to comment.