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
95 lines
2.8 KiB
PHP
95 lines
2.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Database table creation and schema versioning for PGM.
|
|
*
|
|
* Creates {prefix}pgm_ai_visibility via dbDelta (idempotent). Tracks the
|
|
* schema version in the pgm_schema_version option so future migrations can
|
|
* detect when an upgrade is needed without destructive ALTER TABLE.
|
|
*
|
|
* Who triggers: PGM_Activator::activate() on plugin activation.
|
|
* Dependencies: $wpdb global, dbDelta() (requires upgrade.php).
|
|
*
|
|
* @see class-pgm-activator.php — Calls create_table() on activation.
|
|
* @see ARCHITECTURE.md — §Storage.
|
|
* @package PeptideGeoMonitor
|
|
*/
|
|
class PGM_Table_Manager {
|
|
|
|
/**
|
|
* Name of the custom table (without $wpdb->prefix).
|
|
*/
|
|
const TABLE_BASE = 'pgm_ai_visibility';
|
|
|
|
/**
|
|
* Create or upgrade the AI-visibility table.
|
|
*
|
|
* Uses dbDelta to create or non-destructively alter the table.
|
|
* Records the schema version in an option afterwards.
|
|
*
|
|
* Side effects: Writes to the database; sets pgm_schema_version option.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function create_table(): void {
|
|
global $wpdb;
|
|
|
|
$table_name = $wpdb->prefix . self::TABLE_BASE;
|
|
$charset_collate = $wpdb->get_charset_collate();
|
|
|
|
$sql = "CREATE TABLE {$table_name} (
|
|
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
run_id VARCHAR(36) NOT NULL,
|
|
captured_at DATETIME NOT NULL,
|
|
peptide_slug VARCHAR(200) NOT NULL,
|
|
peptide_label VARCHAR(200) NOT NULL,
|
|
model VARCHAR(200) NOT NULL,
|
|
prompt_intent VARCHAR(200) NOT NULL,
|
|
cited TINYINT(1) NOT NULL DEFAULT 0,
|
|
our_position INT(11) NULL DEFAULT NULL,
|
|
source_domains LONGTEXT NOT NULL DEFAULT '',
|
|
raw_excerpt LONGTEXT NOT NULL DEFAULT '',
|
|
cost_usd DECIMAL(12,8) NOT NULL DEFAULT 0.00000000,
|
|
PRIMARY KEY (id),
|
|
KEY run_id (run_id),
|
|
KEY peptide_slug (peptide_slug),
|
|
KEY captured_at (captured_at)
|
|
) {$charset_collate};";
|
|
|
|
if ( ! function_exists( 'dbDelta' ) ) {
|
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
|
}
|
|
|
|
dbDelta( $sql );
|
|
|
|
update_option( 'pgm_schema_version', PGM_SCHEMA_VERSION );
|
|
}
|
|
|
|
/**
|
|
* Get the full table name including the WP prefix.
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function get_table_name(): string {
|
|
global $wpdb;
|
|
return $wpdb->prefix . self::TABLE_BASE;
|
|
}
|
|
|
|
/**
|
|
* Drop the AI-visibility table entirely.
|
|
*
|
|
* Called only from uninstall.php — never during normal deactivation.
|
|
*
|
|
* Side effects: Permanently destroys all probe data.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function drop_table(): void {
|
|
global $wpdb;
|
|
$table = $wpdb->prefix . self::TABLE_BASE;
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
|
$wpdb->query( "DROP TABLE IF EXISTS {$table}" );
|
|
}
|
|
}
|