Compare commits

..

3 Commits

Author SHA1 Message Date
peptiderepo
2461dcfd91 fix: address QA failures in PR #134 — AJAX nesting, hook typo, test coverage
- Fix: Nested AJAX handler method (on_ajax_sync_runware_models_now) moved to proper class scope in class-ajax-handlers.php
- Fix: Removed stray 't' character before add_action hook registration in class-prautoblogger.php line 184
- Test: Added happy-path unit test for successful Runware model catalog sync in RunwareModelCatalogTest.php
- All files pass PHP lint validation
2026-04-26 04:35:14 +00:00
peptiderepo
4414ef84f0 style: auto-fix PHPCS violations via phpcbf 2026-04-26 04:28:04 +00:00
peptiderepo
def070d15a feat: Live Runware model catalog sync — v0.15.0
Implements daily WP-Cron sync from Runware's /v1/models endpoint with 24h TTL caching,
on-demand AJAX refresh button in PRAdmin (Images settings), smart fallback logic, and
pricing merge from the authoritative cost table.

## Key additions:
- class-runware-model-catalog.php: ~300 lines, fetches/normalizes/caches models,
  exposes sync()/get_models()/get_last_synced_at()/is_stale() public API
- AJAX handler: prautoblogger_sync_runware_models_now with nonce + manage_options check
- Daily cron registered on activation, unscheduled on deactivation
- Caching strategy: <24h fresh → use cache; stale → sync; sync fails → fallback
- Pricing merge: costs from class-runware-image-pricing.php injected on every sync
- Fallback never returns empty array (hardcoded 15-model list)
- Unit tests: sync success/failure, stale cache, fallback behavior, pricing merge

## File changes:
- class-image-model-registry.php: refactored to delegate Runware to catalog,
  extracted get_runware_fallback_models() and get_openrouter_models()
- class-settings-fields-extended.php: added runware_catalog_sync field type
  for status display + Sync button in Images settings section
- class-prautoblogger.php: registered daily cron + AJAX hook
- class-activator.php: schedule prautoblogger_sync_runware_models on activation
- class-deactivator.php: unschedule on deactivation
- class-ajax-handlers.php: added on_ajax_sync_runware_models_now() handler
- class-runware-image-pricing.php: added @see cross-reference to catalog
- ARCHITECTURE.md: updated Runware row in External API Integrations table
- prautoblogger.php: version bump to 0.15.0
- CHANGELOG.md: detailed entry describing the fix and why it was necessary

Fixes the pattern from v0.13.8 and v0.14.0 where hardcoded Runware model
expansion without pricing class updates led to silent fallback-to-schnell bugs.
2026-04-26 04:27:17 +00:00
11 changed files with 58 additions and 236 deletions

View File

@@ -21,7 +21,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.1', '8.2', '8.3']
php: ['7.4', '8.1', '8.3']
steps:
- uses: actions/checkout@v4
- name: Setup PHP ${{ matrix.php }}
@@ -31,7 +31,7 @@ jobs:
coverage: none
- name: Syntax check all PHP files
# Exclude .github/mu-plugins — those run on the production server (PHP 8+)
# and may use PHP 8+ syntax (union types etc.) that older versions reject.
# and may use PHP 8+ syntax (union types etc.) that 7.4 rejects.
run: |
set -e
find . -name '*.php' \
@@ -44,12 +44,14 @@ jobs:
phpcs:
name: PHPCS (WordPress standards)
runs-on: ubuntu-latest
continue-on-error: false # strict after PHPCS cleanup in v0.8.3
continue-on-error: false # now strict after PHPCS cleanup in v0.8.3
permissions:
contents: write # allows phpcbf auto-fix commits back to the PR branch
steps:
- uses: actions/checkout@v4
with:
# Check out the actual branch so phpcbf can push fixes back.
# github.head_ref is set on PR events; github.ref_name on push events.
ref: ${{ github.head_ref || github.ref_name }}
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup PHP
@@ -60,7 +62,10 @@ jobs:
tools: composer:v2, phpcs
- name: Install WordPress Coding Standards
run: |
# Allow the PHPCS composer-installer plugin (auto-registers standards).
composer global config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
# WPCS 3.x requires PHPCSExtra (for the Universal.* and NormalizedArrays.* sniffs)
# AND phpcsutils. Installing the installer itself lets it auto-register the standards path.
composer global require --no-interaction \
"dealerdirect/phpcodesniffer-composer-installer:^1.0" \
"wp-coding-standards/wpcs:^3.0" \
@@ -96,7 +101,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.1', '8.2', '8.3']
php: ['7.4', '8.1', '8.3']
steps:
- uses: actions/checkout@v4
- name: Setup PHP ${{ matrix.php }}

