Skip to content
This repository has been archived by the owner on Sep 16, 2019. It is now read-only.

Fixes foundationpress_pagination() - Outputs valid HTML, output matches Foundation's docs. #1372

Merged
merged 3 commits into from
Apr 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 41 additions & 10 deletions library/foundation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,52 @@ function foundationpress_pagination() {
'total' => $wp_query->max_num_pages,
'mid_size' => 5,
'prev_next' => true,
'prev_text' => __( '«', 'foundationpress' ),
'next_text' => __( '»', 'foundationpress' ),
'prev_text' => '',
'next_text' => '',
'type' => 'list',
)
);

$paginate_links = str_replace( "<ul class='page-numbers'>", "<ul class='pagination text-center' role='navigation' aria-label='Pagination'>", $paginate_links );
$paginate_links = str_replace( '<li><span class="page-numbers dots">', "<li><a href='#'>", $paginate_links );
$paginate_links = str_replace( '</span>', '</a>', $paginate_links );
$paginate_links = str_replace( "<li><span class='page-numbers current'>", "<li class='current'>", $paginate_links );
$paginate_links = str_replace( "<li><a href='#'>&hellip;</a></li>", "<li><span class='dots'>&hellip;</span></li>", $paginate_links );
$paginate_links = preg_replace( '/\s*page-numbers/', '', $paginate_links );
// Display the pagination if more than one page is found.
if ( $paginate_links ) {

// Match patterns for preg_replace
$preg_find = [
'/\s*page-numbers\s*/', // Captures string 'page-numbers' and any whitespace before and after
"/\s*class=''/", // Captures any empty class attributes
'/<li><a class="prev" href="(\S+)">/', // '(\S+)' Captures href value for backreference
'/<li><a class="next" href="(\S+)">/', // '(\S+)' Captures href value for backreference
"/<li><span aria-current='page' class='current'>(\d+)<\/span><\/li>/", // '(\d+)' Captures page number for backreference
"/<li><a href='(\S+)'>(\d+)<\/a><\/li>/", // '(\S+)' Captures href value for backreference, (\d+)' Captures page number for backreference
];

// preg_replace replacements
$preg_replace = [
'',
'',
'<li class="pagination-previous"><a href="$1" aria-label="Previous page">', // '$1' Outputs backreference href value
'<li class="pagination-next"><a href="$1" aria-label="Next page">', // '$1' Outputs backreference href value
'<li class="current" aria-current="page"><span class="show-for-sr">You\'re on page </span>$1</li>', // '$1' Outputs backreference page number
'<li><a href="$1" aria-label="Page $2">$2</a>', // '$1' Ouputs backreference href, '$2' outputs backreference page number
];

// Match patterns for str_replace
$str_find = [
"<ul>",
'<li><span class="dots">&hellip;</span></li>',
];

// str_replace replacements
$str_replace = [
'<ul class="pagination text-center">',
'<li class="ellipsis" aria-hidden="true"></li>',
];

$paginate_links = preg_replace( $preg_find, $preg_replace, $paginate_links );
$paginate_links = str_replace( $str_find, $str_replace, $paginate_links );

$paginate_links = '<nav aria-label="Pagination">' . $paginate_links . '</nav>';

// Display the pagination if more than one page is found.
if ( $paginate_links ) {
echo $paginate_links;
}
}
Expand Down