PR Vision v0.2.0: admin/settings interface + correctness fixes
Requirements 1-8 + all non-negotiables:
1. Model manager (CRUD): PRV_Model_Registry, prv_models v2 option, versioned
idempotent migration from v0.1.x flat-string format, Settings page CRUD.
2. Per-model run-health: PRV_Model_Registry::update_health() after each run;
health badges (Healthy/Retired?/Disabled/Unknown) in model table + dashboard.
3. Metric comparability: PRV_Config_Version stamps every run with config_version;
save-bar warning on scoring-relevant changes; orphaned rows excluded by version.
4. Projected cost at save: PRV_Config::get_projected_cost(); over-cap warning;
prv_last_run_truncated option + visible notice when cap hit mid-run.
5. Config surface: peptide set, intents, cadence (reschedules cron), budget cap;
all validated + sanitized; next-run timestamp shown.
6. API-key three-state: not-defined / defined-ok / defined-failed; derived from
ledger; never displays the key.
7. Run-lock: PRV_Run_Lock (transient); cron tick skips if locked; Run-now
refuses with clear message; lock released in finally block.
8. Design fixes: dark-ink focus ring on bright fills; disabled-row full AA
contrast (no opacity); toggle disabled on retired rows; save-bar-level
warning (role=alert); chip text #B4BDC7 6.83:1 AA; keyboard-sortable headers
(<button>+aria-sort); aria-live on Test chip; z-index:50 save-bar;
run-now double-fire guard; @media print light overrides.
New: PRV_Upgrader (runs migrations on plugins_loaded, not just activation),
PRV_Config_Version, PRV_Run_Lock, PRV_Settings_Page, PRV_Settings_Renderer,
PRV_Model_Manager_Table, PRV_Model_Test_Ajax.
Tests: test-model-registry, test-run-lock, test-config-version,
test-projected-cost, test-cron-reschedule (+ updated probe-runner,
uninstall, bootstrap).
Schema: config_version INT column added via dbDelta (idempotent).
Uninstall: purges all v0.2 prv_* options + transients.
This commit is contained in:
@@ -8,17 +8,21 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* WP-Cron management for the weekly AI-visibility probe.
|
||||
* WP-Cron management for the AI-visibility probe.
|
||||
*
|
||||
* Registers the prv_weekly_probe hook, schedules/clears the event, and
|
||||
* dispatches to PRV_Probe_Runner on each tick.
|
||||
* dispatches to PRV_Probe_Runner on each tick. Cadence changes clear the
|
||||
* existing schedule and reschedule at the new recurrence so next-run
|
||||
* timestamp reflects the actual cadence, not just the saved option.
|
||||
*
|
||||
* Who triggers: PRV_Plugin::init() (hook registration), PRV_Activator
|
||||
* (schedule), PRV_Deactivator (clear).
|
||||
* Dependencies: PRV_Probe_Runner.
|
||||
* (schedule), PRV_Deactivator (clear), PRV_Settings_Page
|
||||
* (reschedule on cadence change).
|
||||
* Dependencies: PRV_Probe_Runner, PRV_Run_Lock.
|
||||
*
|
||||
* @see class-prv-probe-runner.php — Runner invoked on each cron tick.
|
||||
* @see class-prv-activator.php — Calls schedule_weekly() on activation.
|
||||
* @see class-prv-probe-runner.php -- Runner invoked on each cron tick.
|
||||
* @see class-prv-activator.php -- Calls schedule() on activation.
|
||||
* @see class-prv-settings-page.php -- Calls reschedule() on cadence change.
|
||||
* @package PrVision
|
||||
*/
|
||||
class PRV_Cron {
|
||||
@@ -35,34 +39,85 @@ class PRV_Cron {
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback fired by WP-Cron on the weekly schedule.
|
||||
* Callback fired by WP-Cron on the scheduled cadence.
|
||||
*
|
||||
* Respects the run-lock; if already running (e.g. "Run now" is active)
|
||||
* the cron tick is skipped silently without double-spending.
|
||||
*
|
||||
* Side effects: Instantiates and runs PRV_Probe_Runner.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle_cron_tick(): void {
|
||||
if ( PRV_Run_Lock::is_locked() ) {
|
||||
// Another run is in progress -- skip this tick.
|
||||
return;
|
||||
}
|
||||
$runner = new PRV_Probe_Runner();
|
||||
$runner->run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule the weekly cron event if not already scheduled.
|
||||
* Schedule the probe cron event using the configured cadence.
|
||||
*
|
||||
* Safe to call on re-activation — no-op when already scheduled.
|
||||
* Safe to call on re-activation -- no-op when already scheduled at the
|
||||
* correct recurrence; clears and reschedules when recurrence differs.
|
||||
*
|
||||
* Side effects: Adds a WP-Cron event.
|
||||
* Side effects: Adds (or replaces) a WP-Cron event.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function schedule(): void {
|
||||
$cadence = PRV_Config::get_cadence();
|
||||
$next = wp_next_scheduled( PRV_CRON_HOOK );
|
||||
|
||||
if ( false !== $next ) {
|
||||
// Check if the current recurrence matches.
|
||||
$events = _get_cron_array();
|
||||
foreach ( $events as $timestamp => $hooks ) {
|
||||
if ( isset( $hooks[ PRV_CRON_HOOK ] ) ) {
|
||||
foreach ( $hooks[ PRV_CRON_HOOK ] as $event ) {
|
||||
if ( isset( $event['schedule'] ) && $event['schedule'] === $cadence ) {
|
||||
return; // Already scheduled at the right cadence.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Wrong cadence -- clear and reschedule.
|
||||
self::clear_schedule();
|
||||
}
|
||||
|
||||
wp_schedule_event( time(), $cadence, PRV_CRON_HOOK );
|
||||
}
|
||||
|
||||
/**
|
||||
* Reschedule the cron at a new cadence.
|
||||
*
|
||||
* Clears the existing event and creates a new one immediately.
|
||||
* Called by PRV_Settings_Page when the cadence option changes.
|
||||
*
|
||||
* Side effects: Removes existing cron event; adds new one.
|
||||
*
|
||||
* @param string $cadence New WP-Cron recurrence identifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function reschedule( string $cadence ): void {
|
||||
self::clear_schedule();
|
||||
wp_schedule_event( time(), $cadence, PRV_CRON_HOOK );
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule using the legacy 'weekly' recurrence (activation compat).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function schedule_weekly(): void {
|
||||
if ( ! wp_next_scheduled( PRV_CRON_HOOK ) ) {
|
||||
wp_schedule_event( time(), 'weekly', PRV_CRON_HOOK );
|
||||
}
|
||||
self::schedule();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the weekly cron schedule.
|
||||
* Remove the cron schedule.
|
||||
*
|
||||
* Called on deactivation. Does not purge data.
|
||||
*
|
||||
@@ -78,11 +133,20 @@ class PRV_Cron {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the weekly event is currently scheduled.
|
||||
* Check whether the probe event is currently scheduled.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_scheduled(): bool {
|
||||
return false !== wp_next_scheduled( PRV_CRON_HOOK );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the timestamp of the next scheduled run.
|
||||
*
|
||||
* @return int|false Timestamp or false if not scheduled.
|
||||
*/
|
||||
public static function next_run_timestamp() {
|
||||
return wp_next_scheduled( PRV_CRON_HOOK );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user