array(), 'summary' => self::summarise( array() ), 'errors' => array( __( 'Post not found.', 'ai-slop-checker' ) ), ); } $content = ( null !== $override ) ? $override : $post->post_content; $limit = (int) AISC_Settings::get( 'max_claims' ); $candidates = AISC_Claims::extract( $content, $limit ); $errors = array(); if ( empty( $candidates ) ) { return array( 'claims' => array(), 'summary' => self::summarise( array() ), 'errors' => array( __( 'No checkable factual claims were found in this post.', 'ai-slop-checker' ) ), ); } $claim_texts = wp_list_pluck( $candidates, 'text' ); $context = array( 'title' => get_the_title( $post ), 'published' => get_the_date( 'F j, Y', $post ), ); // Collect one verdict set per configured provider. $by_provider = array(); foreach ( AISC_Settings::active_providers() as $provider ) { $result = self::call_provider( $provider, $claim_texts, $context ); if ( is_wp_error( $result ) ) { $errors[] = sprintf( /* translators: 1: provider name, 2: error message. */ __( '%1$s: %2$s', 'ai-slop-checker' ), ucfirst( $provider ), $result->get_error_message() ); continue; } $by_provider[ $provider ] = self::index_by_claim( $result ); } $use_google = (bool) AISC_Settings::get( 'use_google' ); $claims = array(); foreach ( $candidates as $candidate ) { $key = self::key( $candidate['text'] ); // Gather whatever each provider said about this specific claim. $opinions = array(); foreach ( $by_provider as $provider => $indexed ) { if ( isset( $indexed[ $key ] ) ) { $opinions[ $provider ] = $indexed[ $key ]; } } $verdict = self::merge( $candidate['text'], $opinions ); $verdict['position'] = $candidate['position']; $verdict['signals'] = $candidate['signals']; $verdict['google'] = array(); // Only spend Google quota where it changes the picture: contested, // disputed, or low-confidence verdicts. if ( $use_google && self::needs_corroboration( $verdict ) ) { $verdict['google'] = self::google_signals( $candidate['text'], $errors ); } $claims[] = $verdict; } usort( $claims, array( __CLASS__, 'compare_severity' ) ); return array( 'claims' => $claims, 'summary' => self::summarise( $claims ), 'errors' => $errors, ); } /** * Dispatch to a provider client. * * @param string $provider Provider slug. * @param array $claims Claim sentences. * @param array $context Article context. * @return array|WP_Error */ private static function call_provider( $provider, array $claims, array $context ) { if ( 'gemini' === $provider ) { return AISC_Gemini::verify( $claims, $context ); } return AISC_Perplexity::verify( $claims, $context ); } /** * Key a verdict list by normalised claim text. * * @param array $verdicts Verdict rows. * @return array */ private static function index_by_claim( array $verdicts ) { $out = array(); foreach ( $verdicts as $verdict ) { $out[ self::key( $verdict['claim'] ) ] = $verdict; } return $out; } /** * Reconcile what each provider said about one claim. * * When two grounded providers agree, that is a much stronger result than * either alone, and the confidence reflects it. When they disagree, the * more severe verdict wins and confidence is cut — a split decision is a * signal for a human to look, not a reason to pick a side confidently. * * @param string $claim Claim text. * @param array $opinions Map of provider slug to verdict row. * @return array */ private static function merge( $claim, array $opinions ) { if ( empty( $opinions ) ) { return array( 'claim' => $claim, 'verdict' => 'unchecked', 'confidence' => 0, 'correction' => '', 'reasoning' => __( 'No verdict was returned for this claim.', 'ai-slop-checker' ), 'sources' => array(), 'provider' => '', 'consensus' => '', 'opinions' => array(), ); } if ( 1 === count( $opinions ) ) { $only = reset( $opinions ); $only['consensus'] = ''; $only['opinions'] = array(); return $only; } $severity = AISC_Verdict::severity(); $verdicts = wp_list_pluck( $opinions, 'verdict' ); $agree = 1 === count( array_unique( $verdicts ) ); // Sort so the most severe opinion leads. uasort( $opinions, static function ( $a, $b ) use ( $severity ) { return ( $severity[ $a['verdict'] ] ?? 5 ) <=> ( $severity[ $b['verdict'] ] ?? 5 ); } ); $lead = reset( $opinions ); $sources = array(); $corrections = array(); $reasonings = array(); foreach ( $opinions as $provider => $opinion ) { $sources = array_merge( $sources, $opinion['sources'] ); if ( $opinion['correction'] ) { $corrections[] = $opinion['correction']; } if ( $opinion['reasoning'] ) { $reasonings[] = ucfirst( $provider ) . ': ' . $opinion['reasoning']; } } if ( $agree ) { // Both landed in the same place — lift confidence toward the higher. $confidence = (int) min( 100, round( max( wp_list_pluck( $opinions, 'confidence' ) ) + 5 ) ); } else { $confidence = (int) round( min( wp_list_pluck( $opinions, 'confidence' ) ) * 0.7 ); } return array( 'claim' => $claim, 'verdict' => $lead['verdict'], 'confidence' => $confidence, 'correction' => $corrections ? $corrections[0] : '', 'reasoning' => implode( ' · ', $reasonings ), // normalise_sources dedupes by URL, which array_unique cannot do now // that each source is a {url, label} pair. 'sources' => array_slice( AISC_Verdict::normalise_sources( $sources ), 0, 4 ), 'provider' => implode( '+', array_keys( $opinions ) ), 'consensus' => $agree ? 'agree' : 'disagree', 'opinions' => array_map( static function ( $opinion ) { return array( 'provider' => $opinion['provider'], 'verdict' => $opinion['verdict'], 'confidence' => $opinion['confidence'], ); }, array_values( $opinions ) ), ); } /** * Whether a verdict warrants spending a Google call. * * @param array $verdict Verdict row. * @return bool */ private static function needs_corroboration( $verdict ) { if ( in_array( $verdict['verdict'], array( 'outdated', 'inaccurate' ), true ) ) { return true; } if ( 'disagree' === ( $verdict['consensus'] ?? '' ) ) { return true; } return $verdict['confidence'] < 60; } /** * Gather Google signals for a claim, collecting errors non-fatally. * * @param string $claim Claim text. * @param array $errors Error accumulator, by reference. * @return array */ private static function google_signals( $claim, array &$errors ) { $out = array( 'reviews' => array(), 'results' => array(), 'newest_year' => null, ); if ( ! AISC_Settings::get( 'google_key' ) ) { return $out; } $reviews = AISC_Google::fact_check( $claim ); if ( is_wp_error( $reviews ) ) { $errors[] = sprintf( /* translators: %s: error message. */ __( 'Google Fact Check: %s', 'ai-slop-checker' ), $reviews->get_error_message() ); } else { $out['reviews'] = $reviews; } if ( AISC_Settings::get( 'google_cx' ) ) { $search = AISC_Google::corroborate( $claim ); if ( is_wp_error( $search ) ) { $errors[] = sprintf( /* translators: %s: error message. */ __( 'Google Search: %s', 'ai-slop-checker' ), $search->get_error_message() ); } else { $out['results'] = $search['results']; $out['newest_year'] = $search['newest_year']; } } return $out; } /** * Sort worst-first: inaccurate, then outdated, then everything else. * * @param array $a First row. * @param array $b Second row. * @return int */ private static function compare_severity( $a, $b ) { $rank = AISC_Verdict::severity(); $ra = $rank[ $a['verdict'] ] ?? 5; $rb = $rank[ $b['verdict'] ] ?? 5; if ( $ra !== $rb ) { return $ra <=> $rb; } return $b['confidence'] <=> $a['confidence']; } /** * Tally verdicts. * * @param array $claims Verdict rows. * @return array */ private static function summarise( $claims ) { $counts = array( 'accurate' => 0, 'outdated' => 0, 'inaccurate' => 0, 'unverifiable' => 0, 'unchecked' => 0, ); $disputed = 0; foreach ( $claims as $claim ) { $verdict = $claim['verdict']; if ( isset( $counts[ $verdict ] ) ) { $counts[ $verdict ]++; } if ( 'disagree' === ( $claim['consensus'] ?? '' ) ) { $disputed++; } } return array( 'counts' => $counts, 'total' => count( $claims ), 'problems' => $counts['outdated'] + $counts['inaccurate'], 'disputed' => $disputed, 'checked_at' => time(), ); } /** * Normalised key for pairing claims back to verdicts. * * @param string $text Claim text. * @return string */ private static function key( $text ) { return md5( preg_replace( '/\s+/u', ' ', strtolower( trim( (string) $text ) ) ) ); } }