query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_mgoms\_orders\_%' OR option_name LIKE '\_transient\_timeout\_mgoms\_orders\_%'" ); // Always: orders in own statuses go to "Processing" when WooCommerce recorded a payment date (the order was paid at some // point), the others to "On hold" (decision of the owner 14.11.2026: an unpaid order must not look paid). WooCommerce sends no e-mail to the customer for these changes; like for // every order in these statuses, it reduces the stock if that was not done yet. // With the data kept, the old status is remembered in the order ("_mgoms_orders_status_before_delete"). $mgoms_orders_config = get_option( 'mgoms_orders_statuses', array() ); if ( ! empty( $mgoms_orders_config['statuses'] ) && is_array( $mgoms_orders_config['statuses'] ) && function_exists( 'wc_get_orders' ) ) { $mgoms_orders_custom = array(); foreach ( $mgoms_orders_config['statuses'] as $mgoms_orders_row ) { if ( ! empty( $mgoms_orders_row['custom'] ) && ! empty( $mgoms_orders_row['slug'] ) ) { $mgoms_orders_custom[ 'wc-' . $mgoms_orders_row['slug'] ] = $mgoms_orders_row; // The plugin is not loaded during uninstall, so WooCommerce must learn the own statuses here to find their orders. register_post_status( 'wc-' . $mgoms_orders_row['slug'], array( 'public' => false, 'show_in_admin_all_list' => true ) ); } } add_filter( 'wc_order_statuses', function ( $statuses ) use ( $mgoms_orders_custom ) { foreach ( $mgoms_orders_custom as $mgoms_orders_status => $mgoms_orders_row ) { $statuses[ $mgoms_orders_status ] = $mgoms_orders_row['name'] ?? $mgoms_orders_row['slug']; } return $statuses; } ); foreach ( $mgoms_orders_custom as $mgoms_orders_status => $mgoms_orders_row ) { do { $mgoms_orders_orders = wc_get_orders( array( 'status' => array( $mgoms_orders_status ), 'limit' => 100, 'type' => 'shop_order' ) ); foreach ( $mgoms_orders_orders as $mgoms_orders_order ) { if ( ! $mgoms_orders_delete_data ) { $mgoms_orders_order->update_meta_data( '_mgoms_orders_status_before_delete', $mgoms_orders_row['slug'] ); } $mgoms_orders_paid = null !== $mgoms_orders_order->get_date_paid(); $mgoms_orders_order->set_status( $mgoms_orders_paid ? 'processing' : 'on-hold' ); $mgoms_orders_order->add_order_note( sprintf( 'MG Orders deleted: status “%1$s” changed to %2$s.', $mgoms_orders_row['name'] ?? $mgoms_orders_row['slug'], $mgoms_orders_paid ? 'Processing' : 'On hold' ) ); $mgoms_orders_order->save(); } } while ( count( $mgoms_orders_orders ) === 100 ); } } if ( ! $mgoms_orders_delete_data ) { return; // Everything else stays for a new installation. } /* ------------------------------------------------------------------ the box was ticked: delete the data */ delete_option( 'mgoms_orders_statuses' ); delete_option( 'mgoms_orders_rules' ); delete_option( 'mgoms_orders_db' ); delete_option( 'mgoms_orders_shipments_since' ); delete_option( 'mgoms_orders_shipments_resumed' ); delete_option( 'mgoms_orders_pack_status' ); // Backup & transfer: settings of the last load and the automatic backups folder (uploads/mg-orders-kopie-). $mgoms_orders_key = get_option( 'mgoms_orders_backup_key' ); if ( $mgoms_orders_key && function_exists( 'wp_upload_dir' ) ) { $mgoms_orders_up = wp_upload_dir( null, false ); $mgoms_orders_dir = trailingslashit( $mgoms_orders_up['basedir'] ) . 'mg-orders-kopie-' . sanitize_key( $mgoms_orders_key ); if ( is_dir( $mgoms_orders_dir ) ) { foreach ( array_merge( (array) glob( $mgoms_orders_dir . '/*' ), array( $mgoms_orders_dir . '/.htaccess' ) ) as $mgoms_orders_file ) { if ( is_file( $mgoms_orders_file ) ) { wp_delete_file( $mgoms_orders_file ); } } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- our own, now empty folder. rmdir( $mgoms_orders_dir ); } } foreach ( array( 'mgoms_orders_backup_key', 'mgoms_orders_backup_last', 'mgoms_orders_backup_hash', 'mgoms_orders_backup_undo' ) as $mgoms_orders_option ) { delete_option( $mgoms_orders_option ); } // Saved filter views of every user (user meta). delete_metadata( 'user', 0, 'mgoms_orders_views', '', true ); // Operations log (own table) and the plugin's markers in orders (tracking numbers and notes stay). // phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.SlowDBQuery -- removing the plugin's own data, once. $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . 'mgoms_orders_log' ) ); $mgoms_orders_metas = array( '_mgoms_orders_paid_event', '_mgoms_orders_tracking_seen', '_mgoms_orders_packed', '_mgoms_orders_status_before_delete', '_mgoms_orders_delivered_seen', '_mgoms_orders_stale_done' ); $mgoms_orders_hpos = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'wc_orders_meta' ) ); foreach ( $mgoms_orders_metas as $mgoms_orders_meta ) { $wpdb->delete( $wpdb->postmeta, array( 'meta_key' => $mgoms_orders_meta ) ); if ( $mgoms_orders_hpos ) { $wpdb->delete( $wpdb->prefix . 'wc_orders_meta', array( 'meta_key' => $mgoms_orders_meta ) ); } } // phpcs:enable