Commit b915ad5bb5 for wordpress.org
commit b915ad5bb55570a71e2faece373b12a337e8fcdd
Author: westonruter <westonruter@1a063a9b-81f0-0310-95a4-ce76da25c4cd>
Date: Wed Nov 4 20:50:01 2026 +0000
Comments: Add conditional return types to comment date functions.
Both `get_comment_date()` and `get_comment_time()` return the result of `mysql2date()`, which is an integer for the 'U' and 'G' formats and false on failure, yet they were documented as returning only a string. Callers such as the Comment Date block therefore had to cast `get_comment_date( 'U', $comment )` before passing it to `human_time_diff()`. They now declare the same conditional return on `$format` as `mysql2date()` and the post date functions, and the `get_comment_date` and `get_comment_time` filters document false as well, since the return type of `apply_filters()` is derived from them.
A nonexistent comment no longer causes `get_comment_date()` to read a property on null. For back-compat, false is still passed through the `get_comment_date` filter, as is done in `get_the_modified_date()` and `get_the_modified_time()`, so the filter's `$comment` parameter is now documented as possibly null. Since r55284, `get_comment_time()` has returned an empty string for a nonexistent comment without applying its filter, and this is left unchanged.
Developed in https://github.com/WordPress/wordpress-develop/pull/15473.
Follow-up to r37866, r55284, r63941.
See #65817.
Built from https://develop.svn.wordpress.org/trunk@65263
git-svn-id: http://core.svn.wordpress.org/trunk@64412 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php
index cc2608d422..86d52e1850 100644
--- a/wp-includes/comment-template.php
+++ b/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/wp-includes/version.php b/wp-includes/version.php
index 01e4e7bb6c..e07789c80a 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-beta4-65262';
+$wp_version = '7.2-beta4-65263';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.