Make WordPress Core

Opened 5 months ago

Closed 4 months ago

Last modified 4 months ago

#65392 closed defect (bug) (fixed)

"Show more comments" button in comments metabox doesn't load new comments if they are of a type different than 'comment'

Reported by: aljullu Owned by: tyxla
Priority: normal Milestone: 7.1
Component: Comments Version: 7.1
Severity: normal Keywords: has-patch commit
Cc: Focuses: administration

Description

When comments have a custom type (ie, review) the 'Load more comments' button in the comments metabox in the editor doesn't work properly: it duplicates the existing comments listed instead of loading the remaining ones.

Steps to reproduce (custom code)

  1. Add this code snippet somewhere in your page. tl;dr, it creates a custom post type and makes its comments have the cpt_response type:
<?php
add_action( 'init', function () {
        register_post_type(
                'my_cpt',
                array(
                        'labels'       => array(
                                'name'          => 'My custom CPT entries',
                                'singular_name' => 'My custom CPT entry',
                                'add_new'       => 'Add entry',
                                'add_new_item'  => 'Add new entry',
                        ),
                        'public'       => true,
                        'show_ui'      => true,
                        'show_in_menu' => true,
                        'show_in_rest' => true,
                        'supports'     => array( 'title', 'comments' ),
                        'menu_icon'    => 'dashicons-testimonial',
                        'has_archive'  => false,
                )
        );
}, 0 );

add_filter( 'preprocess_comment', function ( $data ) {
        $post_id = isset( $data['comment_post_ID'] ) ? (int) $data['comment_post_ID'] : 0;
        if ( $post_id && 'my_cpt' === get_post_type( $post_id ) ) {
                $data['comment_type'] = 'cpt_response';
        }
        return $data;
}, 1 );
  1. In wp-admin, go to My custom CPT entries > Add new entry.
  2. Add a title and publish.
  3. In the frontend, add at least 21 comments to that entry.
  4. Back in the editor, verify the comments metabox shows comments 21-12 (they are ssorted by recency).
  5. Click on Show more comments.
  6. Notice comments 21-12 are loaded again, appearing repeated.

Expected behavior: comments 11-2 should be loaded, instead.

Steps to reproduce (WooCommerce)

The same can be reproduced in WooCommerce.

  1. Create a product.
  2. In the frontend, add 21 reviews.
  3. In the product editor, verify the reviews metabox shows reviews 21-12.
  4. Click on Show more comments.
  5. Notice comments 21-12 are loaded again, appearing repeated.

Explanation

The current logic to load new comments in the comment's metabox in the editor assumes the HTML elements will have the comment class, but that's only the case for comments of type comment. If comments have a different type, like review, they won't be counted and the button will fail to work.

Change History (9)

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


5 months ago
#1

  • Keywords has-patch added

Right now, the comments metabox in WordPress assumes all comments <tr> elements will have the comment class. But that's not true if the comment type is custom, like WooCommerce's reviews.

This PR makes the selector match the <tr> elements by their id starting with comment-, this seems to be a safer approach because it matches regular comments and other comment types like reviews.

It also changes the default number of reviews which are requested in the AJAX call. Until now, it was 20, but the backend was ​falling back to `10`, causing an inconsistency that made the frontend think all comments were already loaded even if they weren't.

Trac ticket: [](https://core.trac.wordpress.org/ticket/65392)

Initial report in WooCommerce repo: ​https://github.com/woocommerce/woocommerce/issues/52731.

## Use of AI Tools

AI assistance: Yes
Tool(s): Cursor
Model(s): Composer
Used for: Researching the code, brainstorming solutions and writing steps to reproduce.

## Steps to reproduce (custom code)

  1. Add this code snippet somewhere in your page. _tl;dr,_ it creates a custom post type and makes its comments have the cpt_response type:

{{{PHP
add_action( 'init', function () {

register_post_type(

'my_cpt',
array(

'labels' => array(

'name' => 'My custom CPT entries',
'singular_name' => 'My custom CPT entry',
'add_new' => 'Add entry',
'add_new_item' => 'Add new entry',

),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'supports' => array( 'title', 'comments' ),
'menu_icon' => 'dashicons-testimonial',
'has_archive' => false,

)

);

}, 0 );

add_filter( 'preprocess_comment', function ( $data ) {

$post_id = isset( $datacomment_post_ID ) ? (int) $datacomment_post_ID : 0;
if ( $post_id && 'my_cpt' === get_post_type( $post_id ) ) {

$datacomment_type = 'cpt_response';

}
return $data;

}, 1 );
}}}

  1. In wp-admin, go to _My custom CPT entries_ > _Add new entry_.
  2. Add a title and publish.
  3. In the frontend, add at least 21 comments to that entry.
  4. Back in the editor, verify the comments metabox shows comments 21-12 (they are ssorted by recency).
  5. Click on _Show more comments_.
  6. Verify comments 11-2 are loaded (before this fix, comments 21-12 were loaded again).
  7. Press _Show more comments_ again and verify comment 1 is loaded (before this fix, the button would not be visible).

## Steps to reproduce (WooCommerce)

  1. Create a product.
  2. In the frontend, add 21 reviews.
  3. In the product editor, verify the reviews metabox shows reviews 21-12.
  4. Click on _Show more comments_.
  5. Verify reviews 11-2 are loaded.
  6. Press _Show more comments_ again and verify review 1 is loaded.

​@aljullu commented on ​PR #12055:


5 months ago
#2

There are some PHPUnit Tests automations failing, but they don't seem related.

#3 @abdullahramzan
5 months ago

  • Keywords 2nd-opinion dev-feedback needs-testing added

This ticket was mentioned in ​Slack in #core by abdullahramzan. ​View the logs.


5 months ago

#5 @sanayasir
5 months ago

  • Keywords 2nd-opinion dev-feedback needs-testing removed

Tested this ticket on WordPress Playground.

Created a test entry and added 44 comments. Verified the Comments metabox in the editor.

After clicking "Show more comments", older comments loaded correctly instead of repeating the previously displayed comments.

Confirmed:

  • No duplicate comments appeared.
  • Comment pagination works as expected.
  • The issue described in the ticket is resolved.

Patch tested successfully. ✅

​@tyxla commented on ​PR #12055:


4 months ago
#6

There are some PHPUnit Tests automations failing, but they don't seem related.

Yep. Rerunning while I'm taking a look and testing.

#7 @tyxla
4 months ago

  • Keywords commit added

Confirmed the bug and tested the fix. It tests well for me, and the code looks good. Will land this soon.

#8 @tyxla
4 months ago

  • Owner set to tyxla
  • Resolution → fixed
  • Status assigned → closed

In 62624:

Comments: Fix "Show more comments" button for custom comment types.

The comments metabox assumed all comment <tr> elements carry the
comment class, which is not the case for custom comment types such as
WooCommerce reviews. Match rows by an id starting with comment-
instead, so regular comments and custom comment types are both counted.

Also correct the default number of comments requested in the AJAX call
from 20 to 10, aligning the frontend with the backend, which falls back
to 10. The mismatch made the frontend believe all comments were already
loaded when they were not.

Props Aljullu, PavloBorysenko, tyxla.
Fixes #65392.

#9 @sabernhardt
4 months ago

  • Milestone Awaiting Review → 7.1
Note: See TracTickets for help on using tickets.