Files
2026-07-02 15:54:39 -06:00

61 lines
1.9 KiB
PHP

<?php
/**
* Failure_Surface - makes unresolved failures impossible to miss even if email
* fails: an admin-bar badge and an admin notice with the count of failed/pending
* records in the active environment. The count is cached in a short transient and
* invalidated on any transaction status write.
*
* @package ESP\Instanda
*/
declare(strict_types=1);
namespace ESP\Instanda;
final class Failure_Surface {
private const CACHE_KEY = 'espinstanda_failure_count';
private const CACHE_TTL = 300;
public function admin_bar_badge(\WP_Admin_Bar $bar): void {
$count = $this->count();
if ($count <= 0) {
return;
}
$bar->add_node([
'id' => 'espinstanda-failures',
'title' => sprintf('INSTANDA: %d need attention', $count),
'href' => admin_url('admin.php?page=espinstanda-transactions&status=failed'),
'meta' => ['class' => 'espinstanda-failure-badge'],
]);
}
public function unresolved_notice(): void {
$count = $this->count();
if ($count <= 0) {
return;
}
$url = admin_url('admin.php?page=espinstanda-transactions&status=failed');
printf(
'<div class="notice notice-error"><p><strong>ESP INSTANDA:</strong> %d submission(s) need attention. <a href="%s">Review</a></p></div>',
$count,
esc_url($url)
);
}
public function count(): int {
$cached = get_transient(self::CACHE_KEY);
if ($cached !== false) {
return (int) $cached;
}
$count = (new Transaction_Store())->unresolved_count();
set_transient(self::CACHE_KEY, $count, self::CACHE_TTL);
return $count;
}
/** Invalidate the cached count after any status write. */
public static function bump(): void {
delete_transient(self::CACHE_KEY);
}
}