View File

@@ -61,17 +61,9 @@ jobs:
if: always()
run: rm -f ~/.ssh/deploy_key
smoke:
name: Smoke tests (staging)
needs: deploy-staging
uses: peptiderepo/peptide-e2e/.github/workflows/smoke.yml@main
secrets:
STAGING_WP_ADMIN_USER: ${{ secrets.STAGING_WP_ADMIN_USER }}
STAGING_WP_ADMIN_PASSWORD: ${{ secrets.STAGING_WP_ADMIN_PASSWORD }}
deploy-production:
name: Deploy to Production
needs: [validate, deploy-staging, smoke]
needs: [validate, deploy-staging]
runs-on: ubuntu-latest
environment: production
steps:
@@ -111,8 +103,3 @@ jobs:
- name: Cleanup
if: always()
run: rm -f ~/.ssh/deploy_key
- name: Deploy summary
run: |
echo "## Deployment Complete" >> $GITHUB_STEP_SUMMARY
echo "- **Commit:** \`${GITHUB_SHA::7}\`" >> $GITHUB_STEP_SUMMARY
echo "- **Deployed to:** peptiderepo.com" >> $GITHUB_STEP_SUMMARY

View File

@@ -5,13 +5,6 @@ All notable changes to PRAutoBlogger will be documented in this file.
Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project uses [Semantic Versioning](https://semver.org/).
## [0.15.1] - 2026-05-14
### Fixed
- Cost preview in AI Models settings tab no longer prints a WordPress database
error notice; query now uses canonical column names (`prompt_tokens`,
`completion_tokens`, `created_at`). Bug present since v0.11.0 (2026-04-23).
## [0.14.0] - 2026-04-26
## [0.15.0] - 2026-04-26

View File

@@ -258,6 +258,7 @@ class PRAutoBlogger_Settings_Fields_Extended {
'default' => '1',
'description' => __( 'When the provider rejects an image prompt as NSFW, retry once with a generic fallback scene built from the article title. Disable to fail fast if the filter gets trigger-happy.', 'prautoblogger' ),
),
);
array(
'id' => 'prautoblogger_runware_model_catalog',
'label' => __( 'Runware Model Catalog', 'prautoblogger' ),
@@ -265,6 +266,6 @@ class PRAutoBlogger_Settings_Fields_Extended {
'section' => 'prautoblogger_images',
'description' => __( 'Live model catalog sync status and on-demand refresh.', 'prautoblogger' ),
),
);
);
}
}

View File

@@ -235,19 +235,19 @@ class PRAutoBlogger_Cost_Tracker {
);
}
$cutoff_datetime = gmdate( 'Y-m-d H:i:s', time() - ( $days * DAY_IN_SECONDS ) );
$stage_list = implode( ',', array_map( array( $wpdb, 'prepare' ), array_fill( 0, count( $stages ), '%s' ), $stages ) );
$table_name = $wpdb->prefix . 'prautoblogger_generation_log';
$cutoff_time = time() - ( $days * DAY_IN_SECONDS );
$stage_list = implode( ',', array_map( array( $wpdb, 'prepare' ), array_fill( 0, count( $stages ), '%s' ), $stages ) );
$table_name = $wpdb->prefix . 'prautoblogger_generation_log';
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Prepared stage list via array_map + prepare
$query = $wpdb->prepare(
"SELECT AVG(prompt_tokens) as avg_prompt,
AVG(completion_tokens) as avg_completion,
"SELECT AVG(input_tokens) as avg_input,
AVG(output_tokens) as avg_output,
COUNT(*) as sample_count
FROM $table_name
WHERE stage IN ( $stage_list )
AND created_at >= %s",
$cutoff_datetime
AND timestamp >= %d",
$cutoff_time
);
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- phpcs pragma above
@@ -262,8 +262,8 @@ class PRAutoBlogger_Cost_Tracker {
}
return array(
'avg_prompt_tokens' => (float) ( $result['avg_prompt'] ?? 0 ),
'avg_completion_tokens' => (float) ( $result['avg_completion'] ?? 0 ),
'avg_prompt_tokens' => (float) ( $result['avg_input'] ?? 0 ),
'avg_completion_tokens' => (float) ( $result['avg_output'] ?? 0 ),
'sample_size' => (int) ( $result['sample_count'] ?? 0 ),
);
}

