<?php
/**
 * Shared-core uninstall coordination. Each DevDome plugin's uninstall.php calls
 * devdcorev1_uninstall_cleanup() with its OWN plugin basename. The shared-core artifacts
 * (the daily feed cron + the cached feed option) are removed only when the plugin being
 * uninstalled is the LAST DevDome plugin still installed — so removing one plugin never
 * orphans the core for the others. (The beacon flood-guard transients auto-expire on their
 * own, so there's nothing to sweep.)
 */

defined('ABSPATH') || exit;

if (!function_exists('devdcorev1_uninstall_cleanup')) {
    function devdcorev1_uninstall_cleanup($self_basename)
    {
        if (!function_exists('get_plugins')) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
        // Detect suite plugins dynamically (a hardcoded list goes stale as the suite
        // grows): any OTHER installed devdome-*/devdome-*.php plugin that vendors the
        // shared core still needs the shared artifacts.
        foreach (array_keys(get_plugins()) as $p) {
            if ($p === $self_basename) {
                continue;
            }
            if (!preg_match('#^devdome-[^/]+/devdome-[^/]+\.php$#', $p)) {
                continue;
            }
            if (is_dir(WP_PLUGIN_DIR . '/' . dirname($p) . '/lib/devdome-core')) {
                return; // another DevDome plugin still uses the shared core — keep it
            }
        }

        // Last one out — remove the shared-core artifacts.
        wp_clear_scheduled_hook('devdcorev1_refresh_feeds');
        delete_option('devdcorev1_feeds');
        delete_transient('devdcorev1_feed_init');

        // ...and the shared hub/account state written by the bundled hub.
        delete_option('devdcorev1_account_connected');
        delete_option('devdcorev1_connected_at');
        delete_option('devdcorev1_hub_connect_dismissed');
    }
}
