* fix(image-pipeline): surface per-image failures in the event log
Two gaps made posts 650/657 appear to silently "lose" their featured
images on 2026-04-20:
1. The batch provider tagged each failed key with ['error' => ...] but
the ERROR log line it emitted didn't identify which key failed, so
operators couldn't tell image_a from image_b in the event log when
one of two parallel requests died.
2. Image_Pipeline::process_image_a/_b folded the per-key error into
$result['errors'] and returned silently, with no WARNING row. Ops
only learned about the miss when the outer catch in the caller fired
— and on the 2026-04-20 incident that outer catch didn't fire, so
nothing was logged at all except an unrelated pricing warning.
Fix:
- class-open-router-image-batch.php: collect_result() takes the request
key as its first argument and every Logger::error message reads
'OpenRouter batch image gen <class> for key "<key>": <detail>'.
Matches the shape of OpenRouter_Image_Support::log_retry.
- class-image-pipeline.php: process_image_a and process_image_b now
emit a Logger::warning('image_pipeline') row whenever $image_data is
missing or carries an error key, in addition to pushing to
$result['errors']. The warning fires whether or not the outer catch
runs.
Test: ImagePipelineTest::test_partial_batch_failure_emits_warning_log_row
mocks $wpdb, runs the pipeline with image_a erroring, and asserts at
least one warning/image_pipeline row was inserted.
No behavior change beyond logging; cost impact zero.
Part of the v0.8.0 image bundle (issues from thread image-mime-bug).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(images): retry Cloudflare NSFW-blocked prompts with a sanitized fallback
Cloudflare's Workers AI FLUX.1 schnell content filter sometimes rejects
peptide-comic prompts as NSFW (HTTP 400, errors[].code === 3030). On
2026-04-20 this produced a silent publish-without-featured-image for
post #650 ("Semaglutide vs Tirzepatide") — the LLM-rewritten scene
tripped the filter and we had no recovery path.
New behaviour: on a 3030 response, the Cloudflare provider throws a
typed PRAutoBlogger_Image_NSFW_Blocked exception. The batch wrapper
catches it and tags the per-slot result with
`error_type: nsfw_blocked`. Image_Pipeline hands the results to a new
PRAutoBlogger_Image_NSFW_Retry helper, which reruns the blocked slot
once with a sanitized fallback prompt (title + style suffix, no LLM
rewrite). On success the retry result replaces the error entry; on a
second block, a WARNING is logged and the pipeline publishes without
that image — same terminal behaviour we had before, just explicit.
Files:
- New providers/class-image-nsfw-blocked.php — typed exception.
- providers/class-cloudflare-image-support.php — is_nsfw_error() +
throw_if_nsfw() helpers.
- providers/class-cloudflare-image-provider.php — calls throw_if_nsfw
inside the 4xx branch; batch catches the typed exception and marks
the result with error_type.
- core/class-image-prompt-builder.php — new public build_fallback_prompt
shim so the retry layer can reach the rule-based fallback without
going through the LLM path.
- New core/class-image-nsfw-retry.php — single-shot retry helper.
- core/class-image-pipeline.php — invokes the retry between batch
completion and per-slot processing.
- admin/class-settings-fields-extended.php — new toggle
`prautoblogger_image_nsfw_retry` (default on) in the Images section.
Tests:
- CloudflareImageProviderTest — 3030 response throws NSFW exception;
batch tags the per-slot result with `error_type: nsfw_blocked`.
- ImagePipelineTest — retry succeeds when provider returns a good
result on the second call; retry is skipped when the setting is off.
Cost impact: worst-case an extra Cloudflare call per NSFW-blocked slot
(~$0.000834). Current incidence ~2% of generations; ~+$0.00002 per
generation amortized. Immaterial.
Uninstall: new option covered by the existing prautoblogger_% LIKE
sweep in uninstall.php.
Part of the v0.8.0 image bundle (issues from thread image-mime-bug).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(images): expose rewriter system prompt as admin setting
CEO feedback: every generated image is anchoring on the cartoon
peptide-with-arms-and-legs character because the hardcoded rewriter
system prompt both lists "anthropomorphized molecules" first in its
primer and ships a few-shot example that literally shows one. The ask
is to give CEO the lever without a code change.
Adds `prautoblogger_image_prompt_instructions` (textarea) in the
Images section. Default value is the existing REWRITER_SYSTEM_PROMPT
constant verbatim, so behaviour is unchanged on upgrade until someone
edits it. `Image_Prompt_Builder::rewrite_via_llm()` reads the option
at call time via a new `resolve_system_prompt()` helper and falls back
to the constant if the option is blank or whitespace-only, so a saved
empty value cannot brick image generation.
Const visibility flipped from `private` to `public` so the settings
field can reference it directly as the field default.
Tests:
- `test_rewrite_uses_setting_when_present` — option set, assert the
LLM's system message equals the option content.
- `test_rewrite_falls_back_to_default_when_setting_empty` — option
whitespace, assert the LLM's system message equals the constant.
Uninstall: new option covered by the existing prautoblogger_% LIKE
sweep in uninstall.php.
Cost impact: none by default. Future CEO edits will change prompt
length; current default is ~180 tokens, budget impact linear from
there.
Part of the v0.8.0 image bundle (thread image-mime-bug, message 03).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(images): collapse Image Provider + Image Model into a single dropdown
Until now, Image Provider and Image Model were two independent selects
in the admin UI. That let the CEO set Model=flux-1-schnell while
Provider was still openrouter — OpenRouter has no such model so every
image gen on posts 650/657 on 2026-04-20 returned HTTP 400, and the
article published without a featured image.
v0.8.0 removes the Image Provider field from the admin UI entirely.
The provider is now derived from the model's entry in the registry
every time the model is saved. A new
PRAutoBlogger_Image_Model_Registry::provider_for() helper centralises
the mapping, so admin save, the on-upgrade migration, and future
callers all read from one place.
On save, PRAutoBlogger_Admin_Page::sanitize_image_model() validates
the submitted slug against the registry:
- Known slug: persist it, and update `prautoblogger_image_provider` to
the paired provider as a side-effect.
- Unknown slug: add_settings_error with a clear message, and return
the previously-saved value so the site stays on a working model.
A one-shot v0.8.0 migration in
PRAutoBlogger::on_check_db_version() derives the provider from the
currently-saved model on first admin_init after the plugin is updated.
This auto-heals sites that were running mismatched pairs before the
upgrade, without requiring the admin to re-open settings.
`prautoblogger_image_provider` option key is preserved — all existing
reads in the pipeline, cost tracker, and validators continue to work
unchanged.
Tests (Services/ImageModelRegistryTest):
- Every registry entry has an id and a provider in {openrouter, cloudflare}.
- provider_for() returns the paired provider for a known slug.
- provider_for() returns '' for unknown/empty slugs.
Cost impact: none.
Migration idempotent via the `prautoblogger_migrated_image_provider_v080`
flag. Uninstall sweeps it along with every other plugin option.
Part of the v0.8.0 image bundle (thread image-mime-bug, Issue 1).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* chore(v0.8.0): version bump + CHANGELOG + ARCHITECTURE.md for image bundle
Bump plugin header and PRAUTOBLOGGER_VERSION 0.7.3 → 0.8.0. Minor bump
because Commit D removes the Image Provider dropdown from admin (a
UX-visible change), not a patch.
CHANGELOG:
- Changed: collapsed Image Provider + Model dropdowns; provider derived.
- Added: Image Prompt Instructions setting; Retry NSFW-Blocked Images
setting.
- Fixed: silent per-image failures in the event log.
ARCHITECTURE.md:
- Stage 6b (Image Pipeline) callout now describes provider derivation
and the NSFW-retry fallback path.
- wp_options table lists the four new/changed keys
(`prautoblogger_image_provider` as derived, new
`prautoblogger_image_prompt_instructions`,
`prautoblogger_image_nsfw_retry`, and the one-shot migration flag
`prautoblogger_migrated_image_provider_v080`).
DB schema unchanged — no PRAUTOBLOGGER_DB_VERSION bump needed.
Final commit of the v0.8.0 image bundle (thread image-mime-bug).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: peptiderepo <peptiderepo@users.noreply.github.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
266 lines
14 KiB
PHP
266 lines
14 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* Extended settings fields: schedule, publishing, analytics, display, and images.
|
||
*
|
||
* What: Declarative field definitions for operational settings sections.
|
||
* Who calls it: PRAutoBlogger_Settings_Fields::get_fields() merges these in.
|
||
* Dependencies: PRAUTOBLOGGER_DEFAULT_IMAGE_MODEL, PRAUTOBLOGGER_DEFAULT_IMAGE_STYLE_SUFFIX constants,
|
||
* PRAutoBlogger_Article_Typography (font choices), PRAutoBlogger_Image_Model_Registry.
|
||
*
|
||
* @see admin/class-settings-fields.php — Core fields and sections; calls get_extended_fields().
|
||
* @see admin/class-admin-page.php — Renders fields registered from these definitions.
|
||
* @see admin/class-image-model-registry.php — Static image model list extracted for 300-line compliance.
|
||
* @see frontend/class-article-typography.php — Reads Display settings on the frontend.
|
||
* @see CONVENTIONS.md — "How To: Add a New Admin Setting".
|
||
*/
|
||
class PRAutoBlogger_Settings_Fields_Extended {
|
||
|
||
/**
|
||
* Static image model list for the admin model picker.
|
||
* Delegates to Image_Model_Registry (extracted for 300-line file limit).
|
||
*
|
||
* @return array<int, array<string, mixed>>
|
||
*/
|
||
public static function get_image_models(): array {
|
||
return PRAutoBlogger_Image_Model_Registry::get_models();
|
||
}
|
||
|
||
/**
|
||
* Get schedule, publishing, analytics, and image settings fields.
|
||
*
|
||
* @return array<int, array<string, mixed>>
|
||
*/
|
||
public static function get_fields(): array {
|
||
return [
|
||
// ── Sources (extended) ──────────────────────────────────────
|
||
[
|
||
'id' => 'prautoblogger_research_model',
|
||
'label' => __( 'Research Model', 'prautoblogger' ),
|
||
'type' => 'model_select',
|
||
'section' => 'prautoblogger_sources',
|
||
'default' => PRAUTOBLOGGER_DEFAULT_ANALYSIS_MODEL,
|
||
'capability' => 'text→text',
|
||
'description' => __( 'Model for LLM Deep Research. Pick a reasoning-capable model (e.g. Grok 4.1 Fast, DeepSeek-R1) for best results. Only used when LLM Deep Research is enabled above.', 'prautoblogger' ),
|
||
'badge' => __( 'Reasoning', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_research_prompt',
|
||
'label' => __( 'Research Prompt', 'prautoblogger' ),
|
||
'type' => 'textarea',
|
||
'section' => 'prautoblogger_sources',
|
||
'default' => 'Conduct a deep research sweep of the {niche} space and identify the most substantive, actionable topics that practitioners and curious newcomers are searching for or actively discussing.' . "\n\n" . 'Focus on:' . "\n" . '- Practical questions real readers would search for (not theoretical debates)' . "\n" . '- Emerging trends gaining momentum in the community' . "\n" . '- Common misconceptions that deserve clarification' . "\n" . '- Comparative analysis (product vs. product, protocol vs. protocol)' . "\n" . '- Safety and efficacy topics that drive concern or curiosity' . "\n" . '- Beginner-to-advanced mix — cover entry-level and depth-focused topics' . "\n\n" . 'Aim for topics that feel timely, underserved by existing content, and likely to drive reader engagement. Return 8-12 findings with detailed analysis in each.',
|
||
'description' => __( 'The research brief sent to the LLM. Use {niche} as a placeholder for your niche description. The system prompt handles output format — this controls WHAT to research.', 'prautoblogger' ),
|
||
],
|
||
|
||
// ── AI Models (extended) ────────────────────────────────────
|
||
[
|
||
'id' => 'prautoblogger_reasoning_enabled',
|
||
'label' => __( 'Enable Reasoning', 'prautoblogger' ),
|
||
'type' => 'toggle',
|
||
'section' => 'prautoblogger_models',
|
||
'default' => '0',
|
||
'description' => __( 'Send reasoning instructions to models that support it (e.g. Grok, DeepSeek-R1). Reasoning tokens are billed as output tokens — expect higher costs per call. Models that don\'t support reasoning will ignore this.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_reasoning_effort',
|
||
'label' => __( 'Reasoning Effort', 'prautoblogger' ),
|
||
'type' => 'select',
|
||
'section' => 'prautoblogger_models',
|
||
'default' => 'medium',
|
||
'options' => [
|
||
'xhigh' => __( 'Extra High — maximum depth, highest cost', 'prautoblogger' ),
|
||
'high' => __( 'High — thorough reasoning', 'prautoblogger' ),
|
||
'medium' => __( 'Medium — balanced (recommended)', 'prautoblogger' ),
|
||
'low' => __( 'Low — light reasoning, lower cost', 'prautoblogger' ),
|
||
'minimal' => __( 'Minimal — barely any reasoning', 'prautoblogger' ),
|
||
],
|
||
'description' => __( 'How much effort the model spends reasoning before answering. Higher effort = more reasoning tokens = higher cost. Only applies when reasoning is enabled above.', 'prautoblogger' ),
|
||
],
|
||
|
||
// ── Schedule & Budget ───────────────────────────────────────
|
||
[
|
||
'id' => 'prautoblogger_daily_article_target',
|
||
'label' => __( 'Articles per Day', 'prautoblogger' ),
|
||
'type' => 'number',
|
||
'section' => 'prautoblogger_schedule',
|
||
'default' => 1,
|
||
'min' => 1,
|
||
'max' => 10,
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_schedule_time',
|
||
'label' => __( 'Generation Time', 'prautoblogger' ),
|
||
'type' => 'time',
|
||
'section' => 'prautoblogger_schedule',
|
||
'default' => '03:00',
|
||
'description' => __( 'Daily generation runs at this time (server timezone).', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_monthly_budget_usd',
|
||
'label' => __( 'Monthly Budget (USD)', 'prautoblogger' ),
|
||
'type' => 'number',
|
||
'section' => 'prautoblogger_schedule',
|
||
'default' => 50,
|
||
'min' => 0,
|
||
'step' => '0.01',
|
||
'description' => __( 'Hard stop when reached. 0 = unlimited.', 'prautoblogger' ),
|
||
],
|
||
|
||
// ── Publishing ──────────────────────────────────────────────
|
||
[
|
||
'id' => 'prautoblogger_auto_publish',
|
||
'label' => __( 'Auto-Publish', 'prautoblogger' ),
|
||
'type' => 'toggle',
|
||
'section' => 'prautoblogger_publishing',
|
||
'default' => '0',
|
||
'description' => __( 'Automatically publish editor-approved posts. When off, all posts go to the Review Queue as drafts.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_default_author',
|
||
'label' => __( 'Default Author', 'prautoblogger' ),
|
||
'type' => 'author_select',
|
||
'section' => 'prautoblogger_publishing',
|
||
'default' => 0,
|
||
'description' => __( 'WordPress user assigned as author of generated posts.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_default_category',
|
||
'label' => __( 'Default Category', 'prautoblogger' ),
|
||
'type' => 'category_select',
|
||
'section' => 'prautoblogger_publishing',
|
||
'default' => 0,
|
||
'description' => __( 'Fallback category for generated posts.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_log_level',
|
||
'label' => __( 'Log Level', 'prautoblogger' ),
|
||
'type' => 'select',
|
||
'section' => 'prautoblogger_publishing',
|
||
'default' => 'info',
|
||
'options' => [
|
||
'error' => __( 'Error — only failures', 'prautoblogger' ),
|
||
'warning' => __( 'Warning — errors + warnings', 'prautoblogger' ),
|
||
'info' => __( 'Info — key events (recommended)', 'prautoblogger' ),
|
||
'debug' => __( 'Debug — everything (verbose)', 'prautoblogger' ),
|
||
],
|
||
'description' => __( 'Controls detail level in the Activity Log.', 'prautoblogger' ),
|
||
],
|
||
|
||
// ── Analytics ───────────────────────────────────────────────
|
||
[
|
||
'id' => 'prautoblogger_ga4_property_id',
|
||
'label' => __( 'GA4 Property ID', 'prautoblogger' ),
|
||
'type' => 'text',
|
||
'section' => 'prautoblogger_analytics',
|
||
'description' => __( 'Format: properties/XXXXXXXXX. Leave blank to skip GA4.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_ga4_credentials_json',
|
||
'label' => __( 'Service Account JSON', 'prautoblogger' ),
|
||
'type' => 'password',
|
||
'section' => 'prautoblogger_analytics',
|
||
'description' => __( 'Paste the full JSON key file for a service account with Analytics read access.', 'prautoblogger' ),
|
||
],
|
||
|
||
// ── Display ────────────────────────────────────────────────
|
||
[
|
||
'id' => 'prautoblogger_article_font_family',
|
||
'label' => __( 'Article Font', 'prautoblogger' ),
|
||
'type' => 'select',
|
||
'section' => 'prautoblogger_display',
|
||
'default' => 'default',
|
||
'options' => PRAutoBlogger_Article_Typography::get_font_choices(),
|
||
'description' => __( 'Font family used for generated article body text. Serif fonts improve long-form readability.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_article_font_size',
|
||
'label' => __( 'Article Font Size (px)', 'prautoblogger' ),
|
||
'type' => 'number',
|
||
'section' => 'prautoblogger_display',
|
||
'default' => 0,
|
||
'min' => 0,
|
||
'max' => 32,
|
||
'description' => __( 'Base font size for article body text. 0 uses the theme default (13px). Recommended: 16–18px for comfortable reading.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_table_borders',
|
||
'label' => __( 'Table Borders', 'prautoblogger' ),
|
||
'type' => 'toggle',
|
||
'section' => 'prautoblogger_display',
|
||
'default' => '1',
|
||
'description' => __( 'Add visible borders, padding, and alternating row colors to tables in generated articles.', 'prautoblogger' ),
|
||
],
|
||
|
||
// ── Images ─────────────────────────────────────────────────
|
||
[
|
||
'id' => 'prautoblogger_image_enabled',
|
||
'label' => __( 'Enable Image Generation', 'prautoblogger' ),
|
||
'type' => 'toggle',
|
||
'section' => 'prautoblogger_images',
|
||
'default' => '0',
|
||
'description' => __( 'Generate images for each published article.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_image_b_enabled',
|
||
'label' => __( 'Generate Second Image (B)', 'prautoblogger' ),
|
||
'type' => 'toggle',
|
||
'section' => 'prautoblogger_images',
|
||
'default' => '1',
|
||
'description' => __( 'Generate a second image from source data for A/B testing. Disabling saves one image generation + one LLM prompt rewrite per article.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_image_model',
|
||
'label' => __( 'Image Model', 'prautoblogger' ),
|
||
'type' => 'model_select',
|
||
'section' => 'prautoblogger_images',
|
||
'default' => PRAUTOBLOGGER_DEFAULT_IMAGE_MODEL,
|
||
'capability' => 'image_generation',
|
||
'description' => __( 'Pick an image model. The provider (OpenRouter or Cloudflare Workers AI) is derived from the model registry on save, so mismatched pairs are no longer possible.', 'prautoblogger' ),
|
||
'badge' => __( 'Quality', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_cloudflare_ai_token',
|
||
'label' => __( 'Cloudflare API Token', 'prautoblogger' ),
|
||
'type' => 'password',
|
||
'section' => 'prautoblogger_images',
|
||
'description' => __( 'Only needed if using Cloudflare provider. Workers AI token with Read + Edit scope.', 'prautoblogger' ),
|
||
'icon' => '🔑',
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_cloudflare_account_id',
|
||
'label' => __( 'Cloudflare Account ID', 'prautoblogger' ),
|
||
'type' => 'text',
|
||
'section' => 'prautoblogger_images',
|
||
'default' => '',
|
||
'description' => __( 'Only needed if using Cloudflare provider.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_image_style_suffix',
|
||
'label' => __( 'Style Suffix', 'prautoblogger' ),
|
||
'type' => 'textarea',
|
||
'section' => 'prautoblogger_images',
|
||
'default' => PRAUTOBLOGGER_DEFAULT_IMAGE_STYLE_SUFFIX,
|
||
'description' => __( 'Appended to every image prompt. Controls visual look. Changing mid-run causes visible style drift.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_image_prompt_instructions',
|
||
'label' => __( 'Image Prompt Instructions', 'prautoblogger' ),
|
||
'type' => 'textarea',
|
||
'section' => 'prautoblogger_images',
|
||
'default' => PRAutoBlogger_Image_Prompt_Builder::REWRITER_SYSTEM_PROMPT,
|
||
'description' => __( 'System prompt given to the rewriter LLM that turns each article into a SCENE + CAPTION for the image generator. Changing this reshapes the look of all future images. Leave blank to use the default.', 'prautoblogger' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_image_nsfw_retry',
|
||
'label' => __( 'Retry NSFW-Blocked Images', 'prautoblogger' ),
|
||
'type' => 'toggle',
|
||
'section' => 'prautoblogger_images',
|
||
'default' => '1',
|
||
'description' => __( 'When the provider rejects an image prompt as NSFW (e.g. Cloudflare code 3030), retry once with a generic fallback scene built from the article title. Disable to fail fast if the filter gets trigger-happy.', 'prautoblogger' ),
|
||
],
|
||
];
|
||
}
|
||
}
|