-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage-mix.php
676 lines (589 loc) · 22.2 KB
/
language-mix.php
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
<?php
/**
* Plugin Name: Language Mix 2
* Plugin URI: http://projects.andriylesyuk.com/project/wordpress/language-mix
* Description: This plugin unhides contents which are in languages you speak.
* Version: 2.0
* Author: Andriy Lesyuk & Remi Peyronnet
* Author URI: http://www.andriylesyuk.com
* Text Domain: language-mix
* License: GPL2
*/
/**
* Copyright 2014 Andriy Lesyuk (email:[email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
define('PLLX_COOKIE', 'pllx_languages');
define('PLLX_PARAMETER', 'pllx_language');
define('PLL_PLUGIN_NAME', 'polylang/polylang.php');
require_once(ABSPATH . 'wp-admin/includes/plugin.php');
require_once('settings.php');
$language_mix_options = get_option( 'language_mix_option_name', $pllx_options_defaults);
$author_rules_backup = array();
/**
* Iterrupt plugin activation if Polylang is not activated/installed
*/
function pllx_check_polylang($network_wide) {
if (!is_plugin_active(PLL_PLUGIN_NAME)) {
deactivate_plugins(plugin_basename(__FILE__));
wp_die(__('Language Mix plugin requires Polylang to be installed and activated.', 'language-mix'));
}
}
register_activation_hook(__FILE__, 'pllx_check_polylang');
/**
* Deactivates Language Mix if Polylang is not active
*/
function pllx_admin_init() {
if (!is_plugin_active(PLL_PLUGIN_NAME)) {
deactivate_plugins(plugin_basename(__FILE__));
}
}
add_action('admin_init', 'pllx_admin_init');
/**
* Load translations
*/
function pllx_loaded() {
load_plugin_textdomain('language-mix', false, dirname(plugin_basename(__FILE__)) . '/languages/');
}
add_action('plugins_loaded', 'pllx_loaded');
/**
* Handles POST requests
*/
function pllx_init() {
global $polylang;
if ($polylang && isset($_POST[PLLX_PARAMETER])) {
if (is_array($_POST[PLLX_PARAMETER]) && !empty($_POST[PLLX_PARAMETER])) {
$languages = array();
foreach ($polylang->model->get_languages_list() as $language) {
if (in_array($language->slug, $_POST[PLLX_PARAMETER])) {
$languages[] = $language->slug;
}
}
if (!empty($languages)) {
$languages_cookie = implode(',', $languages);
setcookie(PLLX_COOKIE, $languages_cookie, time() + (10 * 365 * 86400), COOKIEPATH, COOKIE_DOMAIN);
$_COOKIE[PLLX_COOKIE] = $languages_cookie;
}
} else {
setcookie(PLLX_COOKIE, '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN);
unset($_COOKIE[PLLX_COOKIE]);
}
}
}
add_action('init', 'pllx_init');
require(plugin_dir_path(__FILE__) . 'widgets/languages.php');
require(plugin_dir_path(__FILE__) . 'widgets/translations.php');
/**
* Registers widget(s)
*/
function pllx_widgets_init() {
register_widget('WP_Widget_Languages');
register_widget('WP_Widget_Translations');
}
add_action('widgets_init', 'pllx_widgets_init');
/**
* Remove duplicated posts tanslated
*
* This filter acts after the query and remove unwanted translations.
* This was my historical way of de-duplicating contents with language-mix
* but there is several issues, especially when using pagination as
* count/offset won't be good. Hence the new version :-)
*
* This is left as memory but should not be useful anymore
*
*/
/*
function pllx_filter_the_posts($post_list) {
global $locale;
global $polylang;
$cur_lang = pll_current_language();
// Index post_list
$post_index=array();
foreach ($post_list as $post) { $post_index[] = $post->ID; }
// Filter post_list
foreach ($post_list as $k => $post) {
$post_lang = $polylang->model->post->get_language($post->ID);
// Check if current post in current locale
//if ($post_lang && ($post_lang->locale != $locale))
if ($post_lang && ($post_lang->slug != $cur_lang))
{
// If not, get translations
$trans = $polylang->model->post->get_translations($post->ID);
if ($trans && (count($trans) > 1))
{
unset($trans[$post_lang->slug]);
// If a translation exists, we delete the post
foreach($trans as $slug => $post_id){
// Skip current
if ($post_id != $post->ID) {
// Test if in list
if (in_array($post_id, $post_index)) {
// Found, deleting
unset($post_list[$k]);
}
}
}
}
}
}
return(array_values($post_list));
}
add_filter('the_posts','pllx_filter_the_posts');
*/
/**
* Get languages to consider
*
* Use options and current language
*
*/
function pllx_get_langs() {
global $polylang;
global $language_mix_options;
// If original behaviour, no need to compute much more
if ( ($language_mix_options['language_behaviour'] === 'all') && (! $language_mix_options['use_current_language'])) {
return (pllx_enabled_languages());
}
// Base is to get browser language preference : we get browser and filter by the one enabled to retain the priority
$langs = pllx_browser_languages();
$langs_enabled = pllx_enabled_languages();
foreach ($langs as $lang => $prio) {
if (!in_array( $lang, $langs_enabled )) {
unset($langs[$lang]);
}
}
// Use current language : we add the current language with highest priority
if ($language_mix_options['use_current_language']) {
$langs[pll_current_language()] = 10;
}
// All : we need to add polylang languages not included in the browser with lower priority
if ($language_mix_options['language_behaviour'] === 'all') {
foreach($polylang->model->get_languages_list() as $poly_lang) {
if (!array_key_exists($poly_lang->slug, $langs)) {
// Set very low priority based on (reverse) order field
$langs[$poly_lang->slug] = ((100 - $poly_lang->term_group) / 10000);
}
}
}
// Override : we need to add overridden languages not included in the browser with lower priority
if ($language_mix_options['language_behaviour'] === 'override') {
foreach(explode(',',$language_mix_options['forced_languages']) as $i => $l) {
if (!array_key_exists($l, $langs)) {
// Set very low priority based on (reverse) order field
$langs[$l] = ((100 - $i) / 10000);
}
}
}
// Finaly we sort the array to get proper order
arsort($langs, SORT_NUMERIC);
//error_log("pllx_get_langs: " . print_r($langs, 1));
return(array_keys($langs));
}
/**
* Retrieve the list of posts id to exclude to remove duplicated translated
* posts according to language priority given in $langs parameter
*/
function pllx_get_excluded_posts($langs) {
// Get all translation posts id with the use of terms post_translations
// This is probably not the best way to use polylang (use of internal storage)
// but should be quicker than any other method, especially for blogs with
// not the majority of posts translated (first use case of this plugin)
$tr_terms = get_terms( 'post_translations');
// Ignore special posts
$ignore_ids =[ get_option( 'page_on_front' ), get_option( 'page_for_posts' ) ];
$exclude_posts = [];
foreach ($tr_terms as $tr_term) {
$post_langs = unserialize( $tr_term->description );
if (count($post_langs) > 1) {
// Test if no special page
if (count(array_intersect($ignore_ids, $post_langs)) > 0) {
continue;
}
// Search best lang to keep (by lang preference order)
foreach($langs as $lang) {
if (array_key_exists($lang, $post_langs)) {
// We found the best lang post, we keep it and will exclude the others
unset($post_langs[$lang]);
break;
}
}
foreach ( $post_langs as $post_id) {
$exclude_posts[] = $post_id;
}
}
}
// error_log("pllx_get_excluded_posts: " . print_r($exclude_posts, 1));
return $exclude_posts;
}
/**
* Helper function to add posts to exclude to a query
*/
function pllx_query_exclude_posts($query, $exclude_posts) {
$query_not = $query->get('post__not_in');
if (is_array($query_not)) {
$query->set('post__not_in', array_merge($query_not, $exclude_posts));
} else {
$query->set('post__not_in', $exclude_posts);
}
//error_log("get_excluded_posts - exclude_posts : " . print_r($query, 1));
}
/**
* This method will alter the query before the run
* by excluding duplicate posts according to the language order.
*
* Results are cached for better performance
* (see wordpress doc about persistent caching : https://developer.wordpress.org/reference/classes/wp_object_cache/#persistent-caching)
*/
function pllx_alter_get_posts($query) {
global $language_mix_options;
// Restrict to post & pages (causes problems with other types as menu_order by example)
if (($query->get('post_type')=='') || in_array($query->get('post_type'), explode(',',$language_mix_options['custom_post_types'])) ) {
// Exclude admin page and restrict on other pages ; ajax is needed for pagination support
if ((! ( $query->is_admin() && ! wp_doing_ajax() ) ) && ( $query->is_home() || $query->is_front_page()) || wp_doing_ajax() ) {
$langs = pllx_get_langs();
$langs_str = implode(',',$langs);
// Handle caching of the posts list
$pllx_cache = wp_cache_get("pllx_cache_get_posts");
if ($pllx_cache && is_array($pllx_cache) && array_key_exists($langs_str, $pllx_cache)) {
$excluded_posts = $pllx_cache[$langs_str];
//error_log("pllx_alter_get_posts (cached):" . print_r($pllx_cache,1));
} else {
if (!is_array($pllx_cache)) { $pllx_cache = []; }
$excluded_posts = pllx_get_excluded_posts($langs);
$pllx_cache[$langs_str] = $excluded_posts;
wp_cache_set("pllx_cache_get_posts", $pllx_cache);
//error_log("pllx_alter_get_posts (new):" . print_r($pllx_cache,1));
}
pllx_query_exclude_posts($query, $excluded_posts);
// We need to use lang, if not polylang will translate our excluded post id and ruin our efforts!
$query->set('lang',$langs_str);
}
}
}
if ($language_mix_options['feature_remove_duplicate_posts'] === 'true') {
add_action('pre_get_posts','pllx_alter_get_posts',20);
}
/**
* Refresh the pllx filter post cache by renewing strings that have been
* already loaded in the cache.
* But it seems the cache is cleared before (never seen this working)
*/
function pllx_refresh_cache_post_save($unused_id) {
$pllx_cache = wp_cache_get("pllx_cache_get_posts");
if ($pllx_cache && is_array($pllx_cache)) {
foreach($pllx_cache as $langs_str => $dummy) {
$excluded_posts = pllx_get_excluded_posts(explode(',',$langs_str));
$pllx_cache[$langs_str] = $excluded_posts;
}
wp_cache_set("pllx_cache_get_posts", $pllx_cache);
}
//error_log("pllx_refresh_cache_post_save:" . print_r($pllx_cache,1));
}
add_action( 'save_post', 'pllx_refresh_cache_post_save', 2000);
/**
* Modifies the SQL query for posts
*/
function pllx_posts_where($where) {
global $polylang;
if ($polylang && preg_match("/post_type = 'post'/", $where) && !is_tax('language')) {
//$slugs = pllx_enabled_languages();
$slugs=pllx_get_langs();
if (count($slugs) > 0) {
if ((! ( is_admin() && ! wp_doing_ajax() ) ) && ( is_home() || is_front_page()) || wp_doing_ajax() ) {
$languages = array();
foreach ($slugs as $slug) {
$languages[] = (int)$polylang->model->get_language($slug)->term_taxonomy_id;
}
return preg_replace('/\.term_taxonomy_id IN \([^\)]*\)/', '.term_taxonomy_id IN (' . implode(',', $languages) . ')', $where);
} else if (is_category() || is_tag() || is_tax() ) {
$term = get_queried_object();
$translations = pllx_get_translations_with_children($term->term_id, $term->taxonomy, $slugs);
if (count($translations) > 0) {
return preg_replace('/\.term_taxonomy_id IN \([^\)]*\)/', '.term_taxonomy_id IN (' . implode(',', $translations) . ')', $where);
}
} else if ((preg_match('/\.term_taxonomy_id IN \(([^\)]*)\)/', $where, $matches))) {
$term_ids = $matches[1];
$translations = array();
foreach(explode(",",$term_ids) as $term_id) {
$term = get_term( $term_id );
$translations = array_merge($translations, pllx_get_translations_with_children($term->term_id, $term->taxonomy, $slugs));
}
if (count($translations) > 0) {
return preg_replace('/\.term_taxonomy_id IN \([^\)]*\)/', '.term_taxonomy_id IN (' . implode(',', $translations) . ')', $where);
}
}
}
}
return $where;
}
add_filter('posts_where', 'pllx_posts_where', 20);
/**
* Translates titles of categories in menus
*/
function pllx_nav_menu_objects($items) {
global $locale;
global $polylang;
$queried_object = get_queried_object();
$item_selected = false;
foreach ($items as $item) {
if ($item->current) {
$item_selected = true;
break;
}
}
$pages = array();
$categories = array();
if ($item_selected == false) {
if (is_category()) {
$categories = pllx_get_translations_with_parents($queried_object->cat_ID);
} else if (is_page()) {
$pages = $polylang->model->get_translations('page', $queried_object->ID);
} else if (is_single() && ($queried_object->post_type == 'post')) {
$post_categories = wp_get_post_categories($queried_object->ID);
foreach ($post_categories as $category_id) {
$categories = array_merge($categories, pllx_get_translations_with_parents($category_id));
}
$categories = array_unique($categories);
}
}
foreach ($items as &$item) {
if ($item->object == 'page') {
$page = get_post($item->object_id);
if ($page) {
$language = $polylang->model->post->get_language($item->object_id);
if ($language && ($language->locale != $locale)) {
$translation_id = pll_get_post($item->object_id, $locale);
if ($translation_id) {
$page = get_post($translation_id);
if ($page) {
$item->title = $page->post_title;
}
}
}
}
if ($item_selected == false) {
if (is_page()) {
if (in_array($item->object_id, $pages)) {
$item->classes[] = 'current-menu-item';
$item_selected = true;
}
}
}
} else if ($item->object == 'category') {
$category = get_term($item->object_id, 'category');
if ($category) {
$language = $polylang->model->get_term_language($item->object_id);
if ($language && ($language->locale != $locale)) {
$translation_id = pll_get_term($item->object_id, $locale);
if ($translation_id) {
$translation = get_term($translation_id, 'category');
if ($translation) {
$item->title = $translation->name;
}
}
}
}
if (($item_selected == false) && !empty($categories)) {
if (is_category() || is_single()) {
if (in_array($item->object_id, $categories)) {
$item->classes[] = 'current-menu-item';
$item_selected = true;
}
}
}
}
}
unset($item);
return $items;
}
if ($language_mix_options['feature_translate_cat_in_menus'] === 'true') {
add_filter('wp_nav_menu_objects', 'pllx_nav_menu_objects');
}
/**
* Showing empty categories by default (as their translations can contain posts)
*/
function pllx_widget_categories_args($args) {
$args['hide_empty'] = 0;
return $args;
}
add_filter('widget_categories_dropdown_args', 'pllx_widget_categories_args');
add_filter('widget_categories_args', 'pllx_widget_categories_args');
/**
* Relaces the front page with the corresponding translation
*/
function pllx_get_page_on_front($post_id) {
global $locale;
global $polylang;
if ($polylang && $post_id && !is_admin()) {
$language = $polylang->model->post->get_language($post_id);
if ($language && ($language->locale != $locale)) {
$translated_id = pll_get_post($post_id, $locale);
if ($translated_id) {
return $translated_id;
}
}
}
return $post_id;
}
if ($language_mix_options['feature_replace_front_page'] === 'true') {
add_filter('option_page_on_front', 'pllx_get_page_on_front');
}
/**
* Looks for translations for the category featured post
*/
function pllx_category_featured_post($post_id, $term_id) {
global $polylang;
if ($polylang && !$post_id) {
$translations = $polylang->model->get_translations('term', $term_id);
foreach ($translations as $language => $term_id) {
if ($post_id = get_category_featured_post($term_id)) {
break;
}
}
}
return $post_id;
}
if ($language_mix_options['feature_category_featured_post'] === 'true') {
add_filter('get_category_featured_post', 'pllx_category_featured_post', 10, 2);
}
/**
* Make a copy of author rules
*/
function pllx_backup_author_rules($rules) {
global $author_rules_backup;
$author_rules_backup = $rules;
return $rules;
}
add_filter('author_rewrite_rules', 'pllx_backup_author_rules');
/**
* Restore author rules
*/
function pllx_rewrite_rules($rules) {
global $author_rules_backup;
return $author_rules_backup + $rules;
}
add_filter('rewrite_rules_array', 'pllx_rewrite_rules');
# TODO same for date and search? etc?
/**
* Remove link from author URL
*/
function pllx_author_link($link) {
global $polylang;
return $polylang->links_model->remove_language_from_link($link);
}
add_filter('author_link', 'pllx_author_link', 30);
/**
* Get all translations of the term (including children)
*/
function pllx_get_translations_with_children($term_id, $taxonomy, $languages) {
global $polylang;
$translations = array();
$term_translations = $polylang->model->term->get_translations($term_id);
foreach ($term_translations as $language => $term_id) {
if (in_array($language, $languages)) {
$term = get_term($term_id, $taxonomy);
if ($term) {
$translations[] = $term->term_taxonomy_id;
$term_children = get_term_children($term_id, $taxonomy);
foreach ($term_children as $term_child) {
$term = get_term($term_child, $taxonomy);
if ($term) {
$translations[] = $term->term_taxonomy_id;
}
}
}
}
}
return $translations;
}
/**
* Get all translations of the category (including parents)
*/
function pllx_get_translations_with_parents($category_id) {
global $polylang;
$translations = array();
$cat_translations = $polylang->model->get_translations('term', $category_id);
foreach ($cat_translations as $language => $cat_id) {
$translations[] = $cat_id;
$translations = array_merge($translations, pllx_get_category_parents($cat_id));
}
return $translations;
}
/**
* Merges all category parents into a single array
*/
function pllx_get_category_parents($id, $visited = array()) {
$parents = array();
$parent = get_category($id);
if (!is_wp_error($parent)) {
if ($parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited)) {
$visited[] = $parent->parent;
$parents = array_merge($parents, pllx_get_category_parents($parent->parent, $visited));
}
$parents[] = $parent->term_id;
}
return $parents;
}
/**
* Parses the Accept-Language header
*/
function pllx_browser_languages() {
$languages = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $accepted_language) {
$lang_comps = explode(';', $accepted_language);
if (count($lang_comps) > 1) { list($lang, $q) = $lang_comps; }
else { $lang = $lang_comps[0]; $q=1; }
if ($q && preg_match('/\bq=([0-9]+(?:\.[0-9]+)?)/', $q, $m)) {
$q = floatval($m[1]);
} else {
$q = 1;
}
$lang = str_replace('-', '_', $lang);
$languages[$lang] = $q;
}
arsort($languages);
}
return $languages;
}
/**
* Returns the array of enabled languages
*/
function pllx_enabled_languages() {
global $polylang;
if (isset($_COOKIE[PLLX_COOKIE])) {
return explode(',', $_COOKIE[PLLX_COOKIE]);
} else {
$enabled_languages = array();
$browser_languages = pllx_browser_languages();
foreach ($polylang->model->get_languages_list() as $language) {
if (isset($browser_languages[$language->slug]) ||
isset($browser_languages[$language->locale])) {
$enabled_languages[] = $language->slug;
}
}
return $enabled_languages;
}
}
/**
* Fetches term taxonomy by ID
*/
function pllx_get_term_taxonomy($id) {
global $wpdb;
return $wpdb->get_row($wpdb->prepare(
"SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d",
$id
));
}
?>