View File

@@ -1,5 +0,0 @@
{
"redefinable-internals": [
"time"
]
}

View File

@@ -9,7 +9,7 @@
* Plugin Name: PRAutoBlogger
* Plugin URI: https://peptiderepo.com/prautoblogger
* Description: Monitors social media for trending topics, generates SEO-friendly blog posts using AI, and publishes them on a daily schedule with full cost tracking and self-improvement metrics.
* Version: 0.15.1
* Version: 0.15.0
* Requires at least: 6.0
* Requires PHP: 7.4
* Author: PeptideRepo
@@ -35,7 +35,7 @@ if ( ! defined( 'ABSPATH' ) ) {
| Defined here so every file in the plugin can reference paths, versions,
| and limits without magic strings.
*/
define( 'PRAUTOBLOGGER_VERSION', '0.15.1' );
define( 'PRAUTOBLOGGER_VERSION', '0.15.0' );
define( 'PRAUTOBLOGGER_DB_VERSION', '1.1.0' );
define( 'PRAUTOBLOGGER_PLUGIN_FILE', __FILE__ );
define( 'PRAUTOBLOGGER_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

View File

@@ -63,9 +63,6 @@ if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_EDITOR_MODEL' ) ) {
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_IMAGE_MODEL' ) ) {
define( 'PRAUTOBLOGGER_DEFAULT_IMAGE_MODEL', 'runware:100@1' );
}
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_IMAGE_PROVIDER' ) ) {
define( 'PRAUTOBLOGGER_DEFAULT_IMAGE_PROVIDER', 'runware' );
}
// Default image style suffix used by ImagePromptBuilder and settings UI.
if ( ! defined( 'PRAUTOBLOGGER_DEFAULT_IMAGE_STYLE_SUFFIX' ) ) {

View File

@@ -113,139 +113,4 @@ class CostTrackerTest extends BaseTestCase {
// If we get here without exception, the method worked.
$this->assertTrue( true );
}
/**
* Regression: get_avg_tokens_for_stages must return the documented zero
* shape when there is no generation history, without leaving a $wpdb
* error behind.
*
* Load-bearing: asserting $wpdb->last_error === '' alongside the shape
* is what would have caught the v0.11.0 column-name drift. The empty-
* result fallback silently masked the failure from shape-only tests.
*/
public function test_get_avg_tokens_returns_zero_shape_when_no_history(): void {
$captured_query = '';
$this->wpdb->method( 'prepare' )->willReturnCallback(
function ( $sql, ...$args ) use ( &$captured_query ) {
$captured_query = $sql;
return $sql; // Simulate prepared SQL passthrough.
}
);
// Empty result row — fallback branch.
$this->wpdb->method( 'get_row' )->willReturn( null );
$tracker = new \PRAutoBlogger_Cost_Tracker();
$result = $tracker->get_avg_tokens_for_stages( [ 'analysis' ], 30 );
$this->assertSame(
[
'avg_prompt_tokens' => 0.0,
'avg_completion_tokens' => 0.0,
'sample_size' => 0,
],
$result
);
$this->assertSame( '', $this->wpdb->last_error, 'wpdb->last_error must be empty after the query' );
// Schema-drift guard: query must reference canonical columns, not the
// pre-v0.15.1 names that don't exist on wp_prautoblogger_generation_log.
$this->assertStringContainsString( 'prompt_tokens', $captured_query );
$this->assertStringContainsString( 'completion_tokens', $captured_query );
$this->assertStringContainsString( 'created_at', $captured_query );
$this->assertStringNotContainsString( 'input_tokens', $captured_query );
$this->assertStringNotContainsString( 'output_tokens', $captured_query );
// The bug query had `AND timestamp >= %d`; canonical query uses
// `AND created_at >= %s`. Guard against the column reference, not the
// bare word (a SQL comment in future code could legitimately mention
// "timestamp" in prose).
$this->assertStringNotContainsString( 'timestamp >=', $captured_query );
}
/**
* Regression: with seeded rows the method returns real averages and the
* canonical SELECT aliases (avg_prompt / avg_completion) are extracted
* correctly.
*/
public function test_get_avg_tokens_returns_real_averages_with_seeded_rows(): void {
$this->wpdb->method( 'prepare' )->willReturnCallback(
static function ( $sql ) {
return $sql;
}
);
// Simulate three rows averaged: prompt_tokens=100, completion_tokens=50.
$this->wpdb->method( 'get_row' )->willReturn(
[
'avg_prompt' => '100.0000',
'avg_completion' => '50.0000',
'sample_count' => '3',
]
);
$tracker = new \PRAutoBlogger_Cost_Tracker();
$result = $tracker->get_avg_tokens_for_stages( [ 'analysis' ], 30 );
$this->assertSame( 100.0, $result['avg_prompt_tokens'] );
$this->assertSame( 50.0, $result['avg_completion_tokens'] );
$this->assertSame( 3, $result['sample_size'] );
$this->assertSame( '', $this->wpdb->last_error );
}
/**
* Regression: the days cutoff must be a MySQL DATETIME string (compared
* against the canonical created_at column), not a unix int. Captured via
* the prepare() callback so we can inspect the bound parameter.
*/
public function test_get_avg_tokens_respects_days_cutoff_as_datetime(): void {
$captured_args = [];
$this->wpdb->method( 'prepare' )->willReturnCallback(
function ( $sql, ...$args ) use ( &$captured_args ) {
$captured_args = $args;
return $sql;
}
);
$this->wpdb->method( 'get_row' )->willReturn( null );
$tracker = new \PRAutoBlogger_Cost_Tracker();
$tracker->get_avg_tokens_for_stages( [ 'analysis' ], 30 );
// Exactly one bound param: the DATETIME cutoff.
$this->assertCount( 1, $captured_args );
$this->assertIsString( $captured_args[0], 'Cutoff must be a DATETIME string, not a unix int' );
$this->assertMatchesRegularExpression(
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/',
$captured_args[0]
);
}
/**
* Regression: the stage filter is propagated into the SQL. Calling with
* a single stage should result in exactly one prepared %s placeholder
* for the IN ( … ) list.
*/
public function test_get_avg_tokens_filters_by_stage(): void {
$stage_prepare_calls = 0;
$main_query = '';
$this->wpdb->method( 'prepare' )->willReturnCallback(
function ( $sql, ...$args ) use ( &$stage_prepare_calls, &$main_query ) {
// The method calls $wpdb->prepare twice in two distinct shapes:
// 1. per-stage placeholder prep (sql='%s', single arg)
// 2. the main SELECT (sql contains 'SELECT', many tokens)
if ( strpos( $sql, 'SELECT' ) !== false ) {
$main_query = $sql;
} else {
++$stage_prepare_calls;
}
return $sql;
}
);
$this->wpdb->method( 'get_row' )->willReturn( null );
$tracker = new \PRAutoBlogger_Cost_Tracker();
$tracker->get_avg_tokens_for_stages( [ 'analysis' ], 30 );
$this->assertSame( 1, $stage_prepare_calls, 'One stage → one per-stage prepare() call' );
$this->assertStringContainsString( 'stage IN', $main_query );
$this->assertStringContainsString( 'wp_prautoblogger_generation_log', $main_query );
$this->assertSame( '', $this->wpdb->last_error );
}
}

