diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index cc2608d4220e1..86d52e1850ec4 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -609,23 +609,31 @@ 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. Integer if `$format` is 'U' or 'G', string otherwise. + * False on failure. + * + * @phpstan-return ( $format is 'U'|'G' ? int|false : string|false ) */ function get_comment_date( $format = '', $comment_id = 0 ) { $comment = get_comment( $comment_id ); - $_format = ! empty( $format ) ? $format : get_option( 'date_format' ); + if ( null === $comment ) { + // 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. * * @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|null $comment The comment object, or null if it does not exist. */ return apply_filters( 'get_comment_date', $comment_date, $format, $comment ); } @@ -1106,7 +1114,10 @@ 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. 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 ) */ function get_comment_time( $format = '', $gmt = false, $translate = true, $comment_id = 0 ) { $comment = get_comment( $comment_id ); @@ -1126,11 +1137,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 ); } diff --git a/tests/phpunit/tests/date/getCommentDate.php b/tests/phpunit/tests/date/getCommentDate.php index 2c25a0e2be56b..ebb11852ae161 100644 --- a/tests/phpunit/tests/date/getCommentDate.php +++ b/tests/phpunit/tests/date/getCommentDate.php @@ -28,6 +28,27 @@ 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() 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' ), 10, 3 ); + + $this->assertFalse( get_comment_date( 'U', PHP_INT_MAX ) ); + $this->assertFalse( get_comment_date( 'F j, Y', PHP_INT_MAX ) ); + $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.' + ); + } + /** * @ticket 51184 */