0.0, 'requests' => 0, 'tokens_in' => 0, 'tokens_out' => 0, 'models' => [], 'tasks' => [], 'over' => 0.0]; foreach (UsageTracker::all() as $day => $row) { if (strpos((string) $day, $month) !== 0) { continue; } $days++; $tot['neurons'] += (float) ($row['neurons'] ?? 0); $tot['requests'] += (int) ($row['requests'] ?? 0); $tot['tokens_in'] += (int) ($row['tokens_in'] ?? 0); $tot['tokens_out'] += (int) ($row['tokens_out'] ?? 0); $tot['over'] += max(0.0, (float) ($row['neurons'] ?? 0) - UsageTracker::FREE_TIER); foreach ((array) ($row['models'] ?? []) as $m => $n) { $tot['models'][$m] = ($tot['models'][$m] ?? 0) + (float) $n; } foreach ((array) ($row['tasks'] ?? []) as $t => $n) { $tot['tasks'][$t] = ($tot['tasks'][$t] ?? 0) + (float) $n; } } arsort($tot['models']); arsort($tot['tasks']); $paid = $tot['over'] / 1000 * UsageTracker::USD_PER_1K_NEURONS; return [ 'month' => $month, 'days' => $days, 'totals' => $tot, 'paid' => $paid, ]; } public static function body(array $r): string { $t = $r['totals']; $site = (string) wp_parse_url(home_url(), PHP_URL_HOST); $lines = []; /* translators: 1: the plugin's name (AIPCF), 2: site host name, 3: month and year */ $lines[] = sprintf(__('%1$s on %2$s, %3$s, on Cloudflare Workers AI', 'aipcf-ai-provider-for-cloudflare'), Brand::NAME, $site, self::monthName((string) $r['month'], 'F Y')); $lines[] = ''; /* translators: %s: number of requests */ $lines[] = sprintf(__('Requests: %s', 'aipcf-ai-provider-for-cloudflare'), number_format($t['requests'])); /* translators: 1: input tokens, 2: output tokens */ $lines[] = sprintf(__('Tokens: %1$s in, %2$s out', 'aipcf-ai-provider-for-cloudflare'), number_format($t['tokens_in']), number_format($t['tokens_out'])); $lines[] = sprintf( /* translators: 1: neurons, 2: number of days with usage */ _n('Neurons: %1$s across %2$d active day', 'Neurons: %1$s across %2$d active days', (int) $r['days'], 'aipcf-ai-provider-for-cloudflare'), number_format($t['neurons']), (int) $r['days'] ); $lines[] = ''; $lines[] = $r['paid'] > 0 /* translators: %s: US dollars */ ? sprintf(__('What you paid Cloudflare: $%s (usage above the free daily allowance of 10,000 neurons).', 'aipcf-ai-provider-for-cloudflare'), number_format($r['paid'], 2)) /* translators: %s: US dollars */ : sprintf(__('What you paid Cloudflare: $%s. Everything stayed inside the free daily allowance of 10,000 neurons.', 'aipcf-ai-provider-for-cloudflare'), number_format($r['paid'], 2)); $lines[] = ''; if ($t['tasks'] !== []) { $lines[] = __('By feature (neurons):', 'aipcf-ai-provider-for-cloudflare'); foreach (array_slice($t['tasks'], 0, 8, true) as $task => $n) { $lines[] = sprintf(' %-28s %s', task_label((string) $task), number_format($n)); } $lines[] = ''; } if ($t['models'] !== []) { $lines[] = __('By model (neurons):', 'aipcf-ai-provider-for-cloudflare'); foreach (array_slice($t['models'], 0, 6, true) as $m => $n) { $lines[] = sprintf(' %-28s %s', pretty_model_name((string) $m), number_format($n)); } $lines[] = ''; } /* translators: %s: settings page URL */ $lines[] = sprintf(__('Settings: %s', 'aipcf-ai-provider-for-cloudflare'), \WordPress\CloudflareWorkersAiProvider\Admin\SettingsPage::pageUrl()); $lines[] = ''; /* translators: %s: the plugin's full name */ $lines[] = sprintf(__('Sent by %s.', 'aipcf-ai-provider-for-cloudflare'), Brand::FULL); return implode("\n", $lines); } /** * A month ("2026-08") as a localized name, in UTC like the usage days. */ private static function monthName(string $month, string $format): string { $ts = strtotime($month . '-01 00:00:00 UTC'); return (string) wp_date($format, $ts !== false ? $ts : time(), new \DateTimeZone('UTC')); } public static function send(): void { $r = self::build(); if ($r['totals']['requests'] > 0) { $site = (string) wp_parse_url(home_url(), PHP_URL_HOST); $sent = wp_mail( (string) get_option('admin_email'), sprintf( /* translators: 1: site host name, 2: the plugin's name (AIPCF), 3: month name, 4: neurons, 5: US dollars */ __('[%1$s] %2$s usage for %3$s: %4$s neurons, $%5$s paid', 'aipcf-ai-provider-for-cloudflare'), $site, Brand::NAME, self::monthName((string) $r['month'], 'F'), number_format($r['totals']['neurons']), number_format($r['paid'], 2) ), self::body($r) ); if ($sent) { /** * After the monthly usage report is emailed. * * @param array $r The report. */ do_action('aipcf_report_sent', $r); } } // Re-arm for next month. $next = gmmktime(6, 0, 0, (int) gmdate('n') + 1, 1, (int) gmdate('Y')); if (!wp_next_scheduled(self::HOOK)) { wp_schedule_single_event($next, self::HOOK); } } public static function handlePreview(): void { if (!current_user_can('manage_options')) { wp_die(esc_html__('You do not have permission to do this.', 'aipcf-ai-provider-for-cloudflare'), 403); } check_admin_referer('aipcf_report_preview'); $r = self::build(gmdate('Y-m')); header('Content-Type: text/plain; charset=utf-8'); echo esc_html(self::body($r)); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- plain text response exit; } }