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.

git-svn-id: https://develop.svn.wordpress.org/trunk@45378 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-05-22 21:57:29 +00:00
parent 36df800c3c
commit 48fb69f013
3 changed files with 96 additions and 65 deletions

View File

@ -2340,28 +2340,30 @@ function the_date_xml() {
function the_date( $d = '', $before = '', $after = '', $echo = true ) {
global $currentday, $previousday;
$the_date = '';
if ( is_new_day() ) {
$the_date = $before . get_the_date( $d ) . $after;
$previousday = $currentday;
}
/**
* Filters the date a post was published for display.
*
* @since 0.71
*
* @param string $the_date The formatted date string.
* @param string $d PHP date format. Defaults to 'date_format' option
* if not specified.
* @param string $before HTML output before the date.
* @param string $after HTML output after the date.
*/
$the_date = apply_filters( 'the_date', $the_date, $d, $before, $after );
/**
* Filters the date a post was published for display.
*
* @since 0.71
*
* @param string $the_date The formatted date string.
* @param string $d PHP date format. Defaults to 'date_format' option
* if not specified.
* @param string $before HTML output before the date.
* @param string $after HTML output after the date.
*/
$the_date = apply_filters( 'the_date', $the_date, $d, $before, $after );
if ( $echo ) {
echo $the_date;
} else {
return $the_date;
}
if ( $echo ) {
echo $the_date;
} else {
return $the_date;
}
}
@ -2700,21 +2702,23 @@ function the_weekday() {
*
* @since 0.71
*
* @global WP_Locale $wp_locale The WordPress date and time locale object.
* @global string $currentday The day of the current post in the loop.
* @global string $previousweekday The day of the previous post in the loop.
* @global WP_Locale $wp_locale The WordPress date and time locale object.
* @global string $currentday The day of the current post in the loop.
* @global string $previousday The day of the previous post in the loop.
*
* @param string $before Optional. Output before the date.
* @param string $after Optional. Output after the date.
*/
function the_weekday_date( $before = '', $after = '' ) {
global $wp_locale, $currentday, $previousweekday;
global $wp_locale, $currentday, $previousday;
$the_weekday_date = '';
if ( $currentday != $previousweekday ) {
if ( is_new_day() ) {
$the_weekday_date .= $before;
$the_weekday_date .= $wp_locale->get_weekday( mysql2date( 'w', get_post()->post_date, false ) );
$the_weekday_date .= $after;
$previousweekday = $currentday;
$previousday = $currentday;
}
/**
@ -2726,8 +2730,7 @@ function the_weekday_date( $before = '', $after = '' ) {
* @param string $before The HTML to output before the date.
* @param string $after The HTML to output after the date.
*/
$the_weekday_date = apply_filters( 'the_weekday_date', $the_weekday_date, $before, $after );
echo $the_weekday_date;
echo apply_filters( 'the_weekday_date', $the_weekday_date, $before, $after );
}
/**

View File

@ -81,4 +81,72 @@ class Tests_Date_TheDate extends WP_UnitTestCase {
return $input;
}
/**
* @ticket 33750
*/
function test_the_date() {
ob_start();
the_date();
$actual = ob_get_clean();
$this->assertEquals( '', $actual );
$GLOBALS['post'] = self::factory()->post->create_and_get(
array(
'post_date' => '2015-09-16 08:00:00',
)
);
ob_start();
$GLOBALS['currentday'] = '18.09.15';
$GLOBALS['previousday'] = '17.09.15';
the_date();
$this->assertEquals( 'September 16, 2015', ob_get_clean() );
ob_start();
$GLOBALS['currentday'] = '18.09.15';
$GLOBALS['previousday'] = '17.09.15';
the_date( 'Y' );
$this->assertEquals( '2015', ob_get_clean() );
ob_start();
$GLOBALS['currentday'] = '18.09.15';
$GLOBALS['previousday'] = '17.09.15';
the_date( 'Y', 'before ', ' after' );
$this->assertEquals( 'before 2015 after', ob_get_clean() );
ob_start();
$GLOBALS['currentday'] = '18.09.15';
$GLOBALS['previousday'] = '17.09.15';
the_date( 'Y', 'before ', ' after', false );
$this->assertEquals( '', ob_get_clean() );
}
/**
* @ticket 47354
*/
function test_the_weekday_date() {
ob_start();
the_weekday_date();
$actual = ob_get_clean();
$this->assertEquals( '', $actual );
$GLOBALS['post'] = self::factory()->post->create_and_get(
array(
'post_date' => '2015-09-16 08:00:00',
)
);
ob_start();
$GLOBALS['currentday'] = '18.09.15';
$GLOBALS['previousday'] = '17.09.15';
the_weekday_date();
$this->assertEquals( 'Wednesday', ob_get_clean() );
ob_start();
$GLOBALS['currentday'] = '18.09.15';
$GLOBALS['previousday'] = '17.09.15';
the_weekday_date( 'before ', ' after' );
$this->assertEquals( 'before Wednesday after', ob_get_clean() );
}
}

View File

@ -915,46 +915,6 @@ class Tests_Functions extends WP_UnitTestCase {
$this->assertFalse( $json );
}
/**
* @ticket 33750
*/
function test_the_date() {
ob_start();
the_date();
$actual = ob_get_clean();
$this->assertEquals( '', $actual );
$GLOBALS['post'] = self::factory()->post->create_and_get(
array(
'post_date' => '2015-09-16 08:00:00',
)
);
ob_start();
$GLOBALS['currentday'] = '18.09.15';
$GLOBALS['previousday'] = '17.09.15';
the_date();
$this->assertEquals( 'September 16, 2015', ob_get_clean() );
ob_start();
$GLOBALS['currentday'] = '18.09.15';
$GLOBALS['previousday'] = '17.09.15';
the_date( 'Y' );
$this->assertEquals( '2015', ob_get_clean() );
ob_start();
$GLOBALS['currentday'] = '18.09.15';
$GLOBALS['previousday'] = '17.09.15';
the_date( 'Y', 'before ', ' after' );
$this->assertEquals( 'before 2015 after', ob_get_clean() );
ob_start();
$GLOBALS['currentday'] = '18.09.15';
$GLOBALS['previousday'] = '17.09.15';
the_date( 'Y', 'before ', ' after', false );
$this->assertEquals( '', ob_get_clean() );
}
/**
* @ticket 36054
* @dataProvider datetime_provider