Make WordPress Core

Opened 6 years ago

Last modified 2 weeks ago

#51305 reopened defect (bug)

wp_list_comments returns nothing if "per_page" parameter is defined

Reported by: SGr33n Owned by: metalandcoffee
Priority: normal Milestone:
Component: Comments Version: 5.5.1
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Hi,
The function wp_list_comments returns a notice if "per_page" parameter is defined.
The notice is in wp-includes/comments-template.php

wp_queue_comments_for_comment_meta_lazyload( $_comments )

message: "Undefined variable: _comments"

Thanks!

Change History (11)

#1 @SergeyBiryukov
6 years ago

  • Keywords needs-patch needs-unit-tests added

#2 @metalandcoffee
6 years ago

  • Owner set to metalandcoffee
  • Status new → assigned

Hi @SGr33n - I tried to reproduce this error by calling wp_list_comments( array( 'per_page' => 1 ) ); but I'm not getting any PHP notices when this happens. Can you give steps to reproduce and provide all of the parameters you're using in wp_list_comments? How many comments are you expecting to see on the post in question? What is your PHP version?

#3 @SGr33n
6 years ago

Hi @metalandcoffee,
At first test looks working on the latest version.
Will keep you updated
Thanks!

#4 @SGr33n
6 years ago

First test, working:

<?php
        wp_list_comments(
                array(
                        'page'                  => 1,
                        'per_page'              => 10,
                        'style'                 => 'div',
                )
        );

Second test (non working):

<?php
        wp_list_comments(
                array(
                        'page'                  => 1,
                        'per_page'              => absint( get_option( 'comments_per_page' ) ),
                        'style'                 => 'div',
                )
        );

Is returning

[12-Dec-2020 17:07:55 UTC] PHP Notice:  Undefined variable: _comments in /web/wp-includes/comment-template.php on line 2236
[12-Dec-2020 17:07:55 UTC] PHP Notice:  Undefined variable: _comments in /web/wp-includes/comment-template.php on line 2244

PHP version 7.4
PHP-FPM
Webserver Nginx
WordPress version 5.6

#5 @donmhico
6 years ago

I'm unable to replicate the issue in current master branch (commit 02e9222e2ebaf97c75623112ad2370740a8db6a5).

#6 @Howdy_McGee
4 years ago

I could not replicate this issue on PHP 7.4 with the latest version of WordPress.

#7 @SGr33n
4 years ago

  • Resolution → worksforme
  • Status assigned → closed

I can confirm it's not happening anymore.
Maybe this was fixed over these two years.

#8 @swissspidy
3 years ago

  • Milestone Awaiting Review

This ticket was mentioned in ​PR #15483 on ​WordPress/wordpress-develop by ​@pwtyler.


2 weeks ago
#9

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed

[!NOTE]
This PR currently contains only the regression test, so CI can show it failing on trunk. The fix will be pushed as a follow-up commit once CI has run.

When page or per_page is passed to wp_list_comments() and both match the current comment page, it lists no comments. It returns empty output and triggers Undefined variable $_comments. PHPStan reports this as two variable.undefined errors in comment-template.php.

This is the bug reported in #51305, for example 'page' => 1, 'per_page' => get_option( 'comments_per_page' ). That ticket was closed as worksforme because others couldn't reproduce it. It only reproduces when the arguments match the current comment page; with any other values a separate query runs and comments are listed.

Background

  • r36157 (#35175): added a separate comment query for when page or per_page differ from the current comment page. When they matched, execution fell through to $wp_query->comments, which was already paginated.
  • r36276 (#35356): moved that query into the $comments === null branch, nested under if ( page || per_page ). The fallback to $wp_query->comments became that if's else, so it only runs when neither argument is passed. When the arguments are passed and match, nothing sets $_comments. An "Otherwise, fall back on the comments from $wp_query->comments" comment still marks where the fallback used to happen.

Changes

  • wp_list_comments(): the fallback to $wp_query->comments now runs whenever $_comments hasn't been set, which restores the behavior from before r36276. This is a 3-line structural change.
  • New test: test_pagination_params_matching_current_page_should_list_query_comments() passes the current cpage and comments_per_page explicitly and asserts that the output matches calling wp_list_comments() without them. It covers both default_comments_page settings. On trunk, the test fails with Undefined variable $_comments.
  • Baseline: removes the $_comments entry from tests/phpstan/baselines/variable.undefined.neon.

Testing Instructions

  • npm run test:php -- --filter test_pagination_params_matching_current_page fails without the fix and passes with it.
  • npm run test:php -- --group comment passes on single-site and multisite.
  • npm run typecheck:php reports no errors.

See also: https://core.trac.wordpress.org/ticket/65817

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5, Claude Sonnet 5.5
Used for: Investigating the PHPStan error, reproducing the bug, code archeology, and drafting the fix, test and this description. I reviewed and verified all output.

#10 @pwtyler
2 weeks ago

  • Resolution worksforme
  • Status closed → reopened

I was able to reproduce this and PR'd a fix that reproduces with a failing unit test and then closes the gap.

See ​https://github.com/WordPress/wordpress-develop/pull/15483

​@SGr33n commented on ​PR #15483:


2 weeks ago
#11

It was a trac I submitted 6 years ago. Thanks :)

Note: See TracTickets for help on using tickets.