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
81 lines
2.2 KiB
PHP
81 lines
2.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Detects whether peptiderepo.com appears in a list of source domains.
|
|
*
|
|
* Centralises the citation-detection logic so both providers can share it
|
|
* without duplication. Also parses raw citation arrays/URLs into a clean
|
|
* domain list.
|
|
*
|
|
* Who triggers: PGM_Perplexity_Provider, PGM_OpenRouter_Provider.
|
|
* Dependencies: None (pure PHP).
|
|
*
|
|
* @see CONTEXT.md — "cited", "our_position", "source_domains".
|
|
* @package PeptideGeoMonitor
|
|
*/
|
|
class PGM_Citation_Detector {
|
|
|
|
/**
|
|
* Parse raw citation data from an API response into a clean domain list.
|
|
*
|
|
* Accepts either a flat array of URL strings or an array of objects with
|
|
* a 'url' key (Perplexity style). Unknown shapes are skipped gracefully.
|
|
*
|
|
* @param array<mixed> $raw_citations Raw citations from the API response.
|
|
*
|
|
* @return string[] Lowercase domain names (e.g. ['peptiderepo.com', 'examine.com']).
|
|
*/
|
|
public function parse_domains( array $raw_citations ): array {
|
|
$domains = array();
|
|
|
|
foreach ( $raw_citations as $item ) {
|
|
$url = '';
|
|
if ( is_string( $item ) ) {
|
|
$url = $item;
|
|
} elseif ( is_array( $item ) && isset( $item['url'] ) && is_string( $item['url'] ) ) {
|
|
$url = $item['url'];
|
|
}
|
|
|
|
if ( '' === $url ) {
|
|
continue;
|
|
}
|
|
|
|
$host = (string) wp_parse_url( $url, PHP_URL_HOST );
|
|
if ( '' !== $host ) {
|
|
$domains[] = strtolower( ltrim( $host, 'www.' ) );
|
|
}
|
|
}
|
|
|
|
return array_values( array_unique( $domains ) );
|
|
}
|
|
|
|
/**
|
|
* Detect whether PGM_TARGET_DOMAIN appears in a domain list.
|
|
*
|
|
* @param string[] $domains Parsed domain list from parse_domains().
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function is_cited( array $domains ): bool {
|
|
return in_array( PGM_TARGET_DOMAIN, $domains, true );
|
|
}
|
|
|
|
/**
|
|
* Find the 1-based position of PGM_TARGET_DOMAIN in a domain list.
|
|
*
|
|
* Returns null when the target domain is not found.
|
|
*
|
|
* @param string[] $domains Parsed domain list.
|
|
*
|
|
* @return int|null
|
|
*/
|
|
public function get_our_position( array $domains ): ?int {
|
|
$pos = array_search( PGM_TARGET_DOMAIN, $domains, true );
|
|
if ( false === $pos ) {
|
|
return null;
|
|
}
|
|
return (int) $pos + 1; // Convert 0-based index to 1-based position.
|
|
}
|
|
}
|