getMessage() ); } update_option( self::DONE_OPTION, 1, false ); return $written; } /** * Seed every registered OAuth client, used or not. * * @since 0.4.8 * @return int Rows written. */ private static function seed_registered_clients(): int { global $wpdb; $table = $wpdb->prefix . 'acrossai_mcp_oauth_clients'; if ( ! self::table_exists( $table ) ) { return 0; } // Assembled before `prepare()` so the table name — built from // `$wpdb->prefix`, never from input — is interpolated outside the // placeholder machinery, which has no placeholder for an identifier. $sql = "SELECT client_id, server_id, client_name, connector_slug, created_at FROM `{$table}` ORDER BY id ASC LIMIT %d"; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching $rows = $wpdb->get_results( $wpdb->prepare( $sql, self::MAX_GROUPS ), ARRAY_A ); if ( ! is_array( $rows ) ) { return 0; } $written = 0; foreach ( $rows as $row ) { // A client row's `connector_slug` is what separates an AI connector // from a bare DCR client — the same distinction // `ConnectionContext::resolve_type()` makes on a live request, so // seeded rows and recorded rows agree on the type. $type = '' !== (string) ( $row['connector_slug'] ?? '' ) ? ConnectionContext::TYPE_AI_CONNECTOR : ConnectionContext::TYPE_OAUTH; $seeded = Recorder::record_registered( array( 'server_id' => (int) ( $row['server_id'] ?? 0 ), 'connection_type' => $type, 'client_id' => (string) ( $row['client_id'] ?? '' ), 'client_name' => (string) ( $row['client_name'] ?? '' ), 'connector_slug' => (string) ( $row['connector_slug'] ?? '' ), 'first_seen_at' => (string) ( $row['created_at'] ?? '' ), ) ); if ( $seeded ) { ++$written; } } return $written; } /** * Fold in every client the activity log has actually seen. * * Grouped by the same facts {@see Identity} keys on, so the grouping the * SQL does and the identity the recorder resolves cannot disagree. The * user agent is in the GROUP BY because it is the only thing separating the * callers whose `client_name` is empty — on this site, 94 such requests * turned out to be five distinct clients. * * @since 0.4.8 * @return int Rows written. */ private static function seed_from_activity_log(): int { global $wpdb; $table = $wpdb->prefix . ActivityLogConstants::TABLE; if ( ! self::table_exists( $table ) ) { return 0; } // Assembled before `prepare()`, for the reason given above. $sql = "SELECT server_id, connection_type, connector_slug, fleet_peer, client_name, client_version, user_id, user_agent, ip_address, COUNT(*) AS requests, MIN(created_at) AS first_seen, MAX(created_at) AS last_seen FROM `{$table}` WHERE event_type = %s AND server_id > 0 GROUP BY server_id, connection_type, connector_slug, fleet_peer, client_name, client_version, user_id, user_agent, ip_address ORDER BY last_seen ASC LIMIT %d"; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching $prepared = $wpdb->prepare( $sql, ActivityLogConstants::EVENT_REQUEST, self::MAX_GROUPS ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching $rows = $wpdb->get_results( $prepared, ARRAY_A ); if ( ! is_array( $rows ) ) { return 0; } $written = 0; foreach ( $rows as $row ) { // The log carries neither a `client_id` nor a fleet host key — it // never did, which is why the registry carries both. So a seeded // OAuth caller identifies on its name or agent, a seeded fleet // caller on its peer LABEL, and the first live request from either // supplies the real key under a row of its own. // // Those rows do not merge, and that is the honest outcome: we // cannot prove the historical traffic was the same grant, or the // same host. On this site it shows up as one peer appearing twice // — `wordpress 7.0` and `wordpress-7-0.local`, the two spellings // that motivated keying fleet on the host key in the first place. // Both are real history; neither can be promoted to the other. $seeded = Recorder::record( array( 'server_id' => (int) ( $row['server_id'] ?? 0 ), 'connection_type' => (string) ( $row['connection_type'] ?? '' ), 'connector_slug' => (string) ( $row['connector_slug'] ?? '' ), 'fleet_host_key' => (string) ( $row['fleet_peer'] ?? '' ), 'client_name' => (string) ( $row['client_name'] ?? '' ), 'client_version' => (string) ( $row['client_version'] ?? '' ), 'user_id' => (int) ( $row['user_id'] ?? 0 ), 'user_agent' => (string) ( $row['user_agent'] ?? '' ), 'ip_address' => (string) ( $row['ip_address'] ?? '' ), 'request_count' => (int) ( $row['requests'] ?? 0 ), 'first_seen_at' => (string) ( $row['first_seen'] ?? '' ), 'last_seen_at' => (string) ( $row['last_seen'] ?? '' ), ) ); if ( $seeded ) { ++$written; } } return $written; } /** * Does a source table exist? * * Both sources are optional: the activity log may never have been created * on a site that upgraded straight past it, and a fresh install has no * OAuth clients. Neither absence is an error. * * @since 0.4.8 * @param string $table Fully-prefixed table name. * @return bool */ private static function table_exists( string $table ): bool { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared $found = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ); return is_string( $found ) && '' !== $found; } }