WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#35068 closed defect (bug) (fixed)

Comments not showing up when there are unapproved messages

Reported by: arash_hemmat Owned by: boonebgorges
Milestone: 4.4.1 Priority: normal
Severity: critical Version: 4.4
Component: Comments Keywords: has-patch needs-testing
Focuses: Cc:

Description

Hi,

The problem occurs when there are some unapproved messages for a post.
If (sum of the root comments for a post (approved and unapproved) divided by the number of root comments in page) is more than the shown pagination number on the page, the comments have_comments() function returns false.

In my situation, the number of comments per page is set to 20, showing last page by default, displaying newest comments.
For example my post had 33 approved comments and 8 unapproved comments. The problem occurred when the eighth unapproved comment was added, the problem was resolved when all the eight were approved or one of them was deleted (sum of the comments would be 40 again)

Attachments (1)

35068.diff (5.4 KB) - added by boonebgorges 4 years ago.

Download all attachments as: .zip

Change History (15)

#1 @Asgaros
4 years ago

  • Keywords needs-patch added

#2 @boonebgorges
4 years ago

  • Keywords needs-patch removed
  • Milestone changed from Awaiting Review to 4.4.1
  • Severity changed from normal to critical

Thanks very much for the report.

I've confirmed the problem. The offset calculation in the new comments_template() does not properly account for approval status. I've got a fix coming up.

#3 @boonebgorges
4 years ago

#35078 was marked as a duplicate.

@boonebgorges
4 years ago

#4 @boonebgorges
4 years ago

  • Keywords has-patch needs-testing added

35068.diff should make the problem fairly clear, but just to spell it out a bit more: The improved paginated-comment queries in 4.4 work by only querying for the specific comments that the current comment-page needs, rather than *all* of the post's comments. See #8071. We do this by passing 'number' and 'offset' params to WP_Comment_Query in comments_template().

Generally, the calculation of 'number' and 'offset' is pretty simple, given that we usually have comments_per_page and cpage. But the case where 'default_comments_page=newest' and where we're looking at the post permalink (no cpage) is a special one: we need to show the last page of comments, but the only way to know the proper offset for the "last" comments is to have a count of all comments on the post. The problem in 4.4 is that the total-top-level-comments-for-the-post query does not account for 'comment_approved'. https://core.trac.wordpress.org/browser/tags/4.4/src/wp-includes/comment-template.php?marks=1316-1323#L1300

Could I get a second (and third and fourth) set of eyes on the patch and tests? Sorry for the ugliness of the tests, btw - blame comments_template(), not me :-D

#5 @DrewAPicture
4 years ago

@boonebgorges: 35068.diff looks pretty straightforward and the tests pass. Did you mean to leave the var_dump() in the tests?

#6 @boonebgorges
4 years ago

@DrewAPicture Thanks for looking. No I didn't mean to leave var_dump() there - it didn't show up when testing because it's inside of an output buffer, so good catch.

Note to self that this problem was sorta independently found here https://wordpress.org/support/topic/wp-44-update-comments-with-last-page-displayed-by-default-are-not-showing

#7 follow-up: @pento
4 years ago

I haven't tested, but I just wanted to check - will this include unapproved comments by the current user?

#8 in reply to: ↑ 7 @boonebgorges
4 years ago

Replying to pento:

I haven't tested, but I just wanted to check - will this include unapproved comments by the current user?

Yes: unapproved comments by the current user are (and always have been) counted when calculating pagination; with 35068.diff, they will also be counted when calculating the max-number-of-pages.

#9 @jorbin
3 years ago

  • Owner set to boonebgorges
  • Status changed from new to assigned

One additional thing, looks like you left @group bbg on one of the tests. Otherwise, this looks good to me.

assigning to @boonebgorges as comment query master.

#10 @boonebgorges
3 years ago

assigning to @boonebgorges as comment query master.

A title I wish I'd never taken on. Thanks for the review.

#11 @boonebgorges
3 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 36040:

Respect approval status when determining comment page count in comments_template().

Since 4.4, when fetching the first page of comments and the 'newest' comments
are set to display first, comments_template() must perform arithmetic to
determine which comments to show. See #8071. This arithmetic requires the
total comment count for the current post, which is calculated with a separate
WP_Comment_Query. This secondary comment query did not properly account for
non-approved comment statuses; all unapproved comments should be part of the
comment count for admins, and individual users should have their own
unapproved comments included in the count. As a result, comments_template()
was, in some cases, being fooled into thinking that a post had fewer comments
available for pagination than it actually had, which resulted in empty pages
of comments.

We correct this problem by mirroring 'status' and 'include_unapproved' params
of the main comment query within the secondary query used to calculate
pagination.

Fixes #35068.

#12 @boonebgorges
3 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for 4.4.1.

#13 @boonebgorges
3 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 36041:

Respect approval status when determining comment page count in comments_template().

Since 4.4, when fetching the first page of comments and the 'newest' comments
are set to display first, comments_template() must perform arithmetic to
determine which comments to show. See #8071. This arithmetic requires the
total comment count for the current post, which is calculated with a separate
WP_Comment_Query. This secondary comment query did not properly account for
non-approved comment statuses; all unapproved comments should be part of the
comment count for admins, and individual users should have their own
unapproved comments included in the count. As a result, comments_template()
was, in some cases, being fooled into thinking that a post had fewer comments
available for pagination than it actually had, which resulted in empty pages
of comments.

We correct this problem by mirroring 'status' and 'include_unapproved' params
of the main comment query within the secondary query used to calculate pagination.

Merges [36040] to the 4.4 branch.

Fixes #35068.

#14 @settlersoman
3 years ago

Does it also solve my problem: e.g. on one page, there are "23 thoughts on “post x”. However, when I count comments, there are 20 only. 3 comments (not the oldest and latest) are not shown. An interesting thing is that every new comment is shown correctly.

Note: See TracTickets for help on using tickets.