function qtranxf_slug_replace_rewrite_structs($slugs_org, $slugs_new)
{
    global $wp_rewrite;
    if (empty($wp_rewrite->extra_permastructs)) {
        return;
    }
    if (!is_array($slugs_org)) {
        $info_org = qtranxf_slug_split_permastruct($slugs_org);
        $info_new = qtranxf_slug_split_permastruct($slugs_new);
        qtranxf_slug_replace_rewrite_structs($info_org['slugs'], $info_new['slugs']);
        //recursive call
        return;
    }
    if (empty($slugs_org)) {
        return;
    }
    if (count($slugs_org) != count($slugs_new)) {
        return;
    }
    foreach ($wp_rewrite->extra_permastructs as $k => $v) {
        if (empty($v['struct'])) {
            continue;
        }
        reset($slugs_new);
        foreach ($slugs_org as $value_org) {
            $value_new = next($slugs_new);
            $wp_rewrite->extra_permastructs[$k]['struct'] = preg_replace('#(?:[^a-z0-9_\\-%]|^)' . $value_org . '(?:[^a-z0-9_\\-%]|$)#', $value_new, $v['struct']);
        }
    }
}
/**
 * @param (string) $slug - urldecoded post name or other slug.
*/
function qtranxf_slug_multilingual_base($slug)
{
    global $q_config;
    $is_permastruct = qtranxf_slug_is_permastruct($slug);
    if ($is_permastruct) {
        $info = qtranxf_slug_split_permastruct($slug);
        $val = '';
        foreach ($info['blocks'] as $b) {
            if (qtranxf_slug_is_substitution($b)) {
                $val .= $b;
            } else {
                $val .= qtranxf_slug_multilingual($b);
            }
        }
        return $val;
    } else {
        return qtranxf_slug_multilingual($slug);
    }
}
function qtranxf_slug_update_translations_of($group, $group_name, $default_lang, &$wp_group)
{
    global $q_config;
    foreach ($_POST['qtranslate-slugs'][$group] as $key => &$qfields) {
        if (!isset($wp_group[$key])) {
            qtranxf_add_error(sprintf(__('Translations of "%s" cannot be updated for unknown term "%s".', 'qtranslate'), $group_name, $key));
            continue;
        }
        $is_permastruct;
        $value_new = qtranxf_slug_sanitize($qfields[$default_lang], $is_permastruct);
        //qtranxf_dbg_log('qtranxf_slug_update_translations_of: $value_new: ', $value_new);
        if (isset($qfields['qtranslate-original-value'])) {
            $value_org = $qfields['qtranslate-original-value'];
            if ($value_org != $value_new) {
                //qtranxf_dbg_log('qtranxf_slug_update_translations_of: $value_org: ', $value_org);
                if (!isset($q_config['slugs_opt']['mv'])) {
                    $q_config['slugs_opt']['mv'] = array();
                }
                if (!isset($q_config['slugs_opt']['mv']['terms'])) {
                    $q_config['slugs_opt']['mv']['terms'] = array();
                }
                if (!isset($q_config['slugs_opt']['mv']['terms'][$group])) {
                    $q_config['slugs_opt']['mv']['terms'][$group] = array('group_name' => $group_name, 'values' => array());
                }
                if (!isset($q_config['slugs_opt']['mv']['terms'][$group]['values'][$key])) {
                    $q_config['slugs_opt']['mv']['terms'][$group]['values'][$key] = array();
                    $q_config['slugs_opt']['mv']['terms'][$group]['values'][$key]['value_org'] = $value_org;
                }
                $q_config['slugs_opt']['mv']['terms'][$group]['values'][$key]['value_new'] = $value_new;
            }
            qtranxf_slug_del_translations_permastruct($value_org);
            $qfields = qtranxf_slug_replace($value_org, $value_new, $qfields);
        }
        $info_def = qtranxf_slug_split_permastruct($value_new);
        //qtranxf_dbg_log('qtranxf_slug_update_translations_of: $info_def: ', $info_def);
        $cnt = count($info_def['slugs']);
        //qtranxf_dbg_log('qtranxf_slug_update_translations_of: $cnt: ', $cnt);
        if ($is_permastruct) {
            $names = array();
            foreach ($info_def['slugs'] as $name) {
                $names[$name] = array();
            }
            foreach ($qfields as $lng => $val) {
                if (empty($val)) {
                    continue;
                }
                //default will be used
                $info_lng = qtranxf_slug_split_permastruct($val);
                if (count($info_lng['slugs']) != $cnt) {
                    qtranxf_add_error(sprintf(__('Translation of term "%s" for language code "%s", "%s" -> "%s", is inconsistent. Translation has to have matching structire. Please, correct it and try saving the configuration again.', 'qtranslate'), $group_name, $lng, $value_new, $val));
                    continue;
                }
                foreach ($info_lng['slugs'] as $k => $s) {
                    $name = $info_def['slugs'][$k];
                    $names[$name][$lng] = $s;
                }
            }
            //qtranxf_dbg_log('qtranxf_slug_update_translations_of: $names: ', $names);
            foreach ($names as $name => $slugs) {
                qtranxf_slug_update_translations($name, $slugs, $default_lang);
            }
        } else {
            //qtranxf_dbg_log('qtranxf_slug_update_translations_of: ok $value_new: ', $value_new);
            qtranxf_slug_update_translations($value_new, $qfields, $default_lang);
        }
    }
    /*
    	if(!empty($q_config['slugs_opt']['mv']['terms'][$group]['values'])){
    		$msg = '<br/>'.PHP_EOL;
    		foreach($q_config['slugs_opt']['mv']['terms'][$group]['values'] as $key => $info){
    			$name_org = $info['value_org'];
    			$name_new = $info['value_new'];
    			$msg .= '"'.$name_org.'" -> "'.$name_new.'"<br/>'.PHP_EOL;
    		}
    		qtranxf_add_warning(sprintf(__('The following default language slugs for custom %s have been renamed: %s Those custom types are created by the theme or by some 3rd-party plugins, which have not yet been fully %sintegrated%s with %s. Thefore you have to change the default language slug of those types using their custom tools. Make sure to change them to the same value as shown above, otherwise you will need to re-enter the translations of those strings again on this configuration page.', 'qtranslate'), $group_name, $msg, '<a href="https://qtranslatexteam.wordpress.com/integration/" target="_blank">', '</a>', 'qTranslate&#8209;X'));
    	}
    	//qtranxf_add_warning(sprintf(__('The slug for %s "%s" for default language cannot be changed to "%s" on this page, because it is not known here which tool created it and for what purpose. Please, update this slug on the page where it is originated from. It may be required then to come back here to update the translations, unless the other plugin or theme is %sintegrated%s with %s.', 'qtranslate'), $group_name, $name_old, $name, '<a href="https://qtranslatexteam.wordpress.com/integration/" target="_blank">', '</a>', 'qTranslate&#8209;X'));
    */
    qtranxf_slug_clean_request($group);
}