> $cards Cards to enrich. * @return array> Cards with run_stages_summary set. */ public static function enrich( array $cards ): array { // Initialise empty summary on every card. foreach ( $cards as $i => $card ) { $cards[ $i ]['run_stages_summary'] = array(); } if ( ! PRAutoBlogger_Run_Stage_State::is_available() ) { return $cards; } $run_ids = array(); foreach ( $cards as $card ) { if ( ! empty( $card['run_id'] ) ) { $run_ids[] = (string) $card['run_id']; } } if ( empty( $run_ids ) ) { return $cards; } global $wpdb; $table = PRAutoBlogger_Run_Stage_State::table_name(); $placeholders = implode( ',', array_fill( 0, count( $run_ids ), '%s' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared -- $placeholders is a fixed %s list. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT run_id, stage, status FROM {$table} WHERE run_id IN ({$placeholders}) ORDER BY id ASC", $run_ids ), ARRAY_A ); if ( ! is_array( $rows ) ) { return $cards; } // Index by run_id. $by_run = array(); foreach ( $rows as $row ) { $rid = (string) ( $row['run_id'] ?? '' ); if ( '' === $rid ) { continue; } $by_run[ $rid ][] = array( 'stage' => (string) ( $row['stage'] ?? '' ), 'status' => (string) ( $row['status'] ?? 'pending' ), ); } foreach ( $cards as $i => $card ) { $rid = (string) ( $card['run_id'] ?? '' ); if ( '' !== $rid && isset( $by_run[ $rid ] ) ) { $cards[ $i ]['run_stages_summary'] = $by_run[ $rid ]; } } return $cards; } }