'id, category_id, date, title, distance, duration, calories, comment, strava_url, avg_heart_rate, max_heart_rate, avg_speed, max_speed, avg_cadence, max_cadence, total_elevation_gain, total_elevation_loss, min_altitude, max_altitude, equipment_id, gpx_url, event_type_id', 'categories' => 'id, name, icon, color', 'equipment' => 'id, name, type, purchase_date, initial_cost, status, notes', 'equipment_log' => 'id, equipment_id, log_date, log_type, description, cost, mileage', 'event_types' => 'id, name', 'goals' => 'id, name, goal_type, target_value, year, month, category_id', ); $results = array(); $all_successful = true; foreach ( $table_columns as $table_suffix => $columns ) { $old_table = $wpdb->prefix . 'mystat_' . $table_suffix; $new_table = $wpdb->prefix . 'statpress_' . $table_suffix; // Check if old table exists and new table is empty if ( $wpdb->get_var( "SHOW TABLES LIKE '{$old_table}'" ) === $old_table ) { // Check if there's anything to migrate $old_table_count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$old_table}" ); $new_table_count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$new_table}" ); // Only migrate if the new table is empty but the old one has data. if ( $old_table_count > 0 && 0 === $new_table_count ) { // Use explicit column list for a robust query $query = "INSERT INTO {$new_table} ({$columns}) SELECT {$columns} FROM {$old_table}"; $copied_rows = $wpdb->query( $query ); if ( false === $copied_rows ) { $all_successful = false; $results[ $table_suffix ] = array( 'status' => 'failure', 'error' => $wpdb->last_error, ); } else { $results[ $table_suffix ] = array( 'status' => 'success', 'count' => $copied_rows, ); } } else { // If the new table is not empty, skip it. $results[ $table_suffix ] = array( 'status' => 'skipped', 'count' => $new_table_count, ); } } } // Store the results in a transient to display a notice set_transient( 'statpress_migration_results', $results, 60 ); // Mark migration as complete only if everything was successful or skipped. if ( $all_successful ) { update_option( 'statpress_migration_complete', true ); } // Redirect to the main dashboard to show the notice and remove query args wp_safe_redirect( admin_url( 'admin.php?page=statpress-dashboard' ) ); exit; } /** * Handles various admin tool actions, like resetting the migration flag. */ function statpress_handle_admin_tools() { // Check if the reset migration action is triggered if ( isset( $_POST['statpress_action'] ) && 'reset_migration' === $_POST['statpress_action'] ) { // Verify nonce and permissions if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'statpress_reset_migration_nonce' ) ) { return; } if ( ! current_user_can( 'manage_options' ) ) { return; } // Delete the option that hides the migration button delete_option( 'statpress_migration_complete' ); // Set a transient to show a success notice on the settings page set_transient( 'statpress_migration_reset_notice', true, 60 ); wp_safe_redirect( admin_url( 'admin.php?page=statpress-settings' ) ); exit; } }