posts; // Core table name, never user input. // The interpolations below are trusted: $posts_table is the core posts // table and $type_placeholders is a fixed run of "%s" built from the // sanitized slugs above — no user input reaches the query string. // Quickly get total post count for progress bar // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- see note above: core table + an equivalent number of %s placeholders $total_posts = (int) $wpdb->get_var($wpdb->prepare( "SELECT COUNT(ID) FROM {$posts_table} WHERE post_type IN ($type_placeholders) AND post_status = 'publish'", $allowed_types )); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber if ($total_posts === 0) { self::log_message("No posts found to scan."); update_option('ailm_scout_status_text', __('No posts found to scan.', 'ailinkmaster-internal-auto-manager')); return; } self::log_message("Found {$total_posts} posts to scan."); AILM_Scout_DB::truncate_table(); self::log_message("Truncated `ailm_link_monitor` table."); $token = wp_generate_password(24, false); update_option('ailm_scout_job_token', $token); update_option('ailm_scout_job_phase', 'extract'); // NEW LOGIC: Store only the last post ID instead of a giant array update_option('ailm_scout_last_post_id', 0); update_option('ailm_scout_total_posts', $total_posts); update_option('ailm_scout_processed_posts', 0); update_option('ailm_scout_status_text', __('Phase 1/2: Extracting links...', 'ailinkmaster-internal-auto-manager')); update_option('ailm_scout_scan_running', 'yes'); self::trigger_worker(); } public static function trigger_worker() { self::log_message("Triggering worker via wp_remote_post."); $url = admin_url('admin-ajax.php'); $token = get_option('ailm_scout_job_token'); if (!$token) return; $args = [ 'body' => ['action' => 'ailm_scout_worker', 'token' => $token], 'timeout' => 1.0, 'blocking' => false, 'sslverify' => apply_filters('ailm_scout_ssl_verify', false) ]; wp_remote_post($url, $args); } public static function run_worker() { // phpcs:ignore WordPress.Security.NonceVerification.Missing $sent_token = isset($_POST['token']) ? sanitize_text_field(wp_unslash($_POST['token'])) : ''; $stored_token = get_option('ailm_scout_job_token'); if (empty($sent_token) || !hash_equals($stored_token, $sent_token)) { wp_die(); } $phase = get_option('ailm_scout_job_phase', 'extract'); if ($phase === 'extract') { self::process_extraction_batch(); } else { self::process_ping_batch(); } wp_die(); } private static function process_extraction_batch() { global $wpdb; $last_id = (int) get_option('ailm_scout_last_post_id', 0); $batch_size = 100; // Sanitized + whitelisted exactly as in start_scan(). $allowed_types = get_option('ailm_allowed_post_types', array('post', 'page')); $allowed_types = array_values(array_unique(array_filter(array_map('sanitize_key', (array) $allowed_types)))); if (empty($allowed_types)) { $allowed_types = array('post'); } $type_placeholders = implode(',', array_fill(0, count($allowed_types), '%s')); $posts_table = $wpdb->posts; // Core table name, never user input. // Lightweight query: get the next batch of posts after $last_id. The // interpolations are trusted exactly as in start_scan(): core table + // a fixed run of "%s" placeholders. Note the disable/enable form — the // sniff reports the line of the $wpdb->prepare() call, not the query, // so a per-line ignore on the string would leave a warning behind. // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- see note above: core table + an equivalent number of placeholders $posts = $wpdb->get_results($wpdb->prepare( "SELECT ID FROM {$posts_table} WHERE post_type IN ($type_placeholders) AND post_status = 'publish' AND ID > %d ORDER BY ID ASC LIMIT %d", array_merge($allowed_types, array($last_id, $batch_size)) )); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber if (empty($posts)) { self::log_message("WORKER: Extraction complete. Switching to ping phase."); update_option('ailm_scout_job_phase', 'ping'); self::trigger_worker(); return; } foreach ($posts as $p) { AILM_Scout_Scanner::process_post($p->ID); $last_id = $p->ID; // Remember the ID } $processed_count = (int) get_option('ailm_scout_processed_posts', 0) + count($posts); $total_posts = (int) get_option('ailm_scout_total_posts', 1); $percent = $total_posts > 0 ? round(($processed_count / $total_posts) * 100) : 0; update_option('ailm_scout_last_post_id', $last_id); // Save only a single number update_option('ailm_scout_processed_posts', $processed_count); update_option('ailm_scout_status_text', sprintf( /* translators: %d: progress percentage */ __('Phase 1/2: Extracting links... (%d%%)', 'ailinkmaster-internal-auto-manager'), $percent )); self::trigger_worker(); } private static function process_ping_batch() { global $wpdb; $table_name = esc_sql(AILM_Scout_DB::get_table_name()); // phpcs:ignore $pending_count = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(DISTINCT url) FROM {$table_name} WHERE status_code = %d", 0)); if ($pending_count === 0) { self::complete_scan(); return; } update_option('ailm_scout_status_text', sprintf( /* translators: %d: number of unique URLs remaining */ __('Phase 2/2: Checking links... (~%d unique URLs remaining)', 'ailinkmaster-internal-auto-manager'), $pending_count )); // Ping a batch AILM_Scout_Checker::process_batch(50); self::trigger_worker(); } private static function complete_scan() { self::log_message("--- Scan Complete ---"); global $wpdb; $table_name = esc_sql(AILM_Scout_DB::get_table_name()); $total_posts_scanned = get_option('ailm_scout_total_posts', 0); // phpcs:ignore $posts_with_links = (int) $wpdb->get_var("SELECT COUNT(DISTINCT post_id) FROM {$table_name}"); // phpcs:ignore $total_links_found = (int) $wpdb->get_var("SELECT COUNT(id) FROM {$table_name}"); $timestamp = date_i18n('Y-m-d H:i:s', current_time('timestamp')); $status_text = sprintf( /* translators: 1: timestamp, 2: total posts scanned, 3: total links found, 4: posts with links */ __('Scan complete: %1$s. Processed %2$d posts, found %3$d links in %4$d posts.', 'ailinkmaster-internal-auto-manager'), $timestamp, $total_posts_scanned, $total_links_found, $posts_with_links ); update_option('ailm_scout_status_text', $status_text); delete_option('ailm_scout_scan_running'); self::cancel_scan(); // Clean up temporary options update_option('ailm_scout_status_text', $status_text); // Restore text after cleanup } public static function cancel_scan() { delete_option('ailm_scout_job_token'); delete_option('ailm_scout_job_phase'); delete_option('ailm_scout_last_post_id'); delete_option('ailm_scout_total_posts'); delete_option('ailm_scout_processed_posts'); delete_option('ailm_scout_scan_running'); } public static function is_scan_running() { return get_option('ailm_scout_scan_running') === 'yes'; } public static function get_status() { $total_posts = get_option('ailm_scout_total_posts', 1); $processed_posts = get_option('ailm_scout_processed_posts', 0); $phase = get_option('ailm_scout_job_phase', 'idle'); $percent = 0; if ($phase === 'extract') { $percent = $total_posts > 0 ? round(($processed_posts / $total_posts) * 50) : 0; } elseif ($phase === 'ping') { global $wpdb; $table_name = esc_sql(AILM_Scout_DB::get_table_name()); // phpcs:ignore $total_unique = (int) $wpdb->get_var("SELECT COUNT(DISTINCT url) FROM {$table_name}"); // phpcs:ignore $checked_unique = (int) $wpdb->get_var("SELECT COUNT(DISTINCT url) FROM {$table_name} WHERE status_code != 0"); $ping_percent = $total_unique > 0 ? round(($checked_unique / $total_unique) * 100) : 0; $percent = 50 + ($ping_percent / 2); } return array( 'running' => self::is_scan_running(), 'status_text' => get_option('ailm_scout_status_text', __('Ready to scan.', 'ailinkmaster-internal-auto-manager')), 'percent' => $percent, ); } }