-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fix Comments blocks pagination #39227
Fix Comments blocks pagination #39227
Conversation
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @SantosGuillamot! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
I realized there is something not working yet. When "Newest" is used in the Comments Query Loop settings, but you have "First page displayed by default" in the Discussion settings, the link Steps to reproduce
|
Hey @SantosGuillamot, thank you for this PR! 😄 I tried to reproduce the problem you mentioned above, but I don't see any redirection. It seems to work just fine. By chance, are you testing it adding both the Comments Query Loop and the Post Comments block at the same time? |
I made a quick video reproducing it. https://www.loom.com/share/9b919a3e7bff459bb88958cb5fb85be5 These are the Discussion settings and the Comments Query Loop ones:
I have tested it both ways and it is happening in both cases. I have been taking a look and I think the redirection is happening in the redirect_canonical function. Maybe in the line 503. |
I haven't tested this properly yet, but another path to explore could be to use something similar to this:
This function would basically change the Global option If we are able to run this before the redirect and the next/previous link functions, it could be easier to fix. Maybe just by adding a couple of conditionals in the core functions could solve the issues, and we don't have to refactor them. Any thoughts on this @c4rl0sbr4v0 @DAreRodz ? Is it worth testing it? EDIT: I have just realized this could cause problems if there is more than one Comments Query Loop |
Hey @SantosGuillamot, using filters could work, as long as you add the filter right before calling the function that applies it (in this case, the next/previous link functions), and then remove the filter right after. It also would make sense, because the block attributes are actually overwriting the options from the database. 🙂 Anyway, I think I found an even more difficult problem to solve. 😓 When you submit a comment using the Post Comments Form block, the 302 redirection fails if there are different “per page” values between the database and the Comments Query Loop. I've recorded a video to show an example. Discussion Settings ‹ gutenberg — WordPress - 8 March 2022 - Watch Video We would need a way to pass a The thing is that there's no way to know if the request was sent from a specific Comment Query Loop, and what attributes it had. I don't know. Perhaps we have to drop support for custom attributes in the Comment Query Loop, at least until we find a solution to this issue. 🙁 |
lib/compat/experimental/blocks.php
Outdated
* | ||
* @return string Returns the comments page number link URL. | ||
*/ | ||
function gutenberg_get_comments_pagenum_link( $block, $pagenum = 1 ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the goal for this function to refactor and replace Core's get_comments_pagenum_link()
?
-
If yes, it'll break backwards-compatibility due to the function parameter signature change of the first param being
$block
and missing the$max_page
param. -
If no, is the intend for this to be a new global function in Core that is only for block content?
I'm thinking more about a Pagination API that supports both block and non-block content, thereby merging Core's and Gutenberg's functionality needs together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question for the other functions in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've come to the same conclusion as @hellofromtonya.
IMO, if these new functions (i.e. gutenberg_get_comments_pagenum_link
, gutenberg_get_next_comments_link
, gutenberg_get_previous_comments_link
) will have to be backported to Core later, we need to change their names to not confuse them with existing get_comments_pagenum_link
, get_previous_comments_link
, and get_previous_comments_link
Core functions.
We need to pass
I agree that we can remove this and concentrate on finishing the blocks. However, we must open a new issue that clearly explains the challenges that need to be solved to add these options back so that this work can be resumed later by any other contributor. |
Potential solutionI've been taking a look again at the code and I believe I came up with a solution where changing the core functions isn't needed. If I am not mistaken, most of the problems found were caused because the In order to solve this, the query var could be define in the build_comment_query_vars_from_block function, as the value is already calculated in these lines of code. It could be as simple as adding something like this right after that: if ('' == $page && $comment_args['paged']){
set_query_var('cpage', $comment_args['paged']);
} I've tested it and it seems to solve all the issues found when the settings are inherit from the WordPress discussion settings. What do you think, would that make sense? Having said that, there are still a couple of issues when the settings aren't inherit. Issues left when the Comments Query Loop has its own settingsIf we go with the solution mentioned above, there would still exist a couple of problems detected when the Comments Query Loop settings aren't synced with the global ones.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, that's strange but I cannot reproduce this bug using the latest (trunk) versions of WordPress and Gutenberg.
https://cldup.com/zup6O9fQie.mov
Am I missing something, @SantosGuillamot?
I tested the use case of the block's settings being different from the global settings. I can reproduce the issue. Test ReportEnv
Setup and Testing Steps
ResultsWhen on Values:
Call stack
Thinking hereIt's not due to What about classic themes like TT0 and TT1?TT1 and TT0 are using Next steps
|
Thanks a lot for the triage 🙂
The issue with the links happens when the settings are different in the global settings and the Comments Query Loop:
Moreover, the wrong link is in the "Newer Comments", the numbers you click seem to work properly.
To reproduce this specific use case you have to go to the Discussion settings and set the "last page is displayed by default". If you navigate directly to the post ( |
@SantosGuillamot Thank you very much for the reply. Yes, I'm able to reproduce it now. |
As mentioned in previous comments, I have reverted the changes to the core functions and I defined the |
7d423b5
to
0f11e00
Compare
I've tried to test the recent changes. |
Hi @anton-vlasenko, Thank you for your time on testing. We need to update the description. As we have seen in the discussion, we reverted the changes so we only fix the pagination when we are inheriting the settings ( we will hide or remove the option of adding custom settings to the comment query loop, as it is quite trickier than expected, and will need some investigation and fixes like you already mentioned) So, in this case, the testing instructions would be [the same as here] (#39227 (comment)) but:
Then, the older and newer links should both work as expected, with these options on discussion settings.
If you have any questions, I will be glad to provide more info 🎉 |
I am able to reproduce the issue of the pagination links being out of sync when inheriting the global discussion settings ✅ And using this PR does resolve the issue for me ✅ tl;dr Findings:
|
Test Report:
|
Test Report:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job @SantosGuillamot! A few change suggestions to make the code more readable and guard it for the future.
@anton-vlasenko Can you repeat the 2 test scenarios (#39227 (comment) and #39227 (comment)) to see if you can reproduce and if the PR resolves it for you? Want to make sure you concur with my findings. |
Comments Query Loop Pagination TestThis is a test of the Comments Query Loop block from tl;dr ✅The latest PR update addresses the issue where the "Newer Comments" link was displayed incorrectly. The fix now correctly displays the "Older Comments" link, with correct URL, when comments are ordered ascending or descending, with the last ("newest") page of comments displayed by default. This confirms @hellofromtonya's prior test results. Preparation
(Or manually publish a new post and add 10 comments.)
🏃 Test (Before Applying PR)
Results ❌
Expected
🏃 Test (AFTER Applying PR)
Results ✅
|
Test Report:
Env:
Steps to reproduce:Follow Test ResultsThe results of my testing are entirely consistent with the results from the test reports above (test report 1, test report 2). |
ac9ff34
to
de6b19d
Compare
@@ -60,7 +60,12 @@ function build_comment_query_vars_from_block( $block ) { | |||
} elseif ( 'oldest' === $default_page ) { | |||
$comment_args['paged'] = 1; | |||
} elseif ( 'newest' === $default_page ) { | |||
$comment_args['paged'] = ( new WP_Comment_Query( $comment_args ) )->max_num_pages; | |||
$comment_args['paged'] = (int) ( new WP_Comment_Query( $comment_args ) )->max_num_pages; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type casting the max_num_page
property is appropriate here ✅ Well done @c4rl0sbr4v0 🥇
Let me explain.
The max_num_pages
property is declared as an integer
. BUT it's calculation uses ceil
https://www.php.net/manual/en/function.ceil.php which is then float
. This is an issue in WordPress Core's WP_Comment_Query
class. But as it's a query class, likely it's not "quickly" fixable. Why? Any changes to anything query move slowly on purpose to maximize soak time (for testing and feedback). Why? Query functionality is mission critical and any changes are risky at the scale of 43% of the web. So by design changes happen slowly (often over several release cycles) in WP Core.
Is float to int conversion an issue in PHP 8.1+? No, not when it's an explicit int
casting. No deprecation notice will be thrown and currently there are no plans for fatal error when PHP 9 lands in the future. See details here https://php.watch/versions/8.1/deprecate-implicit-conversion-incompatible-float-string.
To finish up this PR and get it merged, a few more things to resolve:
|
This reverts commit 81f6f0f.
de6b19d
to
a5953bc
Compare
Hi there! I'm going to merge this PR and make another one for the e2e tests. So the PR is cleaner, as we have to add some discussion options to the testing utils. #39502 |
Hey @c4rl0sbr4v0 - Aren't there risks in merging code that does not have automated tests? Typically, I'd expect tests to be bundled with the code changes, i.e. one PR. Why?
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code changes:
- fix the reported issue, confirmed by 3 separate test reports ✅
- are appropriate and do comply with WordPress Core's context and standards ✅
Would like to see automated tests bundled with these changes to document the expected behavior and for historical context. However, as noted, more work is needed in testing utils to build the e2e tests for this PR which would be outside the scope of this PR. A follow-up PR is already in the works to add the tests.
I think it's okay to go ahead and merge this PR with a note to quickly follow-up with automated tests. Approving 👍
Great job everyone 👏
Yep, usually is better to add the tests bundled with the code. But in this case, we will need to update some e2e utils functions in order to work with Discussion Comments, and, also, create the boilerplate and the standard tests for the entire Thanks for the review! You all did an awesome job! 🚀 |
Congratulations on your first merged pull request, @SantosGuillamot! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts: https://profiles.wordpress.org/me/profile/edit/ And if you don't have a WordPress.org account, you can create one on this page: https://login.wordpress.org/register Kudos! |
What
Make comments pagination links work as they should be when we have these conditions:
Why
Both "Older Comments" and "Newer Comments" are not working as expected inside the
Comment Query Loop
.If we enable pagination on Discussion Settings, before the PR we have:
Older/Newer Comments URL, depending on what we have chosen on
Discussion Settings
(first or last page as the default for Comments Pagination), is not pointing to the correct URL. It makes comments pagination navigation inconsistent.The reason for the bug is that we don't have the query var
cpage
defined and we are creating aWP_Comment_Query( $comment_args )
with pagination arguments. In this case, the pagination link functions require this query var in order to know in which page of the comments pagination is the user when she lands on the default page/post view.How
Defining a default value for
cpage
, being the maximum number of pages or 0, depending on the Discussion setting chosen (show the first or the last page).This way, when the user lands on a page/post without the comment pagination defined, it will land with the default value, so comments pagination links can work as expected.
Testing Instructions
There are mainly two cases to test here:
Types of changes
Bug fix