Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom pagination (resolves #188) #209

Merged
merged 29 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8ed90a1
fix: use new tag buttons in current filters, use Pinecone search form
greatislander Feb 18, 2020
11149a4
feat: allow search results to be filtered
greatislander Feb 19, 2020
832f1b7
fix: only search resources
greatislander Feb 19, 2020
0fbfd01
feat: add save search button and search term display
greatislander Feb 19, 2020
e81f059
Merge branch 'dev' into add/search-results
greatislander Feb 20, 2020
80b57d3
fix: don't run code twice on search results page
greatislander Feb 20, 2020
a178762
feat: saved search functionality
greatislander Feb 20, 2020
9d122b0
Merge branch 'dev' into add/search-results
greatislander Feb 20, 2020
95d50b6
fix: notification positioning
greatislander Feb 21, 2020
1bb1881
feat: limit saved searches to 25
greatislander Feb 21, 2020
fb961dc
feat: limit saved searches to 50 instead of 25
greatislander Feb 21, 2020
9549bf5
fix: resolve PHPCS error
greatislander Feb 21, 2020
e7a9600
fix: paginate search results
greatislander Feb 25, 2020
40974eb
Merge branch 'dev' into add/search-results
greatislander Feb 26, 2020
6d39d3f
Merge branch 'dev' into add/search-results
greatislander Feb 27, 2020
35ce02e
Merge branch 'dev' into add/search-results
greatislander Feb 28, 2020
434e0b2
fix: set post type in pre_get_posts
greatislander Feb 28, 2020
5dfde3d
Revert "fix: set post type in pre_get_posts"
greatislander Feb 28, 2020
886e1b2
fix: move str_replace into controllers
greatislander Feb 28, 2020
5e4026c
feat: add current and total params for paginating non-WP queries
greatislander Feb 28, 2020
288f1a2
fix: revert precommit hook
greatislander Feb 28, 2020
ceed4a7
fix: remove unnecessary library
greatislander Feb 28, 2020
8a1b67e
feat: retrieve current page
greatislander Feb 28, 2020
f97a4f5
Merge branch 'add/search-results' into add/custom-pagination
greatislander Feb 28, 2020
0a1522e
feat: rough order for pagination output
greatislander Mar 2, 2020
f577141
Merge branch 'dev' into add/custom-pagination
greatislander Mar 2, 2020
9f4bac9
feat: add custom pagination
greatislander Mar 2, 2020
e15c322
fix: linting errors
greatislander Mar 2, 2020
568f51c
Merge branch 'dev' into add/custom-pagination
greatislander Mar 6, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 242 additions & 7 deletions app/Controllers/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace App\Controllers;

use League\Uri\Components\Query;
use League\Uri\Uri;
use League\Uri\UriModifier;
use Sober\Controller\Controller;

use function \CoopLibraryFramework\Internationalization\get_language_list;
Expand Down Expand Up @@ -71,6 +68,12 @@ public function foundPosts()
return $wp_query->found_posts;
}

public function totalPages()
{
global $wp_query;
return isset($wp_query->max_num_pages) ? $wp_query->max_num_pages : 1;
}

