Files
pr-vision/includes/providers/class-prv-citation-detector.php
peptiderepo 60ae52663c PR Vision v0.1.1: rename from peptide-geo-monitor + P2 fixes
- Exhaustive rename: PGM_ -> PRV_, pgm_ -> prv_, text domain and
  slug peptide-geo-monitor -> pr-vision, admin menu GEO Monitor ->
  PR Vision, main file peptide-geo-monitor.php -> pr-vision.php,
  DB table pgm_ai_visibility -> prv_ai_visibility.
- P2-A: replace ltrim($host,'www.') character-mask with
  str_starts_with/substr prefix strip in PRV_Citation_Detector.
  Adds test coverage for wikipedia.org, webmd.com survival.
- P2-B: drop continue-on-error: true from PHPCS CI step;
  WordPress Coding Standards now gates the build.
- P2-C: remove dead in-run cache from PRV_Probe_Runner::run();
  each (peptide, intent, model) triplet is visited once per run,
  cache hit is structurally impossible.
- Version bump 0.1.0 -> 0.1.1 + CHANGELOG [0.1.1] entry.

Agent-Session: pr-vision rename v0.1.1
2026-06-13 09:32:56 +00:00

82 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: PRV_Perplexity_Provider, PRV_OpenRouter_Provider.
* Dependencies: None (pure PHP).
*
* @see CONTEXT.md — "cited", "our_position", "source_domains".
* @package PrVision
*/
class PRV_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 ) {
$bare = str_starts_with( $host, 'www.' ) ? substr( $host, 4 ) : $host;
$domains[] = strtolower( $bare );
}
}
return array_values( array_unique( $domains ) );
}
/**
* Detect whether PRV_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( PRV_TARGET_DOMAIN, $domains, true );
}
/**
* Find the 1-based position of PRV_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( PRV_TARGET_DOMAIN, $domains, true );
if ( false === $pos ) {
return null;
}
return (int) $pos + 1; // Convert 0-based index to 1-based position.
}
}