post_content; $report = get_post_meta( $post_id, AISC_META_REPORT, true ); $report = is_array( $report ) ? $report : array(); $report['scan'] = AISC_Scanner::scan( $content ); $use_ai = ( null === $with_ai ) ? AISC_Settings::ai_enabled() : ( $with_ai && AISC_Settings::can_verify() ); if ( $use_ai ) { $report['verify'] = AISC_Verifier::run( $post_id, $override ); } $report['mode'] = $use_ai ? 'regex_ai' : 'regex'; $report['scanned_at'] = time(); self::store( $post_id, $report ); return $report; } /** * Persist a report plus the flat meta the dashboard queries against. * * @param int $post_id Post ID. * @param array $report Report array. */ public static function store( $post_id, array $report ) { $score = isset( $report['scan']['score'] ) ? (int) $report['scan']['score'] : 0; $level = $report['scan']['verdict']['level'] ?? 'clean'; $problems = isset( $report['verify']['summary']['problems'] ) ? (int) $report['verify']['summary']['problems'] : 0; $flagged = self::is_flagged( $report, $post_id ); update_post_meta( $post_id, AISC_META_REPORT, $report ); update_post_meta( $post_id, AISC_META_SCORE, $score ); update_post_meta( $post_id, AISC_META_LEVEL, $level ); update_post_meta( $post_id, AISC_META_PROBLEMS, $problems ); update_post_meta( $post_id, AISC_META_SCANNED, (int) ( $report['scanned_at'] ?? time() ) ); update_post_meta( $post_id, AISC_META_FLAGGED, $flagged ? 1 : 0 ); } /** * Pattern categories that constitute proof rather than suspicion. * * Defined in AISC_Patterns alongside the rest of the category metadata; * kept here as a passthrough so callers have one obvious place to ask. * * @return array */ public static function hard_categories() { return AISC_Patterns::hard_categories(); } /** * Count findings in the proof categories. * * @param array $report Stored report. * @return int */ public static function hard_hits( array $report ) { $categories = $report['scan']['categories'] ?? array(); $hits = 0; foreach ( self::hard_categories() as $category ) { if ( ! isset( $categories[ $category ]['count'] ) ) { continue; } // Matches inside a blockquote are quoted material, not the author's // own residue, so they do not count as proof. $hits += (int) $categories[ $category ]['count'] - (int) ( $categories[ $category ]['quoted'] ?? 0 ); } return max( 0, $hits ); } /** * Whether a result should be flagged for review. * * Three independent reasons, checked in order of certainty. Note that the * score test deliberately reads the raw score rather than the derived * verdict level: the level turns "medium" at 40 on its own, so testing it * here would pin the effective threshold at 40 and make any higher setting * do nothing. * * @param array $report Stored report. * @return bool */ public static function is_flagged( array $report, $post_id = 0 ) { // An editor reviewed exactly these findings and judged them wrong. if ( $post_id && self::is_dismissed( $post_id, $report ) ) { return false; } // A fact-check found something wrong. if ( ! empty( $report['verify']['summary']['problems'] ) ) { return true; } // Unambiguous chat residue — flagged whatever the threshold is set to. if ( self::hard_hits( $report ) > 0 ) { return true; } // Otherwise it is down to the score, which the threshold governs. $score = isset( $report['scan']['score'] ) ? (int) $report['scan']['score'] : 0; return $score >= (int) AISC_Settings::get( 'flag_threshold' ); } /** * Fingerprint of what a scan found. * * Built from pattern IDs and the text they matched, so it is stable across * unrelated edits elsewhere in the post but changes the moment a new kind of * problem appears. * * @param array $report Stored report. * @return string */ public static function signature( array $report ) { $parts = array(); foreach ( ( $report['scan']['findings'] ?? array() ) as $finding ) { $parts[] = $finding['id'] . '|' . $finding['match']; } // Outdated claims are part of what was reviewed, so they belong in the // fingerprint too — otherwise dismissing pattern noise would also // silence a later fact-check failure. $problems = (int) ( $report['verify']['summary']['problems'] ?? 0 ); sort( $parts ); return md5( implode( "\n", $parts ) . '#' . $problems ); } /** * Whether this exact result was dismissed as a false positive. * * @param int $post_id Post ID. * @param array $report Current report. * @return bool */ public static function is_dismissed( $post_id, array $report ) { $stored = get_post_meta( $post_id, AISC_META_DISMISSED, true ); if ( ! $stored ) { return false; } return $stored === self::signature( $report ); } /** * Mark the current result as a false positive, or clear that mark. * * @param int $post_id Post ID. * @param bool $dismiss True to dismiss, false to restore. * @return bool Whether the post is flagged afterwards. */ public static function set_dismissed( $post_id, $dismiss ) { $report = get_post_meta( $post_id, AISC_META_REPORT, true ); $report = is_array( $report ) ? $report : array(); if ( $dismiss ) { update_post_meta( $post_id, AISC_META_DISMISSED, self::signature( $report ) ); } else { delete_post_meta( $post_id, AISC_META_DISMISSED ); } $flagged = self::is_flagged( $report, $post_id ); update_post_meta( $post_id, AISC_META_FLAGGED, $flagged ? 1 : 0 ); return $flagged; } /** * Plain-language reason a post was flagged. * * This is what shows in the dashboard next to the link, so it has to make * sense to someone who has not read the pattern catalogue. * * @param array $report Stored report. * @return string */ public static function reason( array $report ) { $bits = array(); $findings = $report['scan']['findings'] ?? array(); $cats = AISC_Patterns::categories(); // The quoted example is returned separately by example(), so the two can // be laid out independently rather than run together in one sentence. // Tally what was found. $counts = array(); foreach ( $findings as $finding ) { $label = $cats[ $finding['category'] ]['label'] ?? $finding['category']; $counts[ $label ] = ( $counts[ $label ] ?? 0 ) + 1; } if ( count( $counts ) > 0 ) { $parts = array(); foreach ( $counts as $label => $count ) { $parts[] = sprintf( '%s (%d)', $label, $count ); } $bits[] = implode( ', ', array_slice( $parts, 0, 4 ) ); } $problems = $report['verify']['summary']['problems'] ?? 0; if ( $problems > 0 ) { $bits[] = sprintf( /* translators: %d: number of claims. */ _n( '%d claim is outdated or inaccurate against current sources', '%d claims are outdated or inaccurate against current sources', $problems, 'ai-slop-checker' ), $problems ); } if ( empty( $bits ) ) { return __( 'Nothing notable found.', 'ai-slop-checker' ); } return implode( ' · ', $bits ); } /** * The single most telling match, for showing verbatim in the results list. * * Seeing the actual text is what lets someone judge a flag at a glance, * rather than opening the post to find out what "Dangling chat offer" * referred to. Hard evidence is preferred; otherwise the heaviest finding. * * @param array $report Stored report. * @return array{text:string, context:string, label:string, position:int, in_tail:bool}|null */ public static function example( array $report ) { $findings = $report['scan']['findings'] ?? array(); if ( empty( $findings ) ) { return null; } $hard = self::hard_categories(); $chosen = null; // Findings arrive sorted by weight, so the first hard-category hit is // both the most damning and the heaviest of its kind. foreach ( $findings as $finding ) { if ( in_array( $finding['category'], $hard, true ) ) { $chosen = $finding; break; } } if ( null === $chosen ) { $chosen = $findings[0]; } $categories = AISC_Patterns::categories(); return array( 'text' => $chosen['match'], 'context' => $chosen['context'] ?? '', 'label' => $categories[ $chosen['category'] ]['label'] ?? $chosen['category'], 'position' => (int) ( $chosen['position'] ?? 0 ), 'in_tail' => ! empty( $chosen['in_tail'] ), 'whole_post' => ! empty( $chosen['whole_post'] ), ); } /** * One-line fact-check tally for a post, or null if it was never checked. * * The dashboard shows the count only; the per-claim verdicts, corrections * and sources live in the editor panel, which is where someone can act on * them. Repeating all that in a table row would bury the slop findings. * * @param array $report Stored report. * @return array|null */ public static function fact_summary( array $report ) { $verify = $report['verify'] ?? null; if ( ! is_array( $verify ) ) { return null; } $summary = $verify['summary'] ?? array(); $total = (int) ( $summary['total'] ?? 0 ); // Checked, but nothing came back — a rate limit, a provider outage, or a // post with no checkable claims. Saying so beats showing "0 of 0". if ( ! $total ) { return array( 'total' => 0, 'failed' => ! empty( $verify['errors'] ), ); } $counts = isset( $summary['counts'] ) && is_array( $summary['counts'] ) ? $summary['counts'] : array(); return array( 'total' => $total, 'failed' => false, 'accurate' => (int) ( $counts['accurate'] ?? 0 ), 'inaccurate' => (int) ( $counts['inaccurate'] ?? 0 ), 'outdated' => (int) ( $counts['outdated'] ?? 0 ), 'unverifiable' => (int) ( $counts['unverifiable'] ?? 0 ), 'problems' => (int) ( $summary['problems'] ?? 0 ), 'disputed' => (int) ( $summary['disputed'] ?? 0 ), ); } /** * Whether a post needs re-scanning. * * A stored scan counts as current only if it ran after the last edit — * otherwise the verdict describes text that no longer exists. * * @param int $post_id Post ID. * @return bool */ public static function needs_scan( $post_id ) { if ( ! AISC_Settings::get( 'skip_scanned' ) ) { return true; } $scanned = (int) get_post_meta( $post_id, AISC_META_SCANNED, true ); if ( ! $scanned ) { return true; } $modified = self::modified_time( $post_id ); return ! $modified || $scanned < $modified; } /** * Last-modified time of a post as a UTC timestamp. * * WordPress leaves post_modified_gmt as 0000-00-00 on drafts, so asking only * for the GMT value returns 0 for exactly the posts most likely to be sitting * in review. Falling back to the local field fixes that — get_post_modified_time() * converts it using the site timezone, so the result is still a real UTC * timestamp comparable to time(). * * @param int $post_id Post ID. * @return int Timestamp, or 0 if it cannot be determined. */ private static function modified_time( $post_id ) { $modified = get_post_modified_time( 'U', true, $post_id ); if ( ! $modified ) { $modified = get_post_modified_time( 'U', false, $post_id ); } return (int) $modified; } /** * Clear all stored scan data for a post. * * @param int $post_id Post ID. */ public static function clear( $post_id ) { foreach ( self::meta_keys() as $key ) { delete_post_meta( $post_id, $key ); } } /** * Every post meta key the plugin writes. * * @return array */ public static function meta_keys() { return array( AISC_META_REPORT, AISC_META_SCORE, AISC_META_LEVEL, AISC_META_PROBLEMS, AISC_META_SCANNED, AISC_META_FLAGGED, AISC_META_DISMISSED, ); } }