fix(settings): wp_date() respects WP timezone; clarify counter is not atomic

- Replace date('Y-m-d') with wp_date('Y-m-d') in increment_drop_counter and
  default_drop_counters to respect WordPress timezone settings.
- Update corresponding test assertions to use wp_date() for consistency.
- Clarify in docblock that increment_drop_counter is not atomic under concurrent
  submissions; read-modify-write pattern via update_option is acceptable for
  low-volume dashboard counters.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vladimir Bryzgalov
2026-04-21 16:25:30 +05:00
parent ae70b42dcb
commit 0034531a3a
2 changed files with 8 additions and 5 deletions
+6 -3
View File
@@ -125,15 +125,18 @@ final class Settings
} }
/** /**
* Atomically increments the drop counter. `$mode` must be 'real' or 'dry_run'; * Increments the drop counter. `$mode` must be 'real' or 'dry_run';
* any other value is a no-op (defensive — guards against typos in callers). * any other value is a no-op (defensive — guards against typos in callers).
* Performs rolling today→yesterday on date change. * Performs rolling today→yesterday on date change.
*
* Note: not atomic under concurrent CF7 submissions — `update_option` is a
* read-modify-write. Acceptable for low-volume dashboard counters.
*/ */
public static function increment_drop_counter(string $mode): void public static function increment_drop_counter(string $mode): void
{ {
if (!in_array($mode, ['real', 'dry_run'], true)) return; if (!in_array($mode, ['real', 'dry_run'], true)) return;
$c = self::get_drop_counters(); $c = self::get_drop_counters();
$today = date('Y-m-d'); $today = wp_date('Y-m-d');
if ($c['today_date'] !== $today) { if ($c['today_date'] !== $today) {
$c['real_yesterday'] = $c['real_today']; $c['real_yesterday'] = $c['real_today'];
$c['dry_yesterday'] = $c['dry_today']; $c['dry_yesterday'] = $c['dry_today'];
@@ -157,7 +160,7 @@ final class Settings
private static function default_drop_counters(): array private static function default_drop_counters(): array
{ {
$today = date('Y-m-d'); $today = wp_date('Y-m-d');
return [ return [
'total' => 0, 'total' => 0,
'real_today' => 0, 'real_today' => 0,
+2 -2
View File
@@ -49,7 +49,7 @@ final class SettingsCounterTest extends Cf7stgTestCase
Settings::increment_drop_counter('real'); Settings::increment_drop_counter('real');
$c = Settings::get_drop_counters(); $c = Settings::get_drop_counters();
self::assertSame(date('Y-m-d'), $c['today_date']); self::assertSame(wp_date('Y-m-d'), $c['today_date']);
self::assertSame(5, $c['real_yesterday']); self::assertSame(5, $c['real_yesterday']);
self::assertSame(1, $c['real_today']); self::assertSame(1, $c['real_today']);
self::assertSame(6, $c['total']); self::assertSame(6, $c['total']);
@@ -64,7 +64,7 @@ final class SettingsCounterTest extends Cf7stgTestCase
self::assertSame(0, $c['total']); self::assertSame(0, $c['total']);
self::assertSame(0, $c['real_today']); self::assertSame(0, $c['real_today']);
self::assertSame(0, $c['dry_today']); self::assertSame(0, $c['dry_today']);
self::assertSame(date('Y-m-d'), $c['reset_at']); self::assertSame(wp_date('Y-m-d'), $c['reset_at']);
} }
public function test_get_dry_run_drop_default_true_when_unset(): void public function test_get_dry_run_drop_default_true_when_unset(): void