'ids', 'number' => 0]) as $blog_id) { switch_to_blog((int) $blog_id); try { Migration::reset_instance(); self::cleanup_current_site(); } finally { restore_current_blog(); Migration::reset_instance(); } } } else { self::cleanup_current_site(); } try { self::cleanup_freemius(); } catch (\Exception $e) { self::log_debug('Freemius cleanup failed: ' . $e->getMessage()); } self::log_debug('Uninstall process completed'); } catch (\Exception $e) { self::log_debug('General uninstall error: ' . $e->getMessage()); } } /** * Removes the plugin data of the current site. * * @return void */ private static function cleanup_current_site() { try { $migration = Migration::get_instance(); $migration->drop_tables(); } catch (\Exception $e) { self::log_debug('Tables could not be dropped: ' . $e->getMessage()); self::drop_prompt_templates_table(); } try { self::cleanup_options(); } catch (\Exception $e) { self::log_debug('Options cleanup failed: ' . $e->getMessage()); } try { self::cleanup_cron_jobs(); } catch (\Exception $e) { self::log_debug('Cron cleanup failed: ' . $e->getMessage()); } try { self::cleanup_cache(); } catch (\Exception $e) { self::log_debug('Cache cleanup failed: ' . $e->getMessage()); } try { self::cleanup_user_meta(); } catch (\Exception $e) { self::log_debug('User meta cleanup failed: ' . $e->getMessage()); } try { self::cleanup_postmeta(); } catch (\Exception $e) { self::log_debug('Post meta cleanup failed: ' . $e->getMessage()); } try { self::cleanup_termmeta(); } catch (\Exception $e) { self::log_debug('Term meta cleanup failed: ' . $e->getMessage()); } try { self::cleanup_capabilities(); } catch (\Exception $e) { self::log_debug('Capabilities cleanup failed: ' . $e->getMessage()); } } /** * Cleans plugin data from options table. * Removes all AIPT related options. * Continues even if errors occur. * * @return void */ private static function cleanup_options() { self::log_debug('Database cleanup started'); try { $credit_options = [ 'aipt_credits_cache_' . md5(get_site_url()), 'aipt_credits_costs_cache', 'aipt_usage_summary', 'aipt_last_sync_time', 'aipt_last_heartbeat_time', 'aipt_sync_counter', 'aipt_credit_api_key', 'aipt_credit_secret_key', 'aipt_credit_registered', 'aipt_credit_plan_name', 'aipt_credit_local_usage', 'aipt_credit_terms_accepted', 'aipt_credit_show_troubleshoot', 'aipt_credit_auth_version', 'aipt_credit_system_version', 'aipt_site_registered', 'aipt_bulk_terms_accepted', 'aipt_credits_enabled', 'aipt_security_events_queue', 'aipt_ctrl_api_key', 'aipt_ctrl_secret_key', 'aipt_ctrl_registered', 'aipt_ctrl_balance_cache', 'aipt_ctrl_costs_cache', 'aipt_ctrl_local_summary', 'aipt_ctrl_balance', 'aipt_ctrl_costs', 'aipt_ctrl_system_status', 'aipt_ctrl_sync_lock', 'aipt_ctrl_system_version', 'aipt_ctrl_summary', 'aipt_ctrl_last_sync', 'aipt_ctrl_sync_error', ]; $main_options = [ 'aipt_version', 'aipt_db_version', 'aipt_cache_version', 'aipt_openai_api_key', 'aipt_gemini_api_key', 'aipt_api_provider', 'aipt_model', 'aipt_system_prompt', 'aipt_user_prompt', 'aipt_short_system_prompt', 'aipt_short_user_prompt', 'aipt_full_system_prompt', 'aipt_full_user_prompt', 'aipt_standard_short_system_prompt', 'aipt_standard_short_user_prompt', 'aipt_standard_full_system_prompt', 'aipt_standard_full_user_prompt', 'aipt_title_additional_prompt', 'aipt_faq_additional_prompt', 'aipt_seo_meta_additional_prompt', 'aipt_taxonomy_additional_prompt', 'aipt_custom_variables', 'aipt_language', 'aipt_writing_style', 'aipt_max_length', 'aipt_max_short_length', 'aipt_max_tags_count', 'aipt_needs_setup', 'aipt_setup_completed', 'aipt_first_activation', 'aipt_first_installed_version', 'aipt_new_products_baseline_id', 'aipt_redirect_to_setup', 'aipt_display_terms_notice', 'aipt_faq_display_settings', 'aipt_usage_recent', 'aipt_usage_milestones', 'aipt_usage_attribution', 'aipt_content_structure_modes', 'aipt_content_structure_migration_v1_done', 'aipt_cs_compare_notice', 'aipt_catalog_watch_settings', 'aipt_catalog_watch_state', 'aipt_content_mode', 'aipt_short_custom_prompt', 'aipt_full_custom_prompt', 'aipt_content_mode_notice', 'aipt_content_mode_migration_v1_done', 'aipt_chatbot_pre_chat_form_migration_v1_done', ]; foreach ($credit_options as $option) { try { delete_option($option); } catch (\Exception $e) { self::log_debug("Could not delete credit option ($option): " . $e->getMessage()); } } foreach ($main_options as $option) { try { delete_option($option); } catch (\Exception $e) { self::log_debug("Could not delete main option ($option): " . $e->getMessage()); } } } catch (\Exception $e) { self::log_debug('General error during option cleanup: ' . $e->getMessage()); } try { global $wpdb; $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like( '_transient_aipt_' ) . '%', $wpdb->esc_like( '_transient_timeout_aipt_' ) . '%' ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $wpdb->esc_like( 'aipt_' ) . '%' ) ); } catch (\Exception $e) { self::log_debug('Error during database queries: ' . $e->getMessage()); } self::log_debug('Database cleanup completed'); } /** * Cleans up cron jobs. * Continues even if errors occur. * * @return void */ private static function cleanup_cron_jobs() { self::log_debug('Cron jobs cleanup started'); try { wp_clear_scheduled_hook('aipt_credit_sync'); $cron_hooks = [ 'aipt_credit_sync', 'aipt_prune_usage_records', ]; foreach ($cron_hooks as $hook) { try { $timestamp = wp_next_scheduled($hook); if ($timestamp) { wp_unschedule_event($timestamp, $hook); self::log_debug("$hook cron job stopped"); } } catch (\Exception $e) { self::log_debug("Could not stop cron job ($hook): " . $e->getMessage()); } } try { $aipt_hooks = []; $cron_tasks = _get_cron_array(); if (!empty($cron_tasks)) { foreach ($cron_tasks as $cron) { if (!is_array($cron)) { continue; } foreach (array_keys($cron) as $hook) { if (strpos($hook, 'aipt_') === 0) { $aipt_hooks[$hook] = true; } } } } foreach (array_keys($aipt_hooks) as $hook) { try { wp_unschedule_hook($hook); self::log_debug("Other cron job stopped: $hook"); } catch (\Exception $e) { self::log_debug("Could not stop other cron job ($hook): " . $e->getMessage()); } } } catch (\Exception $e) { self::log_debug('Could not get cron jobs list: ' . $e->getMessage()); } if (function_exists('as_unschedule_all_actions')) { as_unschedule_all_actions('', [], 'aipt-catalog-watch'); self::log_debug('Catalog watch actions stopped'); } } catch (\Exception $e) { self::log_debug('General error during cron cleanup: ' . $e->getMessage()); } self::log_debug('Cron jobs cleanup completed'); } /** * Cleans up caches. * Continues even if errors occur. * * @return void */ private static function cleanup_cache() { self::log_debug('Cache cleanup started'); try { if (function_exists('wp_cache_flush')) { try { wp_cache_flush(); self::log_debug('WP Object Cache cleared'); } catch (\Exception $e) { self::log_debug('Could not clear WP Object Cache: ' . $e->getMessage()); } } try { delete_transient('aipt_credit_balance'); delete_transient('aipt_credit_costs'); delete_transient('aipt_credits_cache_' . md5(get_site_url())); delete_transient('aipt_credits_costs_cache'); delete_transient('aipt_credits_status'); delete_transient('aipt_api_check'); delete_transient('aipt_last_sync_time'); delete_transient('aipt_last_heartbeat_time'); delete_transient('aipt_sync_counter'); delete_site_transient('aipt_schema_migration_lock'); self::log_debug('Transients cleared'); } catch (\Exception $e) { self::log_debug('Could not clear transients: ' . $e->getMessage()); } try { $upload_dir = wp_upload_dir(); $temp_dir = $upload_dir['basedir'] . '/aipt-temp'; if (file_exists($temp_dir) && is_dir($temp_dir)) { self::delete_directory($temp_dir); self::log_debug('Temp directory cleared: ' . $temp_dir); } } catch (\Exception $e) { self::log_debug('Could not clear temp directory: ' . $e->getMessage()); } } catch (\Exception $e) { self::log_debug('General error during cache cleanup: ' . $e->getMessage()); } self::log_debug('Cache cleanup completed'); } /** * Safely deletes directory and all files within it. * * @param string $dir Directory to delete. * @return bool Whether the operation was successful. */ private static function delete_directory($dir) { try { if (!file_exists($dir)) { return true; } if (!is_dir($dir)) { return wp_delete_file($dir); } $objects = scandir($dir); if ($objects === false) { return false; } foreach ($objects as $object) { if ($object != "." && $object != "..") { $path = $dir . "/" . $object; try { if (is_dir($path)) { self::delete_directory($path); } else { wp_delete_file($path); } } catch (\Exception $e) { self::log_debug("Could not delete file/directory ($path): " . $e->getMessage()); } } } return rmdir($dir); } catch (\Exception $e) { self::log_debug("Could not delete directory ($dir): " . $e->getMessage()); return false; } } /** * FR-018: Delete all user meta with aipt_ prefix. * * @return void */ private static function cleanup_user_meta() { global $wpdb; self::log_debug('User meta cleanup started'); try { $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s", $wpdb->esc_like( 'aipt_' ) . '%' ) ); self::log_debug('User meta cleanup completed'); } catch (\Exception $e) { self::log_debug('User meta cleanup error: ' . $e->getMessage()); } } /** * Delete content-marking postmeta (_aipt_ai_processed rollup + _aipt_ai_mark_* keys). * * These keys drive the AI-status filter and automation skip logic; removing them on * uninstall prevents stale marks from resurfacing after a reinstall. * * @return void */ private static function cleanup_postmeta() { global $wpdb; self::log_debug('Post meta cleanup started'); try { $keys = \AIPT\Core\ContentMarking\MetaKeys::allKeys(); $keys[] = \AIPT\Api\ProductEditor\ProductEditorConcurrencyToken::REVISION_META_KEY; if (empty($keys)) { return; } $placeholders = implode(',', array_fill(0, count($keys), '%s')); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key IN ({$placeholders})", $keys ) ); self::log_debug('Post meta cleanup completed'); } catch (\Throwable $e) { self::log_debug('Post meta cleanup error: ' . $e->getMessage()); } } /** * Delete the term-editor optimistic-concurrency revision meta * (_aipt_term_editor_rev) from wp_termmeta. * * This is the term-domain counterpart of ProductEditorConcurrencyToken's * revision key, which cleanup_postmeta() already removes from wp_postmeta; * leaving it behind orphans a termmeta row per edited category forever. * * @return void */ private static function cleanup_termmeta() { global $wpdb; self::log_debug('Term meta cleanup started'); try { $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->termmeta} WHERE meta_key = %s", \AIPT\Core\ContentSnapshots\TermEditorConcurrencyToken::REVISION_META_KEY ) ); self::log_debug('Term meta cleanup completed'); } catch (\Throwable $e) { self::log_debug('Term meta cleanup error: ' . $e->getMessage()); } } /** * FR-018: Remove plugin capabilities from roles. * * @return void */ private static function cleanup_capabilities() { self::log_debug('Capabilities cleanup started'); try { $roles = array( 'administrator', 'shop_manager' ); $caps = array( 'manage_aipt_settings', 'use_aipt_tools' ); foreach ( $roles as $role_name ) { $role = get_role( $role_name ); if ( $role ) { foreach ( $caps as $cap ) { $role->remove_cap( $cap ); } } } self::log_debug('Capabilities cleanup completed'); } catch (\Exception $e) { self::log_debug('Capabilities cleanup error: ' . $e->getMessage()); } } /** * FR-018: Call Freemius cleanup if available. * * @return void */ private static function cleanup_freemius() { self::log_debug('Freemius cleanup started'); try { if ( function_exists( 'fs_uninstall_cleanup' ) ) { fs_uninstall_cleanup(); self::log_debug('Freemius cleanup called'); } } catch (\Exception $e) { self::log_debug('Freemius cleanup error: ' . $e->getMessage()); } } /** * Fallback cleanup for prompt templates table. * * @return void */ private static function drop_prompt_templates_table() { global $wpdb; try { $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}aipt_prompt_templates" ); } catch (\Exception $e) { self::log_debug('Prompt templates table could not be dropped: ' . $e->getMessage()); } } /** * Creates log entry for debugging purposes. * * @param string $message Log message. * @return void */ private static function log_debug($message) { if (defined('WP_DEBUG') && WP_DEBUG) { } } }