rawurlencode( $key ), 'query' => rawurlencode( $query ), 'pageSize' => 5, 'languageCode' => 'en', ), self::FACTCHECK_ENDPOINT ); $response = AISC_HTTP::request( $url, array( 'method' => 'GET' ), AISC_HTTP::cache_key( 'gfc', $query ) ); if ( is_wp_error( $response ) ) { return $response; } $out = array(); foreach ( ( $response['claims'] ?? array() ) as $claim_row ) { foreach ( ( $claim_row['claimReview'] ?? array() ) as $review ) { $out[] = array( 'publisher' => sanitize_text_field( $review['publisher']['name'] ?? '' ), 'rating' => sanitize_text_field( $review['textualRating'] ?? '' ), 'title' => sanitize_text_field( $review['title'] ?? '' ), 'url' => esc_url_raw( $review['url'] ?? '' ), 'date' => sanitize_text_field( $review['reviewDate'] ?? '' ), ); } } return array_slice( $out, 0, 5 ); } /** * Search the web for corroboration and freshness signals. * * @param string $claim Claim text. * @return array|WP_Error {results:array, newest_year:int|null} */ public static function corroborate( $claim ) { $key = AISC_Settings::get( 'google_key' ); $cx = AISC_Settings::get( 'google_cx' ); if ( empty( $key ) || empty( $cx ) ) { return new WP_Error( 'aisc_no_cx', __( 'Google Custom Search needs both an API key and a Search Engine ID.', 'ai-slop-checker' ) ); } $query = self::to_query( $claim ); if ( '' === $query ) { return array( 'results' => array(), 'newest_year' => null, ); } $url = add_query_arg( array( 'key' => rawurlencode( $key ), 'cx' => rawurlencode( $cx ), 'q' => rawurlencode( $query ), 'num' => 5, 'safe' => 'active', ), self::SEARCH_ENDPOINT ); $response = AISC_HTTP::request( $url, array( 'method' => 'GET' ), AISC_HTTP::cache_key( 'gcs', $query ) ); if ( is_wp_error( $response ) ) { return $response; } $results = array(); $newest_year = null; foreach ( ( $response['items'] ?? array() ) as $item ) { $snippet = sanitize_text_field( $item['snippet'] ?? '' ); $year = self::guess_year( $item ); if ( $year && ( null === $newest_year || $year > $newest_year ) ) { $newest_year = $year; } $results[] = array( 'title' => sanitize_text_field( $item['title'] ?? '' ), 'url' => esc_url_raw( $item['link'] ?? '' ), 'snippet' => $snippet, 'year' => $year, ); } return array( 'results' => $results, 'newest_year' => $newest_year, ); } /** * Reduce a claim sentence to a usable search query. * * Long verbatim sentences return nothing; the meaningful terms need lifting out. * * @param string $claim Claim sentence. * @return string */ private static function to_query( $claim ) { $claim = wp_strip_all_tags( (string) $claim ); $claim = preg_replace( '/[^\p{L}\p{N}\s\.\-%$£€]/u', ' ', $claim ); $claim = preg_replace( '/\s+/u', ' ', trim( $claim ) ); if ( '' === $claim ) { return ''; } $stop = array( 'the', 'a', 'an', 'and', 'or', 'but', 'of', 'to', 'in', 'on', 'at', 'for', 'with', 'is', 'was', 'are', 'were', 'be', 'been', 'being', 'that', 'this', 'these', 'those', 'it', 'its', 'as', 'by', 'from', 'has', 'have', 'had', 'which', 'while', 'also', 'their', 'there', 'they', 'them', 'can', 'will', 'would', 'about', 'into', 'than', ); $words = explode( ' ', $claim ); $kept = array(); foreach ( $words as $word ) { if ( in_array( strtolower( $word ), $stop, true ) ) { continue; } $kept[] = $word; if ( count( $kept ) >= 18 ) { break; } } return implode( ' ', $kept ); } /** * Best-effort publication year for a search result. * * @param array $item Custom Search item. * @return int|null */ private static function guess_year( $item ) { $haystacks = array(); $meta = $item['pagemap']['metatags'][0] ?? array(); foreach ( array( 'article:published_time', 'article:modified_time', 'og:updated_time', 'date' ) as $field ) { if ( ! empty( $meta[ $field ] ) ) { $haystacks[] = $meta[ $field ]; } } $haystacks[] = $item['snippet'] ?? ''; foreach ( $haystacks as $haystack ) { if ( preg_match( '/\b(20[0-3]\d)\b/', (string) $haystack, $m ) ) { return (int) $m[1]; } } return null; } }