WordPress.org

Make WordPress Core

Opened 5 months ago

Last modified 3 months ago

#26240 new defect (bug)

Allow get_comments_number() to accept post object

Reported by: coffee2code Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Comments Keywords: has-patch
Focuses: Cc:

Description

get_comments_number() unnecessarily enforces an int value for its $post_id argument. This is contrary to expectations whereby most template functions accept either a post object or ID, and it doesn't match up with the function's phpDocs which indicate either value is acceptable.

Ultimately, the $post_id argument gets sent to get_post() to obtain the post, so the argument can be directly passed along and left to be handled by get_post().

Attached is a patch which removes the unnecessary typecasting. The patch also adds braces to the if conditional to match the new guideline change.

Also included are some unit tests for it. Since there doesn't appear to be any unit tests for comment-template.php functions, I created tests/comment/output.php to mirror how posts template functions are tested in tests/post/output.php (though post template tests are pretty meager).

Attachments (3)

26240.diff (2.0 KB) - added by coffee2code 5 months ago.
26240-tests.diff (1.3 KB) - added by JanHenkG 3 months ago.
26240.2.diff (715 bytes) - added by JanHenkG 3 months ago.

Download all attachments as: .zip

Change History (5)

coffee2code5 months ago

comment:1 nofearinc5 months ago

Looks good and works for me, although I think that the test should be in a separate diff.

JanHenkG3 months ago

JanHenkG3 months ago

comment:2 JanHenkG3 months ago

The fix of coffee2code also worked for me, I kept this fix unchanged in the files I attached. The changes I made are:

  • Created separate patch files for the fix itself and the tests.
  • Refactored the tests a bit, for example I removed the use of the $post_id member variable.
Note: See TracTickets for help on using tickets.