Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import is not respecting supplied group #3831

Open
n9yty opened this issue Mar 19, 2024 · 4 comments
Open

Import is not respecting supplied group #3831

n9yty opened this issue Mar 19, 2024 · 4 comments

Comments

@n9yty
Copy link

n9yty commented Mar 19, 2024

Trying to do an import in JSON format.

Sample:

{
        "redirects": [
{
        "url": "^/sourceurl",
        "match_url": "^/sourceurl",
        "match_data": {
                "source": {
                        "flag_query": "exact",
                        "flag_case": true,
                        "flag_trailing": true,
                        "flag_regex": true
                }
        },
        "action_code": 301,
        "action_type": "url",
        "action_data": {
                "url": "/destination/"
        },
        "match_type": "url",
        "title": "Generated redirect rule note",
        "regex": true,
        "group_id": 3,
        "enabled": false
}
]
}

It is not respecting the group id, it creates a new group called “Group” with the rules in it.

@n9yty
Copy link
Author

n9yty commented Mar 22, 2024

I'll amend this to say that looking at the code it seems there is no consideration to the group_id to import to that group, unless you define the group in the import JSON file, and even then it does not attempt to correlate to any existing group it would make a new one. :(

@mailrucp
Copy link

mailrucp commented Sep 4, 2024

Tell me when this problem will be solved? I also get the constant creation of a new group, instead of adding to an existing one.

@mailrucp
Copy link

mailrucp commented Sep 4, 2024

Please add to this file https://github.com/johngodley/redirection/blob/trunk/fileio/json.php an additional check for the existence of the group id, it might look something like this

// Import redirects
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'] ] ) ) {
			// 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();
		$wpdb->num_queries = 0;
	}
}

Thank you in advance!

@hiteshtare
Copy link

@johngodley This is small BUT necessary enhancement. It would be really appreciated , if this could be closed early.

Thanks :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants