Skip to content

Commit

Permalink
enhance(pagi): Improve handling of queries (#13)
Browse files Browse the repository at this point in the history
* enhance(pagi): Remove unnecessary call to `get_posts` by using `found_posts` instead
* fix(paginator): Fix page, next and previous urls when query string exists by removing escaping through `get_pagenum_link()`
* fix(pagi): Remove unnecessary `is_tax` check that assumes we only have one term
* chore(pagi): Fix docblock param class on `setQuery()`

Co-authored-by: Brandon <[email protected]>
  • Loading branch information
davidwebca and Log1x authored Oct 24, 2022
1 parent 37bccb2 commit ff9f056
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 45 deletions.
31 changes: 25 additions & 6 deletions src/LengthAwarePaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function url($page)
$page = 1;
}

$url = get_pagenum_link($page);
$url = get_pagenum_link($page, false);

if (count($this->query) > 0) {
return $url .
Expand All @@ -54,10 +54,20 @@ public function url($page)
*/
public function nextPageUrl()
{
return next_posts(
count($this->items),
false
);
global $paged;

if (is_single()) {
return;
}

$paged = empty($paged) ? 1 : $paged;

$nextPage = (int) $paged + 1;
$maxPages = count($this->items);

if (! $maxPages || $maxPages >= $nextPage) {
return get_pagenum_link($nextPage, false);
}
}

/**
Expand All @@ -67,7 +77,16 @@ public function nextPageUrl()
*/
public function previousPageUrl()
{
return previous_posts(false);
global $paged;

if (is_single()) {
return;
}

$nextPage = (int) $paged - 1;
$nextPage = $nextPage < 1 ? 1 : $nextPage;

return get_pagenum_link($nextPage, false);
}

/**
Expand Down
42 changes: 3 additions & 39 deletions src/Pagi.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,64 +34,27 @@ class Pagi
*/
protected $perPage = 1;

/**
* Create a new Pagi instance.
*
* @return void
*/
public function __construct()
{
$this->items = collect();
}

/**
* Prepare the WordPress pagination.
*
* @return void
*/
protected function prepare()
{
$isGlobalQuery = false;

if (! isset($this->query)) {
$this->query = collect(
Arr::get($GLOBALS, 'wp_query')->query_vars ?? []
)->filter();

$isGlobalQuery = true;
$this->items = collect()->range(0, Arr::get($GLOBALS, 'wp_query')->found_posts);
}

if ($this->query->isEmpty()) {
return;
}

if ($isGlobalQuery) {
if (! is_search()) {
$this->query->put('post_type', get_post_type());
}

if (is_tax()) {
$this->query->put('tax_query', [[
'taxonomy' => $this->query->get('taxonomy'),
'terms' => $this->query->get('term'),
'field' => 'name',
]]);
}
}

$this->perPage = $this->query->get('posts_per_page');
$this->currentPage = max(1, absint(get_query_var('paged')));

$this->items = $this->items->make(
get_posts(
$this->query->put('posts_per_page', '-1')->all()
)
)->map(function ($item) {
return [
'id' => $item->ID,
'url' => get_the_permalink($item->ID)
];
});
}

/**
Expand All @@ -114,11 +77,12 @@ public function build()
/**
* Set the WordPress query.
*
* @param WP_Query
* @param \WP_Query
* @return void
*/
public function setQuery($query)
{
$this->items = collect()->range(0, $query->found_posts);
$this->query = collect(
$query->query_vars ?? []
)->filter();
Expand Down

0 comments on commit ff9f056

Please sign in to comment.