Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/Bindings/DateFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Locale-independent date formatter for binding providers.
*
* @package Feedwright
*/

declare(strict_types=1);

namespace Feedwright\Bindings;

defined( 'ABSPATH' ) || exit;

/**
* Format Unix timestamps with the site timezone but English day/month names.
*
* `wp_date()` (and helpers like `get_the_date()` that wrap it) translate
* `D` / `M` / `l` / `F` format characters to the site locale. That breaks
* format strings such as `r` (RFC 2822) and `D, d M Y H:i:s O`, which the
* RFC 2822 / RFC 3339 specs require to be in English regardless of locale.
*
* This helper uses `DateTimeImmutable::format()` instead, which always
* produces English names — exactly matching PHP's bare `date()` behavior.
* Numeric formats (`Y`, `m`, `d`, etc.) are unaffected either way.
*/
final class DateFormatter {

/**
* Format a Unix timestamp as a string using the given PHP date format.
*
* @param int $timestamp Unix timestamp (UTC).
* @param string $format PHP date format string.
* @param \DateTimeZone|string|null $timezone Output timezone. Defaults to site timezone.
*/
public static function format( int $timestamp, string $format, $timezone = null ): string {
if ( null === $timezone ) {
$timezone = wp_timezone();
} elseif ( is_string( $timezone ) ) {
$timezone = new \DateTimeZone( $timezone );
}
$dt = ( new \DateTimeImmutable( '@' . $timestamp ) )->setTimezone( $timezone );
return $dt->format( $format );
}
}
3 changes: 2 additions & 1 deletion src/Bindings/Providers/FeedProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Feedwright\Renderer\Context;
use Feedwright\Routing\FeedEndpoint;
use Feedwright\Bindings\DateFormatter;
use Feedwright\Bindings\ProviderInterface;

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -86,7 +87,7 @@ private function compute_last_build_date( \WP_Post $feed_post ): int {
*/
private function format_date( int $timestamp, string $modifier ): string {
$format = '' === $modifier ? 'r' : $modifier;
return (string) wp_date( $format, $timestamp );
return DateFormatter::format( $timestamp, $format );
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Bindings/Providers/NowProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Feedwright\Bindings\Providers;

use Feedwright\Renderer\Context;
use Feedwright\Bindings\DateFormatter;
use Feedwright\Bindings\ProviderInterface;

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function resolve( string $path, string $modifier, Context $ctx ): ?string
return null;
}
$format = '' === $modifier ? 'c' : $modifier;
return (string) wp_date( $format );
return DateFormatter::format( time(), $format );
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/Bindings/Providers/PostProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Feedwright\Bindings\Providers;

use Feedwright\Renderer\Context;
use Feedwright\Bindings\DateFormatter;
use Feedwright\Bindings\ProviderInterface;

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -50,9 +51,15 @@ public function resolve( string $path, string $modifier, Context $ctx ): ?string
case 'post_excerpt':
return (string) get_the_excerpt( $post );
case 'post_date':
return (string) get_the_date( '' === $modifier ? 'c' : $modifier, $post );
return DateFormatter::format(
(int) get_post_time( 'U', true, $post ),
'' === $modifier ? 'c' : $modifier
);
case 'post_modified':
return (string) get_the_modified_date( '' === $modifier ? 'c' : $modifier, $post );
return DateFormatter::format(
(int) get_post_modified_time( 'U', true, $post ),
'' === $modifier ? 'c' : $modifier
);
case 'post_status':
return (string) get_post_status( $post );
case 'post_name':
Expand Down
3 changes: 2 additions & 1 deletion src/Bindings/Providers/PostRawProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Feedwright\Bindings\Providers;

use Feedwright\Renderer\Context;
use Feedwright\Bindings\DateFormatter;
use Feedwright\Bindings\ProviderInterface;

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -79,7 +80,7 @@ public function resolve( string $path, string $modifier, Context $ctx ): ?string
if ( false === $date_time ) {
return $raw;
}
return (string) wp_date( $modifier, $date_time->getTimestamp(), $timezone );
return DateFormatter::format( $date_time->getTimestamp(), $modifier, $timezone );
}

return null;
Expand Down
Loading