Files
pr-vision/includes/providers/class-prv-citation-detector.php
peptiderepo 401077fe45 PR Vision v0.1.1: rename from peptide-geo-monitor + QA P2 fixes (#2)
Rename to PR Vision (PRV_/pr-vision); fix www-strip char-mask in competitor display (str_starts_with); make PHPCS a CI gate; remove dead in-run cache.

QA: APPROVE — qa-reviews/pr-vision/2026-06-13-c633fdb9e1541386a713a6de12d2ee2ae4239640.md

Agent-Session: pr-vision rename v0.1.1 merge
2026-06-13 17:17:52 +07:00

88 lines
2.3 KiB
PHP

<?php
/**
* Detects whether peptiderepo.com appears in LLM source citations.
*
* @package PrVision
*/
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.
}
}