Extract read-only reporting methods from PRAutoBlogger_Cost_Tracker into a new PRAutoBlogger_Cost_Reporter class. This reduces Cost_Tracker from 313 to 206 lines and Reporter to 141 lines, both under the 300-line cap. Changed files: - Create includes/core/class-cost-reporter.php with reporting methods: * get_monthly_spend(): float * get_daily_spend(int $days = 30): array * get_spend_by_stage(string $start_date, string $end_date): array * get_budget_utilization(): float - Update includes/core/class-cost-tracker.php: * Remove reporting methods * Update is_budget_exceeded() and would_exceed_budget() to use Cost_Reporter * Keep get_current_run_cost() (tracks current pipeline run only) * Add @see cross-reference to Cost_Reporter - Update templates and admin classes to instantiate Cost_Reporter: * templates/admin/metrics-page.php * templates/admin/settings-page.php * includes/admin/class-admin-notices.php * includes/admin/class-dashboard-widget.php - Split tests/unit/Core/CostTrackerTest.php: * Create tests/unit/Core/CostReporterTest.php for reporter methods * Keep run ID and budget enforcement tests in CostTrackerTest Autoloader automatically discovers PRAutoBlogger_Cost_Reporter class. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
97 lines
2.7 KiB
PHP
Executable File
97 lines
2.7 KiB
PHP
Executable File
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Displays admin notices for onboarding, errors, and budget warnings.
|
|
*
|
|
* Triggered by: PRAutoBlogger::register_admin_hooks() on `admin_notices`.
|
|
* Dependencies: PRAutoBlogger_Cost_Reporter (for budget warnings).
|
|
*
|
|
* @see class-prautoblogger.php — Registers the hook.
|
|
*/
|
|
class PRAutoBlogger_Admin_Notices {
|
|
|
|
/**
|
|
* Display relevant admin notices.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function on_display_notices(): void {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
return;
|
|
}
|
|
|
|
$this->check_api_key_notice();
|
|
$this->check_budget_notice();
|
|
$this->check_subreddit_notice();
|
|
}
|
|
|
|
/**
|
|
* Show a notice if the OpenRouter API key is not configured.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function check_api_key_notice(): void {
|
|
$api_key = get_option( 'prautoblogger_openrouter_api_key', '' );
|
|
if ( '' !== $api_key ) {
|
|
return;
|
|
}
|
|
|
|
printf(
|
|
'<div class="notice notice-warning is-dismissible"><p>%s <a href="%s">%s</a></p></div>',
|
|
esc_html__( 'PRAutoBlogger: OpenRouter API key is not configured. Content generation is disabled.', 'prautoblogger' ),
|
|
esc_url( admin_url( 'admin.php?page=prautoblogger-settings' ) ),
|
|
esc_html__( 'Configure now', 'prautoblogger' )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Show a warning if monthly budget is approaching or exceeded.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function check_budget_notice(): void {
|
|
$cost_reporter = new PRAutoBlogger_Cost_Reporter();
|
|
$utilization = $cost_reporter->get_budget_utilization();
|
|
|
|
if ( $utilization >= 100.0 ) {
|
|
printf(
|
|
'<div class="notice notice-error"><p>%s</p></div>',
|
|
esc_html__( 'PRAutoBlogger: Monthly API budget EXCEEDED. Content generation is paused until next month or budget increase.', 'prautoblogger' )
|
|
);
|
|
} elseif ( $utilization >= 80.0 ) {
|
|
printf(
|
|
'<div class="notice notice-warning"><p>%s</p></div>',
|
|
esc_html( sprintf(
|
|
/* translators: %s: budget utilization percentage */
|
|
__( 'PRAutoBlogger: Monthly API budget is at %.0f%%. Consider increasing the budget or reducing daily article count.', 'prautoblogger' ),
|
|
$utilization
|
|
) )
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show a notice if no subreddits are configured.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function check_subreddit_notice(): void {
|
|
$subreddits = json_decode( get_option( 'prautoblogger_target_subreddits', '[]' ), true );
|
|
if ( ! empty( $subreddits ) ) {
|
|
return;
|
|
}
|
|
|
|
// Only show on our settings page.
|
|
$screen = get_current_screen();
|
|
if ( ! $screen || 'toplevel_page_prautoblogger-settings' !== $screen->id ) {
|
|
return;
|
|
}
|
|
|
|
printf(
|
|
'<div class="notice notice-info is-dismissible"><p>%s</p></div>',
|
|
esc_html__( 'PRAutoBlogger: No subreddits configured. Add target subreddits in Source Configuration to start collecting data.', 'prautoblogger' )
|
|
);
|
|
}
|
|
}
|