Opened 14 months ago
Last modified 12 months ago
#20319 new defect (bug)
$page value not being defaulted to 1 in the absence of cpage query var in get_next_comments_link()/get_previous_comments_link()
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Comments | Version: | 3.0 |
| Severity: | normal | Keywords: | has-patch |
| Cc: |
Description
get_next_comments_link() generates incorrect comment pagination links on the first page when used alongside a user-made call to wp_list_comments().
Currently, get_next_comments_link() doesn't take into account instances where there is no 'cpage' query var (which is the case when a user-made call to wp_list_comments() is performed). The intval() of the empty $page variable returns 0, and at this point should then be defaulted to 1 in order for the correct link to page 2 to be generated. Instead, it's left as 0, and the resulting link is self-referencing to page 1.
In wp-includes/link-template.php:
function get_next_comments_link( $label = '', $max_page = 0 ) {
global $wp_query;
if ( !is_singular() || !get_option('page_comments') )
return;
$page = get_query_var('cpage'); //RETURNS EMPTY IN ABSENCE OF A cpage VALUE ON PAGE 1 OF A USER-MADE CALL TO wp_list_comments()
$nextpage = intval($page) + 1; //intval() RETURNS 0, 0+1=1, RESULTING IN THE "Newer Comments" LINK SELF-REFERENCING PAGE 1, INSTEAD OF POINTING TO PAGE 2
...
Coincidentally, in paginate_comments_links() (wp-includes/link-template.php) this situation is handled correctly, and $page does get correctly defaulted to 1. The same conditional used in paginate_comments_links() can be applied to both get_next_comments_link() and get_previous_comments_link(). The code to properly account for it is:
function get_next_comments_link( $label = '', $max_page = 0 ) {
global $wp_query;
if ( !is_singular() || !get_option('page_comments') )
return;
$page = get_query_var('cpage');
if ( !$page ) //******** ADDED
$page = 1; //******** ADDED
$nextpage = intval($page) + 1; //intval() RETURNS 0, 0+1=1, RESULTING IN THE "Newer Comments" LINK NOT POINTING TO PAGE 2
...
The same addition should be made to get_previous_comments_link() as well, although it's obviously unnoticeable on the first page.

Looks good, though not a new problem. Likely introduced way before 3.0.