diff --git a/includes/class-e-reader.php b/includes/class-e-reader.php index 25fa078..5378ac8 100644 --- a/includes/class-e-reader.php +++ b/includes/class-e-reader.php @@ -142,7 +142,7 @@ protected function get_content( $format, \WP_Post $post ) { array( 'title' => $post_title, 'author' => $post->author_name, - 'date' => get_the_time( 'l, F j, Y', $post ), + 'date' => $this->get_post_display_date( $post, 'l, F j, Y' ), ) ); @@ -161,6 +161,29 @@ protected function get_content( $format, \WP_Post $post ) { return $content; } + /** + * Get the date to show for a post in generated e-reader content. + * + * Collected posts can preserve the original article date as metadata. Prefer + * that value in the chapter byline, while leaving ordinary posts on their + * WordPress publish date. + * + * @param \WP_Post $post The post. + * @param string $format Date format. + * @return string Formatted date. + */ + protected function get_post_display_date( \WP_Post $post, $format ) { + $published_time = get_post_meta( $post->ID, 'published_time', true ); + if ( $published_time ) { + $timestamp = is_numeric( $published_time ) ? (int) $published_time : strtotime( (string) $published_time ); + if ( $timestamp ) { + return date_i18n( $format, $timestamp ); + } + } + + return get_the_time( $format, $post ); + } + protected function update_author_name( \WP_Post $post ) { if ( ! isset( $post->author_name ) ) { $author = self::get_post_author( $post ); diff --git a/tests/Test_E_Reader.php b/tests/Test_E_Reader.php index 3f44ad7..9473f45 100644 --- a/tests/Test_E_Reader.php +++ b/tests/Test_E_Reader.php @@ -186,6 +186,22 @@ function () { $this->assertSame( 'Zetaphor', $post->author_name ); } + /** + * Test collected article dates replace the saved post date in chapter bylines. + */ + public function test_collected_article_date_replaces_post_date_in_chapter_byline() { + $post = new \WP_Post(); + $post->ID = 124; + $post->post_author = 1; + update_post_meta( $post->ID, 'published_time', '2026-06-29T16:44:13+00:00' ); + + $ereader = new E_Reader_Download( 'Test' ); + $method = new \ReflectionMethod( $ereader, 'get_post_display_date' ); + $method->setAccessible( true ); + + $this->assertSame( 'Monday, June 29, 2026', $method->invoke( $ereader, $post, 'l, F j, Y' ) ); + } + /** * Test same-site upload image URLs are embedded in generated ePubs. */