'idle', 'total' => 0, 'processed' => 0, 'flagged' => 0, 'skipped' => 0, 'cursor' => 0, 'with_ai' => false, 'started_at' => 0, 'updated_at' => 0, 'message' => '', // The post currently being scanned, persisted before the scan starts. // If a run dies, this is still set on the next run — which is how a // post that crashes the scanner gets identified instead of retried // forever. 'in_flight' => 0, // Post IDs that killed a run and were stepped over. 'failed' => array(), ); } /** * Read current state. * * @return array */ public static function state() { $state = get_option( self::STATE_KEY, array() ); if ( ! is_array( $state ) ) { $state = array(); } $state = wp_parse_args( $state, self::default_state() ); // A running scan whose cron died should not spin forever. With cron // switched off the event stays queued but never fires, so a stale // timestamp is the only reliable evidence either way. if ( 'running' === $state['status'] && $state['updated_at'] && ( time() - $state['updated_at'] ) > self::LOCK_TTL ) { if ( self::cron_disabled() || ! wp_next_scheduled( self::CRON_HOOK ) ) { $state['status'] = 'stalled'; $state['message'] = self::cron_disabled() ? __( 'WP-Cron is turned off on this site, so the scan cannot run in the background. Resume and keep this page open — it will run from your browser instead.', 'ai-slop-checker' ) : __( 'The scan stopped unexpectedly. Resume and keep this page open — it will continue from where it left off, running from your browser if the site cannot run it in the background.', 'ai-slop-checker' ); } } return $state; } /** * Whether this site has WP-Cron switched off in config. * * Note this is not the only way cron fails — a host that blocks loopback * requests breaks it just as thoroughly while leaving the constant unset — * so this narrows the message rather than deciding whether cron works. * * @return bool */ public static function cron_disabled() { return defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON; } /** * Write state. * * @param array $state State array. */ private static function save( array $state ) { $state['updated_at'] = time(); update_option( self::STATE_KEY, $state, false ); } /** * Begin a site-wide scan. * * @param bool $with_ai Whether to include AI fact-checking. * @param bool $resuming Whether to keep the existing cursor. * @return array New state. */ public static function start( $with_ai = false, $resuming = false ) { $state = $resuming ? self::state() : self::default_state(); $state['status'] = 'running'; $state['with_ai'] = (bool) $with_ai && AISC_Settings::can_verify(); $state['message'] = ''; // A run that died still holds its lock for up to LOCK_TTL. Clearing it // here is what makes Resume take effect now rather than in five minutes. delete_transient( self::LOCK_KEY ); if ( ! $resuming ) { $state['total'] = self::count_targets(); $state['processed'] = 0; $state['flagged'] = 0; $state['skipped'] = 0; $state['cursor'] = 0; $state['started_at'] = time(); } self::save( $state ); self::schedule(); return self::state(); } /** * Stop a running scan. The cursor is kept so it can be resumed. * * @return array New state. */ public static function stop() { $state = self::state(); $state['status'] = 'paused'; $state['message'] = __( 'Scan paused. Resume to pick up from the same place.', 'ai-slop-checker' ); self::save( $state ); wp_clear_scheduled_hook( self::CRON_HOOK ); delete_transient( self::LOCK_KEY ); return self::state(); } /** * Reset state entirely. * * @return array New state. */ public static function reset() { wp_clear_scheduled_hook( self::CRON_HOOK ); delete_transient( self::LOCK_KEY ); delete_option( self::STATE_KEY ); return self::state(); } /** * Queue the next batch. */ private static function schedule() { if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { wp_schedule_single_event( time() + 5, self::CRON_HOOK ); } // WP-Cron only fires on a front-end request, which may be a long wait on // a quiet site. Nudge it so the scan starts moving immediately. spawn_cron(); } /** * Seconds of work to do per cron fire before yielding. * * Kept well under a typical max_execution_time. Each fire chews through as * many batches as fit rather than doing one and waiting for the next cron * tick, which on a low-traffic site could be a long time coming. */ const TIME_BUDGET = 20; /** * Seconds of work to do when the browser drives a batch directly. * * Shorter than the cron budget: this one is a request someone is waiting on. */ const BROWSER_BUDGET = 10; /** * Process work for one cron fire. Loops batches until the time budget runs out. * * @param int $budget Optional seconds to work for. Defaults to TIME_BUDGET. */ public static function process_batch( $budget = 0 ) { $state = self::state(); if ( 'running' !== $state['status'] && 'stalled' !== $state['status'] ) { return; } // Guard against overlapping cron runs double-processing the cursor. if ( get_transient( self::LOCK_KEY ) ) { return; } set_transient( self::LOCK_KEY, 1, self::LOCK_TTL ); $state['status'] = 'running'; // Persist "running" with a fresh timestamp before any work, so the // stall check below cannot mistake this run for a dead one. self::save( $state ); $size = $state['with_ai'] ? self::BATCH_AI : self::BATCH_REGEX; $deadline = time() + ( $budget > 0 ? (int) $budget : self::TIME_BUDGET ); $drained = false; do { $posts = self::next_posts( $state['cursor'], $size ); if ( empty( $posts ) ) { $drained = true; break; } foreach ( $posts as $post_id ) { $post_id = (int) $post_id; // This post was mid-scan when a previous run died. Trying it // again would just kill this run too, so step over it and say // so rather than looping on it forever. if ( $post_id === (int) $state['in_flight'] ) { $state['failed'][] = $post_id; $state['in_flight'] = 0; $state['cursor'] = $post_id; $state['skipped']++; $state['processed']++; self::save( $state ); continue; } // Checkpoint before the scan, not after: with AI enabled a // single post is a slow network call, and a run that dies // must not cost more than the one post it was working on. $state['cursor'] = $post_id; $state['in_flight'] = $post_id; self::save( $state ); if ( AISC_Post_Scan::needs_scan( $post_id ) ) { $report = AISC_Post_Scan::run( $post_id, $state['with_ai'] ); if ( ! is_wp_error( $report ) && get_post_meta( $post_id, AISC_META_FLAGGED, true ) ) { $state['flagged']++; } } else { $state['skipped']++; } $state['in_flight'] = 0; $state['processed']++; self::save( $state ); // With AI on, one post can take longer than the whole budget. // Checking between posts keeps a run from overrunning // max_execution_time and losing the batch. if ( time() >= $deadline ) { break 2; } } $fresh = self::state(); // Someone hit Reset while this run was working: the state is gone // and must not be recreated from this run's stale copy. if ( 'idle' === $fresh['status'] ) { delete_transient( self::LOCK_KEY ); return; } // Someone hit Stop. Keep the progress, honour the pause. if ( 'running' !== $fresh['status'] && 'stalled' !== $fresh['status'] ) { $state['status'] = $fresh['status']; $state['message'] = $fresh['message']; self::save( $state ); delete_transient( self::LOCK_KEY ); return; } } while ( time() < $deadline ); if ( $drained ) { $state['status'] = 'complete'; $state['in_flight'] = 0; $state['message'] = ''; // Posts that crashed a run were stepped over, so the totals look // clean. Say so, otherwise they vanish without a trace. $failed = array_unique( array_map( 'intval', (array) $state['failed'] ) ); if ( $failed ) { $state['message'] = sprintf( /* translators: %d: number of posts. */ _n( '%d post could not be checked and was skipped.', '%d posts could not be checked and were skipped.', count( $failed ), 'ai-slop-checker' ), count( $failed ) ); } self::save( $state ); delete_transient( self::LOCK_KEY ); return; } delete_transient( self::LOCK_KEY ); wp_schedule_single_event( time() + 5, self::CRON_HOOK ); spawn_cron(); } /** * Fetch the next batch of post IDs after a cursor. * * @param int $cursor Last processed post ID. * @param int $size Batch size. * @return array */ private static function next_posts( $cursor, $size ) { $cursor = (int) $cursor; // WP_Query has no "ID greater than" argument, so the cursor is the one // piece that has to be added by hand. Everything else — post types, // statuses, ordering, limit — is left to core to build and escape. $add_cursor = static function ( $where ) use ( $cursor ) { global $wpdb; return $where . $wpdb->prepare( ' AND ' . $wpdb->posts . '.ID > %d', $cursor ); }; add_filter( 'posts_where', $add_cursor ); $query = new WP_Query( array( 'post_type' => AISC_Admin::post_types(), 'post_status' => self::statuses(), 'posts_per_page' => (int) $size, 'orderby' => 'ID', 'order' => 'ASC', 'fields' => 'ids', 'no_found_rows' => true, 'ignore_sticky_posts' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'cache_results' => false, ) ); remove_filter( 'posts_where', $add_cursor ); return array_map( 'intval', $query->posts ); } /** * Total posts eligible for scanning. * * Uses core's per-type status tallies, which are object-cached, rather than * counting rows directly. * * @return int */ public static function count_targets() { $statuses = self::statuses(); $total = 0; foreach ( AISC_Admin::post_types() as $type ) { $counts = wp_count_posts( $type ); if ( ! $counts ) { continue; } foreach ( $statuses as $status ) { $total += isset( $counts->$status ) ? (int) $counts->$status : 0; } } return $total; } /** * Post statuses included in a site scan. * * @return array */ public static function statuses() { /** * Filter which post statuses the site scan covers. * * @param array $statuses Status slugs. */ return apply_filters( 'aisc_scan_statuses', array( 'publish', 'draft', 'pending', 'private', 'future' ) ); } /** * Human-readable progress percentage. * * @param array $state State array. * @return int */ public static function percent( array $state ) { if ( empty( $state['total'] ) ) { return 0; } return (int) min( 100, round( ( $state['processed'] / $state['total'] ) * 100 ) ); } }