Files
pr-vision/includes/core/class-pgm-cron.php
peptiderepo ca1a41f6f9 feat: peptide-geo-monitor v0.1.0 — AI-visibility dashboard
Add initial plugin implementation per the CTO engineering handoff
(geomonitor/threads/2026-06-ai-visibility-v1/01-cto-eng-handoff.md).

## What
- PGM_Probe_Provider interface + PGM_Probe_Result value object
- PGM_Perplexity_Provider (sonar via OpenRouter/CF AI Gateway)
- PGM_OpenRouter_Provider (GPT-search + Gemini-search, generic parameterised)
- PGM_Gateway_Client: shared HTTP + exponential-backoff retry (mirrors PRAB)
- PGM_Citation_Detector: domain extraction, is_cited(), get_our_position()
- PGM_Cost_Ledger: MTD cost from DB, can_afford() pre-check, hard monthly cap
- PGM_Probe_Runner: peptides×intents×models run, budget abort, in-run cache
- PGM_Table_Manager: pgm_ai_visibility dbDelta, schema-version, drop_table()
- PGM_Config: typed getters, seed_defaults() (12 peptides, 3 intents, 3 models)
- PGM_Cron: weekly pgm_weekly_probe schedule/clear
- PGM_Data_Collector + PGM_Dashboard_Panel interfaces (seam for future panels)
- PGM_Collector_Registry: singleton registry
- PGM_Ai_Visibility_Collector: trendline, standings, MTD cost (score formula)
- PGM_Ai_Visibility_Panel: proxy note, Chart.js trendline, standings table
- PGM_Admin_Page: GEO Monitor menu, Run now POST handler (nonce+capability)
- uninstall.php: DROP TABLE + DELETE pgm_* options + wp_clear_scheduled_hook
- Unit tests (6 files): citation detection, score calc, cost ledger + budget
  abort, provider response parsing (mock HTTP), cron, uninstall purge
- ARCHITECTURE.md, CONVENTIONS.md, CONTEXT.md (score formula), README.md,
  CHANGELOG.md — same-commit per DoD
- CI workflow: PHP lint (8.1/8.2/8.3) + PHPCS + 300-line check

## Security
- Secrets from wp-config constants only; none hardcoded
- manage_options + nonce on every admin action
- Input sanitized; output escaped

Agent-Session: geomonitor v0.1.0
2026-06-13 08:18:56 +00:00

83 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
/**
* WP-Cron management for the weekly AI-visibility probe.
*
* Registers the pgm_weekly_probe hook, schedules/clears the event, and
* dispatches to PGM_Probe_Runner on each tick.
*
* Who triggers: PGM_Plugin::init() (hook registration), PGM_Activator
* (schedule), PGM_Deactivator (clear).
* Dependencies: PGM_Probe_Runner.
*
* @see class-pgm-probe-runner.php — Runner invoked on each cron tick.
* @see class-pgm-activator.php — Calls schedule_weekly() on activation.
* @package PeptideGeoMonitor
*/
class PGM_Cron {
/**
* Register WordPress action hooks.
*
* Side effects: Adds action for PGM_CRON_HOOK.
*
* @return void
*/
public function register_hooks(): void {
add_action( PGM_CRON_HOOK, array( $this, 'handle_cron_tick' ) );
}
/**
* Callback fired by WP-Cron on the weekly schedule.
*
* Side effects: Instantiates and runs PGM_Probe_Runner.
*
* @return void
*/
public function handle_cron_tick(): void {
$runner = new PGM_Probe_Runner();
$runner->run();
}
/**
* Schedule the weekly cron event if not already scheduled.
*
* Safe to call on re-activation — no-op when already scheduled.
*
* Side effects: Adds a WP-Cron event.
*
* @return void
*/
public static function schedule_weekly(): void {
if ( ! wp_next_scheduled( PGM_CRON_HOOK ) ) {
wp_schedule_event( time(), 'weekly', PGM_CRON_HOOK );
}
}
/**
* Remove the weekly cron schedule.
*
* Called on deactivation. Does not purge data.
*
* Side effects: Removes the WP-Cron event.
*
* @return void
*/
public static function clear_schedule(): void {
$timestamp = wp_next_scheduled( PGM_CRON_HOOK );
if ( false !== $timestamp ) {
wp_unschedule_event( $timestamp, PGM_CRON_HOOK );
}
}
/**
* Check whether the weekly event is currently scheduled.
*
* @return bool
*/
public static function is_scheduled(): bool {
return false !== wp_next_scheduled( PGM_CRON_HOOK );
}
}