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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.


remove_filter( 'next_posts_link_attributes', $filter_link_attributes );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function render_block_core_comments_pagination_previous( $attributes, $content,
};
add_filter( 'previous_comments_link_attributes', $filter_link_attributes );

$previous_comments_link = get_previous_comments_link( $label );
$comment_vars = build_comment_query_vars_from_block( $block );
$previous_comments_link = get_previous_comments_link( $label, $comment_vars['paged'] );

remove_filter( 'previous_comments_link_attributes', $filter_link_attributes );

Expand Down
Loading