diff --git a/src/wp-includes/class-wp-locale.php b/src/wp-includes/class-wp-locale.php index 3671c1b27927f..cd022fdaf9c83 100644 --- a/src/wp-includes/class-wp-locale.php +++ b/src/wp-includes/class-wp-locale.php @@ -264,6 +264,8 @@ public function init() { * * @param int $weekday_number 0 for Sunday through 6 Saturday. * @return string Full translated weekday. + * + * @phpstan-param int<0, 6> $weekday_number */ public function get_weekday( $weekday_number ) { return $this->weekday[ $weekday_number ]; @@ -355,6 +357,8 @@ public function get_month_abbrev( $month_name ) { * * @param string|int $month_number '01' through '12'. * @return string Translated genitive month name. + * + * @phpstan-param int<1, 12>|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|'10'|'11'|'12'|'01'|'02'|'03'|'04'|'05'|'06'|'07'|'08'|'09' $month_number */ public function get_month_genitive( $month_number ) { return $this->month_genitive[ zeroise( $month_number, 2 ) ]; @@ -369,6 +373,8 @@ public function get_month_genitive( $month_number ) { * * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version. * @return string Translated version + * + * @phpstan-param 'am'|'pm'|'AM'|'PM' $meridiem */ public function get_meridiem( $meridiem ) { return $this->meridiem[ $meridiem ]; diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 10449deeba5c5..d9927baffbcd5 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -258,6 +258,11 @@ function wp_date( $format, $timestamp = null, $timezone = null ) { } $datetime = date_create( '@' . $timestamp ); + + if ( false === $datetime ) { + return false; + } + $datetime->setTimezone( $timezone ); if ( empty( $wp_locale->month ) || empty( $wp_locale->weekday ) ) { @@ -269,7 +274,7 @@ function wp_date( $format, $timestamp = null, $timezone = null ) { $new_format = ''; $format_length = strlen( $format ); $month = $wp_locale->get_month( $datetime->format( 'm' ) ); - $weekday = $wp_locale->get_weekday( $datetime->format( 'w' ) ); + $weekday = $wp_locale->get_weekday( (int) $datetime->format( 'w' ) ); for ( $i = 0; $i < $format_length; $i++ ) { switch ( $format[ $i ] ) { diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 4c6945a62648d..bfb400cbda4cd 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -2666,7 +2666,7 @@ function get_calendar( $args = array() ) { } // week_begins = 0 stands for Sunday. - $week_begins = (int) get_option( 'start_of_week' ); + $week_begins = absint( get_option( 'start_of_week' ) ) % 7; // Read the current date. list( $current_year, $current_month, $current_day ) = array_map( 'intval', explode( '-', current_time( 'Y-m-j' ) ) ); @@ -3181,7 +3181,21 @@ function get_the_time( $format = '', $post = null ) { * @return string|int|false Formatted date string or Unix timestamp if `$format` is 'U' or 'G'. * False on failure. * - * @phpstan-return ( $format is 'U'|'G' ? int|false : string|false ) + * @phpstan-return ( + * $format is 'U'|'G' ? int|false : ( + * $format is 'w' ? '0'|'1'|'2'|'3'|'4'|'5'|'6'|false : ( + * $format is 'N' ? '1'|'2'|'3'|'4'|'5'|'6'|'7'|false : ( + * $format is 'L'|'I' ? '0'|'1'|false : ( + * $format is 't' ? '28'|'29'|'30'|'31'|false : ( + * $format is 'm'|'h' ? '01'|'02'|'03'|'04'|'05'|'06'|'07'|'08'|'09'|'10'|'11'|'12'|false : ( + * $format is 'n'|'g' ? '1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|'10'|'11'|'12'|false : ( + * $format is 'H' ? '00'|'01'|'02'|'03'|'04'|'05'|'06'|'07'|'08'|'09'|'10'|'11'|'12'|'13'|'14'|'15'|'16'|'17'|'18'|'19'|'20'|'21'|'22'|'23'|false : ( + * $format is 'd' ? '01'|'02'|'03'|'04'|'05'|'06'|'07'|'08'|'09'|'10'|'11'|'12'|'13'|'14'|'15'|'16'|'17'|'18'|'19'|'20'|'21'|'22'|'23'|'24'|'25'|'26'|'27'|'28'|'29'|'30'|'31'|false : ( + * $format is 'j' ? '1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|'10'|'11'|'12'|'13'|'14'|'15'|'16'|'17'|'18'|'19'|'20'|'21'|'22'|'23'|'24'|'25'|'26'|'27'|'28'|'29'|'30'|'31'|false : ( + * $format is 'z'|'W'|'o'|'Y'|'y'|'B'|'i'|'s'|'u'|'v'|'Z' ? numeric-string|false : + * string|false + * ) ) ) ) ) ) ) ) ) ) + * ) */ function get_post_time( $format = 'U', $gmt = false, $post = null, $translate = false ) { $post = get_post( $post ); @@ -3217,8 +3231,14 @@ function get_post_time( $format = 'U', $gmt = false, $post = null, $translate = /** * Filters the localized time of the post. * + * The filtered value should match the type and values that `$format` produces, + * for example, an integer for 'U' or 'G', or a numeric string from '0' to '6' for 'w'. + * * @since 2.6.0 * + * @see get_post_time() + * @link https://www.php.net/manual/en/datetime.format.php + * * @param string|int $time Formatted date string or Unix timestamp if `$format` is 'U' or 'G'. * @param string $format Format to use for retrieving the date of the post. * Accepts 'G', 'U', or PHP date format. @@ -3424,6 +3444,7 @@ function get_post_modified_time( $format = 'U', $gmt = false, $post = null, $tra * Displays the localized weekday for the post. * * @since 0.71 + * @since 7.2.0 Passes an empty string to the filter instead of 'Sunday' if the post date is invalid. * * @global WP_Locale $wp_locale WordPress date and time locale object. */ @@ -3436,7 +3457,12 @@ function the_weekday() { return; } - $the_weekday = $wp_locale->get_weekday( get_post_time( 'w', false, $post ) ); + $the_weekday = ''; + $weekday_number = get_post_time( 'w', false, $post ); + + if ( false !== $weekday_number ) { + $the_weekday = $wp_locale->get_weekday( (int) $weekday_number ); + } /** * Filters the localized weekday of the post, for display. @@ -3455,6 +3481,7 @@ function the_weekday() { * the previous one output. * * @since 0.71 + * @since 7.2.0 Outputs nothing instead of 'Sunday' if the post date is invalid. * * @global WP_Locale $wp_locale WordPress date and time locale object. * @global string $currentday The day of the current post in the loop. @@ -3475,10 +3502,15 @@ function the_weekday_date( $before = '', $after = '' ) { $the_weekday_date = ''; if ( $currentday !== $previousweekday ) { - $the_weekday_date .= $before; - $the_weekday_date .= $wp_locale->get_weekday( get_post_time( 'w', false, $post ) ); - $the_weekday_date .= $after; - $previousweekday = $currentday; + $weekday_number = get_post_time( 'w', false, $post ); + + if ( false !== $weekday_number ) { + $the_weekday_date .= $before; + $the_weekday_date .= $wp_locale->get_weekday( (int) $weekday_number ); + $the_weekday_date .= $after; + } + + $previousweekday = $currentday; } /** diff --git a/tests/phpstan/baselines/argument.type.neon b/tests/phpstan/baselines/argument.type.neon index ae21d3cfc7196..f84185d7d770c 100644 --- a/tests/phpstan/baselines/argument.type.neon +++ b/tests/phpstan/baselines/argument.type.neon @@ -838,11 +838,6 @@ parameters: identifier: argument.type count: 2 path: ../../../src/wp-includes/functions.php - - - message: '#^Parameter \#1 \$weekday_number of method WP_Locale\:\:get_weekday\(\) expects int, string given\.$#' - identifier: argument.type - count: 1 - path: ../../../src/wp-includes/functions.php - message: '#^Parameter \#2 \$fallback_url of function wp_validate_redirect expects string, false given\.$#' identifier: argument.type @@ -883,11 +878,6 @@ parameters: identifier: argument.type count: 2 path: ../../../src/wp-includes/general-template.php - - - message: '#^Parameter \#1 \$weekday_number of method WP_Locale\:\:get_weekday\(\) expects int, string\|false given\.$#' - identifier: argument.type - count: 2 - path: ../../../src/wp-includes/general-template.php - message: '#^Parameter \#2 \$replace of function str_replace expects array\\|string, int\<1, max\> given\.$#' identifier: argument.type diff --git a/tests/phpunit/tests/date/theDate.php b/tests/phpunit/tests/date/theDate.php index c87ac089e7add..61beb8d3ccda2 100644 --- a/tests/phpunit/tests/date/theDate.php +++ b/tests/phpunit/tests/date/theDate.php @@ -172,4 +172,50 @@ static function ( $output ) { the_weekday_date(); $this->assertSame( 'Filtered Wednesday', ob_get_clean() ); } + + /** + * Tests that the_weekday_date() does not retrieve the post time when the day has not changed. + * + * @ticket 64522 + */ + public function test_the_weekday_date_should_not_get_post_time_for_same_day() { + $GLOBALS['post'] = self::factory()->post->create_and_get( + array( + 'post_date' => '2015-09-16 08:00:00', + ) + ); + + $GLOBALS['currentday'] = '18.09.15'; + $GLOBALS['previousweekday'] = '18.09.15'; + + $filter = new MockAction(); + add_filter( 'get_post_time', array( $filter, 'filter' ) ); + + $weekday_date = get_echo( 'the_weekday_date' ); + $this->assertSame( '', $weekday_date, 'No weekday should be output for the same day.' ); + $this->assertSame( 0, $filter->get_call_count(), 'The post time should not be retrieved for the same day.' ); + } + + /** + * Tests that the_weekday_date() tracks the day change even when the post date is invalid. + * + * @ticket 64522 + */ + public function test_the_weekday_date_should_track_day_change_for_invalid_post_date() { + $post = self::factory()->post->create_and_get( + array( + 'post_date' => '2015-09-16 08:00:00', + ) + ); + $this->assertInstanceOf( WP_Post::class, $post ); + $post->post_date = ''; + + $GLOBALS['post'] = $post; + $GLOBALS['currentday'] = '18.09.15'; + $GLOBALS['previousweekday'] = '17.09.15'; + + $weekday_date = get_echo( 'the_weekday_date', array( 'before ', ' after' ) ); + $this->assertSame( '', $weekday_date, 'Nothing should be output for an invalid post date.' ); + $this->assertSame( '18.09.15', $GLOBALS['previousweekday'], 'The previous weekday should be updated to the current day.' ); + } } diff --git a/tests/phpunit/tests/date/wpDate.php b/tests/phpunit/tests/date/wpDate.php index cff2f11967f3b..b79fb0de6c090 100644 --- a/tests/phpunit/tests/date/wpDate.php +++ b/tests/phpunit/tests/date/wpDate.php @@ -34,6 +34,34 @@ public function test_should_return_false_on_invalid_timestamp() { $this->assertFalse( wp_date( DATE_RFC3339, 'invalid' ) ); } + /** + * Tests that a numeric timestamp which {@see date_create()} cannot parse returns false. + * + * @ticket 64522 + * + * @dataProvider data_should_return_false_on_unparsable_numeric_timestamp + * + * @param int|float|string $timestamp Numeric timestamp. + */ + public function test_should_return_false_on_unparsable_numeric_timestamp( $timestamp ): void { + $this->assertTrue( is_numeric( $timestamp ), 'Expected the timestamp to pass the is_numeric() check.' ); + $this->assertFalse( wp_date( DATE_RFC3339, $timestamp ) ); // @phpstan-ignore argument.type (Intentionally invalid input.) + } + + /** + * Data provider for {@see self::test_should_return_false_on_unparsable_numeric_timestamp()}. + * + * @return array + */ + public static function data_should_return_false_on_unparsable_numeric_timestamp(): array { + return array( + 'exponent notation' => array( '1e3' ), + 'out-of-range float' => array( 1e20 ), + 'leading whitespace' => array( ' 5' ), + 'leading decimal' => array( '.5' ), + ); + } + /** * @ticket 48319 */ diff --git a/tests/phpunit/tests/general/theWeekday.php b/tests/phpunit/tests/general/theWeekday.php index 3f33a84e7dc5a..35c5453e97772 100644 --- a/tests/phpunit/tests/general/theWeekday.php +++ b/tests/phpunit/tests/general/theWeekday.php @@ -75,4 +75,26 @@ static function ( $weekday ) { $this->expectOutputString( 'Custom Friday' ); the_weekday(); } + + /** + * Tests that the 'the_weekday' filter is applied with an empty string when the post date is invalid. + * + * @ticket 64522 + */ + public function test_the_weekday_filter_should_apply_for_invalid_post_date() { + global $post; + + $post = get_post( self::$post_id ); + $this->assertInstanceOf( WP_Post::class, $post ); + $post->post_date = ''; + + $filter = new MockAction(); + add_filter( 'the_weekday', array( $filter, 'filter' ) ); + + $weekday = get_echo( 'the_weekday' ); + + $this->assertSame( '', $weekday, 'Nothing should be output for an invalid post date.' ); + $this->assertSame( 1, $filter->get_call_count(), 'The the_weekday filter should be applied.' ); + $this->assertSame( array( '' ), $filter->get_args()[0], 'The the_weekday filter should receive an empty string.' ); + } }