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

[Block Library - Query Pagination Numbers]: Fix first page's link #33629

Merged
merged 2 commits into from
Aug 9, 2021

Conversation

ntsekouras
Copy link
Contributor

Description

Fixes: #32792

paginate_links doesn't use the provided format when the page is 1. This is great for the main query as it removes the extra query params making the URL shorter, but in the case of multiple custom queries is problematic. It results in returning an empty link which ends up with a link to the current page.

A way to address this is to add a fake query arg with no value that is the same for all custom queries. This way the link is not empty and preserves all the other existent query args.

Reference: https://developer.wordpress.org/reference/functions/paginate_links/
$link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); $link = str_replace( '%#%', $n, $link );

How has this been tested?

  1. In page with many Query Loop blocks with QueryPaginationNumbers blocks check that by clicking another page number first, the pagination link for the first page (1), works as expected.
  2. This can be tested with a single custom Query in a page as well, but it's better to have multiple Query Loop blocks which can be both custom and ones that inherit the global query.

Notes

  1. The issue can be replicated only when we have no other query args, because in that case the link is not empty - thus the hacky approach here to add an empty query arg.

I couldn't find any better alternative so a more elegant solution would be more than welcomed. We could also look at making some changes in core paginate_links but needs more exploration there. For example we could extend the paginate_links filter by passing the current page number ($n) and use this filter in the block??(🤔 ).

@ntsekouras ntsekouras added [Type] Bug An existing feature does not function as intended [Block] Query Pagination Affects the Query Pagination Block - used for pagination within the Query Loop Block labels Jul 22, 2021
@ntsekouras ntsekouras requested a review from a team July 22, 2021 11:17
@ntsekouras ntsekouras self-assigned this Jul 22, 2021
@ntsekouras ntsekouras requested a review from ajitbohra as a code owner July 22, 2021 11:17
*
* @see https://developer.wordpress.org/reference/functions/paginate_links/
*/
$paginate_args['add_args'] = array( 'cst' => '' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does cst stand for here, is it just a random useless param?

Copy link
Contributor

@youknowriad youknowriad Jul 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel ideally, we should add a dedicated argument/option in paginate_links to disable the default behavior of skipping format. That said, this is fine a stopgap solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this causes ?cst to be added to every link? 🤔 Seems a bit unexpected/weird.

Why not just filter paginate_links_output?

Theoretically it could cause some confusion if someone's already using a query arg with this name and expects it to do something else.

Could you perhaps share what the expected output is with this change?

Agreed that this should be fixed with a patch in core.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does cst stand for here, is it just a random useless param?

Yes.

Theoretically it could cause some confusion if someone's already using a query arg with this name

We can change this to whatever. That's why I didn't used a single character, as it would increase those chances. I'm open to any suggestions.

Why not just filter paginate_links_output?

This would require parsing the html string and I'm not sure it worths it and how complicated it can get with other filters having been applied before, to other parts of this html construction. An extra variable in core should be the best solution probably.

@ntsekouras
Copy link
Contributor Author

Should we land this as a stopgap solution then? 🤔 I'll create a core patch as well.

@ntsekouras
Copy link
Contributor Author

Created a core patch here: WordPress/wordpress-develop#1540

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should land this since Gutenberg needs to support at least two WP versions but maybe we should clarify that we can remove it once the core patch lands in an inline comment.

@ntsekouras ntsekouras merged commit 6c3c97c into trunk Aug 9, 2021
@ntsekouras ntsekouras deleted the fix/query-pagination-first-page-link branch August 9, 2021 11:27
@github-actions github-actions bot added this to the Gutenberg 11.3 milestone Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Query Pagination Affects the Query Pagination Block - used for pagination within the Query Loop Block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pagination query block issue
3 participants