error( 'Catalog export lifecycle could not be removed cleanly.', ['reason' => $market_sync_exception->getMessage()] ); } if ( '' !== $market_sync_base_url && '' !== $market_sync_client_key && '' !== $market_sync_store_token && '' !== $market_sync_domain ) { $market_sync_response = wp_remote_request($market_sync_base_url . '/api/woocommerce/uninstall/' . rawurlencode($market_sync_domain), [ 'method' => 'DELETE', 'timeout' => 15, 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $market_sync_client_key, 'X-Woo-Store-Token' => $market_sync_store_token, 'X-ECommerce-Platform' => 'woocommerce', ], ]); if (is_wp_error($market_sync_response)) { $market_sync_logger->error( 'Could not notify Market Sync about the uninstall.', ['domain' => $market_sync_domain, 'reason' => $market_sync_response->get_error_message()] ); } else { $market_sync_status = (int)wp_remote_retrieve_response_code($market_sync_response); if ($market_sync_status >= 200 && $market_sync_status < 300) { $market_sync_logger->info( 'Notified Market Sync about the uninstall.', [ 'domain' => $market_sync_domain, 'status' => $market_sync_status, ] ); } else { $market_sync_logger->error( 'Market Sync rejected the uninstall notification.', [ 'domain' => $market_sync_domain, 'status' => $market_sync_status, ] ); } } } else { $market_sync_logger->warning( 'Skipped the uninstall notification: base URL, client key, store token or domain is missing.', ['domain' => $market_sync_domain] ); } delete_option('market_sync_store_token'); delete_option('market_sync_shop_id'); delete_option('market_sync_connected_at'); delete_option('market_sync_connection_status'); delete_option('market_sync_last_error'); delete_option('market_sync_handshake_session'); delete_option('market_sync_last_sync_at'); delete_option('market_sync_pending_install_auth'); delete_option('market_sync_pending_order_imports'); delete_option('market_sync_active_catalog_export'); delete_option('market_sync_catalog_export_lifecycle'); delete_option('market_sync_saas_base_url'); delete_option('market_sync_plugin_client_key'); delete_transient('market_sync_auth_lock'); delete_transient('market_sync_check_status_cooldown'); delete_transient('market_sync_reauthorize_cooldown'); delete_transient('market_sync_schedules_verified'); if (function_exists('as_unschedule_all_actions')) { foreach (\MarketSync\Action_Registry::HOOKS as $market_sync_hook) { as_unschedule_all_actions($market_sync_hook); } } // Action Scheduler owns shared tables, so remove only terminal actions from // this plugin's registered hooks. A worker that is already running is left // alone; the catalog lifecycle fence prevents it from publishing new work. if ( function_exists('as_get_scheduled_actions') && class_exists('\ActionScheduler') && class_exists('\ActionScheduler_Store') ) { $market_sync_action_store = \ActionScheduler::store(); $market_sync_statuses = [ \ActionScheduler_Store::STATUS_COMPLETE, \ActionScheduler_Store::STATUS_CANCELED, \ActionScheduler_Store::STATUS_FAILED, ]; foreach (\MarketSync\Action_Registry::HOOKS as $market_sync_hook) { foreach ($market_sync_statuses as $market_sync_status) { do { $market_sync_action_ids = as_get_scheduled_actions( [ 'hook' => $market_sync_hook, 'status' => $market_sync_status, 'per_page' => 1000, 'orderby' => 'none', ], 'ids' ); $market_sync_deleted_any = false; foreach ($market_sync_action_ids as $market_sync_action_id) { try { $market_sync_action_store->delete_action((int)$market_sync_action_id); $market_sync_deleted_any = true; } catch (\Throwable $market_sync_exception) { $market_sync_logger->warning( 'Could not remove an Action Scheduler history record.', [ 'action_id' => (int)$market_sync_action_id, 'hook' => $market_sync_hook, 'reason' => $market_sync_exception->getMessage(), ] ); } } } while ([] !== $market_sync_action_ids && $market_sync_deleted_any); } } } $market_sync_logger->info('Uninstall finished; plugin data removed.'); \MarketSync\Logging\Log_File::purge();