Skip to content
Open
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
35 changes: 20 additions & 15 deletions src/php/Client/Welcome_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Code_Snippets\Client;

use DateTimeImmutable;
use Exception;
use WP_Filesystem_Direct;
use const Code_Snippets\PLUGIN_FILE;

Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -265,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 ) ) {
Expand All @@ -279,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<int, array{version: string, date: string, entries: array<string, array<string, string[]>>}>
*/
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 ) {
Expand Down Expand Up @@ -317,12 +326,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,
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/Client/Welcome_Client_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Code_Snippets\Client;

use Code_Snippets\UnitTestCase;
use WP_Filesystem_Direct;
use const Code_Snippets\PLUGIN_FILE;

/**
* Tests for reading the welcome screen data.
*
* @group welcome
*/
class Welcome_Client_Test extends UnitTestCase {

/**
* 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' );

$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"
);

$this->assertSame( [ '9.1.0', '9.0.0' ], array_column( $changelog, 'version' ) );
$this->assertSame( [ 'UPCOMING', '2026-11-05' ], array_column( $changelog, 'date' ) );
}
}
Loading