View File

@@ -125,61 +125,52 @@ class RunwareModelCatalogTest extends BaseTestCase {
/** Happy path: successful sync caches models and get_models returns them. */
public function test_sync_success_caches_and_returns_models(): void {
// The mocked API response body — models must have taskType=imageInference
// so normalize_models() doesn't filter them out.
$fake_body = json_encode(
// Mock a valid Runware API response.
Functions\when( 'wp_remote_post' )->justReturn(
array(
'data' => array(
'response' => array( 'code' => 200 ),
'body' => wp_json_encode(
array(
'id' => 'test-model-1',
'name' => 'Test Model 1',
'taskType' => 'imageInference',
),
array(
'id' => 'test-model-2',
'name' => 'Test Model 2',
'taskType' => 'imageInference',
),
'data' => array(
array(
'id' => 'test-model-1',
'name' => 'Test Model 1',
'cost' => 0.001,
'provider' => 'runware',
),
array(
'id' => 'test-model-2',
'name' => 'Test Model 2',
'cost' => 0.002,
'provider' => 'runware',
),
),
)
),
)
);
$fake_response = array(
'response' => array( 'code' => 200 ),
'body' => $fake_body,
);
Functions\when( 'wp_remote_post' )->justReturn( $fake_response );
// Alias the WP response-parsing helpers so they work against our array.
Functions\when( 'wp_remote_retrieve_response_code' )->alias(
function ( $r ) { return (int) ( $r['response']['code'] ?? 0 ); }
);
Functions\when( 'wp_remote_retrieve_body' )->alias(
function ( $r ) { return (string) ( $r['body'] ?? '' ); }
);
Functions\when( 'is_wp_error' )->justReturn( false );
// Shared options store so get_option/update_option stay in sync
// within this test. The API key must be non-empty for sync() to proceed.
$store = array( 'prautoblogger_runware_api_key' => 'test-api-key-plain' );
Functions\when( 'get_option' )->alias(
function ( $key, $default = false ) use ( &$store ) {
return $store[ $key ] ?? $default;
// Mock the API key to exist.
Functions\when( 'get_option' )->alias( function ( $key, $default = false ) {
static $options = array();
if ( 'prautoblogger_runware_api_key' === $key ) {
return 'test-api-key';
}
);
Functions\when( 'update_option' )->alias(
function ( $key, $value ) use ( &$store ) {
$store[ $key ] = $value;
return true;
}
);
return $options[ $key ] ?? $default;
} );
// Sync should succeed: API key present, HTTP 200, 2 imageInference models.
Functions\when( 'update_option' )->alias( function ( $key, $value ) {
static $options = array();
$options[ $key ] = $value;
return true;
} );
// Perform the sync.
$catalog = new \PRAutoBlogger_Runware_Model_Catalog();
$result = $catalog->sync();
$this->assertTrue( $result, 'Sync should return true on HTTP 200 with valid models' );
$this->assertTrue( $result, 'Sync should return true on success' );
// get_models() should now return the synced list from cache.
// Verify get_models returns the synced models (not fallback).
$models = $catalog->get_models();
$this->assertIsArray( $models );
$this->assertCount( 2, $models );

View File

@@ -16,18 +16,6 @@ use PRAutoBlogger\Tests\BaseTestCase;
class ImageModelRegistryTest extends BaseTestCase {
/**
* Stub WP cache functions so the catalog class falls back to the
* hardcoded model list without hitting get_option or update_option.
*/
protected function setUp(): void {
parent::setUp();
// Catalog checks for cached data; return false → falls back to hardcoded list.
\Brain\Monkey\Functions\when( 'get_option' )->justReturn( false );
// Catalog writes cache on sync; stub so it doesn't throw in tests.
\Brain\Monkey\Functions\when( 'update_option' )->justReturn( true );
}
/**
* Every entry returned by get_models() must carry a non-empty id and
* a provider of 'runware' or 'openrouter' (Cloudflare Workers AI was