*/ public static function get(string $id): array { $stored = get_transient(self::PREFIX . $id); return is_array($stored) ? array_values(array_filter($stored, 'is_string')) : []; } /** @param list $names the approved tools whose definition no longer matches; [] clears the marker */ public static function set(string $id, array $names): void { if ($names === []) { delete_transient(self::PREFIX . $id); return; } set_transient(self::PREFIX . $id, $names, self::TTL); } /** * `update_option_alpaca_bot_settings`: for each server `$old` lists, takes out of its marker * every name whose pin `$value` does not hold unchanged, and so clears the marker of a server * `$value` no longer lists. It reads one marker per server `$old` lists, and writes one only * when a name comes out. A value that is not the settings array lists no servers. */ public static function afterSave(mixed $old, mixed $value): void { $after = self::pins($value); foreach (self::pins($old) as $id => $before) { $names = self::get($id); $kept = array_values(array_filter( $names, static fn(string $name): bool => isset($after[$id][$name], $before[$name]) && $after[$id][$name] === $before[$name], )); if ($kept !== $names) { self::set($id, $kept); } } } /** * Server id => its `approved` map, for each row of the settings array's `toolkits.mcp_servers` * that has a string id. * * @return array> */ private static function pins(mixed $settings): array { $rows = is_array($settings) ? ($settings['toolkits.mcp_servers'] ?? null) : null; $out = []; foreach (is_array($rows) ? $rows : [] as $row) { if (is_array($row) && is_string($row['id'] ?? null)) { $out[$row['id']] = is_array($row['approved'] ?? null) ? $row['approved'] : []; } } return $out; } }