From df73e34aa8ac3ebd0db4884224b14e360b804eb8 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 4 Nov 2026 13:08:24 -0700 Subject: [PATCH 1/4] Comments: Add conditional return types to `get_comment_date()` and `get_comment_time()`. Both functions return the result of `mysql2date()`, which is an integer for the 'U' and 'G' formats and false when the date is missing or cannot be parsed, yet they were documented as returning only a string. This forced callers such as `human_time_diff( get_comment_date( 'U' ) )` to cast the result. Document the actual `string|int|false` return type, with a `@phpstan-return` conditional on `$format` that matches the ones on `mysql2date()` and the post date functions (`get_the_date()`, `get_post_time()`, etc.). `get_comment_time()` also returns an empty string when the comment does not exist. The `get_comment_date` and `get_comment_time` filters now document false too, since `apply_filters()` is typed from them. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af --- src/wp-includes/comment-template.php | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index cc2608d4220e1..739d262c722e3 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -609,7 +609,9 @@ function get_comment_class( $css_class = '', $comment_id = null, $post = null ) * @param string $format Optional. PHP date format. Defaults to the 'date_format' option. * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the date. * Default current comment. - * @return string The comment's date. + * @return string|int|false The comment's date. + * + * @phpstan-return ( $format is 'U'|'G' ? int|false : string|false ) */ function get_comment_date( $format = '', $comment_id = 0 ) { $comment = get_comment( $comment_id ); @@ -623,9 +625,9 @@ function get_comment_date( $format = '', $comment_id = 0 ) { * * @since 1.5.0 * - * @param string|int $comment_date Formatted date string or Unix timestamp. - * @param string $format PHP date format. - * @param WP_Comment $comment The comment object. + * @param string|int|false $comment_date Formatted date string or Unix timestamp. False on failure. + * @param string $format PHP date format. + * @param WP_Comment $comment The comment object. */ return apply_filters( 'get_comment_date', $comment_date, $format, $comment ); } @@ -1106,7 +1108,9 @@ function comment_text( $comment_id = 0, $args = array() ) { * Default true. * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the time. * Default current comment. - * @return string The formatted time. + * @return string|int|false The formatted time, or an empty string if the comment does not exist. + * + * @phpstan-return ( $format is 'U'|'G' ? int|false|'' : string|false ) */ function get_comment_time( $format = '', $gmt = false, $translate = true, $comment_id = 0 ) { $comment = get_comment( $comment_id ); @@ -1126,11 +1130,12 @@ function get_comment_time( $format = '', $gmt = false, $translate = true, $comme * * @since 1.5.0 * - * @param string|int $comment_time The comment time, formatted as a date string or Unix timestamp. - * @param string $format PHP date format. - * @param bool $gmt Whether the GMT date is in use. - * @param bool $translate Whether the time is translated. - * @param WP_Comment $comment The comment object. + * @param string|int|false $comment_time The comment time, formatted as a date string or Unix timestamp. + * False on failure. + * @param string $format PHP date format. + * @param bool $gmt Whether the GMT date is in use. + * @param bool $translate Whether the time is translated. + * @param WP_Comment $comment The comment object. */ return apply_filters( 'get_comment_time', $comment_time, $format, $gmt, $translate, $comment ); } From e1e179984599d96dbbd6ea085732e0232e796ec9 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 4 Nov 2026 13:15:35 -0700 Subject: [PATCH 2/4] Comments: Explain when the comment date and time functions return an integer or false. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af --- src/wp-includes/comment-template.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 739d262c722e3..043898050de7d 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -609,7 +609,8 @@ function get_comment_class( $css_class = '', $comment_id = null, $post = null ) * @param string $format Optional. PHP date format. Defaults to the 'date_format' option. * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the date. * Default current comment. - * @return string|int|false The comment's date. + * @return string|int|false The comment's date. Integer if `$format` is 'U' or 'G', string otherwise. + * False on failure. * * @phpstan-return ( $format is 'U'|'G' ? int|false : string|false ) */ @@ -1108,7 +1109,8 @@ function comment_text( $comment_id = 0, $args = array() ) { * Default true. * @param int|WP_Comment $comment_id Optional. WP_Comment or ID of the comment for which to get the time. * Default current comment. - * @return string|int|false The formatted time, or an empty string if the comment does not exist. + * @return string|int|false The formatted time. Integer if `$format` is 'U' or 'G', string otherwise. + * False on failure, or an empty string if the comment does not exist. * * @phpstan-return ( $format is 'U'|'G' ? int|false|'' : string|false ) */ From 271dea495bb679b8f34abbdd2f597e9f2cea30b3 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 4 Nov 2026 13:29:22 -0700 Subject: [PATCH 3/4] Comments: Return false from `get_comment_date()` for a nonexistent comment. Previously a missing comment led to reading `comment_date` on null, which emits a warning on PHP 8, and to passing null to the `get_comment_date` filter, whose `$comment` parameter is documented as `WP_Comment`. Return false early instead, as `get_the_date()` does for a missing post and as `get_comment_time()` does (with an empty string) for a missing comment. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af --- src/wp-includes/comment-template.php | 4 ++++ tests/phpunit/tests/date/getCommentDate.php | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 043898050de7d..999552cd7ce27 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -617,6 +617,10 @@ function get_comment_class( $css_class = '', $comment_id = null, $post = null ) function get_comment_date( $format = '', $comment_id = 0 ) { $comment = get_comment( $comment_id ); + if ( null === $comment ) { + return false; + } + $_format = ! empty( $format ) ? $format : get_option( 'date_format' ); $comment_date = mysql2date( $_format, $comment->comment_date ); diff --git a/tests/phpunit/tests/date/getCommentDate.php b/tests/phpunit/tests/date/getCommentDate.php index 2c25a0e2be56b..2f46bc961cab8 100644 --- a/tests/phpunit/tests/date/getCommentDate.php +++ b/tests/phpunit/tests/date/getCommentDate.php @@ -28,6 +28,20 @@ public function test_get_comment_date_returns_correct_time_with_empty_format() { $this->assertSame( 'August 29, 2020', get_comment_date( false, $c ) ); } + /** + * Tests that get_comment_date() returns false for a nonexistent comment without calling the filter. + * + * @ticket 65817 + */ + public function test_get_comment_date_returns_false_for_nonexistent_comment() { + $filter = new MockAction(); + add_filter( 'get_comment_date', array( $filter, 'filter' ) ); + + $this->assertFalse( get_comment_date( 'U', PHP_INT_MAX ) ); + $this->assertFalse( get_comment_date( 'F j, Y', PHP_INT_MAX ) ); + $this->assertSame( 0, $filter->get_call_count(), 'Expected the get_comment_date filter not to be applied.' ); + } + /** * @ticket 51184 */ From 40bd97ca1466544f49780628ebf13d62a75945d9 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 4 Nov 2026 13:33:35 -0700 Subject: [PATCH 4/4] Comments: Pass a nonexistent comment's date through the `get_comment_date` filter. The early return added in the previous commit skipped the filter, which has always run for a nonexistent comment. Follow the back-compat pattern of `get_the_modified_date()` and `get_the_modified_time()` from r37866 instead: use false as the date and still apply the filter, now documenting that its `$comment` parameter can be null. Co-Authored-By: Claude Opus 5.5 (medium effort) Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af --- src/wp-includes/comment-template.php | 13 +++++++------ tests/phpunit/tests/date/getCommentDate.php | 13 ++++++++++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 999552cd7ce27..86d52e1850ec4 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -618,12 +618,13 @@ function get_comment_date( $format = '', $comment_id = 0 ) { $comment = get_comment( $comment_id ); if ( null === $comment ) { - return false; - } - - $_format = ! empty( $format ) ? $format : get_option( 'date_format' ); + // For backward compatibility, failures go through the filter below. + $comment_date = false; + } else { + $_format = ! empty( $format ) ? $format : get_option( 'date_format' ); - $comment_date = mysql2date( $_format, $comment->comment_date ); + $comment_date = mysql2date( $_format, $comment->comment_date ); + } /** * Filters the returned comment date. @@ -632,7 +633,7 @@ function get_comment_date( $format = '', $comment_id = 0 ) { * * @param string|int|false $comment_date Formatted date string or Unix timestamp. False on failure. * @param string $format PHP date format. - * @param WP_Comment $comment The comment object. + * @param WP_Comment|null $comment The comment object, or null if it does not exist. */ return apply_filters( 'get_comment_date', $comment_date, $format, $comment ); } diff --git a/tests/phpunit/tests/date/getCommentDate.php b/tests/phpunit/tests/date/getCommentDate.php index 2f46bc961cab8..ebb11852ae161 100644 --- a/tests/phpunit/tests/date/getCommentDate.php +++ b/tests/phpunit/tests/date/getCommentDate.php @@ -29,17 +29,24 @@ public function test_get_comment_date_returns_correct_time_with_empty_format() { } /** - * Tests that get_comment_date() returns false for a nonexistent comment without calling the filter. + * Tests that get_comment_date() passes false through the filter for a nonexistent comment. * * @ticket 65817 */ public function test_get_comment_date_returns_false_for_nonexistent_comment() { $filter = new MockAction(); - add_filter( 'get_comment_date', array( $filter, 'filter' ) ); + add_filter( 'get_comment_date', array( $filter, 'filter' ), 10, 3 ); $this->assertFalse( get_comment_date( 'U', PHP_INT_MAX ) ); $this->assertFalse( get_comment_date( 'F j, Y', PHP_INT_MAX ) ); - $this->assertSame( 0, $filter->get_call_count(), 'Expected the get_comment_date filter not to be applied.' ); + $this->assertSame( + array( + array( false, 'U', null ), + array( false, 'F j, Y', null ), + ), + $filter->get_args(), + 'Expected the get_comment_date filter to be applied with false and a null comment.' + ); } /**