function qtranxf_slug_update_translations($name, &$qfields, $default_lang) { global $wpdb; //$name = $qfields[$default_lang]; unset($qfields['qtranslate-original-value']); unset($qfields[$default_lang]); //use $post->post_name instead since it may have been adjusted by WP if (qtranxf_slug_translate_name($default_lang, $name)) { qtranxf_slug_del_translation($default_lang, $name); } foreach ($qfields as $lang => $slug) { $slug = sanitize_title($slug); //qtranxf_dbg_log('qtranxf_slug_update_translations: origin $slug: ', $slug); $slug = qtranxf_slug_unique($slug, $lang, $name); //qtranxf_dbg_log('qtranxf_slug_update_translations: unique $slug: ',$slug); $sql = null; if (qtranxf_slug_translate_name($lang, $name)) { if ($name != $slug) { $sql = 'UPDATE ' . $wpdb->prefix . 'i18n_slugs SET slug = %s WHERE lang = %s AND name = %s'; } else { $sql = 'DELETE FROM ' . $wpdb->prefix . 'i18n_slugs WHERE slug = %s AND lang = %s AND name = %s'; } } else { if ($name != $slug) { $sql = 'INSERT INTO ' . $wpdb->prefix . 'i18n_slugs (slug, lang, name) VALUES (%s, %s, %s)'; } } if ($sql) { $query = $wpdb->prepare($sql, $slug, $lang, $name); $wpdb->query($query); } } }
/** * @param (string) $name - urldecoded post name or other single slug (not a path with '/'). */ function qtranxf_slug_update_translations($name, &$qfields, $default_lang) { global $q_config, $wpdb; if (empty($name)) { return; } //qtranxf_dbg_log('qtranxf_slug_update_translations: $name="'.$name.'", $qfields: ', $qfields); if (isset($qfields['qtranslate-original-value'])) { if ($qfields['qtranslate-original-value'] != $name) { qtranxf_slug_del_translations(qtranxf_slug_encode($qfields['qtranslate-original-value'])); } unset($qfields['qtranslate-original-value']); } unset($qfields[$default_lang]); //use $post->post_name instead since it may have been adjusted by WP $name = qtranxf_slug_encode($name); //qtranxf_dbg_log('qtranxf_slug_update_translations: encoded $name: ', $name); if (qtranxf_slug_translate_name($name, $default_lang)) { qtranxf_slug_del_translation($default_lang, $name); } //$translations = qtranxf_slug_get_translations($name); foreach ($qfields as $lang => $slug) { $translation = qtranxf_slug_translate_name($name, $lang); $slug = qtranxf_slug_encode($slug); //qtranxf_dbg_log('qtranxf_slug_update_translations: origin $slug: ', $slug); if ($name == $slug) { if ($translation) { qtranxf_slug_del_translation($lang, $name); } continue; } $slug = qtranxf_slug_unique($slug, $lang, $name); //qtranxf_dbg_log('qtranxf_slug_update_translations: unique $slug: ',$slug); if ($translation) { $sql = 'UPDATE ' . $wpdb->prefix . 'i18n_slugs SET slug = %s WHERE lang = %s AND name = %s'; } else { $sql = 'INSERT INTO ' . $wpdb->prefix . 'i18n_slugs (slug, lang, name) VALUES (%s, %s, %s)'; } $query = $wpdb->prepare($sql, $slug, $lang, $name); $wpdb->query($query); if (!isset($q_config['slugs-cache']['names'][$name])) { $q_config['slugs-cache']['names'][$name] = array(); } $q_config['slugs-cache']['names'][$name][$lang] = $slug; } //cleanup in case a language was disabled $slugs = qtranxf_slug_get_translations($name); foreach ($slugs as $lang => $slug) { if (qtranxf_isEnabled($lang)) { continue; } qtranxf_slug_del_translation($lang, $name); } }
/** * @param (string) $slug - rawurlencoded slug of unknown arbitrary language. * @param (string) $lang - two-letter language code. * @return string - rawurlencoded slug for language $lang. */ function qtranxf_slug_translate($slug, $lang) { $name = qtranxf_slug_get_name($slug); if (!$name) { return $slug; } $lang_slug = qtranxf_slug_translate_name($name, $lang); if ($lang_slug) { return $lang_slug; } return $slug; }