From 3309309f6b37fa09d34abe631b275855193a0dea Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 31 Aug 2020 15:56:41 +0000 Subject: [PATCH] Date/Time: Make sure `get_the_date()` and related functions return correct time if the format was specified as `false`. Technically, the `$format` argument should always be a string, but passing `false` used to work before [47808], so this restores backward compatibility. The list of affected functions: * `get_the_date()` * `get_the_time()` * `get_comment_date()` * `get_comment_time()` Props wittich, Rarst, akabarikalpesh, SergeyBiryukov. Fixes #51184. git-svn-id: https://develop.svn.wordpress.org/trunk@48912 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 16 +++---- src/wp-includes/general-template.php | 16 +++---- tests/phpunit/tests/date/getCommentDate.php | 49 +++++++++++++++++++++ tests/phpunit/tests/date/getPostTime.php | 6 ++- tests/phpunit/tests/date/getTheDate.php | 26 ++++++++++- 5 files changed, 93 insertions(+), 20 deletions(-) create mode 100644 tests/phpunit/tests/date/getCommentDate.php diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 9a2d62c586..a048d3870c 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -552,12 +552,12 @@ function get_comment_class( $class = '', $comment_id = null, $post_id = null ) { function get_comment_date( $format = '', $comment_ID = 0 ) { $comment = get_comment( $comment_ID ); - if ( '' === $format ) { - $date = mysql2date( get_option( 'date_format' ), $comment->comment_date ); - } else { - $date = mysql2date( $format, $comment->comment_date ); + if ( ! is_string( $format ) || '' === $format ) { + $format = get_option( 'date_format' ); } + $date = mysql2date( $format, $comment->comment_date ); + /** * Filters the returned comment date. * @@ -1046,12 +1046,12 @@ function get_comment_time( $format = '', $gmt = false, $translate = true ) { $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date; - if ( '' === $format ) { - $date = mysql2date( get_option( 'time_format' ), $comment_date, $translate ); - } else { - $date = mysql2date( $format, $comment_date, $translate ); + if ( ! is_string( $format ) || '' === $format ) { + $format = get_option( 'time_format' ); } + $date = mysql2date( $format, $comment_date, $translate ); + /** * Filters the returned comment time. * diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 094e4a4062..89748f1968 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -2525,12 +2525,12 @@ function get_the_date( $format = '', $post = null ) { return false; } - if ( '' === $format ) { - $the_date = get_post_time( get_option( 'date_format' ), false, $post, true ); - } else { - $the_date = get_post_time( $format, false, $post, true ); + if ( ! is_string( $format ) || '' === $format ) { + $format = get_option( 'date_format' ); } + $the_date = get_post_time( $format, false, $post, true ); + /** * Filters the date a post was published. * @@ -2654,12 +2654,12 @@ function get_the_time( $format = '', $post = null ) { return false; } - if ( '' === $format ) { - $the_time = get_post_time( get_option( 'time_format' ), false, $post, true ); - } else { - $the_time = get_post_time( $format, false, $post, true ); + if ( ! is_string( $format ) || '' === $format ) { + $format = get_option( 'time_format' ); } + $the_time = get_post_time( $format, false, $post, true ); + /** * Filters the time a post was written. * diff --git a/tests/phpunit/tests/date/getCommentDate.php b/tests/phpunit/tests/date/getCommentDate.php new file mode 100644 index 0000000000..5369d96326 --- /dev/null +++ b/tests/phpunit/tests/date/getCommentDate.php @@ -0,0 +1,49 @@ +comment->create( array( 'comment_date' => '2020-08-29 01:51:00' ) ); + + $this->assertEquals( 'August 29, 2020', get_comment_date( 'F j, Y', $c ) ); + } + + /** + * @ticket 51184 + */ + function test_get_comment_date_returns_correct_time_with_empty_format() { + $c = self::factory()->comment->create( array( 'comment_date' => '2020-08-29 01:51:00' ) ); + + $this->assertEquals( 'August 29, 2020', get_comment_date( '', $c ) ); + $this->assertEquals( 'August 29, 2020', get_comment_date( false, $c ) ); + } + + /** + * @ticket 51184 + */ + function test_get_comment_time_returns_correct_time() { + $c = self::factory()->comment->create( array( 'comment_date' => '2020-08-29 01:51:00' ) ); + + $GLOBALS['comment'] = get_comment( $c ); + $this->assertEquals( '1:51 am', get_comment_time( 'g:i a' ) ); + } + + /** + * @ticket 51184 + */ + function test_get_comment_time_returns_correct_time_with_empty_format() { + $c = self::factory()->comment->create( array( 'comment_date' => '2020-08-29 01:51:00' ) ); + + $GLOBALS['comment'] = get_comment( $c ); + $this->assertEquals( '1:51 am', get_comment_time( '' ) ); + $this->assertEquals( '1:51 am', get_comment_time( false ) ); + } +} diff --git a/tests/phpunit/tests/date/getPostTime.php b/tests/phpunit/tests/date/getPostTime.php index c28add1450..d4cd203aa6 100644 --- a/tests/phpunit/tests/date/getPostTime.php +++ b/tests/phpunit/tests/date/getPostTime.php @@ -10,8 +10,9 @@ class Tests_Date_Get_Post_Time extends WP_UnitTestCase { /** * @ticket 28310 */ - public function test_get_post_time_with_id_returns_correct_time() { + public function test_get_post_time_returns_correct_time_with_post_id() { $post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) ); + $this->assertEquals( '16:35:00', get_post_time( 'H:i:s', false, $post_id ) ); } @@ -28,8 +29,9 @@ class Tests_Date_Get_Post_Time extends WP_UnitTestCase { /** * @ticket 28310 */ - public function test_get_post_modified_time_with_id_returns_correct_time() { + public function test_get_post_modified_time_returns_correct_time_with_post_id() { $post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) ); + $this->assertEquals( '16:35:00', get_post_modified_time( 'H:i:s', false, $post_id ) ); } diff --git a/tests/phpunit/tests/date/getTheDate.php b/tests/phpunit/tests/date/getTheDate.php index 03248dba5f..2e45cd9c39 100644 --- a/tests/phpunit/tests/date/getTheDate.php +++ b/tests/phpunit/tests/date/getTheDate.php @@ -10,8 +10,9 @@ class Tests_Date_Get_The_Date extends WP_UnitTestCase { /** * @ticket 13771 */ - function test_get_the_date_with_id_returns_correct_time() { + function test_get_the_date_returns_correct_time_with_post_id() { $post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) ); + $this->assertEquals( 'March 1, 2014', get_the_date( 'F j, Y', $post_id ) ); } @@ -25,11 +26,22 @@ class Tests_Date_Get_The_Date extends WP_UnitTestCase { $this->assertFalse( get_the_date( 'F j, Y h:i:s', 9 ) ); } + /** + * @ticket 51184 + */ + function test_get_the_date_returns_correct_time_with_empty_format() { + $post_id = self::factory()->post->create( array( 'post_date' => '2020-08-29 01:51:00' ) ); + + $this->assertEquals( 'August 29, 2020', get_the_date( '', $post_id ) ); + $this->assertEquals( 'August 29, 2020', get_the_date( false, $post_id ) ); + } + /** * @ticket 28310 */ - function test_get_the_time_with_id_returns_correct_time() { + function test_get_the_time_returns_correct_time_with_post_id() { $post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) ); + $this->assertEquals( '16:35:00', get_the_time( 'H:i:s', $post_id ) ); } @@ -42,4 +54,14 @@ class Tests_Date_Get_The_Date extends WP_UnitTestCase { $this->assertFalse( get_the_time( '', 9 ) ); $this->assertFalse( get_the_time( 'h:i:s', 9 ) ); } + + /** + * @ticket 51184 + */ + function test_get_the_time_returns_correct_time_with_empty_format() { + $post_id = self::factory()->post->create( array( 'post_date' => '2020-08-29 01:51:00' ) ); + + $this->assertEquals( '1:51 am', get_the_time( '', $post_id ) ); + $this->assertEquals( '1:51 am', get_the_time( false, $post_id ) ); + } }