the whole name, for option names the plugin builds at run time. $option_patterns = [ // Rest\StreamBudget: _, the person a user id or RateLimit::subject()'s ip_. 'alpaca_bot_stream_slot_' => '/^alpaca_bot_stream_slot_(?:\d+|ip_[A-Za-z0-9]+)_\d+\z/', // 0.4.17's shortcode cache kept as an option. 'alpaca_bot_cache_' => '/^alpaca_bot_cache_[0-9a-f]{32}\z/', ]; $transient_patterns = [ // RateLimit: __. 'alpaca_bot_rl_' => '/^alpaca_bot_rl_[a-z]+_(?:\d+|ip_[A-Za-z0-9]+)_\d{12}\z/', // Chat\UsageMeter: _. 'alpaca_bot_usage_' => '/^alpaca_bot_usage_(?:\d+|site)_\d{4}-\d{2}\z/', // Shortcodes\Chat: an md5. 'alpaca_bot_shortcode_' => '/^alpaca_bot_shortcode_[0-9a-f]{32}\z/', // Rest\ChatController's stream ticket: wp_generate_password(32, false). 'alpaca_bot_stream_' => '/^alpaca_bot_stream_[A-Za-z0-9]{32}\z/', // Mcp\Drift: an MCP server id, as Settings\Schema's MCP_ID allows it. 'alpaca_bot_mcp_drift_' => '/^alpaca_bot_mcp_drift_[a-z][a-z0-9_]{0,23}\z/', // Mcp\TransientSessions: an md5 of php-agents' session key. 'alpaca_bot_mcp_session_' => '/^alpaca_bot_mcp_session_[0-9a-f]{32}\z/', // 0.4.17's shortcode cache kept as a transient. 'alpaca_bot_cache_' => '/^alpaca_bot_cache_[0-9a-f]{32}\z/', ]; // 0.4.17's shortcode cache kept as post meta on the page that showed it. $post_meta_patterns = ['alpaca_bot_cache_' => '/^alpaca_bot_cache_[0-9a-f]{32}\z/']; $post_types = ['chat_history', 'chat_log']; $cron_hooks = ['alpaca_bot/usage/cleanup']; $user_meta = ['alpaca_bot_default_model', 'alpaca_bot_drawer_open', 'alpaca_bot_drawer_conversation', 'alpaca_bot_user_settings']; /** * The distinct values of `$column` that start with `$prefix`: the LIKE is on the escaped * prefix and anchored at the start, and the caller then checks each value whole. * * @return list */ $starting = static function (string $table, string $column, string $prefix) use ($wpdb): array { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- No API lists options or meta keys by prefix. The table and column are $wpdb's table name and this file's own literal; the prefix is escaped with esc_like() and prepared. $found = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT `{$column}` FROM `{$table}` WHERE `{$column}` LIKE %s", $wpdb->esc_like($prefix) . '%')); return array_values(array_filter($found, 'is_string')); }; $site = static function () use ($wpdb, $starting, $options, $transients, $option_patterns, $transient_patterns, $post_meta_patterns, $post_types, $cron_hooks): void { foreach ($options as $name) { delete_option($name); } foreach ($option_patterns as $prefix => $pattern) { foreach ($starting($wpdb->options, 'option_name', $prefix) as $name) { if (preg_match($pattern, $name) === 1) { delete_option($name); } } } foreach ($transients as $name) { delete_transient($name); } // Value and timeout rows are matched on their own, so a timeout whose value is already // gone goes too: delete_transient() removes the timeout only when removing the value // succeeded. foreach ($transient_patterns as $prefix => $pattern) { foreach (['_transient_', '_transient_timeout_'] as $kind) { foreach ($starting($wpdb->options, 'option_name', $kind . $prefix) as $name) { if (preg_match($pattern, substr($name, strlen($kind))) === 1) { delete_option($name); } } } } foreach ($post_meta_patterns as $prefix => $pattern) { foreach ($starting($wpdb->postmeta, 'meta_key', $prefix) as $key) { if (preg_match($pattern, $key) === 1) { delete_metadata('post', 0, $key, '', true); } } } // Taxonomy => term_taxonomy_id => true, for the terms that lost a post, recounted once below. $recount = []; foreach ($post_types as $type) { // Registered now, by anything but this plugin (whose registrations carry the mark), // means another loaded plugin uses the name and the posts may be its own. $object = get_post_type_object($type); if ($object !== null && !(property_exists($object, 'alpaca_bot_owned') && $object->alpaca_bot_owned === true)) { continue; } do { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- The file docblock says why posts are deleted in SQL. The type is prepared; the interpolation is $wpdb's table name. $ids = array_map('intval', $wpdb->get_col($wpdb->prepare("SELECT ID FROM `{$wpdb->posts}` WHERE post_type = %s ORDER BY ID LIMIT 500", $type))); if ($ids === []) { break; } $in = implode(',', $ids); // Term relationships. object_id is only a number, and a taxonomy decides what kind // of object it names, so a row goes only when its taxonomy is registered for post // types alone: `link_category` names links, a user taxonomy names users, and a // taxonomy nothing loaded registers cannot say. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $in is a list of integers cast above; the rest is $wpdb's table names. $rows = $wpdb->get_results("SELECT tr.object_id, tr.term_taxonomy_id, tt.taxonomy FROM `{$wpdb->term_relationships}` tr INNER JOIN `{$wpdb->term_taxonomy}` tt ON tt.term_taxonomy_id = tr.term_taxonomy_id WHERE tr.object_id IN ({$in})"); foreach (is_array($rows) ? $rows : [] as $row) { $taxonomy = get_taxonomy((string) $row->taxonomy); $types = $taxonomy === false ? [] : (array) $taxonomy->object_type; $forPosts = $types !== [] && array_filter($types, static fn(string $t): bool => !in_array($t, $post_types, true) && !post_type_exists($t)) === []; if (!$forPosts) { continue; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- As above; the term count and cache are put right below. $wpdb->delete($wpdb->term_relationships, ['object_id' => (int) $row->object_id, 'term_taxonomy_id' => (int) $row->term_taxonomy_id], ['%d', '%d']); wp_cache_delete((int) $row->object_id, $taxonomy->name . '_relationships'); $recount[$taxonomy->name][(int) $row->term_taxonomy_id] = true; } // Comments on them, and those comments' meta. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- As above. $comments = array_map('intval', $wpdb->get_col("SELECT comment_ID FROM `{$wpdb->comments}` WHERE comment_post_ID IN ({$in})")); if ($comments !== []) { $cin = implode(',', $comments); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $cin is a list of integers cast above; the rest is $wpdb's table name. $wpdb->query("DELETE FROM `{$wpdb->commentmeta}` WHERE comment_id IN ({$cin})"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- As above. $wpdb->query("DELETE FROM `{$wpdb->comments}` WHERE comment_ID IN ({$cin})"); clean_comment_cache($comments); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $in is a list of integers cast above; the rest is $wpdb's table name. The caches are dropped below. $wpdb->query("DELETE FROM `{$wpdb->postmeta}` WHERE post_id IN ({$in})"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- As above. $deleted = $wpdb->query("DELETE FROM `{$wpdb->posts}` WHERE ID IN ({$in})"); foreach ($ids as $id) { wp_cache_delete($id, 'posts'); wp_cache_delete($id, 'post_meta'); } // A batch that deleted nothing would come back as the same batch: stop instead. $more = is_int($deleted) && $deleted > 0 && count($ids) === 500; } while ($more); } foreach ($recount as $taxonomy => $tt) { wp_update_term_count_now(array_keys($tt), $taxonomy); } wp_cache_set_posts_last_changed(); foreach ($cron_hooks as $hook) { wp_unschedule_hook($hook); } }; if (is_multisite()) { foreach (get_sites(['fields' => 'ids', 'number' => 0]) as $id) { switch_to_blog((int) $id); $site(); restore_current_blog(); } } else { $site(); } foreach ($user_meta as $key) { delete_metadata('user', 0, $key, '', true); } })();