From 3c83c33816d5cba849cce58c7f8a4fc8308a8b99 Mon Sep 17 00:00:00 2001 From: TallblokeUK Date: Sun, 8 Nov 2026 11:14:57 +0000 Subject: [PATCH 1/2] fix: stop an unreadable changelog date from breaking admin pages --- src/php/Client/Welcome_Client.php | 17 ++----- tests/unit/Client/Welcome_Client_Test.php | 59 +++++++++++++++++++++++ 2 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 tests/unit/Client/Welcome_Client_Test.php diff --git a/src/php/Client/Welcome_Client.php b/src/php/Client/Welcome_Client.php index d71145901..a19b61e80 100644 --- a/src/php/Client/Welcome_Client.php +++ b/src/php/Client/Welcome_Client.php @@ -3,7 +3,6 @@ namespace Code_Snippets\Client; use DateTimeImmutable; -use Exception; use WP_Filesystem_Direct; use const Code_Snippets\PLUGIN_FILE; @@ -123,11 +122,9 @@ private static function safe_get_array( array $items, string $key ): array { * @return DateTimeImmutable|null */ private static function safe_parse_datetime( string $datetime ): ?DateTimeImmutable { - try { - return new DateTimeImmutable( $datetime ); - } catch ( Exception $e ) { - return null; - } + $parsed = date_create_immutable( $datetime ); + + return $parsed ? $parsed : null; } /** @@ -317,12 +314,8 @@ public static function read_changelog(): array { $version = trim( trim( $header_parts[0] ), '[]' ); $date = trim( trim( $header_parts[1] ?? '' ), '()' ); - try { - $datetime = new DateTimeImmutable( $date ); - $parsed_date = $datetime->format( get_option( 'date_format' ) ); - } catch ( Exception $e ) { - $parsed_date = $date; - } + $datetime = self::safe_parse_datetime( $date ); + $parsed_date = $datetime ? $datetime->format( get_option( 'date_format' ) ) : $date; $changelog[] = [ 'version' => $version, diff --git a/tests/unit/Client/Welcome_Client_Test.php b/tests/unit/Client/Welcome_Client_Test.php new file mode 100644 index 000000000..14fa19199 --- /dev/null +++ b/tests/unit/Client/Welcome_Client_Test.php @@ -0,0 +1,59 @@ +changelog_path() ) ) { + wp_delete_file( $this->changelog_path() ); + } + + parent::tear_down(); + } + + /** + * A release heading with a status label in place of a date is read with that label. + * + * @return void + */ + public function test_release_without_a_date_keeps_its_label(): void { + update_option( 'date_format', 'Y-m-d' ); + + require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; + require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; + + ( new WP_Filesystem_Direct( null ) )->put_contents( + $this->changelog_path(), + "# Changelog\n\n## [9.1.0] (UPCOMING)\n\n### Fixed\n* Second fix.\n\n## [9.0.0] (2026-11-05)\n\n### Added\n* First feature.\n" + ); + + $changelog = Welcome_Client::read_changelog(); + + $this->assertSame( [ '9.1.0', '9.0.0' ], array_column( $changelog, 'version' ) ); + $this->assertSame( [ 'UPCOMING', '2026-11-05' ], array_column( $changelog, 'date' ) ); + } +} From a58bf64af30e6aa74e6d71305d3fa833255b036d Mon Sep 17 00:00:00 2001 From: Shea Bunge Date: Fri, 20 Nov 2026 12:23:00 +1100 Subject: [PATCH 2/2] Update Welcome Client so changelog generation can be tested without involving the filesystem. --- src/php/Client/Welcome_Client.php | 18 +++++++++++--- tests/unit/Client/Welcome_Client_Test.php | 30 +---------------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/php/Client/Welcome_Client.php b/src/php/Client/Welcome_Client.php index a19b61e80..661a3cdf2 100644 --- a/src/php/Client/Welcome_Client.php +++ b/src/php/Client/Welcome_Client.php @@ -262,9 +262,6 @@ public static function read_changelog(): array { $filesystem = new WP_Filesystem_Direct( null ); $changelog_filename = 'CHANGELOG.md'; - $changelog = []; - - $section_titles = [ 'Added', 'Changed', 'Deprecated', 'Removed', 'Fixed', 'Security', 'Other' ]; $changelog_dir = plugin_dir_path( PLUGIN_FILE ); while ( plugin_dir_path( $changelog_dir ) !== $changelog_dir && ! $filesystem->exists( $changelog_dir . $changelog_filename ) ) { @@ -276,6 +273,21 @@ public static function read_changelog(): array { } $changelog_contents = $filesystem->get_contents( $changelog_dir . $changelog_filename ); + return self::parse_changelog_contents( $changelog_contents ); + } + + + /** + * Parse the contents of a changelog file into an array of releases. + * + * @param string $changelog_contents + * + * @return array>}> + */ + public static function parse_changelog_contents( string $changelog_contents ): array { + $changelog = []; + $section_titles = [ 'Added', 'Changed', 'Deprecated', 'Removed', 'Fixed', 'Security', 'Other' ]; + $changelog_releases = explode( "\n## ", $changelog_contents ); foreach ( array_slice( $changelog_releases, 1, self::MAX_CHANGELOG_ENTRIES ) as $changelog_release ) { diff --git a/tests/unit/Client/Welcome_Client_Test.php b/tests/unit/Client/Welcome_Client_Test.php index 14fa19199..a8d555024 100644 --- a/tests/unit/Client/Welcome_Client_Test.php +++ b/tests/unit/Client/Welcome_Client_Test.php @@ -13,28 +13,6 @@ */ class Welcome_Client_Test extends UnitTestCase { - /** - * Changelog placed in the plugin directory, found before any changelog further up. - * - * @return string - */ - private function changelog_path(): string { - return plugin_dir_path( PLUGIN_FILE ) . 'CHANGELOG.md'; - } - - /** - * Remove the test changelog after each test. - * - * @return void - */ - public function tear_down() { - if ( file_exists( $this->changelog_path() ) ) { - wp_delete_file( $this->changelog_path() ); - } - - parent::tear_down(); - } - /** * A release heading with a status label in place of a date is read with that label. * @@ -43,16 +21,10 @@ public function tear_down() { public function test_release_without_a_date_keeps_its_label(): void { update_option( 'date_format', 'Y-m-d' ); - require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; - require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; - - ( new WP_Filesystem_Direct( null ) )->put_contents( - $this->changelog_path(), + $changelog = Welcome_Client::parse_changelog_contents( "# Changelog\n\n## [9.1.0] (UPCOMING)\n\n### Fixed\n* Second fix.\n\n## [9.0.0] (2026-11-05)\n\n### Added\n* First feature.\n" ); - $changelog = Welcome_Client::read_changelog(); - $this->assertSame( [ '9.1.0', '9.0.0' ], array_column( $changelog, 'version' ) ); $this->assertSame( [ 'UPCOMING', '2026-11-05' ], array_column( $changelog, 'date' ) ); }