Make WordPress Core

Changeset 45378


Ignore:
Timestamp:
05/22/2019 09:57:29 PM (7 years ago)
Author:
SergeyBiryukov
Message:

Date/Time: Bring some consistency to the_date() and the_weekday_date():

  • Make the_date() always apply the the filter and return a value.
  • Use is_new_day() in the_weekday_date().
  • Add a unit test for the_weekday_date().

Fixes #47354.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r45377 r45378  
    23412341        global $currentday, $previousday;
    23422342
     2343        $the_date = '';
     2344
    23432345        if ( is_new_day() ) {
    23442346                $the_date    = $before . get_the_date( $d ) . $after;
    23452347                $previousday = $currentday;
    2346 
    2347                 /**
    2348                  * Filters the date a post was published for display.
    2349                  *
    2350                  * @since 0.71
    2351                  *
    2352                  * @param string $the_date The formatted date string.
    2353                  * @param string $d        PHP date format. Defaults to 'date_format' option
    2354                  *                         if not specified.
    2355                  * @param string $before   HTML output before the date.
    2356                  * @param string $after    HTML output after the date.
    2357                  */
    2358                 $the_date = apply_filters( 'the_date', $the_date, $d, $before, $after );
    2359 
    2360                 if ( $echo ) {
    2361                         echo $the_date;
    2362                 } else {
    2363                         return $the_date;
    2364                 }
     2348        }
     2349
     2350        /**
     2351         * Filters the date a post was published for display.
     2352         *
     2353         * @since 0.71
     2354         *
     2355         * @param string $the_date The formatted date string.
     2356         * @param string $d        PHP date format. Defaults to 'date_format' option
     2357         *                         if not specified.
     2358         * @param string $before   HTML output before the date.
     2359         * @param string $after    HTML output after the date.
     2360         */
     2361        $the_date = apply_filters( 'the_date', $the_date, $d, $before, $after );
     2362
     2363        if ( $echo ) {
     2364                echo $the_date;
     2365        } else {
     2366                return $the_date;
    23652367        }
    23662368}
    … …  
    27012703 * @since 0.71
    27022704 *
    2703  * @global WP_Locale $wp_locale       The WordPress date and time locale object.
    2704  * @global string    $currentday      The day of the current post in the loop.
    2705  * @global string    $previousweekday The day of the previous post in the loop.
     2705 * @global WP_Locale $wp_locale   The WordPress date and time locale object.
     2706 * @global string    $currentday  The day of the current post in the loop.
     2707 * @global string    $previousday The day of the previous post in the loop.
    27062708 *
    27072709 * @param string $before Optional. Output before the date.
    … …  
    27092711 */
    27102712function the_weekday_date( $before = '', $after = '' ) {
    2711         global $wp_locale, $currentday, $previousweekday;
     2713        global $wp_locale, $currentday, $previousday;
     2714
    27122715        $the_weekday_date = '';
    2713         if ( $currentday != $previousweekday ) {
     2716
     2717        if ( is_new_day() ) {
    27142718                $the_weekday_date .= $before;
    27152719                $the_weekday_date .= $wp_locale->get_weekday( mysql2date( 'w', get_post()->post_date, false ) );
    27162720                $the_weekday_date .= $after;
    2717                 $previousweekday   = $currentday;
     2721                $previousday       = $currentday;
    27182722        }
    27192723
    … …  
    27272731         * @param string $after            The HTML to output after the date.
    27282732         */
    2729         $the_weekday_date = apply_filters( 'the_weekday_date', $the_weekday_date, $before, $after );
    2730         echo $the_weekday_date;
     2733        echo apply_filters( 'the_weekday_date', $the_weekday_date, $before, $after );
    27312734}
    27322735
  • trunk/tests/phpunit/tests/date/theDate.php

    r43591 r45378  
    8282                return $input;
    8383        }
     84
     85        /**
     86         * @ticket 33750
     87         */
     88        function test_the_date() {
     89                ob_start();
     90                the_date();
     91                $actual = ob_get_clean();
     92                $this->assertEquals( '', $actual );
     93
     94                $GLOBALS['post'] = self::factory()->post->create_and_get(
     95                        array(
     96                                'post_date' => '2015-09-16 08:00:00',
     97                        )
     98                );
     99
     100                ob_start();
     101                $GLOBALS['currentday']  = '18.09.15';
     102                $GLOBALS['previousday'] = '17.09.15';
     103                the_date();
     104                $this->assertEquals( 'September 16, 2015', ob_get_clean() );
     105
     106                ob_start();
     107                $GLOBALS['currentday']  = '18.09.15';
     108                $GLOBALS['previousday'] = '17.09.15';
     109                the_date( 'Y' );
     110                $this->assertEquals( '2015', ob_get_clean() );
     111
     112                ob_start();
     113                $GLOBALS['currentday']  = '18.09.15';
     114                $GLOBALS['previousday'] = '17.09.15';
     115                the_date( 'Y', 'before ', ' after' );
     116                $this->assertEquals( 'before 2015 after', ob_get_clean() );
     117
     118                ob_start();
     119                $GLOBALS['currentday']  = '18.09.15';
     120                $GLOBALS['previousday'] = '17.09.15';
     121                the_date( 'Y', 'before ', ' after', false );
     122                $this->assertEquals( '', ob_get_clean() );
     123        }
     124
     125        /**
     126         * @ticket 47354
     127         */
     128        function test_the_weekday_date() {
     129                ob_start();
     130                the_weekday_date();
     131                $actual = ob_get_clean();
     132                $this->assertEquals( '', $actual );
     133
     134                $GLOBALS['post'] = self::factory()->post->create_and_get(
     135                        array(
     136                                'post_date' => '2015-09-16 08:00:00',
     137                        )
     138                );
     139
     140                ob_start();
     141                $GLOBALS['currentday']  = '18.09.15';
     142                $GLOBALS['previousday'] = '17.09.15';
     143                the_weekday_date();
     144                $this->assertEquals( 'Wednesday', ob_get_clean() );
     145
     146                ob_start();
     147                $GLOBALS['currentday']  = '18.09.15';
     148                $GLOBALS['previousday'] = '17.09.15';
     149                the_weekday_date( 'before ', ' after' );
     150                $this->assertEquals( 'before Wednesday after', ob_get_clean() );
     151        }
    84152}
  • trunk/tests/phpunit/tests/functions.php

    r45270 r45378  
    914914                $json = wp_json_encode( $data, 0, 1 );
    915915                $this->assertFalse( $json );
    916         }
    917 
    918         /**
    919          * @ticket 33750
    920          */
    921         function test_the_date() {
    922                 ob_start();
    923                 the_date();
    924                 $actual = ob_get_clean();
    925                 $this->assertEquals( '', $actual );
    926 
    927                 $GLOBALS['post'] = self::factory()->post->create_and_get(
    928                         array(
    929                                 'post_date' => '2015-09-16 08:00:00',
    930                         )
    931                 );
    932 
    933                 ob_start();
    934                 $GLOBALS['currentday']  = '18.09.15';
    935                 $GLOBALS['previousday'] = '17.09.15';
    936                 the_date();
    937                 $this->assertEquals( 'September 16, 2015', ob_get_clean() );
    938 
    939                 ob_start();
    940                 $GLOBALS['currentday']  = '18.09.15';
    941                 $GLOBALS['previousday'] = '17.09.15';
    942                 the_date( 'Y' );
    943                 $this->assertEquals( '2015', ob_get_clean() );
    944 
    945                 ob_start();
    946                 $GLOBALS['currentday']  = '18.09.15';
    947                 $GLOBALS['previousday'] = '17.09.15';
    948                 the_date( 'Y', 'before ', ' after' );
    949                 $this->assertEquals( 'before 2015 after', ob_get_clean() );
    950 
    951                 ob_start();
    952                 $GLOBALS['currentday']  = '18.09.15';
    953                 $GLOBALS['previousday'] = '17.09.15';
    954                 the_date( 'Y', 'before ', ' after', false );
    955                 $this->assertEquals( '', ob_get_clean() );
    956916        }
    957917
Note: See TracChangeset for help on using the changeset viewer.