* feat(images): route Cloudflare Workers AI via AI Gateway by default
Direct probe on 2026-04-21 confirmed the Cloudflare AI Gateway
route to Workers AI (`gateway.ai.cloudflare.com/.../workers-ai/@cf/...`)
is no longer 403ing — the upstream regression noted in v0.8.0's
ARCHITECTURE.md #16 and in thread image-pipeline seq 6 has been
resolved. Routing via the gateway gives us the same benefits we
already get for OpenRouter LLM calls: response caching, cost/latency
dashboard, rate limiting.
Changes:
- `Cloudflare_Image_Support::build_endpoint_url()` now prefers the
gateway URL derived from the existing `prautoblogger_ai_gateway_base_url`
option. Direct API remains the fallback.
- New private helper `build_gateway_workers_ai_url()` strips the
OpenRouter path suffix from the configured gateway URL and appends
`/workers-ai/{model}`. Returns empty string (fallback to direct) when:
no gateway URL configured, the new toggle is off, or the URL isn't a
valid `https://gateway.ai.cloudflare.com/...` shape.
- New admin toggle `prautoblogger_cf_image_via_gateway` (default on) in
the Images section. Operators can disable to force direct API without
clearing the LLM-path gateway URL.
- ARCHITECTURE.md #16 rewritten to reflect the v0.8.2+ gateway routing.
- CHANGELOG.md v0.8.2 entry under Changed.
Tests (new `CloudflareImageSupportTest`):
- Direct URL when gateway not configured.
- Gateway URL derived correctly when configured + toggle on.
- Toggle off forces direct URL even with gateway configured.
- Malformed gateway URL falls back to direct (defensive).
- `is_nsfw_error()` only matches error code 3030 (pinned separately so
a tolerance regression fails early).
Cost impact: near-zero. Gateway adds a few ms of latency and enables
response caching — could *reduce* cost on identical prompts within the
configurable cache TTL.
Uninstall: new option covered by existing prautoblogger_% LIKE sweep.
Implements Commit 1b (narrow AI-Gateway-migration scope) per thread
image-pipeline seq 7.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* chore(cost): seed new toggles in activator defaults; tighten URL stripper
QA review on cd45ae2 surfaced two things; one real, one hallucinated:
- **Real (fixed):** `prautoblogger_cf_image_via_gateway` and v0.8.1's
`prautoblogger_image_nsfw_retry` weren't seeded in
`Activator::set_default_options()`. Belt-and-braces — the `get_option()`
fallback at the read site already defaults to '1', so no user-visible
regression exists, but seeding the options follows the established
pattern for every other plugin setting and makes intent legible to
operators inspecting `wp_options` directly.
- **Hallucinated (rejected):** the reviewer claimed
`CloudflareImageSupportTest` methods 2–5 were empty stubs. Verified
— every test has real `$this->assert*` calls (10 total across the 5
cases). Ignoring. Flagging the false-positive to the CTO per the
qa-review quality-tracking protocol.
Also clarified the docblock + tightened the ternary inside
`build_gateway_workers_ai_url()` — explicit `strrpos` false-check
instead of the `?:` short-circuit, so a no-slash base (defensive edge)
falls through cleanly instead of returning a weirdly-sliced URL.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(schedule): interpret `prautoblogger_schedule_time` in site timezone
Per thread scheduled-gen-timezone seq 1: WordPress core calls
`date_default_timezone_set('UTC')` early in bootstrap, so the activator's
`strtotime("tomorrow HH:MM")` was resolving in UTC regardless of the
site's configured timezone. On peptiderepo.com (Asia/Singapore, UTC+8)
a `schedule_time` of `06:00` fired at 14:00 local.
Fold this into the existing v0.8.2 branch (PR #102) since both this fix
and the earlier CF-image-gateway change target the same version and
touch overlapping files (activator, settings-fields-extended).
Changes:
- `Activator::next_daily_generation_timestamp()` — new public helper
that reads `prautoblogger_schedule_time`, constructs a
`DateTimeImmutable('tomorrow', wp_timezone())`, applies the
configured hour/minute, returns a Unix timestamp. Defensive fallback
to the old UTC-interpretation behaviour on any DateTime parse error
so a bad tz string can't brick the schedule.
- `Activator::schedule_cron()` delegates to the helper instead of
calling `strtotime` directly.
- New `Activator::reschedule_daily_in_site_timezone_v082()` — one-shot
migration that clears the stale UTC-scheduled event and re-queues
using the helper. Gated by `prautoblogger_migrated_schedule_tz_v082`
option so it runs exactly once. Logs an INFO line comparing
before/after next_run for auditability.
- `class-prautoblogger.php::on_check_db_version()` invokes the
migration on every admin_init; the one-shot flag handles re-run
guarding (no DB_VERSION bump needed — the migration is independent
of the schema version).
- Settings description for "Generation Time" now says
"...in your site's configured timezone (Settings → General →
Timezone). Current site timezone: <wp_timezone_string()>" instead
of the ambiguous "server timezone" phrasing.
File-size compliance:
- Adding the timezone helpers pushed `class-activator.php` to 352
lines. Extracted the 104-line `create_tables()` SQL to a new
`class-schema-installer.php` (136 lines). Activator now 245 lines,
schema file fully dedicated to dbDelta.
Tests (new):
- `ActivatorScheduleTest` — SGT 06:00 → 22:00 UTC prev day; UTC 06:00
→ 06:00 UTC (no-op case); Asia/Kathmandu UTC+5:45 → 00:15 UTC
(fractional offset); malformed time input doesn't throw.
- `ActivatorMigrationTest` — pre-seeded UTC cron gets cleared and
rescheduled in SGT; second pass is idempotent.
CHANGELOG v0.8.2 entry now also covers the timezone fix under Fixed +
the description wording change under Changed.
Implements thread `scheduled-gen-timezone` seq 1 spec.
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>
278 lines
14 KiB
PHP
278 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' => sprintf(
|
||
/* translators: %s: site timezone string, e.g. "Asia/Singapore". */
|
||
esc_html__( "Daily generation runs at this time in your site's configured timezone (Settings → General → Timezone). Current site timezone: %s.", 'prautoblogger' ),
|
||
esc_html( function_exists( 'wp_timezone_string' ) ? wp_timezone_string() : 'UTC' )
|
||
),
|
||
],
|
||
[
|
||
'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' => __( 'Your GA4 Property ID — digits only, e.g. 123456789 (the code adds the "properties/" prefix internally). Find it in GA4 → Admin → Property details. 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' ),
|
||
],
|
||
[
|
||
'id' => 'prautoblogger_cf_image_via_gateway',
|
||
'label' => __( 'Route Cloudflare Images via AI Gateway', 'prautoblogger' ),
|
||
'type' => 'toggle',
|
||
'section' => 'prautoblogger_images',
|
||
'default' => '1',
|
||
'description' => __( 'Route Cloudflare Workers AI image calls through the Cloudflare AI Gateway (requires the AI Gateway URL configured under API Keys). Adds response caching, cost logging, and rate-limiting. Turn off to hit the direct Workers AI API — only needed if the gateway route regresses.', 'prautoblogger' ),
|
||
],
|
||
];
|
||
}
|
||
}
|