Compare commits
14 Commits
claude/ima
...
refactor/s
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f81ae1fdaa | ||
|
|
ebf4f87f98 | ||
|
|
e7d498574f | ||
|
|
49d094b4f8 | ||
|
|
7197249cab | ||
|
|
364d246863 | ||
|
|
941fc96c53 | ||
|
|
e60cb83770 | ||
|
|
5e7fd882bc | ||
|
|
4f13afbb77 | ||
|
|
38157f0b41 | ||
|
|
76b258bcc3 | ||
|
|
f88d752a79 | ||
|
|
2e41c3f2f6 |
@@ -5,7 +5,7 @@ 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_Tracker (for budget warnings).
|
||||
* Dependencies: PRAutoBlogger_Cost_Reporter (for budget warnings).
|
||||
*
|
||||
* @see class-prautoblogger.php — Registers the hook.
|
||||
*/
|
||||
@@ -51,8 +51,8 @@ class PRAutoBlogger_Admin_Notices {
|
||||
* @return void
|
||||
*/
|
||||
private function check_budget_notice(): void {
|
||||
$cost_tracker = new PRAutoBlogger_Cost_Tracker();
|
||||
$utilization = $cost_tracker->get_budget_utilization();
|
||||
$cost_reporter = new PRAutoBlogger_Cost_Reporter();
|
||||
$utilization = $cost_reporter->get_budget_utilization();
|
||||
|
||||
if ( $utilization >= 100.0 ) {
|
||||
printf(
|
||||
|
||||
@@ -8,7 +8,7 @@ declare(strict_types=1);
|
||||
* and budget utilization.
|
||||
*
|
||||
* Triggered by: Could be registered via wp_dashboard_setup hook.
|
||||
* Dependencies: PRAutoBlogger_Cost_Tracker.
|
||||
* Dependencies: PRAutoBlogger_Cost_Reporter.
|
||||
*
|
||||
* @see class-prautoblogger.php — Can register this on wp_dashboard_setup.
|
||||
*/
|
||||
@@ -37,10 +37,10 @@ class PRAutoBlogger_Dashboard_Widget {
|
||||
* @return void
|
||||
*/
|
||||
public function render_widget(): void {
|
||||
$cost_tracker = new PRAutoBlogger_Cost_Tracker();
|
||||
$monthly_spend = $cost_tracker->get_monthly_spend();
|
||||
$cost_reporter = new PRAutoBlogger_Cost_Reporter();
|
||||
$monthly_spend = $cost_reporter->get_monthly_spend();
|
||||
$budget = (float) get_option( 'prautoblogger_monthly_budget_usd', 50.00 );
|
||||
$utilization = $cost_tracker->get_budget_utilization();
|
||||
$utilization = $cost_reporter->get_budget_utilization();
|
||||
|
||||
$next_run = wp_next_scheduled( 'prautoblogger_daily_generation' );
|
||||
|
||||
|
||||
141
includes/core/class-cost-reporter.php
Normal file
141
includes/core/class-cost-reporter.php
Normal file
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Provides historical cost reporting and analysis methods.
|
||||
*
|
||||
* Extracted from PRAutoBlogger_Cost_Tracker, this class handles all read-only
|
||||
* reporting queries: monthly spend, daily spend trends, spend-by-stage breakdowns,
|
||||
* and budget utilization percentage.
|
||||
*
|
||||
* Called by: Metrics dashboard, admin notices, and dashboard widget.
|
||||
* Dependencies: WordPress $wpdb, get_option() for budget configuration.
|
||||
*
|
||||
* @see core/class-cost-tracker.php — Complementary class for logging and budget enforcement.
|
||||
* @see admin/class-metrics-page.php — Displays cost reports.
|
||||
* @see admin/class-admin-notices.php — Shows budget warnings via get_budget_utilization().
|
||||
* @see admin/class-dashboard-widget.php — Displays current month costs.
|
||||
*/
|
||||
class PRAutoBlogger_Cost_Reporter {
|
||||
|
||||
/**
|
||||
* Get total estimated spend for the current calendar month.
|
||||
*
|
||||
* @return float Total USD spend this month.
|
||||
*/
|
||||
public function get_monthly_spend(): float {
|
||||
global $wpdb;
|
||||
if ( null === $wpdb ) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
$table = $wpdb->prefix . 'prautoblogger_generation_log';
|
||||
|
||||
$first_of_month = gmdate( 'Y-m-01 00:00:00' );
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$result = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"SELECT COALESCE(SUM(estimated_cost), 0) FROM {$table} WHERE created_at >= %s AND response_status = 'success'",
|
||||
$first_of_month
|
||||
)
|
||||
);
|
||||
|
||||
return (float) $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get daily spend for the last N days (for the metrics dashboard chart).
|
||||
*
|
||||
* @param int $days Number of days to look back.
|
||||
*
|
||||
* @return array<string, float> Associative array of date => total_cost.
|
||||
*/
|
||||
public function get_daily_spend( int $days = 30 ): array {
|
||||
global $wpdb;
|
||||
if ( null === $wpdb ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$table = $wpdb->prefix . 'prautoblogger_generation_log';
|
||||
|
||||
$start_date = gmdate( 'Y-m-d', time() - ( $days * DAY_IN_SECONDS ) );
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$results = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT DATE(created_at) as day, SUM(estimated_cost) as total_cost
|
||||
FROM {$table}
|
||||
WHERE created_at >= %s AND response_status = 'success'
|
||||
GROUP BY DATE(created_at)
|
||||
ORDER BY day ASC",
|
||||
$start_date . ' 00:00:00'
|
||||
),
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
$daily = [];
|
||||
foreach ( ( $results ?: [] ) as $row ) {
|
||||
$daily[ $row['day'] ] = (float) $row['total_cost'];
|
||||
}
|
||||
|
||||
return $daily;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get spend breakdown by pipeline stage for a given period.
|
||||
*
|
||||
* @param string $start_date Start date (Y-m-d).
|
||||
* @param string $end_date End date (Y-m-d).
|
||||
*
|
||||
* @return array<string, array{cost: float, tokens: int, calls: int}> Breakdown by stage.
|
||||
*/
|
||||
public function get_spend_by_stage( string $start_date, string $end_date ): array {
|
||||
global $wpdb;
|
||||
if ( null === $wpdb ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$table = $wpdb->prefix . 'prautoblogger_generation_log';
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$results = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT stage,
|
||||
SUM(estimated_cost) as total_cost,
|
||||
SUM(prompt_tokens + completion_tokens) as total_tokens,
|
||||
COUNT(*) as call_count
|
||||
FROM {$table}
|
||||
WHERE created_at BETWEEN %s AND %s AND response_status = 'success'
|
||||
GROUP BY stage",
|
||||
$start_date . ' 00:00:00',
|
||||
$end_date . ' 23:59:59'
|
||||
),
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
$breakdown = [];
|
||||
foreach ( ( $results ?: [] ) as $row ) {
|
||||
$breakdown[ $row['stage'] ] = [
|
||||
'cost' => (float) $row['total_cost'],
|
||||
'tokens' => (int) $row['total_tokens'],
|
||||
'calls' => (int) $row['call_count'],
|
||||
];
|
||||
}
|
||||
|
||||
return $breakdown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get budget utilization percentage for the current month.
|
||||
*
|
||||
* @return float Percentage (0-100+). Can exceed 100 if overspent.
|
||||
*/
|
||||
public function get_budget_utilization(): float {
|
||||
$budget = (float) get_option( 'prautoblogger_monthly_budget_usd', 50.00 );
|
||||
if ( $budget <= 0 ) {
|
||||
return 0.0;
|
||||
}
|
||||
return ( $this->get_monthly_spend() / $budget ) * 100.0;
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,7 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Tracks all API costs, enforces monthly budget limits, and provides
|
||||
* cost reporting data for the admin dashboard.
|
||||
* Tracks all API costs and enforces monthly budget limits.
|
||||
*
|
||||
* Every LLM API call in the plugin is logged through this class. The budget
|
||||
* enforcement is a hard stop — if the monthly budget is exceeded, no further
|
||||
@@ -16,7 +15,8 @@ declare(strict_types=1);
|
||||
* @see core/class-content-analyzer.php — Calls log_api_call() after analysis.
|
||||
* @see core/class-content-generator.php — Calls log_api_call() after each stage.
|
||||
* @see core/class-chief-editor.php — Calls log_api_call() after review.
|
||||
* @see admin/class-metrics-page.php — Displays cost data from this class.
|
||||
* @see core/class-cost-reporter.php — Extracted reporting methods (read-only queries).
|
||||
* @see admin/class-metrics-page.php — Displays cost data via Cost_Reporter.
|
||||
* @see ARCHITECTURE.md — prab_generation_log table schema.
|
||||
*/
|
||||
class PRAutoBlogger_Cost_Tracker {
|
||||
@@ -127,7 +127,8 @@ class PRAutoBlogger_Cost_Tracker {
|
||||
return false;
|
||||
}
|
||||
|
||||
$monthly_spend = $this->get_monthly_spend();
|
||||
$reporter = new PRAutoBlogger_Cost_Reporter();
|
||||
$monthly_spend = $reporter->get_monthly_spend();
|
||||
|
||||
// Round to 4 decimal places (0.01 cent precision) to avoid
|
||||
// floating-point representation artifacts triggering false positives.
|
||||
@@ -135,25 +136,63 @@ class PRAutoBlogger_Cost_Tracker {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total estimated spend for the current calendar month.
|
||||
* Log an image generation API call with a known cost.
|
||||
*
|
||||
* @return float Total USD spend this month.
|
||||
* Unlike log_api_call() which calculates cost from token counts, image
|
||||
* generation uses per-megapixel pricing that the image provider already
|
||||
* computed. This method accepts the pre-calculated cost directly.
|
||||
*
|
||||
* @param float $cost_usd Estimated cost in USD (from image provider pricing).
|
||||
* @param string $model Image model alias (e.g. 'flux-1-schnell').
|
||||
* @param int $post_id WordPress post ID the image is attached to.
|
||||
* @param string $stage Pipeline stage ('image_a' or 'image_b').
|
||||
* @return void
|
||||
*/
|
||||
public function get_monthly_spend(): float {
|
||||
public function log_image_generation( float $cost_usd, string $model, int $post_id, string $stage ): void {
|
||||
$this->current_run_cost += $cost_usd;
|
||||
|
||||
global $wpdb;
|
||||
if ( null === $wpdb ) {
|
||||
return;
|
||||
}
|
||||
$table = $wpdb->prefix . 'prautoblogger_generation_log';
|
||||
|
||||
$first_of_month = gmdate( 'Y-m-01 00:00:00' );
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$result = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"SELECT COALESCE(SUM(estimated_cost), 0) FROM {$table} WHERE created_at >= %s AND response_status = 'success'",
|
||||
$first_of_month
|
||||
)
|
||||
);
|
||||
$wpdb->insert( $table, [
|
||||
'post_id' => $post_id,
|
||||
'run_id' => $this->run_id,
|
||||
'stage' => $stage,
|
||||
'provider' => 'cloudflare-workers-ai',
|
||||
'model' => $model,
|
||||
'prompt_tokens' => 0,
|
||||
'completion_tokens' => 0,
|
||||
'estimated_cost' => $cost_usd,
|
||||
'response_status' => 'success',
|
||||
'error_message' => '',
|
||||
'created_at' => current_time( 'mysql' ),
|
||||
] );
|
||||
}
|
||||
|
||||
return (float) $result;
|
||||
/**
|
||||
* Check if adding an estimated cost would exceed the monthly budget.
|
||||
*
|
||||
* Unlike is_budget_exceeded() which checks current state, this method
|
||||
* proactively checks whether a planned expenditure would push spend
|
||||
* over the budget. Used by the image pipeline to pre-check before
|
||||
* making an API call.
|
||||
*
|
||||
* @param float $estimated_cost_usd Estimated cost in USD for the planned operation.
|
||||
* @return bool True if (current spend + estimated cost) >= configured budget.
|
||||
*/
|
||||
public function would_exceed_budget( float $estimated_cost_usd ): bool {
|
||||
$budget = (float) get_option( 'prautoblogger_monthly_budget_usd', 50.00 );
|
||||
if ( $budget <= 0 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$reporter = new PRAutoBlogger_Cost_Reporter();
|
||||
$projected = $reporter->get_monthly_spend() + $estimated_cost_usd;
|
||||
return round( $projected, 4 ) >= round( $budget, 4 );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,91 +203,4 @@ class PRAutoBlogger_Cost_Tracker {
|
||||
public function get_current_run_cost(): float {
|
||||
return $this->current_run_cost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get daily spend for the last N days (for the metrics dashboard chart).
|
||||
*
|
||||
* @param int $days Number of days to look back.
|
||||
*
|
||||
* @return array<string, float> Associative array of date => total_cost.
|
||||
*/
|
||||
public function get_daily_spend( int $days = 30 ): array {
|
||||
global $wpdb;
|
||||
$table = $wpdb->prefix . 'prautoblogger_generation_log';
|
||||
|
||||
$start_date = gmdate( 'Y-m-d', time() - ( $days * DAY_IN_SECONDS ) );
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$results = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT DATE(created_at) as day, SUM(estimated_cost) as total_cost
|
||||
FROM {$table}
|
||||
WHERE created_at >= %s AND response_status = 'success'
|
||||
GROUP BY DATE(created_at)
|
||||
ORDER BY day ASC",
|
||||
$start_date . ' 00:00:00'
|
||||
),
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
$daily = [];
|
||||
foreach ( ( $results ?: [] ) as $row ) {
|
||||
$daily[ $row['day'] ] = (float) $row['total_cost'];
|
||||
}
|
||||
|
||||
return $daily;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get spend breakdown by pipeline stage for a given period.
|
||||
*
|
||||
* @param string $start_date Start date (Y-m-d).
|
||||
* @param string $end_date End date (Y-m-d).
|
||||
*
|
||||
* @return array<string, array{cost: float, tokens: int, calls: int}> Breakdown by stage.
|
||||
*/
|
||||
public function get_spend_by_stage( string $start_date, string $end_date ): array {
|
||||
global $wpdb;
|
||||
$table = $wpdb->prefix . 'prautoblogger_generation_log';
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$results = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT stage,
|
||||
SUM(estimated_cost) as total_cost,
|
||||
SUM(prompt_tokens + completion_tokens) as total_tokens,
|
||||
COUNT(*) as call_count
|
||||
FROM {$table}
|
||||
WHERE created_at BETWEEN %s AND %s AND response_status = 'success'
|
||||
GROUP BY stage",
|
||||
$start_date . ' 00:00:00',
|
||||
$end_date . ' 23:59:59'
|
||||
),
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
$breakdown = [];
|
||||
foreach ( ( $results ?: [] ) as $row ) {
|
||||
$breakdown[ $row['stage'] ] = [
|
||||
'cost' => (float) $row['total_cost'],
|
||||
'tokens' => (int) $row['total_tokens'],
|
||||
'calls' => (int) $row['call_count'],
|
||||
];
|
||||
}
|
||||
|
||||
return $breakdown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get budget utilization percentage for the current month.
|
||||
*
|
||||
* @return float Percentage (0-100+). Can exceed 100 if overspent.
|
||||
*/
|
||||
public function get_budget_utilization(): float {
|
||||
$budget = (float) get_option( 'prautoblogger_monthly_budget_usd', 50.00 );
|
||||
if ( $budget <= 0 ) {
|
||||
return 0.0;
|
||||
}
|
||||
return ( $this->get_monthly_spend() / $budget ) * 100.0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ class PRAutoBlogger_Image_Media_Sideloader {
|
||||
*
|
||||
* @return int|\WP_Error Attachment ID on success, WP_Error on failure.
|
||||
*/
|
||||
public function sideload_image( array $image_data, int $post_id, string $alt_text = '' ): int | \WP_Error {
|
||||
public function sideload_image( array $image_data, int $post_id, string $alt_text = '' ) {
|
||||
// Validate input.
|
||||
if ( empty( $image_data['bytes'] ) ) {
|
||||
return new \WP_Error( 'invalid_image_data', 'Image data (bytes) is empty.' );
|
||||
@@ -97,7 +97,7 @@ class PRAutoBlogger_Image_Media_Sideloader {
|
||||
*
|
||||
* @return string|\WP_Error Path to temporary file on success, WP_Error on failure.
|
||||
*/
|
||||
private function create_temp_file( string $bytes, string $mime_type ): string | \WP_Error {
|
||||
private function create_temp_file( string $bytes, string $mime_type ) {
|
||||
$temp_dir = get_temp_dir();
|
||||
$filename = 'prautoblogger_img_' . wp_generate_uuid4() . $this->get_extension_for_mime( $mime_type );
|
||||
$temp_path = $temp_dir . $filename;
|
||||
|
||||
@@ -124,7 +124,9 @@ class PRAutoBlogger_Image_Pipeline {
|
||||
? $image_a_result->get_error_message()
|
||||
: 'Image A generation produced no attachment ID.';
|
||||
}
|
||||
$result['cost_usd'] += $image_a_result['cost_usd'] ?? 0.0;
|
||||
if ( ! is_wp_error( $image_a_result ) ) {
|
||||
$result['cost_usd'] += $image_a_result['cost_usd'] ?? 0.0;
|
||||
}
|
||||
|
||||
// Generate Image B (source-driven prompt) if source data is available.
|
||||
if ( null !== $source_data && ! empty( $source_data ) ) {
|
||||
@@ -136,7 +138,9 @@ class PRAutoBlogger_Image_Pipeline {
|
||||
? $image_b_result->get_error_message()
|
||||
: 'Image B generation produced no attachment ID.';
|
||||
}
|
||||
$result['cost_usd'] += $image_b_result['cost_usd'] ?? 0.0;
|
||||
if ( ! is_wp_error( $image_b_result ) ) {
|
||||
$result['cost_usd'] += $image_b_result['cost_usd'] ?? 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
@@ -153,7 +157,7 @@ class PRAutoBlogger_Image_Pipeline {
|
||||
* cost_usd: float,
|
||||
* }|\WP_Error
|
||||
*/
|
||||
private function generate_image_a( int $post_id, array $article_data ): array | \WP_Error {
|
||||
private function generate_image_a( int $post_id, array $article_data ) {
|
||||
try {
|
||||
// Build the article-driven prompt.
|
||||
$prompt = $this->prompt_builder->build_article_prompt( $article_data );
|
||||
@@ -219,7 +223,7 @@ class PRAutoBlogger_Image_Pipeline {
|
||||
* cost_usd: float,
|
||||
* }|\WP_Error
|
||||
*/
|
||||
private function generate_image_b( int $post_id, array $source_data ): array | \WP_Error {
|
||||
private function generate_image_b( int $post_id, array $source_data ) {
|
||||
try {
|
||||
// Build the source-driven prompt.
|
||||
$prompt = $this->prompt_builder->build_source_prompt( $source_data );
|
||||
|
||||
@@ -126,6 +126,11 @@ class PRAutoBlogger_Logger {
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
if ( null === $wpdb ) {
|
||||
// No database available (unit test environment). PHP error_log
|
||||
// was already called above for warnings/errors; silently skip DB write.
|
||||
return;
|
||||
}
|
||||
$table = $wpdb->prefix . 'prautoblogger_event_log';
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
|
||||
196
includes/providers/class-reddit-availability.php
Normal file
196
includes/providers/class-reddit-availability.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Reddit endpoint availability checker.
|
||||
*
|
||||
* Probes Reddit's RSS and .json endpoints to determine which data sources
|
||||
* are reachable from the current server. Datacenter IPs (Hostinger) are
|
||||
* often blocked by Reddit for .json but not RSS, so the client must check
|
||||
* both before deciding which strategy to use.
|
||||
*
|
||||
* Triggered by: PRAutoBlogger_Reddit_JSON_Client::is_available() delegation,
|
||||
* Admin settings page "test connection" button.
|
||||
* Dependencies: wp_remote_get(), PRAutoBlogger_Logger.
|
||||
*
|
||||
* @see providers/class-reddit-json-client.php — Instantiates and delegates to this class.
|
||||
* @see ARCHITECTURE.md — External API integrations table.
|
||||
*/
|
||||
class PRAutoBlogger_Reddit_Availability {
|
||||
|
||||
/**
|
||||
* Reddit base URL for all endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private const BASE_URL = 'https://www.reddit.com';
|
||||
|
||||
/**
|
||||
* User agent — Reddit requires a descriptive UA for unauthenticated requests.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private const USER_AGENT = 'Mozilla/5.0 (compatible; PRAutoBlogger/0.2.1; +https://peptiderepo.com)';
|
||||
|
||||
/**
|
||||
* Transient key for rate limit tracking (.json endpoints only).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private const RATE_LIMIT_TRANSIENT = 'prautoblogger_reddit_json_rate_limit';
|
||||
|
||||
/**
|
||||
* Check if any Reddit endpoint is reachable (RSS or .json).
|
||||
*
|
||||
* Side effects: HTTP request to Reddit.
|
||||
*
|
||||
* @return bool True if at least one endpoint is accessible.
|
||||
*/
|
||||
public function is_available(): bool {
|
||||
return $this->is_rss_available() || $this->is_json_available();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Reddit RSS endpoints are reachable.
|
||||
*
|
||||
* RSS endpoints are rarely blocked by Reddit, even for datacenter IPs.
|
||||
*
|
||||
* Side effects: One HTTP GET to Reddit.
|
||||
*
|
||||
* @return bool True if RSS endpoint returns HTTP 200.
|
||||
*/
|
||||
public function is_rss_available(): bool {
|
||||
$url = self::BASE_URL . '/r/all/hot.rss?limit=1';
|
||||
$response = wp_remote_get(
|
||||
$url,
|
||||
[
|
||||
'timeout' => 10,
|
||||
'headers' => [
|
||||
'User-Agent' => self::USER_AGENT,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return 200 === wp_remote_retrieve_response_code( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Reddit .json endpoints are reachable.
|
||||
*
|
||||
* Datacenter IPs are often blocked by Reddit for .json endpoints.
|
||||
*
|
||||
* Side effects: One HTTP GET to Reddit.
|
||||
*
|
||||
* @return bool True if .json endpoint returns HTTP 200.
|
||||
*/
|
||||
public function is_json_available(): bool {
|
||||
$url = self::BASE_URL . '/r/all/hot.json?limit=1';
|
||||
$response = wp_remote_get(
|
||||
$url,
|
||||
[
|
||||
'timeout' => 10,
|
||||
'headers' => [
|
||||
'User-Agent' => self::USER_AGENT,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return 200 === wp_remote_retrieve_response_code( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current rate limit status from cached .json response headers.
|
||||
*
|
||||
* Reddit .json endpoints return rate limit headers. RSS has no rate limits.
|
||||
* The rate data is written by api_get() and read here.
|
||||
*
|
||||
* @return array{remaining: int, limit: int, resets_at: string}
|
||||
*/
|
||||
public function get_rate_limit_status(): array {
|
||||
$status = get_transient( self::RATE_LIMIT_TRANSIENT );
|
||||
|
||||
if ( is_array( $status ) ) {
|
||||
return $status;
|
||||
}
|
||||
|
||||
return [
|
||||
'remaining' => 10,
|
||||
'limit' => 10,
|
||||
'resets_at' => '',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an HTTP GET request to a Reddit .json endpoint.
|
||||
*
|
||||
* No authentication — just a plain GET with a descriptive User-Agent.
|
||||
* Tracks rate limits from response headers when available.
|
||||
*
|
||||
* Side effects: HTTP request, sets rate limit transient, logs errors.
|
||||
*
|
||||
* @param string $url Full URL with .json suffix and query params.
|
||||
*
|
||||
* @return array<string, mixed>|null Decoded JSON, or null on failure.
|
||||
*/
|
||||
public function api_get( string $url ): ?array {
|
||||
$response = wp_remote_get(
|
||||
$url,
|
||||
[
|
||||
'timeout' => 30,
|
||||
'headers' => [
|
||||
'User-Agent' => self::USER_AGENT,
|
||||
'Accept' => 'application/json',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
PRAutoBlogger_Logger::instance()->error(
|
||||
'Reddit .json GET failed: ' . $response->get_error_message(),
|
||||
'reddit'
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Track rate limits from response headers.
|
||||
$rate_remaining = wp_remote_retrieve_header( $response, 'x-ratelimit-remaining' );
|
||||
$rate_used = wp_remote_retrieve_header( $response, 'x-ratelimit-used' );
|
||||
$rate_reset = wp_remote_retrieve_header( $response, 'x-ratelimit-reset' );
|
||||
|
||||
if ( '' !== $rate_remaining ) {
|
||||
set_transient(
|
||||
self::RATE_LIMIT_TRANSIENT,
|
||||
[
|
||||
'remaining' => (int) $rate_remaining,
|
||||
'limit' => (int) $rate_used + (int) $rate_remaining,
|
||||
'resets_at' => gmdate( 'Y-m-d H:i:s', time() + (int) $rate_reset ),
|
||||
],
|
||||
120
|
||||
);
|
||||
}
|
||||
|
||||
$status_code = wp_remote_retrieve_response_code( $response );
|
||||
|
||||
if ( 200 !== $status_code ) {
|
||||
PRAutoBlogger_Logger::instance()->error(
|
||||
sprintf(
|
||||
'Reddit .json HTTP %d: %s',
|
||||
$status_code,
|
||||
substr( wp_remote_retrieve_body( $response ), 0, 300 )
|
||||
),
|
||||
'reddit'
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
return json_decode( wp_remote_retrieve_body( $response ), true );
|
||||
}
|
||||
}
|
||||
@@ -10,10 +10,12 @@ declare(strict_types=1);
|
||||
* for comment fetching (comments are not available in RSS feeds).
|
||||
*
|
||||
* Triggered by: PRAutoBlogger_Reddit_Provider::collect_data().
|
||||
* Dependencies: wp_remote_get(), PRAutoBlogger_Logger.
|
||||
* Dependencies: wp_remote_get(), PRAutoBlogger_Logger, PRAutoBlogger_Reddit_RSS_Parser.
|
||||
*
|
||||
* @see providers/class-reddit-provider.php — Instantiates and calls this class.
|
||||
* @see ARCHITECTURE.md — External API integrations table.
|
||||
* @see providers/class-reddit-provider.php — Instantiates and calls this class.
|
||||
* @see providers/class-reddit-rss-parser.php — Parses Atom XML into post arrays.
|
||||
* @see providers/class-reddit-availability.php — Endpoint health checks.
|
||||
* @see ARCHITECTURE.md — External API integrations table.
|
||||
*/
|
||||
class PRAutoBlogger_Reddit_JSON_Client {
|
||||
|
||||
@@ -39,13 +41,6 @@ class PRAutoBlogger_Reddit_JSON_Client {
|
||||
*/
|
||||
private const TIMEOUT_SECONDS = 30;
|
||||
|
||||
/**
|
||||
* Transient key for rate limit tracking (.json endpoints only).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private const RATE_LIMIT_TRANSIENT = 'prautoblogger_reddit_json_rate_limit';
|
||||
|
||||
/**
|
||||
* Fetch hot posts from a subreddit. Tries RSS first, .json fallback.
|
||||
*
|
||||
@@ -129,7 +124,8 @@ class PRAutoBlogger_Reddit_JSON_Client {
|
||||
}
|
||||
|
||||
$body = wp_remote_retrieve_body( $response );
|
||||
return $this->parse_atom_feed( $body, $subreddit );
|
||||
$parser = new PRAutoBlogger_Reddit_RSS_Parser();
|
||||
return $parser->parse( $body, $subreddit );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -173,105 +169,6 @@ class PRAutoBlogger_Reddit_JSON_Client {
|
||||
return $posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a Reddit Atom feed into post-data arrays matching .json format.
|
||||
*
|
||||
* Maps Atom entry fields to the standard Reddit post fields so the rest
|
||||
* of the pipeline can consume RSS data without changes. Fields unavailable
|
||||
* in RSS (score, num_comments, upvote_ratio) are set to reasonable defaults.
|
||||
*
|
||||
* @param string $xml Raw Atom XML string.
|
||||
* @param string $subreddit The subreddit name for context.
|
||||
*
|
||||
* @return array<int, array<string, mixed>> Parsed post data.
|
||||
*/
|
||||
private function parse_atom_feed( string $xml, string $subreddit ): array {
|
||||
// Suppress XML errors for malformed feeds.
|
||||
$prev_errors = libxml_use_internal_errors( true );
|
||||
$feed = simplexml_load_string( $xml );
|
||||
libxml_use_internal_errors( $prev_errors );
|
||||
|
||||
if ( false === $feed ) {
|
||||
PRAutoBlogger_Logger::instance()->error( 'Failed to parse Reddit RSS XML.', 'reddit' );
|
||||
return [];
|
||||
}
|
||||
|
||||
// Register the Atom namespace.
|
||||
$feed->registerXPathNamespace( 'atom', 'http://www.w3.org/2005/Atom' );
|
||||
$entries = $feed->xpath( '//atom:entry' );
|
||||
|
||||
if ( empty( $entries ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$posts = [];
|
||||
foreach ( $entries as $entry ) {
|
||||
$title = (string) $entry->title;
|
||||
$link = '';
|
||||
$content = '';
|
||||
|
||||
// Get the HTML link (type="text/html").
|
||||
foreach ( $entry->link as $link_el ) {
|
||||
if ( 'text/html' === (string) $link_el['type'] || 'alternate' === (string) $link_el['rel'] ) {
|
||||
$link = (string) $link_el['href'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Content is in <content> tag as HTML.
|
||||
if ( isset( $entry->content ) ) {
|
||||
$content = wp_strip_all_tags( (string) $entry->content );
|
||||
}
|
||||
|
||||
// Extract post ID from the entry id (format: /r/subreddit/comments/ID/...).
|
||||
$entry_id = (string) $entry->id;
|
||||
$post_id = '';
|
||||
if ( preg_match( '#/comments/([a-z0-9]+)#', $entry_id, $m ) ) {
|
||||
$post_id = $m[1];
|
||||
}
|
||||
|
||||
// Extract author name.
|
||||
$author = '[deleted]';
|
||||
if ( isset( $entry->author->name ) ) {
|
||||
$author = str_replace( '/u/', '', (string) $entry->author->name );
|
||||
}
|
||||
|
||||
// Parse published date to Unix timestamp.
|
||||
$published = (string) ( $entry->published ?? $entry->updated ?? '' );
|
||||
$created_utc = '' !== $published ? (int) strtotime( $published ) : time();
|
||||
|
||||
// Extract permalink (relative path) from full URL.
|
||||
$permalink = '';
|
||||
if ( '' !== $link ) {
|
||||
$parsed = wp_parse_url( $link );
|
||||
$permalink = $parsed['path'] ?? '';
|
||||
}
|
||||
|
||||
$posts[] = [
|
||||
'id' => $post_id,
|
||||
'title' => $title,
|
||||
'selftext' => $content,
|
||||
'author' => $author,
|
||||
'score' => 1, // Not available in RSS.
|
||||
'num_comments' => 0, // Not available in RSS.
|
||||
'permalink' => $permalink,
|
||||
'created_utc' => $created_utc,
|
||||
'is_self' => true,
|
||||
'link_flair_text' => null,
|
||||
'upvote_ratio' => null,
|
||||
'is_original_content' => false,
|
||||
'data_source' => 'reddit_rss',
|
||||
];
|
||||
}
|
||||
|
||||
PRAutoBlogger_Logger::instance()->info(
|
||||
sprintf( 'Parsed %d posts from Reddit RSS for r/%s.', count( $posts ), $subreddit ),
|
||||
'reddit'
|
||||
);
|
||||
|
||||
return $posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch top comments for a specific post via Reddit's .json endpoint.
|
||||
*
|
||||
@@ -324,145 +221,47 @@ class PRAutoBlogger_Reddit_JSON_Client {
|
||||
* @return bool True if we got a valid response from either endpoint.
|
||||
*/
|
||||
public function is_available(): bool {
|
||||
return $this->is_rss_available() || $this->is_json_available();
|
||||
return ( new PRAutoBlogger_Reddit_Availability() )->is_available();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Reddit RSS endpoints are reachable.
|
||||
*
|
||||
* RSS endpoints are rarely blocked by Reddit, even for datacenter IPs.
|
||||
*
|
||||
* @return bool True if RSS endpoint returns HTTP 200.
|
||||
*/
|
||||
public function is_rss_available(): bool {
|
||||
$url = self::BASE_URL . '/r/all/hot.rss?limit=1';
|
||||
$response = wp_remote_get(
|
||||
$url,
|
||||
[
|
||||
'timeout' => 10,
|
||||
'headers' => [
|
||||
'User-Agent' => self::USER_AGENT,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return 200 === wp_remote_retrieve_response_code( $response );
|
||||
return ( new PRAutoBlogger_Reddit_Availability() )->is_rss_available();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Reddit .json endpoints are reachable.
|
||||
*
|
||||
* Datacenter IPs are often blocked by Reddit for .json endpoints.
|
||||
*
|
||||
* @return bool True if .json endpoint returns HTTP 200.
|
||||
*/
|
||||
public function is_json_available(): bool {
|
||||
$url = self::BASE_URL . '/r/all/hot.json?limit=1';
|
||||
$response = wp_remote_get(
|
||||
$url,
|
||||
[
|
||||
'timeout' => 10,
|
||||
'headers' => [
|
||||
'User-Agent' => self::USER_AGENT,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return 200 === wp_remote_retrieve_response_code( $response );
|
||||
return ( new PRAutoBlogger_Reddit_Availability() )->is_json_available();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current rate limit status.
|
||||
*
|
||||
* Reddit .json endpoints return rate limit headers. RSS has no rate limits.
|
||||
*
|
||||
* @return array{remaining: int, limit: int, resets_at: string}
|
||||
*/
|
||||
public function get_rate_limit_status(): array {
|
||||
$status = get_transient( self::RATE_LIMIT_TRANSIENT );
|
||||
|
||||
if ( is_array( $status ) ) {
|
||||
return $status;
|
||||
}
|
||||
|
||||
return [
|
||||
'remaining' => 10,
|
||||
'limit' => 10,
|
||||
'resets_at' => '',
|
||||
];
|
||||
return ( new PRAutoBlogger_Reddit_Availability() )->get_rate_limit_status();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an HTTP GET request to a Reddit .json endpoint.
|
||||
*
|
||||
* No authentication — just a plain GET with a descriptive User-Agent.
|
||||
* Tracks rate limits from response headers when available.
|
||||
*
|
||||
* Side effects: HTTP request, sets rate limit transient, logs errors.
|
||||
* Delegates to PRAutoBlogger_Reddit_Availability which owns the HTTP
|
||||
* transport layer and rate-limit tracking.
|
||||
*
|
||||
* @param string $url Full URL with .json suffix and query params.
|
||||
*
|
||||
* @return array<string, mixed>|null Decoded JSON, or null on failure.
|
||||
*/
|
||||
private function api_get( string $url ): ?array {
|
||||
$response = wp_remote_get(
|
||||
$url,
|
||||
[
|
||||
'timeout' => self::TIMEOUT_SECONDS,
|
||||
'headers' => [
|
||||
'User-Agent' => self::USER_AGENT,
|
||||
'Accept' => 'application/json',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
PRAutoBlogger_Logger::instance()->error(
|
||||
'Reddit .json GET failed: ' . $response->get_error_message(),
|
||||
'reddit'
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Track rate limits from response headers.
|
||||
$rate_remaining = wp_remote_retrieve_header( $response, 'x-ratelimit-remaining' );
|
||||
$rate_used = wp_remote_retrieve_header( $response, 'x-ratelimit-used' );
|
||||
$rate_reset = wp_remote_retrieve_header( $response, 'x-ratelimit-reset' );
|
||||
|
||||
if ( '' !== $rate_remaining ) {
|
||||
set_transient(
|
||||
self::RATE_LIMIT_TRANSIENT,
|
||||
[
|
||||
'remaining' => (int) $rate_remaining,
|
||||
'limit' => (int) $rate_used + (int) $rate_remaining,
|
||||
'resets_at' => gmdate( 'Y-m-d H:i:s', time() + (int) $rate_reset ),
|
||||
],
|
||||
120
|
||||
);
|
||||
}
|
||||
|
||||
$status_code = wp_remote_retrieve_response_code( $response );
|
||||
|
||||
if ( 200 !== $status_code ) {
|
||||
PRAutoBlogger_Logger::instance()->error(
|
||||
sprintf(
|
||||
'Reddit .json HTTP %d: %s',
|
||||
$status_code,
|
||||
substr( wp_remote_retrieve_body( $response ), 0, 300 )
|
||||
),
|
||||
'reddit'
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
return json_decode( wp_remote_retrieve_body( $response ), true );
|
||||
return ( new PRAutoBlogger_Reddit_Availability() )->api_get( $url );
|
||||
}
|
||||
}
|
||||
|
||||
137
includes/providers/class-reddit-rss-parser.php
Normal file
137
includes/providers/class-reddit-rss-parser.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Reddit RSS/Atom feed parser — parses Atom XML into normalized post arrays.
|
||||
*
|
||||
* Handles parsing of Reddit's Atom/RSS feeds and mapping them to the standard
|
||||
* Reddit post data format (same shape as .json responses). This parser is
|
||||
* self-contained: it takes raw XML and produces normalized arrays.
|
||||
*
|
||||
* Fields unavailable in RSS feeds (score, num_comments, upvote_ratio) are set
|
||||
* to sensible defaults so the rest of the pipeline can treat RSS and .json data uniformly.
|
||||
*
|
||||
* Usage: Called by PRAutoBlogger_Reddit_JSON_Client::fetch_posts_via_rss()
|
||||
* to parse the response body.
|
||||
*
|
||||
* Dependencies: wp_strip_all_tags(), PRAutoBlogger_Logger, built-in SimpleXML.
|
||||
*
|
||||
* @see providers/class-reddit-json-client.php — Main client that calls this parser.
|
||||
* @see ARCHITECTURE.md — External API integrations table.
|
||||
*/
|
||||
class PRAutoBlogger_Reddit_RSS_Parser {
|
||||
|
||||
/**
|
||||
* Parse a Reddit Atom feed into post-data arrays matching .json format.
|
||||
*
|
||||
* Maps Atom entry fields to the standard Reddit post fields so the rest
|
||||
* of the pipeline can consume RSS data without changes. Fields unavailable
|
||||
* in RSS (score, num_comments, upvote_ratio) are set to reasonable defaults.
|
||||
*
|
||||
* @param string $xml Raw Atom XML string.
|
||||
* @param string $subreddit The subreddit name for context (logging only).
|
||||
*
|
||||
* @return array<int, array<string, mixed>> Parsed post data.
|
||||
*/
|
||||
public function parse( string $xml, string $subreddit ): array {
|
||||
// Suppress XML errors for malformed feeds.
|
||||
$prev_errors = libxml_use_internal_errors( true );
|
||||
$feed = simplexml_load_string( $xml );
|
||||
libxml_use_internal_errors( $prev_errors );
|
||||
|
||||
if ( false === $feed ) {
|
||||
PRAutoBlogger_Logger::instance()->error( 'Failed to parse Reddit RSS XML.', 'reddit' );
|
||||
return [];
|
||||
}
|
||||
|
||||
// Register the Atom namespace.
|
||||
$feed->registerXPathNamespace( 'atom', 'http://www.w3.org/2005/Atom' );
|
||||
$entries = $feed->xpath( '//atom:entry' );
|
||||
|
||||
if ( empty( $entries ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$posts = [];
|
||||
foreach ( $entries as $entry ) {
|
||||
$post = $this->parse_entry( $entry, $subreddit );
|
||||
if ( null !== $post ) {
|
||||
$posts[] = $post;
|
||||
}
|
||||
}
|
||||
|
||||
PRAutoBlogger_Logger::instance()->info(
|
||||
sprintf( 'Parsed %d posts from Reddit RSS for r/%s.', count( $posts ), $subreddit ),
|
||||
'reddit'
|
||||
);
|
||||
|
||||
return $posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a single Atom entry element into a normalized post array.
|
||||
*
|
||||
* @param \SimpleXMLElement $entry An Atom entry element.
|
||||
* @param string $subreddit The subreddit name (for context/logging).
|
||||
*
|
||||
* @return array<string, mixed>|null The parsed post data, or null if parsing fails.
|
||||
*/
|
||||
private function parse_entry( $entry, string $subreddit ): ?array {
|
||||
$title = (string) $entry->title;
|
||||
$link = '';
|
||||
$content = '';
|
||||
|
||||
// Get the HTML link (type="text/html").
|
||||
foreach ( $entry->link as $link_el ) {
|
||||
if ( 'text/html' === (string) $link_el['type'] || 'alternate' === (string) $link_el['rel'] ) {
|
||||
$link = (string) $link_el['href'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Content is in <content> tag as HTML.
|
||||
if ( isset( $entry->content ) ) {
|
||||
$content = wp_strip_all_tags( (string) $entry->content );
|
||||
}
|
||||
|
||||
// Extract post ID from the entry id (format: /r/subreddit/comments/ID/...).
|
||||
$entry_id = (string) $entry->id;
|
||||
$post_id = '';
|
||||
if ( preg_match( '#/comments/([a-z0-9]+)#', $entry_id, $m ) ) {
|
||||
$post_id = $m[1];
|
||||
}
|
||||
|
||||
// Extract author name.
|
||||
$author = '[deleted]';
|
||||
if ( isset( $entry->author->name ) ) {
|
||||
$author = str_replace( '/u/', '', (string) $entry->author->name );
|
||||
}
|
||||
|
||||
// Parse published date to Unix timestamp.
|
||||
$published = (string) ( $entry->published ?? $entry->updated ?? '' );
|
||||
$created_utc = '' !== $published ? (int) strtotime( $published ) : time();
|
||||
|
||||
// Extract permalink (relative path) from full URL.
|
||||
$permalink = '';
|
||||
if ( '' !== $link ) {
|
||||
$parsed = wp_parse_url( $link );
|
||||
$permalink = $parsed['path'] ?? '';
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $post_id,
|
||||
'title' => $title,
|
||||
'selftext' => $content,
|
||||
'author' => $author,
|
||||
'score' => 1, // Not available in RSS.
|
||||
'num_comments' => 0, // Not available in RSS.
|
||||
'permalink' => $permalink,
|
||||
'created_utc' => $created_utc,
|
||||
'is_self' => true,
|
||||
'link_flair_text' => null,
|
||||
'upvote_ratio' => null,
|
||||
'is_original_content' => false,
|
||||
'data_source' => 'reddit_rss',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -9,15 +9,15 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$cost_tracker = new PRAutoBlogger_Cost_Tracker();
|
||||
$monthly_spend = $cost_tracker->get_monthly_spend();
|
||||
$cost_reporter = new PRAutoBlogger_Cost_Reporter();
|
||||
$monthly_spend = $cost_reporter->get_monthly_spend();
|
||||
$budget = (float) get_option( 'prautoblogger_monthly_budget_usd', 50.00 );
|
||||
$utilization = $cost_tracker->get_budget_utilization();
|
||||
$daily_spend = $cost_tracker->get_daily_spend( 30 );
|
||||
$utilization = $cost_reporter->get_budget_utilization();
|
||||
$daily_spend = $cost_reporter->get_daily_spend( 30 );
|
||||
|
||||
$first_of_month = gmdate( 'Y-m-01' );
|
||||
$today = gmdate( 'Y-m-d' );
|
||||
$stage_breakdown = $cost_tracker->get_spend_by_stage( $first_of_month, $today );
|
||||
$stage_breakdown = $cost_reporter->get_spend_by_stage( $first_of_month, $today );
|
||||
?>
|
||||
<div class="wrap prautoblogger-metrics">
|
||||
<h1><?php esc_html_e( 'PRAutoBlogger — Metrics & Costs', 'prautoblogger' ); ?></h1>
|
||||
|
||||
@@ -14,8 +14,8 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
}
|
||||
|
||||
$active_tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'prautoblogger_api';
|
||||
$cost_tracker = new PRAutoBlogger_Cost_Tracker();
|
||||
$monthly_spend = $cost_tracker->get_monthly_spend();
|
||||
$cost_reporter = new PRAutoBlogger_Cost_Reporter();
|
||||
$monthly_spend = $cost_reporter->get_monthly_spend();
|
||||
$budget = (float) get_option( 'prautoblogger_monthly_budget_usd', 50.00 );
|
||||
$utilization = $budget > 0 ? ( $monthly_spend / $budget ) * 100.0 : 0;
|
||||
$next_run = wp_next_scheduled( 'prautoblogger_daily_generation' );
|
||||
|
||||
@@ -55,6 +55,11 @@ if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_EDITOR_MODEL' ) ) {
|
||||
define( 'PRAUTOBLOGGER_DEFAULT_EDITOR_MODEL', 'google/gemini-2.5-flash-lite' );
|
||||
}
|
||||
|
||||
// Default image style suffix used by ImagePromptBuilder and settings UI.
|
||||
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_IMAGE_STYLE_SUFFIX' ) ) {
|
||||
define( 'PRAUTOBLOGGER_DEFAULT_IMAGE_STYLE_SUFFIX', 'Style: test infomercial style.' );
|
||||
}
|
||||
|
||||
// WordPress database constants used in $wpdb queries.
|
||||
if ( ! defined( 'ARRAY_A' ) ) {
|
||||
define( 'ARRAY_A', 'ARRAY_A' );
|
||||
@@ -92,3 +97,39 @@ if ( ! class_exists( 'WP_Query' ) ) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: Do NOT stub wp_strip_all_tags() or sanitize_text_field() here.
|
||||
// Brain Monkey / Patchwork manages these stubs in individual tests via
|
||||
// Functions\when(). Defining them in bootstrap causes Patchwork's
|
||||
// "DefinedTooEarly" error.
|
||||
|
||||
// WP_Error stub — used by model registry and image pipeline tests.
|
||||
// This is safe to define here because it's a class (not a function),
|
||||
// so Patchwork doesn't need to intercept its definition.
|
||||
if ( ! class_exists( 'WP_Error' ) ) {
|
||||
class WP_Error {
|
||||
public $errors = [];
|
||||
public $error_data = [];
|
||||
|
||||
public function __construct( $code = '', $message = '', $data = '' ) {
|
||||
if ( $code ) {
|
||||
$this->errors[ $code ][] = $message;
|
||||
if ( $data ) {
|
||||
$this->error_data[ $code ] = $data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function get_error_code() {
|
||||
$codes = array_keys( $this->errors );
|
||||
return $codes ? $codes[0] : '';
|
||||
}
|
||||
|
||||
public function get_error_message( $code = '' ) {
|
||||
if ( ! $code ) {
|
||||
$code = $this->get_error_code();
|
||||
}
|
||||
return isset( $this->errors[ $code ] ) ? $this->errors[ $code ][0] : '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
93
tests/unit/Core/CostReporterTest.php
Normal file
93
tests/unit/Core/CostReporterTest.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for PRAutoBlogger_Cost_Reporter.
|
||||
*
|
||||
* Validates monthly spend, daily spend trends, spend-by-stage breakdowns,
|
||||
* and budget utilization reporting methods.
|
||||
*
|
||||
* @package PRAutoBlogger\Tests\Core
|
||||
*/
|
||||
|
||||
namespace PRAutoBlogger\Tests\Core;
|
||||
|
||||
use PRAutoBlogger\Tests\BaseTestCase;
|
||||
use Brain\Monkey\Functions;
|
||||
|
||||
class CostReporterTest extends BaseTestCase {
|
||||
|
||||
/**
|
||||
* @var \PHPUnit\Framework\MockObject\MockObject Mock $wpdb instance.
|
||||
*/
|
||||
private $wpdb;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->wpdb = $this->create_mock_wpdb();
|
||||
$GLOBALS['wpdb'] = $this->wpdb;
|
||||
|
||||
// CostReporter methods call get_option for budget settings.
|
||||
$this->stub_get_option( [
|
||||
'prautoblogger_monthly_budget_usd' => '50.00',
|
||||
] );
|
||||
|
||||
// Stub current_time used in date-based queries.
|
||||
$this->stub_current_time( '2026-04-12 10:00:00' );
|
||||
}
|
||||
|
||||
protected function tearDown(): void {
|
||||
unset( $GLOBALS['wpdb'] );
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_monthly_spend returns float.
|
||||
*/
|
||||
public function test_get_monthly_spend_returns_float(): void {
|
||||
$this->wpdb->method( 'prepare' )->willReturn( 'prepared' );
|
||||
$this->wpdb->method( 'get_var' )->willReturn( '10.50' );
|
||||
|
||||
$reporter = new \PRAutoBlogger_Cost_Reporter();
|
||||
$spend = $reporter->get_monthly_spend();
|
||||
|
||||
$this->assertIsFloat( $spend );
|
||||
$this->assertGreaterThanOrEqual( 0.0, $spend );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_daily_spend returns array.
|
||||
*/
|
||||
public function test_get_daily_spend_returns_array(): void {
|
||||
$this->wpdb->method( 'prepare' )->willReturn( 'prepared' );
|
||||
$this->wpdb->method( 'get_results' )->willReturn( [] );
|
||||
|
||||
$reporter = new \PRAutoBlogger_Cost_Reporter();
|
||||
$daily = $reporter->get_daily_spend( 30 );
|
||||
|
||||
$this->assertIsArray( $daily );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_spend_by_stage returns array.
|
||||
*/
|
||||
public function test_get_spend_by_stage_returns_array(): void {
|
||||
$this->wpdb->method( 'prepare' )->willReturn( 'prepared' );
|
||||
$this->wpdb->method( 'get_results' )->willReturn( [] );
|
||||
|
||||
$reporter = new \PRAutoBlogger_Cost_Reporter();
|
||||
$spend = $reporter->get_spend_by_stage( '2026-04-01', '2026-04-30' );
|
||||
|
||||
$this->assertIsArray( $spend );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_budget_utilization returns float between 0 and 100+.
|
||||
*/
|
||||
public function test_get_budget_utilization_returns_float(): void {
|
||||
$reporter = new \PRAutoBlogger_Cost_Reporter();
|
||||
$utilization = $reporter->get_budget_utilization();
|
||||
|
||||
$this->assertIsFloat( $utilization );
|
||||
$this->assertGreaterThanOrEqual( 0.0, $utilization );
|
||||
}
|
||||
}
|
||||
@@ -68,20 +68,6 @@ class CostTrackerTest extends BaseTestCase {
|
||||
$this->assertFalse( $tracker->is_budget_exceeded() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_monthly_spend returns float.
|
||||
*/
|
||||
public function test_get_monthly_spend_returns_float(): void {
|
||||
$this->wpdb->method( 'prepare' )->willReturn( 'prepared' );
|
||||
$this->wpdb->method( 'get_var' )->willReturn( '10.50' );
|
||||
|
||||
$tracker = new \PRAutoBlogger_Cost_Tracker();
|
||||
$spend = $tracker->get_monthly_spend();
|
||||
|
||||
$this->assertIsFloat( $spend );
|
||||
$this->assertGreaterThanOrEqual( 0.0, $spend );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_current_run_cost returns float.
|
||||
*/
|
||||
@@ -98,43 +84,6 @@ class CostTrackerTest extends BaseTestCase {
|
||||
$this->assertGreaterThanOrEqual( 0.0, $cost );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_daily_spend returns array.
|
||||
*/
|
||||
public function test_get_daily_spend_returns_array(): void {
|
||||
$this->wpdb->method( 'prepare' )->willReturn( 'prepared' );
|
||||
$this->wpdb->method( 'get_results' )->willReturn( [] );
|
||||
|
||||
$tracker = new \PRAutoBlogger_Cost_Tracker();
|
||||
$daily = $tracker->get_daily_spend( 30 );
|
||||
|
||||
$this->assertIsArray( $daily );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_spend_by_stage returns array.
|
||||
*/
|
||||
public function test_get_spend_by_stage_returns_array(): void {
|
||||
$this->wpdb->method( 'prepare' )->willReturn( 'prepared' );
|
||||
$this->wpdb->method( 'get_results' )->willReturn( [] );
|
||||
|
||||
$tracker = new \PRAutoBlogger_Cost_Tracker();
|
||||
$spend = $tracker->get_spend_by_stage( '2026-04-01', '2026-04-30' );
|
||||
|
||||
$this->assertIsArray( $spend );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_budget_utilization returns float between 0 and 1.
|
||||
*/
|
||||
public function test_get_budget_utilization_returns_float(): void {
|
||||
$tracker = new \PRAutoBlogger_Cost_Tracker();
|
||||
$utilization = $tracker->get_budget_utilization();
|
||||
|
||||
$this->assertIsFloat( $utilization );
|
||||
$this->assertGreaterThanOrEqual( 0.0, $utilization );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test log_api_call with mock database.
|
||||
*/
|
||||
|
||||
@@ -18,8 +18,11 @@ class ImagePipelineTest extends BaseTestCase {
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
// Mock WordPress functions.
|
||||
Functions\when( 'wp_generate_uuid4' )->returnArg();
|
||||
// Mock WordPress functions used by the pipeline and its dependencies.
|
||||
Functions\when( 'is_wp_error' )->alias( function ( $thing ) {
|
||||
return $thing instanceof \WP_Error;
|
||||
} );
|
||||
Functions\when( 'wp_generate_uuid4' )->justReturn( 'test-uuid' );
|
||||
Functions\when( 'get_option' )->alias( function ( $key, $default = false ) {
|
||||
static $options = [
|
||||
'prautoblogger_image_enabled' => '1',
|
||||
@@ -28,7 +31,12 @@ class ImagePipelineTest extends BaseTestCase {
|
||||
return $options[ $key ] ?? $default;
|
||||
} );
|
||||
Functions\when( 'sanitize_text_field' )->returnArg();
|
||||
Functions\when( 'wp_strip_all_tags' )->alias( function ( $str ) {
|
||||
return trim( strip_tags( $str ) );
|
||||
} );
|
||||
Functions\when( 'update_post_meta' )->justReturn( true );
|
||||
Functions\when( 'sanitize_title' )->returnArg();
|
||||
Functions\when( 'esc_html__' )->returnArg();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,13 +48,10 @@ class ImagePipelineTest extends BaseTestCase {
|
||||
return $options[ $key ] ?? $default;
|
||||
} );
|
||||
|
||||
// Mock the logger.
|
||||
$logger = $this->createMock( \PRAutoBlogger_Logger::class );
|
||||
$logger->expects( $this->once() )->method( 'info' );
|
||||
|
||||
$provider = $this->createMock( \PRAutoBlogger_Image_Provider_Interface::class );
|
||||
$provider = $this->createMock( \PRAutoBlogger_Image_Provider_Interface::class );
|
||||
$provider->expects( $this->never() )->method( 'generate_image' );
|
||||
$cost_tracker = $this->createMock( \PRAutoBlogger_Cost_Tracker::class );
|
||||
|
||||
$cost_tracker = $this->createMock( \PRAutoBlogger_Cost_Tracker::class );
|
||||
|
||||
$pipeline = new \PRAutoBlogger_Image_Pipeline( $provider, $cost_tracker );
|
||||
$result = $pipeline->generate_and_attach_images( 1, [ 'post_title' => 'Test' ] );
|
||||
@@ -75,7 +80,6 @@ class ImagePipelineTest extends BaseTestCase {
|
||||
|
||||
$cost_tracker = $this->createMock( \PRAutoBlogger_Cost_Tracker::class );
|
||||
$cost_tracker->method( 'would_exceed_budget' )->willReturn( false );
|
||||
$cost_tracker->method( 'log_image_generation' )->willReturn( true );
|
||||
|
||||
$pipeline = new \PRAutoBlogger_Image_Pipeline( $provider, $cost_tracker );
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ class ImagePromptBuilderTest extends BaseTestCase {
|
||||
|
||||
// Mock WordPress functions.
|
||||
Functions\when( 'wp_strip_all_tags' )->alias( function ( $str ) {
|
||||
return wp_strip_all_tags( $str );
|
||||
return trim( strip_tags( $str ) );
|
||||
} );
|
||||
Functions\when( 'sanitize_text_field' )->returnArg();
|
||||
Functions\when( 'get_option' )->alias( function ( $key, $default = false ) {
|
||||
|
||||
@@ -224,8 +224,7 @@ class OpenRouterModelRegistryTest extends BaseTestCase {
|
||||
return 'Connection timed out';
|
||||
};
|
||||
Functions\when( 'wp_remote_get' )->alias( function () {
|
||||
$e = new \WP_Error_Stub();
|
||||
return $e;
|
||||
return new \WP_Error( 'http_request_failed', 'Connection timed out' );
|
||||
} );
|
||||
|
||||
// Accept that update_option will be called for the stale flag.
|
||||
@@ -240,13 +239,3 @@ class OpenRouterModelRegistryTest extends BaseTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Minimal WP_Error stub for testing is_wp_error() branches.
|
||||
*/
|
||||
if ( ! class_exists( 'WP_Error_Stub' ) ) {
|
||||
class WP_Error_Stub {
|
||||
public function get_error_message(): string {
|
||||
return 'Connection timed out';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user