1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
<?php
/*
Plugin Name: Gengo to Polylang
Plugin URI:
Version: 0.1
Author: Julien Viard de Galbert
Description: imports Gengo data into Polylang
*/
/*
* Copyright 2014 Julien Viard de Galbert
* based on WPML to Polylang
* Copyright 2013 Frédéric Demarle
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
/*
* a class to manage migration from Gengo to Polylang
* needs Polylang 1.2.3 or later
* needs WP 3.1 or later
* tested only with my hacked Gengo version
*
* @since 0.1
*
*/
class Gengo_To_Polylang {
public $model;
/*
* constructor
*
* @since 0.1
*/
public function __construct() {
// adds the link to the languages panel in the wordpress admin menu
add_action('admin_menu', array(&$this, 'add_menus'));
if (is_admin() && isset($_GET['page']) && $_GET['page'] == 'gengo-importer' && class_exists('PLL_Admin_Model')) {
$options = get_option('polylang');
$this->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?>
<div class="wrap">
<?php screen_icon('tools'); ?>
<h2>Gengo PLL Importer</h2>
<div class="form-wrap">
<form id="import" method="post" action="admin.php?page=gengo-importer">
<input type="hidden" name="pll_action" value="import" /><?php
wp_nonce_field('gengo-importer', '_wpnonce_gengo-importer');?>
<table class="form-table"><?php
foreach ($checks as $check) {
printf('<tr><th style="width:300px">%s</th><td style="color:%s">%s</td></tr>',
$check[0],
$check[1] ? 'green' : 'red',
$check[1] ? __('OK', 'gengo-to-polylang') : __('KO', 'gengo-to-polylang')
);
if (!$check[1])
$deactivated = true;
}?>
</table><?php
$attr = empty($deactivated) ? array() : array('disabled' => 'disabled');
submit_button(__('Import'), 'primary', 'submit', true, $attr); // since WP 3.1 ?>
</form>
</div><!-- wrap --><?php
if (isset($_POST['pll_action']) && 'import' == $_POST['pll_action']) {
check_admin_referer( 'gengo-importer', '_wpnonce_gengo-importer' );
$this->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( "<br/>\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 '<div id="message" class="error"><p>' . $error_string . '</p></div>';
} 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();
|