Make WordPress Core

Changeset 48921


Ignore:
Timestamp:
08/31/2020 06:56:22 PM (4 years ago)
Author:
desrosj
Message:

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.
Merges [48912] to the 5.5 branch.
Fixes #51184.

Location:
branches/5.5
Files:
5 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/5.5

  • branches/5.5/src/wp-includes/comment-template.php

    r48597 r48921  
    553553    $comment = get_comment( $comment_ID );
    554554
    555     if ( '' === $format ) {
    556         $date = mysql2date( get_option( 'date_format' ), $comment->comment_date );
    557     } else {
    558         $date = mysql2date( $format, $comment->comment_date );
    559     }
     555    if ( ! is_string( $format ) || '' === $format ) {
     556        $format = get_option( 'date_format' );
     557    }
     558
     559    $date = mysql2date( $format, $comment->comment_date );
    560560
    561561    /**
     
    10471047    $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
    10481048
    1049     if ( '' === $format ) {
    1050         $date = mysql2date( get_option( 'time_format' ), $comment_date, $translate );
    1051     } else {
    1052         $date = mysql2date( $format, $comment_date, $translate );
    1053     }
     1049    if ( ! is_string( $format ) || '' === $format ) {
     1050        $format = get_option( 'time_format' );
     1051    }
     1052
     1053    $date = mysql2date( $format, $comment_date, $translate );
    10541054
    10551055    /**
  • branches/5.5/src/wp-includes/general-template.php

    r48871 r48921  
    25262526    }
    25272527
    2528     if ( '' === $format ) {
    2529         $the_date = get_post_time( get_option( 'date_format' ), false, $post, true );
    2530     } else {
    2531         $the_date = get_post_time( $format, false, $post, true );
    2532     }
     2528    if ( ! is_string( $format ) || '' === $format ) {
     2529        $format = get_option( 'date_format' );
     2530    }
     2531
     2532    $the_date = get_post_time( $format, false, $post, true );
    25332533
    25342534    /**
     
    26552655    }
    26562656
    2657     if ( '' === $format ) {
    2658         $the_time = get_post_time( get_option( 'time_format' ), false, $post, true );
    2659     } else {
    2660         $the_time = get_post_time( $format, false, $post, true );
    2661     }
     2657    if ( ! is_string( $format ) || '' === $format ) {
     2658        $format = get_option( 'time_format' );
     2659    }
     2660
     2661    $the_time = get_post_time( $format, false, $post, true );
    26622662
    26632663    /**
  • branches/5.5/tests/phpunit/tests/date/getPostTime.php

    r48920 r48921  
    1111     * @ticket 28310
    1212     */
    13     public function test_get_post_time_with_id_returns_correct_time() {
     13    public function test_get_post_time_returns_correct_time_with_post_id() {
    1414        $post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
     15
    1516        $this->assertEquals( '16:35:00', get_post_time( 'H:i:s', false, $post_id ) );
    1617    }
     
    2930     * @ticket 28310
    3031     */
    31     public function test_get_post_modified_time_with_id_returns_correct_time() {
     32    public function test_get_post_modified_time_returns_correct_time_with_post_id() {
    3233        $post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
     34
    3335        $this->assertEquals( '16:35:00', get_post_modified_time( 'H:i:s', false, $post_id ) );
    3436    }
  • branches/5.5/tests/phpunit/tests/date/getTheDate.php

    r48920 r48921  
    1111     * @ticket 13771
    1212     */
    13     function test_get_the_date_with_id_returns_correct_time() {
     13    function test_get_the_date_returns_correct_time_with_post_id() {
    1414        $post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
     15
    1516        $this->assertEquals( 'March 1, 2014', get_the_date( 'F j, Y', $post_id ) );
    1617    }
     
    2728
    2829    /**
     30     * @ticket 51184
     31     */
     32    function test_get_the_date_returns_correct_time_with_empty_format() {
     33        $post_id = self::factory()->post->create( array( 'post_date' => '2020-08-29 01:51:00' ) );
     34
     35        $this->assertEquals( 'August 29, 2020', get_the_date( '', $post_id ) );
     36        $this->assertEquals( 'August 29, 2020', get_the_date( false, $post_id ) );
     37    }
     38
     39    /**
    2940     * @ticket 28310
    3041     */
    31     function test_get_the_time_with_id_returns_correct_time() {
     42    function test_get_the_time_returns_correct_time_with_post_id() {
    3243        $post_id = self::factory()->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
     44
    3345        $this->assertEquals( '16:35:00', get_the_time( 'H:i:s', $post_id ) );
    3446    }
     
    4355        $this->assertFalse( get_the_time( 'h:i:s', 9 ) );
    4456    }
     57
     58    /**
     59     * @ticket 51184
     60     */
     61    function test_get_the_time_returns_correct_time_with_empty_format() {
     62        $post_id = self::factory()->post->create( array( 'post_date' => '2020-08-29 01:51:00' ) );
     63
     64        $this->assertEquals( '1:51 am', get_the_time( '', $post_id ) );
     65        $this->assertEquals( '1:51 am', get_the_time( false, $post_id ) );
     66    }
    4567}
Note: See TracChangeset for help on using the changeset viewer.