From a1fa99e91d66c9b652285fa4adc4548947baf378 Mon Sep 17 00:00:00 2001 From: Ramon Corrales Date: Thu, 26 Mar 2026 13:00:44 -0500 Subject: [PATCH 1/3] fix(post-date): preserve classic theme markup and fix archive titles Co-Authored-By: Claude Opus 4.6 (1M context) --- includes/class-post-date.php | 28 ++++++++++------------- src/other-scripts/relative-time/index.js | 4 +++- tests/unit-tests/class-post-date-test.php | 5 ++++ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/includes/class-post-date.php b/includes/class-post-date.php index f067cd6b46..41c76d48a1 100644 --- a/includes/class-post-date.php +++ b/includes/class-post-date.php @@ -52,7 +52,7 @@ public static function init() { add_filter( 'newspack_blocks_formatted_displayed_post_date', [ __CLASS__, 'filter_blocks_formatted_date' ], 10, 2 ); add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_scripts' ] ); add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'enqueue_editor_assets' ] ); - add_action( 'newspack_theme_after_posted_on', [ __CLASS__, 'render_updated_date_classic' ] ); + add_action( 'newspack_theme_posted_on', [ __CLASS__, 'render_updated_date_classic' ] ); } /** @@ -257,6 +257,12 @@ public static function filter_get_the_date( $the_date, $format, $post ) { return $the_date; } + // Only convert dates in the loop on singular views to avoid affecting + // archive titles (e.g. "Daily Archives: 2 days ago") and other contexts. + if ( ! in_the_loop() ) { + return $the_date; + } + $time_ago = self::convert_to_time_ago( $post->post_date_gmt, self::get_time_ago_cutoff_days() ); return null !== $time_ago ? $time_ago : $the_date; @@ -281,7 +287,7 @@ public static function filter_blocks_formatted_date( $date, $post ) { /** * Render updated date for classic (non-block) themes. - * Hooked to `newspack_theme_after_posted_on` which fires after `newspack_posted_on()`. + * Hooked to `newspack_theme_posted_on` which fires inside `newspack_posted_on()`. */ public static function render_updated_date_classic() { if ( wp_is_block_theme() || ! is_singular() ) { @@ -306,13 +312,11 @@ public static function render_updated_date_classic() { } } - /* translators: %s: Modified date. */ - $label = sprintf( esc_html__( 'Updated %s', 'newspack-plugin' ), $modified_date ); - printf( - '', + '%1$s ', + esc_html__( 'Updated', 'newspack-plugin' ), esc_attr( get_the_modified_date( DATE_W3C, $post ) ), - wp_kses_post( $label ) + esc_html( $modified_date ) ); } @@ -382,17 +386,9 @@ public static function migrate_date_settings( $old_name, $old_theme ) { } /** - * Enqueue relative-time script and updated date styles on frontend. + * Enqueue relative-time script on frontend. */ public static function enqueue_scripts() { - // Inline styles for the classic theme updated date. - // Always enqueue on classic themes since per-post overrides can show the date even when sitewide is off. - if ( ! wp_is_block_theme() ) { - wp_register_style( 'newspack-post-date', false, [], NEWSPACK_PLUGIN_VERSION ); - wp_enqueue_style( 'newspack-post-date' ); - wp_add_inline_style( 'newspack-post-date', '.entry-meta .updated-date { margin-inline-start: 1em; }' ); - } - if ( ! get_theme_mod( 'post_time_ago', false ) ) { return; } diff --git a/src/other-scripts/relative-time/index.js b/src/other-scripts/relative-time/index.js index 1c93955d94..18e4b8d71f 100644 --- a/src/other-scripts/relative-time/index.js +++ b/src/other-scripts/relative-time/index.js @@ -58,7 +58,9 @@ return; // No Intl support. } - const elements = document.querySelectorAll( '.wp-block-post-date time[datetime], time.entry-date[datetime], .comment-meta time[datetime]' ); + const elements = document.querySelectorAll( + '.wp-block-post-date time[datetime], time.entry-date[datetime], time.updated[datetime], .comment-meta time[datetime]' + ); const now = Date.now(); elements.forEach( function ( el ) { diff --git a/tests/unit-tests/class-post-date-test.php b/tests/unit-tests/class-post-date-test.php index 4cbbc693ea..307559ce3e 100644 --- a/tests/unit-tests/class-post-date-test.php +++ b/tests/unit-tests/class-post-date-test.php @@ -157,8 +157,13 @@ public function test_get_the_date_filter_enabled() { ] ); + // Simulate being in the loop so the filter applies. + $GLOBALS['wp_query']->in_the_loop = true; + $date = get_the_date( '', $post_id ); $this->assertStringContainsString( 'ago', $date, 'get_the_date should return relative date when feature is on.' ); + + $GLOBALS['wp_query']->in_the_loop = false; } /** From b551ae066aa1db699b889e537a2c09adebd20f83 Mon Sep 17 00:00:00 2001 From: Ramon Corrales Date: Thu, 26 Mar 2026 13:25:14 -0500 Subject: [PATCH 2/3] fix(post-date): address Copilot review feedback Co-Authored-By: Claude Opus 4.6 (1M context) --- includes/class-post-date.php | 4 ++-- src/other-scripts/relative-time/index.js | 2 +- tests/unit-tests/class-post-date-test.php | 29 +++++++++++++++++++---- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/includes/class-post-date.php b/includes/class-post-date.php index 41c76d48a1..681d36d2f5 100644 --- a/includes/class-post-date.php +++ b/includes/class-post-date.php @@ -257,8 +257,8 @@ public static function filter_get_the_date( $the_date, $format, $post ) { return $the_date; } - // Only convert dates in the loop on singular views to avoid affecting - // archive titles (e.g. "Daily Archives: 2 days ago") and other contexts. + // Only convert dates in the loop to avoid affecting archive titles + // (e.g. "Daily Archives: 2 days ago") and other contexts. if ( ! in_the_loop() ) { return $the_date; } diff --git a/src/other-scripts/relative-time/index.js b/src/other-scripts/relative-time/index.js index 18e4b8d71f..ee7259a405 100644 --- a/src/other-scripts/relative-time/index.js +++ b/src/other-scripts/relative-time/index.js @@ -74,7 +74,7 @@ el.setAttribute( 'title', new Date( datetime ).toLocaleString( localeTag ) ); } - // Only replace text on publish dates, not modified date blocks. + // Skip block-theme modified dates (label is inside