add_action( 'abds_seo_audit_batch', $this, 'audit_batch', 10, 2 ); } /** * Reads one run's own progress/result row — the shape the R3 Audit * screen's live-progress poll (D4) reads every ~2s while a run is active. * * @param int $run_id wp_abds_seo_audit_runs row ID. * @return array{id:int, status:string, trigger_type:string, urls_total:int, urls_completed:int, urls_failed:int, issues_found:int, avg_score:?float, started_at:?string, finished_at:?string}|null Null if the run doesn't exist. */ public function get_run_status( int $run_id ): ?array { global $wpdb; $runs_table = $wpdb->prefix . 'abds_seo_audit_runs'; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $runs_table is built only from $wpdb->prefix plus a hardcoded literal, never user input; $run_id is passed through $wpdb->prepare(). Polled at a ~2s interval only while a run's own screen is open and visible (D4), not a high-traffic path. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$runs_table} WHERE id = %d", $run_id ) ); if ( ! $row ) { return null; } return array( 'id' => (int) $row->id, 'status' => (string) $row->status, 'trigger_type' => (string) $row->trigger_type, 'urls_total' => (int) $row->urls_total, 'urls_completed' => (int) $row->urls_completed, 'urls_failed' => (int) $row->urls_failed, 'issues_found' => (int) $row->issues_found, 'avg_score' => null !== $row->avg_score ? (float) $row->avg_score : null, 'started_at' => $row->started_at, 'finished_at' => $row->finished_at, ); } /** * Reads one run's issues, grouped by issue_key — the shape the Audit * screen's IssueList (mockup Screen 3) renders: one card per distinct * issue, with the number of affected URLs, not one row per URL. * * @param int $run_id wp_abds_seo_audit_runs row ID. * @return array */ public function get_run_issues( int $run_id ): array { global $wpdb; $issues_table = $wpdb->prefix . 'abds_seo_audit_issues'; $sql = 'SELECT issue_key, category, severity, MIN(message) AS message, COUNT(DISTINCT audit_url_id) AS affected_count' . " FROM {$issues_table}" . ' WHERE run_id = %d' . ' GROUP BY issue_key, category, severity' . " ORDER BY FIELD(severity, 'critical', 'high', 'medium', 'low', 'notice'), affected_count DESC"; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $issues_table is built only from $wpdb->prefix plus a hardcoded literal, never user input; $sql is a fixed literal built just above (no runtime input in it), $run_id is passed through $wpdb->prepare(). Read once when the Audit screen opens a finished run's results, not a high-traffic path. $rows = $wpdb->get_results( $wpdb->prepare( $sql, $run_id ) ); return array_map( function ( $row ): array { return array( 'issue_key' => (string) $row->issue_key, 'category' => (string) $row->category, 'severity' => (string) $row->severity, 'message' => (string) $row->message, 'affected_count' => (int) $row->affected_count, ); }, $rows ); } /** * Starts a new audit run: creates the wp_abds_seo_audit_runs row, * pre-creates one wp_abds_seo_audit_urls row per published post * (status='pending'), and schedules one throttled Action Scheduler * batch per BATCH_SIZE URLs. Public/synchronous (unlike * ABDS_SEO_Scan_Scheduler::run_site_scan(), which is itself the cron/ * action-hooked entry point) — this is called directly by the REST * controller/admin-post handler that starts a run, so its return value * (the new run_id) can be handed straight back to the caller for the * live-progress poll (D4) to start against immediately. * * @param string $trigger_type 'manual' or 'cli' — never 'recurring' in 0.12.0 (D4). * @param int $initiated_by User ID who started this run, 0 for CLI/system. * @return int The new wp_abds_seo_audit_runs row ID, or 0 if Action Scheduler is unavailable. */ public function start_audit_run( string $trigger_type = 'manual', int $initiated_by = 0 ): int { if ( ! function_exists( 'as_schedule_single_action' ) ) { return 0; // Action Scheduler not available; caller surfaces this rather than silently doing nothing. } global $wpdb; $post_ids = get_posts( array( 'post_type' => 'any', 'post_status' => 'publish', 'posts_per_page' => -1, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page -- internal background batch: collects every published post's ID once, same as ABDS_SEO_Scan_Scheduler::run_site_scan(). 'fields' => 'ids', ) ); $runs_table = $wpdb->prefix . 'abds_seo_audit_runs'; $now = current_time( 'mysql' ); $has_urls = ! empty( $post_ids ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $runs_table is built only from $wpdb->prefix plus a hardcoded literal, never user input; $wpdb->insert() safely escapes its values via the format array. Runs once per manually-triggered audit, not a cacheable read path. $wpdb->insert( $runs_table, array( 'status' => $has_urls ? 'running' : 'completed', 'trigger_type' => $trigger_type, 'started_at' => $now, 'finished_at' => $has_urls ? null : $now, 'urls_total' => count( $post_ids ), 'initiated_by' => $initiated_by > 0 ? $initiated_by : null, 'created_at' => $now, ), array( '%s', '%s', '%s', '%s', '%d', '%d', '%s' ) ); $run_id = (int) $wpdb->insert_id; if ( ! $has_urls ) { return $run_id; } $audit_url_ids = $this->create_pending_urls( $run_id, $post_ids, $now ); $batches = array_chunk( $audit_url_ids, self::BATCH_SIZE ); foreach ( $batches as $index => $batch_ids ) { as_schedule_single_action( time() + ( $index * self::BATCH_THROTTLE_SECONDS ), 'abds_seo_audit_batch', array( $batch_ids, $run_id ), self::GROUP ); } return $run_id; } /** * Pre-creates one pending wp_abds_seo_audit_urls row per post, so every * URL this run will touch has a real row (and ID) before any batch * action runs — the batches below schedule against these IDs, not raw * post IDs, so ABDS_SEO_Audit_Engine::run_check_for_url() always has a * row to read/update. * * @param int $run_id Owning audit run. * @param int[] $post_ids Published post IDs to audit. * @param string $now Shared created_at timestamp for this batch of inserts. * @return int[] The new wp_abds_seo_audit_urls row IDs, same order as $post_ids. */ protected function create_pending_urls( int $run_id, array $post_ids, string $now ): array { global $wpdb; $urls_table = $wpdb->prefix . 'abds_seo_audit_urls'; $audit_url_ids = array(); foreach ( $post_ids as $post_id ) { $post_id = (int) $post_id; $url = (string) get_permalink( $post_id ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $urls_table is built only from $wpdb->prefix plus a hardcoded literal, never user input; $wpdb->insert() safely escapes its values via the format array. Runs once per post in a manually-triggered audit, not a cacheable read path. $wpdb->insert( $urls_table, array( 'run_id' => $run_id, 'object_type' => 'post', 'object_id' => $post_id, 'url' => $url, 'url_hash' => hash( 'sha256', $url ), 'status' => 'pending', 'created_at' => $now, ), array( '%d', '%s', '%d', '%s', '%s', '%s', '%s' ) ); $audit_url_ids[] = (int) $wpdb->insert_id; } return $audit_url_ids; } /** * Runs one batch: checks every URL in it, then updates the run's own * completed/failed counters and finishes the run once every URL has * been accounted for. * * @param int[] $audit_url_ids wp_abds_seo_audit_urls row IDs in this batch. * @param int $run_id Owning wp_abds_seo_audit_runs row ID. */ public function audit_batch( array $audit_url_ids, int $run_id ): void { $engine = new ABDS_SEO_Audit_Engine(); foreach ( $audit_url_ids as $audit_url_id ) { $engine->run_check_for_url( (int) $audit_url_id ); } global $wpdb; $urls_table = $wpdb->prefix . 'abds_seo_audit_urls'; $runs_table = $wpdb->prefix . 'abds_seo_audit_runs'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $urls_table/$runs_table are built only from $wpdb->prefix plus a hardcoded literal, never user input; values are passed through $wpdb->prepare(). Runs once per background batch, not on a cacheable read path. $completed = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$urls_table} WHERE run_id = %d AND status = 'completed'", $run_id ) ); $failed = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$urls_table} WHERE run_id = %d AND status = 'failed'", $run_id ) ); $wpdb->update( $runs_table, array( 'urls_completed' => $completed, 'urls_failed' => $failed, ), array( 'id' => $run_id ), array( '%d', '%d' ), array( '%d' ) ); $urls_total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT urls_total FROM {$runs_table} WHERE id = %d", $run_id ) ); // phpcs:enable if ( $completed + $failed >= $urls_total ) { $this->finish_audit_run( $run_id ); } } /** * Marks a run completed and aggregates its own avg_score/issues_found — * scoped to THIS run_id via the run_id column on audit_urls/audit_issues, * deliberately not repeating ABDS_SEO_Scan_Scheduler::finish_scan()'s * known bug of averaging over the entire page_scores table regardless of * which scan actually ran (see this plan's own risk-file note). * * @param int $run_id wp_abds_seo_audit_runs row ID. */ protected function finish_audit_run( int $run_id ): void { global $wpdb; $runs_table = $wpdb->prefix . 'abds_seo_audit_runs'; $urls_table = $wpdb->prefix . 'abds_seo_audit_urls'; $issues_table = $wpdb->prefix . 'abds_seo_audit_issues'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $urls_table/$issues_table/$runs_table are built only from $wpdb->prefix plus a hardcoded literal, never user input; values are passed through $wpdb->prepare()/the update() format array. Runs once per completed run, not on a cacheable read path. $avg_score = $wpdb->get_var( $wpdb->prepare( "SELECT AVG(score) FROM {$urls_table} WHERE run_id = %d AND status = 'completed'", $run_id ) ); $issues_found = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$issues_table} WHERE run_id = %d", $run_id ) ); $wpdb->update( $runs_table, array( 'status' => 'completed', 'finished_at' => current_time( 'mysql' ), 'avg_score' => $avg_score, 'issues_found' => $issues_found, ), array( 'id' => $run_id ), array( '%s', '%s', '%f', '%d' ), array( '%d' ) ); // phpcs:enable } }