public function maxPages()
{
global $wp_query;
Expand All @@ -80,8 +83,241 @@ public function maxPages()
public function currentPage()
{
global $wp_query;
$current_page = $wp_query->query_vars['paged'] > 1 ? $wp_query->query_vars['paged'] : 1;
return $current_page;
return $wp_query->query_vars['paged'] > 1 ? intval($wp_query->query_vars['paged']) : 1;
}

public static function paginateLinks($current = false, $total = false, $args = [])
{
global $wp_query, $wp_rewrite;

// Setting up default values based on the current URL.
$pagenum_link = html_entity_decode(get_pagenum_link());
$url_parts = explode('?', $pagenum_link);

// Get max pages and current page out of the current query, if available.
if (!$total) {
$total = isset($wp_query->max_num_pages) ? $wp_query->max_num_pages : 1;
}
if (!$current) {
$current = get_query_var('paged') ? intval(get_query_var('paged')) : 1;
}

// Append the format placeholder to the base URL.
$pagenum_link = trailingslashit($url_parts[0]) . '%_%';

// URL base depends on permalink settings.
$format = $wp_rewrite->using_index_permalinks() && ! strpos($pagenum_link, 'index.php') ? 'index.php/' : '';
$format .= $wp_rewrite->using_permalinks() ?
user_trailingslashit($wp_rewrite->pagination_base . '/%#%', 'paged') :
'?paged=%#%';

$defaults = array(
'base' => $pagenum_link, // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
'format' => $format, // ?page=%#% : %#% is replaced by the page number
'total' => $total,
'current' => $current,
'aria_current' => 'page',
'show_all' => false,
'prev_next' => true,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'end_size' => 1,
'mid_size' => 2,
'type' => 'plain',
'add_args' => array(), // array of query args to add
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => '',
);

$args = wp_parse_args($args, $defaults);

if (! is_array($args['add_args'])) {
$args['add_args'] = array();
}

// Merge additional query vars found in the original URL into 'add_args' array.
if (isset($url_parts[1])) {
// Find the format argument.
$format = explode('?', str_replace('%_%', $args['format'], $args['base']));
$format_query = isset($format[1]) ? $format[1] : '';
wp_parse_str($format_query, $format_args);

// Find the query args of the requested URL.
wp_parse_str($url_parts[1], $url_query_args);

// Remove the format argument from the array of query arguments, to avoid overwriting custom format.
foreach ($format_args as $format_arg => $format_arg_value) {
unset($url_query_args[ $format_arg ]);
}

$args['add_args'] = array_merge($args['add_args'], urlencode_deep($url_query_args));
}

// Who knows what else people pass in $args
$total = (int) $args['total'];
if ($total < 2) {
return;
}
$current = (int) $args['current'];
$end_size = (int) $args['end_size']; // Out of bounds? Make it the default.
if ($end_size < 1) {
$end_size = 1;
}
$mid_size = (int) $args['mid_size'];
if ($mid_size < 0) {
$mid_size = 2;
}

$add_args = $args['add_args'];
$r = '';
$page_links = array();
$dots = false;

if ($args['prev_next'] && $current && 1 < $current) :
$link = str_replace('%_%', 2 == $current ? '' : $args['format'], $args['base']);
$link = str_replace('%#%', $current - 1, $link);
if ($add_args) {
$link = add_query_arg($add_args, $link);
}
$link .= $args['add_fragment'];

$page_links[] = sprintf(
'<a class="link link--pagination prev" href="%s">%s</a>',
/**
* Filters the paginated links for the given archive pages.
*
* @since 3.0.0
*
* @param string $link The paginated link URL.
*/
esc_url(apply_filters('paginate_links', $link)),
$args['prev_text']
);
endif;

for ($n = 1; $n <= $total; $n++) :
if ($n == $current) :
$page_links[] = sprintf(
'<span aria-current="%s" class="page current">%s</span>',
esc_attr($args['aria_current']),
$args['before_page_number'] . number_format_i18n($n) . $args['after_page_number']
);

$dots = true;
else :
if ($args['show_all'] ||
( $n <= $end_size ||
( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) ||
$n > $total - $end_size
)) :
$link = str_replace('%_%', 1 == $n ? '' : $args['format'], $args['base']);
$link = str_replace('%#%', $n, $link);
if ($add_args) {
$link = add_query_arg($add_args, $link);
}
$link .= $args['add_fragment'];

$page_links[] = sprintf(
'<a class="link link--pagination" href="%s">%s</a>',
/** This filter is documented in wp-includes/general-template.php */
esc_url(apply_filters('paginate_links', $link)),
$args['before_page_number'] . number_format_i18n($n) . $args['after_page_number']
);

$dots = true;
elseif ($dots && ! $args['show_all']) :
$page_links[] = '<span class="page dots">' . __('&hellip;') . '</span>';

$dots = false;
endif;
endif;
endfor;

if ($args['prev_next'] && $current && $current < $total) :
$link = str_replace('%_%', $args['format'], $args['base']);
$link = str_replace('%#%', $current + 1, $link);
if ($add_args) {
$link = add_query_arg($add_args, $link);
}
$link .= $args['add_fragment'];

$page_links[] = sprintf(
'<a class="link link--pagination next" href="%s">%s</a>',
/** This filter is documented in wp-includes/general-template.php */
esc_url(apply_filters('paginate_links', $link)),
$args['next_text']
);
endif;

switch ($args['type']) {
case 'array':
return $page_links;

case 'list':
$r .= "<ul class='page-numbers'>\n\t<li>";
$r .= join("</li>\n\t<li>", $page_links);
$r .= "</li>\n</ul>\n";
break;

default:
$r = join("\n", $page_links);
break;
}

return $r;
}

public static function getPagination($args = [])
{
$navigation = '';

if (!isset($args['total'])) {
$args['total'] = isset($wp_query->max_num_pages) ? $wp_query->max_num_pages : 1;
}

// Don't print empty markup if there's only one page.
if ($args['total'] > 1) {
// Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
if (! empty($args['screen_reader_text']) && empty($args['aria_label'])) {
$args['aria_label'] = $args['screen_reader_text'];
}

$args = wp_parse_args(
$args,
array(
'mid_size' => 1,
'prev_text' => sprintf(
'&lsaquo; <span class="screen-reader-text">%s</span>',
__('previous', 'coop-library')
),
'next_text' => sprintf(
' <span class="screen-reader-text">%s</span> &rsaquo;',
__('next', 'coop-library')
),
'screen_reader_text' => __('Resources navigation', 'coop-library'),
'aria_label' => __('resources', 'coop-library'),
)
);

// Make sure we get a string back. Plain is the next best thing.
if (isset($args['type']) && 'array' == $args['type']) {
$args['type'] = 'plain';
}

// Set up paginated links.
$links = App::paginateLinks($current, $total, $args);
if ($links) {
$navigation = _navigation_markup(
$links,
'pagination',
$args['screen_reader_text'],
$args['aria_label']
);
}
}

return $navigation;
}

public static function totalPosts($post_type = null)
Expand All @@ -95,8 +331,7 @@ public static function totalPosts($post_type = null)

public static function sortUrl($order_by)
{
$uri = Uri::createFromString($_SERVER['REQUEST_URI']);
return UriModifier::mergeQuery($uri, "order_by=$order_by");
return add_query_arg('order_by', $order_by);
}

public static function title()
Expand Down
3 changes: 2 additions & 1 deletion app/Controllers/PageFavorites.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public function favorites()
'post_type' => 'lc_resource',
'post__in' => explode(',', $_COOKIE['favorites']),
'orderby' => 'title',
'posts_per_page' => -1,
'posts_per_page' => 12,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
'order' => 'asc',
'lang' => '',
]);
Expand Down
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
"php": ">=7.1",
"composer/installers": "~1.0",
"illuminate/support": "5.6.*",
"league/uri": "^6.0",
"league/uri-components": "^2.1",
"log1x/blade-svg-sage": "^2.0",
"log1x/sage-directives": "^1.1",
"roots/sage-lib": "~9.0.9",
Expand Down
Loading