array( 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'content' => array( 'required' => false, 'type' => 'string', ), ); register_rest_route( self::NS, '/scan', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'scan' ), 'permission_callback' => array( __CLASS__, 'can_edit' ), 'args' => $post_args, ) ); register_rest_route( self::NS, '/verify', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'verify' ), 'permission_callback' => array( __CLASS__, 'can_edit' ), 'args' => $post_args, ) ); register_rest_route( self::NS, '/recheck', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'recheck' ), 'permission_callback' => array( __CLASS__, 'can_edit' ), 'args' => array( 'post_id' => array( 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', ), ), ) ); register_rest_route( self::NS, '/dismiss', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'dismiss' ), 'permission_callback' => array( __CLASS__, 'can_edit' ), 'args' => array( 'post_id' => array( 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', ), 'dismiss' => array( 'required' => false, 'type' => 'boolean', 'default' => true, ), ), ) ); register_rest_route( self::NS, '/site-scan', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'scan_state' ), 'permission_callback' => array( __CLASS__, 'can_manage' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( __CLASS__, 'scan_control' ), 'permission_callback' => array( __CLASS__, 'can_manage' ), 'args' => array( 'action' => array( 'required' => true, 'type' => 'string', 'enum' => array( 'start', 'resume', 'stop', 'reset', 'tick' ), ), 'with_ai' => array( 'required' => false, 'type' => 'boolean', 'default' => false, ), ), ), ) ); } /** * Permission check: the caller must be able to edit this post. * * @param WP_REST_Request $request Request. * @return bool|WP_Error */ public static function can_edit( $request ) { $post_id = absint( $request->get_param( 'post_id' ) ); if ( ! $post_id || ! get_post( $post_id ) ) { return new WP_Error( 'aisc_bad_post', __( 'That post does not exist.', 'ai-slop-checker' ), array( 'status' => 404 ) ); } if ( ! current_user_can( 'edit_post', $post_id ) ) { return new WP_Error( 'aisc_forbidden', __( 'You are not allowed to edit this post.', 'ai-slop-checker' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Permission check for site-wide operations. * * @return bool|WP_Error */ public static function can_manage() { if ( ! current_user_can( 'manage_options' ) ) { return new WP_Error( 'aisc_forbidden', __( 'You are not allowed to manage site scans.', 'ai-slop-checker' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Local pattern scan. * * Scans unsaved editor content when supplied, so the panel reflects what is * on screen rather than what was last saved. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public static function scan( $request ) { $post_id = absint( $request->get_param( 'post_id' ) ); $content = $request->get_param( 'content' ); $override = ( null !== $content && '' !== trim( (string) $content ) ) ? $content : null; $report = AISC_Post_Scan::run( $post_id, false, $override ); delete_transient( 'aisc_flagged_count' ); if ( is_wp_error( $report ) ) { return $report; } return rest_ensure_response( array( 'scan' => $report['scan'], 'flagged' => (bool) get_post_meta( $post_id, AISC_META_FLAGGED, true ), 'reason' => AISC_Post_Scan::reason( $report ), 'categories' => AISC_Patterns::categories(), ) ); } /** * Fact-check the post's strongest claims. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function verify( $request ) { if ( ! AISC_Settings::can_verify() ) { return new WP_Error( 'aisc_not_configured', __( 'Add a Perplexity or Gemini API key before running a fact check.', 'ai-slop-checker' ), array( 'status' => 400 ) ); } $post_id = absint( $request->get_param( 'post_id' ) ); $content = $request->get_param( 'content' ); // Check what is on screen, but never write it back — that would push // unsaved edits live on an already-published post. $override = ( null !== $content && '' !== trim( (string) $content ) ) ? $content : null; $report = AISC_Post_Scan::run( $post_id, true, $override ); delete_transient( 'aisc_flagged_count' ); if ( is_wp_error( $report ) ) { return $report; } return rest_ensure_response( $report['verify'] ?? array() ); } /** * Re-run the check on one post from the dashboard. * * Always reads the saved post body — the point is to confirm an edit that * has already been published, not to check an editor buffer. Follows the * configured check mode so a re-check means the same thing as the original. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public static function recheck( $request ) { $post_id = absint( $request->get_param( 'post_id' ) ); $report = AISC_Post_Scan::run( $post_id, null ); if ( is_wp_error( $report ) ) { return $report; } delete_transient( 'aisc_flagged_count' ); $example = AISC_Post_Scan::example( $report ); return rest_ensure_response( array( 'post_id' => $post_id, 'flagged' => (bool) get_post_meta( $post_id, AISC_META_FLAGGED, true ), 'score' => (int) get_post_meta( $post_id, AISC_META_SCORE, true ), 'level' => (string) get_post_meta( $post_id, AISC_META_LEVEL, true ), 'reason' => AISC_Post_Scan::reason( $report ), 'example' => $example ? $example['text'] : '', 'remain' => AISC_Admin::flagged_count(), ) ); } /** * Mark a result as a false positive, or restore it. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public static function dismiss( $request ) { $post_id = absint( $request->get_param( 'post_id' ) ); $dismiss = (bool) $request->get_param( 'dismiss' ); $flagged = AISC_Post_Scan::set_dismissed( $post_id, $dismiss ); delete_transient( 'aisc_flagged_count' ); return rest_ensure_response( array( 'post_id' => $post_id, 'dismissed' => $dismiss, 'flagged' => $flagged, 'remain' => AISC_Admin::flagged_count(), ) ); } /** * Current site-scan progress. * * @return WP_REST_Response */ public static function scan_state() { $state = AISC_Scan_Queue::state(); return rest_ensure_response( array( 'state' => $state, 'percent' => AISC_Scan_Queue::percent( $state ), ) ); } /** * Start, resume, pause, or reset the site scan. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public static function scan_control( $request ) { $action = $request->get_param( 'action' ); $with_ai = (bool) $request->get_param( 'with_ai' ); switch ( $action ) { case 'start': $state = AISC_Scan_Queue::start( $with_ai, false ); break; case 'resume': $state = AISC_Scan_Queue::start( $with_ai, true ); break; case 'stop': $state = AISC_Scan_Queue::stop(); break; case 'reset': self::clear_all_results(); $state = AISC_Scan_Queue::reset(); break; case 'tick': // The dashboard driving the scan itself. Cron is unreliable — // disabled in config on plenty of hosts, and silently broken on // plenty more that block loopback requests — so a scan must be // able to finish on nothing but an open browser tab. AISC_Scan_Queue::process_batch( AISC_Scan_Queue::BROWSER_BUDGET ); $state = AISC_Scan_Queue::state(); break; default: $state = AISC_Scan_Queue::state(); } delete_transient( 'aisc_flagged_count' ); return rest_ensure_response( array( 'state' => $state, 'percent' => AISC_Scan_Queue::percent( $state ), ) ); } /** * Wipe every stored scan result. */ private static function clear_all_results() { foreach ( AISC_Post_Scan::meta_keys() as $key ) { delete_post_meta_by_key( $key ); } } }