Skip to content

Commit

Permalink
fix: resolves issues with archive lists and dates (#1325)
Browse files Browse the repository at this point in the history
* fix: resolves issues with archive lists and dates

* fix: lint

* fix: check for common date formats before assuming its a date

---------

Co-authored-by: Sebastian Thulin <[email protected]>
  • Loading branch information
sebastianthulin and Sebastian Thulin authored Feb 11, 2025
1 parent 76d5a15 commit 0cc028e
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions library/Controller/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ protected function getListItems(array $posts): array
'href' => WP::getPermalink($post->id),
'columns' => [
$post->postTitle,
$post->post_date = $post->getArchiveDateTimestamp()
$post->post_date = wp_date(get_option('date_format'), $post->getArchiveDateTimestamp())
]
];

Expand Down Expand Up @@ -657,8 +657,29 @@ private function prepareTaxonomyColumns(PostObjectInterface $post, $preparedPost
continue;
}

$termNames = array_map(fn($term) => $term->name, $terms);
array_unshift($termNames, $post->getArchiveDateTimestamp() ?? '');
$termNames = array_map(function ($term) {
$name = trim($term->name ?? '');

$datePatterns = [
'/^\d{4}-\d{2}-\d{2}$/', // YYYY-MM-DD
'/^\d{2}\/\d{2}\/\d{4}$/', // DD/MM/YYYY or MM/DD/YYYY
'/^\d{2}-\d{2}-\d{4}$/', // DD-MM-YYYY
'/^\w+ \d{4}$/', // "Month YYYY"
'/^\d{1,2} \p{L}+, \d{4}$/u', // "30 january, 2025"
];
foreach ($datePatterns as $pattern) {
if (preg_match($pattern, $name)) {
$timestamp = strtotime($name);

// Ensure strtotime() returned a valid timestamp
if ($timestamp && $timestamp > 0) {
return wp_date(get_option('date_format'), $timestamp);
}
}
}

return $name;
}, $terms);

$preparedPosts['items'][count($preparedPosts['items']) - 1]['columns'][$taxonomy] = join(', ', $termNames);
}
Expand Down

0 comments on commit 0cc028e

Please sign in to comment.