model = new PLL_Admin_Model($options); } } /* * adds the link to the languages panel in the wordpress admin menu * * @since 0.1 */ public function add_menus() { load_plugin_textdomain('gengo-to-polylang', false, basename(dirname(__FILE__)).'/languages'); // plugin i18n add_submenu_page('tools.php', $title = __('Gengo PLL importer', 'gengo-to-polylang'), $title, 'manage_options', 'gengo-importer', array(&$this, 'tools_page')); } /* * displays the import page * processes the import action * * @since 0.1 */ public function tools_page() { global $gengo, $wp_version; $min_wp_version = '3.1'; $min_pll_version = '1.2.3'; $checks[] = array( sprintf(__('You are using WordPress %s or later', 'gengo-to-polylang'), $min_wp_version), version_compare($wp_version, $min_wp_version, '>=') ? 1 : 0 ); $checks[] = array( __('Gengo is installed on this website', 'gengo-to-polylang'), false !== get_option('gengo_version') ? 1 : 0 ); $checks[] = array( __('Gengo is deactivated', 'gengo-to-polylang'), empty($gengo) ? 1 : 0 ); $checks[] = array( sprintf(__('Polylang %s or later is activated', 'gengo-to-polylang'), $min_pll_version), defined('POLYLANG_VERSION') && version_compare(POLYLANG_VERSION, $min_pll_version, '>=') ? 1 : 0 ); if ($checks[3][1]) $checks[] = array( __('No language has been created with Polylang', 'gengo-to-polylang'), $this->model->get_languages_list() ? 0 : 1 ); // html form?>

Gengo PLL Importer

', $check[0], $check[1] ? 'green' : 'red', $check[1] ? __('OK', 'gengo-to-polylang') : __('KO', 'gengo-to-polylang') ); if (!$check[1]) $deactivated = true; }?>
%s%s
'disabled'); submit_button(__('Import'), 'primary', 'submit', true, $attr); // since WP 3.1 ?>
import(); } } /* * dispatches the different import steps * * @since 0.1 */ public function import() { global $wpdb; set_time_limit(0); $this->add_languages(); if (false) { // get WPML translations $results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}icl_translations"); $this->process_post_term_languages($results); $this->process_post_term_translations($results); } $this->process_posts(); $this->process_strings_translations(); $this->process_options(); $this->process_terms(); flush_rewrite_rules(); } /* * creates the Polylang languages * * @since 0.1 */ public function add_languages() { global $wpdb; // get WPML languages $languages = $wpdb->get_results("SELECT l.code AS slug, l.locale AS locale, l.language AS name, l.rtl AS rtl FROM {$wpdb->prefix}languages AS l", ARRAY_A); foreach ($languages as $language) { $language['term_group'] = 0; $language['no_default_cat'] = 1; // prevent the creation of a new default category $this->model->add_language($language); } $this->model->clean_languages_cache(); //update the languages list } /* * process posts language and translations * * @since 0.1 */ public function process_posts() { global $wpdb; // get Gengo post2lang info // NOTE: summary_group is not used $results = $wpdb->get_results("SELECT p2l.post_id, l.code AS language_code, p2l.translation_group, p.post_type FROM {$wpdb->prefix}post2lang AS p2l INNER JOIN {$wpdb->prefix}languages AS l ON p2l.language_id = l.language_id INNER JOIN {$wpdb->prefix}posts AS p ON p.ID = p2l.post_id"); // pll foreach($this->model->get_languages_list() as $lang) $languages[$lang->slug] = $lang; // preprocess post data foreach ($results as $r) { if ( in_array($r->post_type, array('post', 'page')) ) { $post_languages[] = $wpdb->prepare('(%d, %d)', $r->post_id, $languages[$r->language_code]->term_taxonomy_id); if ($r->translation_group) $translations[$r->translation_group][$r->language_code] = $r->post_id; } } // posts languages $post_languages = array_unique($post_languages); if (!empty($post_languages)) $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES " . implode(',', $post_languages)); foreach($this->model->get_languages_list() as $lang) $lang->update_count(); // posts translations $this->process_translations('post', $translations); } /* * process terms language and translation * run after process_options so default_lang is set * @since 0.1 */ public function process_terms() { global $wpdb; // get Gengo term2syn info $results = $wpdb->get_results("SELECT t2s.term_id, l.code, t2s.synonym, t2s.sanitised, t2s.description, t.taxonomy, t.parent, t.term_taxonomy_id FROM {$wpdb->prefix}term2syn AS t2s INNER JOIN {$wpdb->prefix}languages AS l ON t2s.language_id = l.language_id INNER JOIN {$wpdb->prefix}term_taxonomy AS t ON t2s.term_id = t.term_id"); /* !! gengo does not syncronize tags but provide translations directly, * so we need to update one lang and create the others. * Then whe need to update posts in other language to point to new terms.*/ /* see SELECT * FROM `wp_terms` JOIN `wp_term2syn` ON wp_terms.term_id = wp_term2syn.`term_id` JOIN `wp_term_taxonomy` ON wp_term_taxonomy.term_id = wp_term2syn.`term_id` */ // pll foreach($this->model->get_languages_list() as $lang) $languages[$lang->slug] = $lang; $default_cat = (int) get_option('default_category'); $g_tax_trans= array(); $g_tax_ucnt= array(); foreach ($results as $r) { //print_r( $r ); //print( "
\n" ); if (in_array($r->taxonomy, array('category', 'post_tag')) ) { $g_tax_ucnt[$r->taxonomy][]=$r->term_taxonomy_id; if ( $r->code == $this->default_lang) { $tid=$r->term_id; $term_tax=wp_update_term($r->term_id, $r->taxonomy, array( 'name' => $r->synonym, 'description'=> $r->description, 'slug' => $r->sanitised, 'parent'=> $r->parent )); } else { if ( term_exists($r->synonym, $r->taxonomy) ) { // see pre_term_slug in polylang/admin/admin-filters-term.php $slug = $r->sanitised . '-' . $r->code; } else { $slug = $r->sanitised; } $term_tax=wp_insert_term($r->synonym, $r->taxonomy, array( 'description'=> $r->description, 'slug' => $slug, 'parent'=> $r->parent )); if ( is_wp_error( $term_tax ) ) { $error_string = $term_tax->get_error_message(); echo '

' . $error_string . '

'; } else { $g_tax_trans[$r->code][$r->term_taxonomy_id] = $term_tax['term_taxonomy_id']; $g_tax_ucnt[$r->taxonomy][]=$term_tax['term_taxonomy_id']; $tid=$term_tax['term_id']; } } // already set by pll if ($tid != $default_cat) $term_languages[] = $wpdb->prepare('(%d, %d)', $tid, $languages[$r->code]->tl_term_taxonomy_id); $translations[$r->term_id][$r->code] = $tid; } } $term_languages = array_unique($term_languages); if (!empty($term_languages)) $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES " . implode(',', $term_languages)); // terms translations $this->process_translations('term', $translations); // update relationship to pont to transleted terms foreach($g_tax_trans as $code=>$tax_trans) { foreach($tax_trans as $o_tax_id=>$n_tax_id) { if ($o_tax_id==$n_tax_id) continue; $lang_tax_id=$languages[$code]->term_taxonomy_id; $wpdb->query("UPDATE $wpdb->term_relationships AS tr JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = tr.object_id AND pll_tr.term_taxonomy_id = $lang_tax_id SET tr.term_taxonomy_id = $n_tax_id WHERE tr.term_taxonomy_id = $o_tax_id"); } } foreach($this->model->get_languages_list() as $lang) $lang->update_count(); foreach($g_tax_ucnt as $tax=>$ids) { $ids=array_unique($ids); wp_update_term_count($ids, $tax); } } /* * adds strings translations * * @since 0.1 */ public function process_strings_translations() { global $wpdb; // get Gengo synbloks as string translations $results = $wpdb->get_results("SELECT s.block_name AS string, l.code AS language, s.text AS translation FROM {$wpdb->prefix}synblocks AS s INNER JOIN {$wpdb->prefix}languages AS l ON s.language_id = l.language_id"); // order them in a convenient way foreach ($results as $st) { switch ($st->string) { case "blogtitle": $string=get_option('blogname'); break; case "blogtagline": $string=get_option('blogdescription'); break; default: $string=$st->string; } $string_translations[$st->language][] = array($string, $st->translation); } foreach($this->model->get_languages_list() as $lang) $languages[$lang->slug] = $lang; // save Polylang string translations if (isset($string_translations)) { foreach ($string_translations as $slug=>$strings) { $mo = new PLL_MO(); foreach ($strings as $msg) if(!empty($msg[1])) { $mo->add_entry($mo->make_entry($msg[0], $msg[1])); } $mo->export_to_db($languages[$slug]); } } /* works but used synblock should be created by theme/plugin using pll_register_string or * the translation will be lost after editing pll strings. */ } /* * defines Polylang options * * @since 0.1 */ public function process_options() { global $wpdb; $gengo_default_language_id = get_option('gengo_blog_default_language'); $options = get_option('polylang'); $options['rewrite'] = 1; // remove /language/ in permalinks (was the opposite before 0.7.2) $options['hide_default'] = 1; // remove URL language information for default language $options['redirect_lang'] = 1; // redirect the language page to the homepage // default language $options['default_lang'] = $this->default_lang = $wpdb->get_var("SELECT code FROM {$wpdb->prefix}languages AS l WHERE l.language_id = $gengo_default_language_id LIMIT 1"); // DO NOT EVEN TRY TO UPDATE SOME OPTIONS... // urls modifications // $options['force_lang'] = 0; // domains // $options['domains'] = array(); // post types // $options['post_types'] = ... // taxonomies // $options['taxonomies'] = ... // sync // $options['sync'] = array(); update_option('polylang', $options); // default categories ... NOPE } /* * add translations relationships * * @since 0.1 * * @param string $type * @param array $translations */ public function process_translations($type, $translations) { global $wpdb; if (!empty($translations)) { $terms = $slugs = $tts = $trs = array(); foreach ($translations as $t) { $term = uniqid('pll_'); // the term name $terms[] = $wpdb->prepare('("%1$s", "%1$s")', $term); $slugs[] = $wpdb->prepare('"%s"', $term); $description[$term] = serialize($t); } $terms = array_unique($terms); // insert terms if (!empty($terms)) $wpdb->query("INSERT INTO $wpdb->terms (slug, name) VALUES " . implode(',', $terms)); // get all terms with their term_id $terms = $wpdb->get_results("SELECT term_id, slug FROM $wpdb->terms WHERE slug IN (" . implode(',', $slugs) . ")"); // prepare terms taxonomy relationship foreach ($terms as $term) $tts[] = $wpdb->prepare('(%d, "%s", "%s")', $term->term_id, $type . '_translations', $description[$term->slug]); $tts = array_unique($tts); // insert term_taxonomy if (!empty($tts)) $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description) VALUES " . implode(',', $tts)); // get all terms with term_taxonomy_id $terms = get_terms($type . '_translations', array('hide_empty' => false)); // prepare objects relationships foreach ($terms as $term) { $translations = unserialize($term->description); foreach ($translations as $object_id) if (!empty($object_id)) $trs[] = $wpdb->prepare('(%d, %d)', $object_id, $term->term_taxonomy_id); } $trs = array_unique($trs); // insert term_relationships if (!empty($trs)) $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES " . implode(',', $trs)); } } } /* Make sure Polylang is loaded */ require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); if ( is_plugin_active( 'polylang/polylang.php' ) ) require_once ( WP_PLUGIN_DIR . '/polylang/polylang.php' ); new Gengo_To_Polylang();