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

Pass the comments query paged arg to functions get_next_comments_link and get_previous_comments_link #63698

Conversation

SantosGuillamot
Copy link
Contributor

@SantosGuillamot SantosGuillamot commented Jul 18, 2024

What?

According to this report, the solution introduced in this pull request to solved the mentioned bug, where cpage was updated based on the query, is causing problems with plugins relying on it being 0 or undefined.

This pull request explores another alternative to solve the initial bug without modifying cpage.

It needs to be tested with this other core pull request, which modifies the relevant functions to accept the new parameter.

How?

Pass the paged argument from the query to get_next_comments_link and get_previous_comments_link so they only read the cpage when it is not defined. A check like the following must be added in the core functions:

	if ( empty( $page ) ) {
		$page = get_query_var( 'cpage' );
	}

Testing Instructions

It needs to be tested with this other core pull request, which modifies the relevant functions to accept the new parameter.

  1. Add comments to a post.
  2. Go to Settings -> Discussion and set "Break comments into pages with 2 top-level comments per page and the
    last page displayed by default.
  3. Go to the post and check that the pagination links are correct.

@SantosGuillamot SantosGuillamot added [Type] Bug An existing feature does not function as intended [Block] Comments Affects the Comments Block - formerly known as Comments Query Loop labels Jul 18, 2024
@SantosGuillamot SantosGuillamot changed the title Pass the comments query paged arg to functions Pass the comments query paged arg to functions get_next_comments_link and get_previous_comments_link Jul 18, 2024
@gziolo
Copy link
Member

gziolo commented Aug 20, 2024

Is there a similar fix required for the paginate_comments_links in Comments Pagination Numbers block?

It looks like it also should get covered as it calls get_query_var:

https://github.com/WordPress/wordpress-develop/blob/c8b744d12b2af5bc8eb01b2e8782c20d681fdadd/src/wp-includes/link-template.php#L3249

@SantosGuillamot
Copy link
Contributor Author

Is there a similar fix required for the paginate_comments_links in Comments Pagination Numbers block?

I believe you are right and we should cover paginate_comments_links as well if we decide to follow this approach.

@gziolo
Copy link
Member

gziolo commented Aug 26, 2024

@ockham, can you confirm this approach would work from your perspective? As far as I'm concerned, it might resolve the issue.

@ockham
Copy link
Contributor

ockham commented Aug 29, 2024

Apologies for the late reply. Yeah, I think making the argument explicit should be the way forward to make this work. We will have to implement the "other side" -- i.e. the changes to get_next_comments_link and get_previous_comments_link in Core -- to know for sure, though.

@SantosGuillamot Do you have some bandwidth to take that on? I'm a bit swamped with other things these days 😅

Copy link

github-actions bot commented Aug 30, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: SantosGuillamot <[email protected]>
Co-authored-by: ockham <[email protected]>
Co-authored-by: gziolo <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@SantosGuillamot
Copy link
Contributor Author

I have created this alternative pull request in core, which removes the problematic set_query_var while keeping the pagination blocks working as expected.

That PR plus this one should solve both problems.

Is there a similar fix required for the paginate_comments_links in Comments Pagination Numbers block?

Regarding this, I've been taking a deeper look and I believe it is not needed. Let me explain my reasoning:

  • In the pagination numbers block, we are populating a query with current storing the page: link.
  • paginate_comments_links is using get_query_var, but only to define the "defaults": link.
  • Those defaults are overridden with the values passed in the args, which contains the proper page: link.

@@ -37,7 +37,7 @@ function render_block_core_comments_pagination_next( $attributes, $content, $blo
$label .= $pagination_arrow;
}

$next_comments_link = get_next_comments_link( $label, $max_page );
$next_comments_link = get_next_comments_link( $label, $max_page, $comment_vars['paged'] );
Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned in WordPress/wordpress-develop#7275 (comment), we need to add some safeguards to make sure that we're not passing this extra argument if get_next_comments_link() -- as provided by whatever WordPress version is running -- doesn't support the additional parameter yet.

To do so, I think our best course of action is PHP's ReflectionFunction:

Something like (untested):

Suggested change
$next_comments_link = get_next_comments_link( $label, $max_page, $comment_vars['paged'] );
$get_next_comments_link_reflection = new ReflectionFunction( 'get_next_comments_link' );
if ( $get_next_comments_link_reflection->getNumberOfParameters() >= 3 ) {
$next_comments_link = get_next_comments_link( $label, $max_page, $comment_vars['paged'] );
} else {
$next_comments_link = get_next_comments_link( $label, $max_page );
}

and accordingly for get_previous_comments_link.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we really need to add this safety check? If I am not mistaken, when passing the third argument to a function that only accepts two, PHP will just ignore it, right? At least, that's what I can see from my testing.

Copy link
Member

@gziolo gziolo Sep 4, 2024

Choose a reason for hiding this comment

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

There are functions like func_get_args that allow to handle any number of params, so as far as I tested it, providing an additional param that isn't handled works just fine.

<?php
function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs \n";
    if ($numargs >= 2) {
        echo "Second argument is: " . func_get_arg(1) . "\n";
    }
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "Argument $i is: " . $arg_list[$i] . "\n";
    }
}

foo(1, 2, 3);
?>

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, fair enough! I was assuming that PHP would at least issue a warning or a notice if strict error reporting was enabled, but it seems that that isn't the case. (Here's a related SO thread.)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it fatals on built-in functions I tested, but not on custom functions 🤷🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested it directly with get_next_comments_link in a branch without the changes and it doesn't seem to break.

Copy link
Contributor

@ockham ockham left a comment

Choose a reason for hiding this comment

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

Thank you! LGTM 👍

@SantosGuillamot SantosGuillamot merged commit d9e3075 into trunk Sep 16, 2024
77 of 78 checks passed
@SantosGuillamot SantosGuillamot deleted the try/pass-paged-comment-query-arg-to-pagination-functions branch September 16, 2024 07:27
@github-actions github-actions bot added this to the Gutenberg 19.3 milestone Sep 16, 2024
@michalczaplinski
Copy link
Contributor

I found an issue that was likely caused by changes in this PR: #65424

@ockham
Copy link
Contributor

ockham commented Sep 18, 2024

I found an issue that was likely caused by changes in this PR: #65424

I just came here to report the same thing 😕

@SantosGuillamot
Copy link
Contributor Author

I've started a PR trying to solve that: #65435

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Comments Affects the Comments Block - formerly known as Comments Query Loop [Type] Bug An existing feature does not function as intended
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants