From 4d2d4fe55a7fb034b9b46e0703a8971fa71fa1fd Mon Sep 17 00:00:00 2001 From: mailrucp <50190667+mailrucp@users.noreply.github.com> Date: Wed, 4 Sep 2024 15:39:59 +0700 Subject: [PATCH] Fix group duplication issue during import by checking for existing group before creating a new one "Fix group duplication issue": Clearly states what the commit addresses. "during import": Specifies the context where the issue occurs. "by checking for existing group before creating a new one": Briefly describes the solution implemented. --- fileio/json.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/fileio/json.php b/fileio/json.php index 01b785a84..c55fe1a1b 100644 --- a/fileio/json.php +++ b/fileio/json.php @@ -56,20 +56,30 @@ public function load( $group, $filename, $data ) { if ( isset( $json['redirects'] ) ) { foreach ( $json['redirects'] as $pos => $redirect ) { unset( $redirect['id'] ); - + + // Check if the group ID already exists in the group map if ( ! isset( $group_map[ $redirect['group_id'] ] ) ) { - $new_group = Red_Group::create( 'Group', 1 ); - $group_map[ $redirect['group_id'] ] = $new_group->get_id(); + // Use the get method to check if the group exists in the database by its ID + $existing_group = Red_Group::get( $redirect['group_id'] ); + + if ( $existing_group ) { + // If the group exists, store its ID in the group map for later use + $group_map[ $redirect['group_id'] ] = $existing_group->get_id(); + } else { + // If the group does not exist, create a new group + $new_group = Red_Group::create( 'Group', 1 ); + $group_map[ $redirect['group_id'] ] = $new_group->get_id(); + } } - + if ( $redirect['match_type'] === 'url' && isset( $redirect['action_data'] ) && ! is_array( $redirect['action_data'] ) ) { $redirect['action_data'] = array( 'url' => $redirect['action_data'] ); } - + $redirect['group_id'] = $group_map[ $redirect['group_id'] ]; Red_Item::create( $redirect ); $count++; - + // Helps reduce memory usage unset( $json['redirects'][ $pos ] ); $wpdb->queries = array();