diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 056e8ffb43..f88398d0ee 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -209,8 +209,6 @@ add_filter( 'widget_text_content', 'wpautop' ); add_filter( 'widget_text_content', 'shortcode_unautop' ); add_filter( 'widget_text_content', 'do_shortcode', 11 ); // Runs after wpautop(); note that $post global will be null when shortcodes run. -add_filter( 'date_i18n', 'wp_maybe_decline_date' ); - // RSS filters add_filter( 'the_title_rss', 'strip_tags' ); add_filter( 'the_title_rss', 'ent2ncr', 8 ); diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index d89ff1663e..8f73812867 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -91,7 +91,7 @@ function current_datetime() { } /** - * Retrieves the timezone from current settings as a string. + * Retrieves the timezone from site settings as a string. * * Uses the `timezone_string` option to get a proper timezone if available, * otherwise falls back to an offset. @@ -120,7 +120,7 @@ function wp_timezone_string() { } /** - * Retrieves the timezone from current settings as a `DateTimeZone` object. + * Retrieves the timezone from site settings as a `DateTimeZone` object. * * Timezone can be based on a PHP timezone string or a ±HH:MM offset. * @@ -146,63 +146,101 @@ function wp_timezone() { * will lead to invalid output. * * @since 0.71 + * @since 5.3.0 Converted into a wrapper for wp_date(). * * @global WP_Locale $wp_locale WordPress date and time locale object. * - * @param string $dateformatstring Format to display the date. + * @param string $format Format to display the date. * @param int|bool $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset in seconds. * Default false. * @param bool $gmt Optional. Whether to use GMT timezone. Only applies if timestamp is * not provided. Default false. - * * @return string The date, translated if locale specifies it. */ -function date_i18n( $dateformatstring, $timestamp_with_offset = false, $gmt = false ) { - global $wp_locale; - - $i = $timestamp_with_offset; - - if ( ! is_numeric( $i ) ) { - $i = current_time( 'timestamp', $gmt ); - } - - /* - * Store original value for language with untypical grammars. - * See https://core.trac.wordpress.org/ticket/9396 - */ - $req_format = $dateformatstring; - $new_format = ''; - - // We need to unpack shorthand `r` format because it has parts that might be localized. - $dateformatstring = preg_replace( '/(?getTimestamp(), $timezone ); } - if ( ! $timestamp_mode && ! empty( $wp_locale->month ) && ! empty( $wp_locale->weekday ) ) { - $month = $wp_locale->get_month( $datetime->format( 'm' ) ); - $weekday = $wp_locale->get_weekday( $datetime->format( 'w' ) ); + /** + * Filters the date formatted based on the locale. + * + * @since 2.8.0 + * + * @param string $date Formatted date string. + * @param string $format Format to display the date. + * @param int $timestamp A sum of Unix timestamp and timezone offset in seconds. + * Might be without offset if input omitted timestamp but requested GMT. + * @param bool $gmt Whether to use GMT timezone. Only applies if timestamp was not provided. + * Default false. + */ + $date = apply_filters( 'date_i18n', $date, $format, $timestamp, $gmt ); - $format_length = strlen( $dateformatstring ); + return $date; +} + +/** + * Retrieves the date, in localized format. + * + * This is a newer function, intended to replace `date_i18n()` without legacy quirks in it. + * + * Note that, unlike `date_i18n()`, this function accepts a true Unix timestamp, not summed with timezone offset. + * + * @since 5.3.0 + * + * @param string $format PHP date format. + * @param int $timestamp Optional. Unix timestamp. Defaults to current time. + * @param DateTimeZone $timezone Optional. Timezone to output result in. Defaults to timezone from site settings. + * + * @return string The date, translated if locale specifies it. + */ +function wp_date( $format, $timestamp = null, $timezone = null ) { + global $wp_locale; + + if ( ! $timestamp ) { + $timestamp = time(); + } + + if ( ! $timezone ) { + $timezone = wp_timezone(); + } + + $datetime = date_create( '@' . $timestamp ); + $datetime->setTimezone( $timezone ); + + if ( empty( $wp_locale->month ) || empty( $wp_locale->weekday ) ) { + $date = $datetime->format( $format ); + } else { + // We need to unpack shorthand `r` format because it has parts that might be localized. + $format = preg_replace( '/(?get_month( $datetime->format( 'm' ) ); + $weekday = $wp_locale->get_weekday( $datetime->format( 'w' ) ); for ( $i = 0; $i < $format_length; $i ++ ) { - switch ( $dateformatstring[ $i ] ) { + switch ( $format[ $i ] ) { case 'D': $new_format .= backslashit( $wp_locale->get_weekday_abbrev( $weekday ) ); break; @@ -222,36 +260,37 @@ function date_i18n( $dateformatstring, $timestamp_with_offset = false, $gmt = fa $new_format .= backslashit( $wp_locale->get_meridiem( $datetime->format( 'A' ) ) ); break; case '\\': - $new_format .= $dateformatstring[ $i ]; + $new_format .= $format[ $i ]; // If character follows a slash, we add it without translating. if ( $i < $format_length ) { - $new_format .= $dateformatstring[ ++$i ]; + $new_format .= $format[ ++$i ]; } break; default: - $new_format .= $dateformatstring[ $i ]; + $new_format .= $format[ $i ]; break; } } - } - $j = $datetime->format( $new_format ); + $date = $datetime->format( $new_format ); + $date = wp_maybe_decline_date( $date ); + } /** * Filters the date formatted based on the locale. * - * @since 2.8.0 + * @since 5.3.0 + * + * @param string $date Formatted date string. + * @param string $format Format to display the date. + * @param int $timestamp Unix timestamp. + * @param DateTimeZone $timezone Timezone. * - * @param string $j Formatted date string. - * @param string $req_format Format to display the date. - * @param int $i A sum of Unix timestamp and timezone offset in seconds. - * @param bool $gmt Whether to use GMT timezone. Only applies if timestamp was - * not provided. Default false. */ - $j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt ); + $date = apply_filters( 'wp_date', $date, $format, $timestamp, $timezone ); - return $j; + return $date; } /**