forked from maximeschoeni/sublanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-site.php
1436 lines (946 loc) · 33.3 KB
/
class-site.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class Sublanguage_site extends Sublanguage_current {
/**
* @var boolean
*/
var $canonical = true;
/**
* @compat from 2.0
* @var int
*/
var $current_language;
/**
*
* @from 1.0
*/
public function __construct() {
add_filter('locale', array($this, 'get_locale'));
add_action('plugins_loaded', array($this, 'load'));
}
/**
* @from 1.4.7
*/
public function load() {
if ($this->current_language = $this->get_language()) {
parent::load();
add_filter('the_content', array($this, 'translate_post_content'), 9); // -> allowed @from 2.5
add_filter('the_title', array($this, 'translate_post_title'), 10, 2);
add_filter('get_the_excerpt', array($this, 'translate_post_excerpt'), 9);
add_filter('single_post_title', array($this, 'translate_single_post_title'), 10, 2);
add_filter('get_post_metadata', array($this, 'translate_meta_data'), 10, 4);
add_filter('wp_setup_nav_menu_item', array($this, 'translate_menu_nav_item'));
add_filter('wp_nav_menu_objects', array($this, 'filter_nav_menu_objects'), 10, 2); // -> @from 1.5. Filter list for hidden items
add_filter('tag_cloud_sort', array($this,'translate_tag_cloud'), 10, 2);
add_action('init', array($this, 'init'));
$this->add_options_filters();
}
}
/**
* @from 1.0
*/
public function init() {
if (get_option('permalink_structure')) {
add_filter('query_vars', array($this, 'query_vars'));
add_filter('request', array($this, 'catch_translation')); // detect query type and language out of query vars
add_action('wp', array($this, 'redirect_uncanonical'), 11);
}
// enqueue AJAX script
if ($this->get_option('frontend_ajax')) {
add_action('wp_enqueue_scripts', array($this, 'ajax_enqueue_scripts'));
}
// link filters only after request have been parsed
add_action('parse_request', array($this, 'add_links_translation_filters'));
// login
add_filter('login_url', array($this, 'translate_login_url'));
add_filter('lostpassword_url', array($this, 'translate_login_url'));
add_filter('logout_url', array($this, 'translate_login_url'));
add_filter('register_url', array($this, 'translate_login_url'));
add_action('login_form', array($this, 'translate_login_form'));
add_action('lostpassword_form', array($this, 'translate_login_form'));
add_action('resetpass_form', array($this, 'translate_login_form'));
add_action('register_form', array($this, 'translate_login_form'));
add_filter('retrieve_password_message', array($this, 'translate_retrieve_password_message'));
add_filter('lostpassword_redirect', array($this, 'lostpassword_redirect'));
add_filter('registration_redirect', array($this, 'registration_redirect'));
// print hreflang in template head
add_action('wp_head', array($this, 'print_hreflang')); // -> added in 1.4.5
// API
add_action('sublanguage_print_language_switch', array($this, 'print_language_switch'));
add_filter('sublanguage_custom_translate', array($this, 'custom_translate'), 10, 3);
/**
* Hook called after initializing most hooks and filters
*
* @from 1.2
*
* @param Sublanguage_site object
*/
do_action('sublanguage_init', $this);
}
/**
* Override set_language for compat
*
* @from 2.0
*
* @param object WP_post $language Language. Optional
*/
public function set_language($language = null) {
Sublanguage_core::set_language($language);
$this->current_language = $this->get_language();
}
/**
* Custom translation. Sublanguage API
*
* Filter for 'sublanguage_custom_translate'
*
* @from 1.0
*/
public function custom_translate($content, $callback, $args = null) {
if ($this->has_language()) {
return call_user_func($callback, $content, $this->get_language(), $this, $args);
}
return $content;
}
/**
* Request current language
*
* Override Sublanguage_core::request_language()
*
* @from 2.0
*
* @return object WP_post
*/
public function request_language() {
if (isset($_REQUEST[$this->language_query_var])) {
$language = $this->get_language_by($_REQUEST[$this->language_query_var], 'post_name');
} else if (isset($_SERVER['REQUEST_URI'])) {
if (preg_match('/\/('.implode('|', $this->get_language_column('post_name')).')(\/|$|\?|#)/', $_SERVER['REQUEST_URI'], $matches)) { // -> language detected!
$language = $this->get_language_by($matches[1], 'post_name');
if ($this->is_default($language) && !$this->get_option('show_slug')) {
$this->canonical = false;
}
} else {
if ($this->get_option('show_slug')) {
$this->canonical = false;
}
if ($this->get_option('autodetect')) { // auto detect language on home page
// detect only on home page? --> rtrim($_SERVER['SCRIPT_URI'], '/') == rtrim(home_url())
$detected_language = $this->auto_detect_language();
if ($detected_language) {
$language = $detected_language;
}
}
}
}
if (empty($language)) {
$language = $this->get_default_language();
}
if ($language && get_post_meta($language->ID, 'rtl', true)) {
$GLOBALS['text_direction'] = 'rtl';
}
return $language;
}
/**
* Filter for 'locale'
*
* @from 1.0
*/
public function get_locale($locale) {
if ($language = $this->get_language()) {
return $language->post_content;
}
return $locale;
}
/**
* Translate menu nav items
* Filter for 'wp_setup_nav_menu_item'
*/
public function translate_menu_nav_item($menu_item) {
if ($menu_item->type == 'post_type') {
if ($this->is_post_type_translatable($menu_item->object)) {
$original_post = get_post($menu_item->object_id);
$menu_item = $this->translate_nav_menu_item($menu_item);
if (empty($menu_item->post_title)) {
$menu_item->title = $this->translate_post_field($original_post, 'post_title', null, $menu_item->title);
} else {
$menu_item->title = $menu_item->post_title;
}
$menu_item->url = get_permalink($original_post);
}
} else if ($menu_item->type == 'taxonomy') {
if ($this->is_taxonomy_translatable($menu_item->object)) {
$original_term = get_term($menu_item->object_id, $menu_item->object);
if ($original_term && !is_wp_error($original_term)) {
$menu_item = $this->translate_nav_menu_item($menu_item);
if (empty($menu_item->post_title)) {
$menu_item->title = $this->translate_term_field($original_term, $original_term->taxonomy, 'name', null, $menu_item->title);
} else {
$menu_item->title = $menu_item->post_title;
}
// url already filtered
}
}
} else if ($menu_item->type == 'custom') {
if ($menu_item->title == 'language') {
static $languages, $language_index;
if (!isset($languages)) {
$languages = $this->get_sorted_languages();
}
if (!isset($language_index) || $language_index >= count($languages)) {
$language_index = 0;
}
$language = $languages[$language_index];
/**
* Filter language name
*
* @from 1.2
*
* @param WP_post object
*/
$menu_item->title = apply_filters('sublanguage_language_name', $language->post_title, $language);
$menu_item->url = $this->get_translation_link($language);
$menu_item->classes[] = $this->is_current($language) ? 'active_language' : 'inactive_language';
$menu_item->classes[] = 'sublanguage';
$menu_item->classes[] = $language->post_name;
$language_index++;
}
$menu_item = $this->translate_nav_menu_item($menu_item, true);
}
return $menu_item;
}
/**
* Translate a nav menu item
*
* @param object WP_Post $menu_item
* @return object WP_Post
*
* @from 1.5
*/
public function translate_nav_menu_item($menu_item, $fill_default_title = false) {
if ($this->is_sub() && $this->is_post_type_translatable('nav_menu_item')) {
$menu_item->post_title = $this->translate_post_field($menu_item, 'post_title', null, ($fill_default_title ? $menu_item->title : ''));
$menu_item->description = $this->translate_post_field($menu_item, 'post_content', null, $menu_item->description);
$menu_item->attr_title = $this->translate_post_field($menu_item, 'post_excerpt', null, $menu_item->attr_title);
}
return $menu_item;
}
/**
* Remove items that need to be hidden in current language
*
* Filter for 'wp_nav_menu_objects'
*
* @from 1.5
*/
public function filter_nav_menu_objects($sorted_menu_items, $args) {
if ($this->is_post_type_translatable('nav_menu_item') && in_array('sublanguage_hide', $this->get_post_type_metakeys('nav_menu_item'))) {
$filtered_items = array();
foreach ($sorted_menu_items as $menu_item) {
if ($this->is_sub() && !get_post_meta($menu_item->ID, $this->get_prefix().'sublanguage_hide', true) || $this->is_main() && !get_post_meta($menu_item->ID, 'sublanguage_hide', true)) {
$filtered_items[] = $menu_item;
}
}
return $filtered_items;
}
return $sorted_menu_items;
}
/**
* Print language switch
*
* hook for 'sublanguage_print_language_switch'
*
* @from 1.0
*/
public function print_language_switch($context = null) {
$languages = $this->get_sorted_languages();
if (has_action('sublanguage_custom_switch')) {
/**
* Customize language switch output
*
* @from 1.2
*
* @param array of WP_Post language custom post
* @param Sublanguage_site $this The Sublanguage instance.
* @param mixed context
*/
do_action_ref_array('sublanguage_custom_switch', array($languages, $this, $context));
} else {
$output = '<ul>';
foreach ($languages as $language) {
/**
* Filter language name
*
* @from 1.2
*
* @param string language name
* @param WP_Post language custom post
*/
$output .= sprintf('<li class="%s%s"><a href="%s">%s</a></li>',
$language->post_name,
($this->is_current($language) ? ' current' : ''),
$this->get_translation_link($language),
apply_filters('sublanguage_language_name', $language->post_title, $language)
);
}
$output .= '</ul>';
echo $output;
}
}
/**
* Register sublanguage specific query_vars
*
* @filter 'query_vars'
*
* @from 1.0
*/
public function query_vars($public_query_vars) {
$public_query_vars[] = 'sublanguage_slug';
$public_query_vars[] = 'sublanguage_page';
$public_query_vars[] = 'preview_language';
return $public_query_vars;
}
/**
* Intercept query_vars to find out type of query and get parent.
* Must return an array of query vars
*
* Hook for 'request'
*
* @from 1.0
*/
public function catch_translation($query_vars) {
global $wp_rewrite;
if (isset($query_vars['sublanguage_page']) || isset($query_vars['pagename']) || isset($query_vars['name'])) { // -> page, post or custom post type
$name = '';
if (isset($query_vars['sublanguage_page'])) {
$name = $query_vars['sublanguage_page'];
} else if (isset($query_vars['pagename'])) {
$name = $query_vars['pagename'];
} else if (isset($query_vars['name'])) {
$name = $query_vars['name'];
}
$ancestors = explode('/', $name);
// -> remove the permalink structure prefix if there is one
if (isset($query_vars['sublanguage_page']) && !empty($wp_rewrite->front) && $wp_rewrite->front !== '/' && trim($wp_rewrite->front, '/') === $ancestors[0]) {
array_shift($ancestors);
}
$post_name = array_pop($ancestors);
$post_types = isset($query_vars['post_type']) ? array($query_vars['post_type']) : array('page', 'post');
$post = $this->query_post($post_name, $post_types, $ancestors);
if ($post) {
$post_type_obj = get_post_type_object($post->post_type);
if (isset($query_vars['sublanguage_slug']) && $query_vars['sublanguage_slug'] !== $this->translate_cpt($post->post_type, null, $post->post_type)) {
// wrong slug
$this->canonical = false;
}
if ($post_type_obj->hierarchical) {
$path = '';
$parent_id = $post->post_parent;
while ($parent_id) {
$parent = get_post($parent_id);
$path = $parent->post_name . '/' . $path;
$parent_id = $parent->post_parent;
}
if (isset($query_vars[$post->post_type])) {
$query_vars[$post->post_type] = $path . $post->post_name;
}
if (isset($query_vars['name'])) {
$query_vars['name'] = $path . $post->post_name;
} else {
$query_vars['pagename'] = $path . $post->post_name;
}
} else {
if (isset($query_vars['pagename'])) {
$query_vars['pagename'] = $post->post_name;
} else {
$query_vars['name'] = $post->post_name;
}
if (isset($query_vars[$post->post_type])) {
$query_vars[$post->post_type] = $post->post_name;
}
}
} else if (isset($query_vars['sublanguage_page'])) { // -> nothing found. Let's pretend we did not see
$query_vars['name'] = $query_vars['sublanguage_page'];
}
} else if (isset($query_vars['attachment']) && $this->is_post_type_translatable('attachment')) { // -> attachment (this is a child of a "post" post-type)
$post = $this->query_post($query_vars['attachment'], 'attachment');
if ($post) {
$query_vars['attachment'] = $post->post_name;
}
} else if (isset($query_vars['post_type'])) { // -> custom-post-type archive
$post_type = $query_vars['post_type'];
if ($this->is_post_type_translatable($post_type)) {
if (isset($query_vars['sublanguage_slug']) && $query_vars['sublanguage_slug'] !== $this->translate_cpt_archive($post_type, null)) {
// wrong slug
$this->canonical = false;
}
}
} else if ($results = array_filter(array_map(array($this, 'query_var_to_taxonomy'), array_keys($query_vars)), array($this, 'is_taxonomy_translatable'))) { // -> untranslated taxonomy
if (isset($query_vars['sublanguage_slug'])) {
$taxonomy = '';
foreach ($results as $r) {
$taxonomy = $r;
break;
}
if (!$taxonomy) throw new Exception('Taxonomy not found!');
$tax_obj = get_taxonomy($taxonomy);
$tax_qv = $tax_obj->query_var;
$term_name = $query_vars[$tax_qv];
$term = $this->query_taxonomy($term_name, $taxonomy);
if ($term) {
$query_vars[$tax_qv] = $term->slug; // -> restore original language name in query_var
$tax_translation = $this->translate_taxonomy($taxonomy, null, $taxonomy);
if ($this->translate_taxonomy($taxonomy, null, $taxonomy) !== $query_vars['sublanguage_slug']) { // taxonomy should be translated
$this->canonical = false;
}
}
}
}
if (isset($query_vars['preview'])) {
$this->canonical = true;
}
return $query_vars;
}
/**
* Add links translation filters after all query variables for the current request have been parsed.
*
* @hook 'parse_request'
* @from 2.0
*/
public function add_links_translation_filters($wp = null) {
add_filter('home_url', array($this,'translate_home_url'), 10, 4);
add_filter('pre_post_link', array($this, 'pre_translate_permalink'), 10, 3);
add_filter('post_link', array($this, 'translate_permalink'), 10, 3);
add_filter('page_link', array($this, 'translate_page_link'), 10, 3);
add_filter('post_type_link', array($this, 'translate_custom_post_link'), 10, 3);
add_filter('attachment_link', array($this, 'translate_attachment_link'), 10, 2);
add_filter('post_link_category', array($this, 'translate_post_link_category'), 10, 3); // not implemented yet
add_filter('post_type_archive_link', array($this, 'translate_post_type_archive_link'), 10, 2);
add_filter('year_link', array($this,'translate_month_link'));
add_filter('month_link', array($this,'translate_month_link'));
add_filter('day_link', array($this,'translate_month_link'));
add_filter('term_link', array($this, 'translate_term_link'), 10, 3);
add_filter('get_edit_post_link', array($this, 'translate_edit_post_link'), 10, 3);
}
/**
* Redirection when not canoncal url
* Must be fired after filters.
* Must be fired after conditional tags are set.
*
* @from 1.0
*/
public function redirect_uncanonical() {
$query_object = get_queried_object();
if (!$this->canonical) {
if (is_singular()) {
$url = get_permalink($query_object->ID);
} else if (is_post_type_archive()) {
$url = get_post_type_archive_link($query_object->name);
} else if (is_category() || is_tag() || is_tax()) {
$url = get_term_link($query_object->term_id, $query_object->taxonomy);
} else {
$url = home_url();
}
wp_redirect($url);
exit;
}
}
/**
* Find original post based on query vars info.
*
* @from 1.0
*
* @param string $post_name
* @param string|array $post_types
*/
public function query_post($post_name, $post_types, $ancestors = array()) {
global $wpdb;
$post_types = esc_sql($post_types);
$post_type_strings = is_array($post_types) ? "'".implode("','", $post_types)."'" : "'".$post_types."'";
$translation_slug = $this->get_prefix().'post_name';
$post_ids = $wpdb->get_col( $wpdb->prepare(
"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s",
$translation_slug,
$post_name
));
if ($post_ids) {
// Translations found but we're not sure about post_type
$posts = $wpdb->get_results(
"SELECT post.* FROM $wpdb->posts AS post
WHERE post.post_type IN ($post_type_strings)
AND post.ID IN (".implode(",", array_map('intval', $post_ids)).")"
);
// Use WP_Query (for caching)
// $posts_query = new WP_Query(array(
// 'post_type' => $post_types,
// 'post__in' => $post_ids,
// 'posts_per_page' => -1,
// 'ignore_sticky_posts' => true
// ));
// $posts = $posts_query->posts;
foreach ($posts as $post) {
if ($ancestors) { // -> verify ancestors recursively
$parent_name = array_pop($ancestors);
$parent = $this->query_post($parent_name, $post_types, $ancestors);
if ($parent && $post->post_parent == $parent->ID) {
return $post;
}
} else {
// This one will just do
return $post;
}
}
foreach ($posts as $post) {
// -> no ancestor matched
$this->canonical = false;
// This one will do
return $post;
}
}
$posts = $wpdb->get_results( $wpdb->prepare(
"SELECT post.* FROM $wpdb->posts AS post
WHERE post.post_name = %s AND post.post_type IN ($post_type_strings)",
$post_name
));
// Use WP_Query
// $posts_query = new WP_Query(array(
// 'post_type' => $post_types,
// 'name' => $post_name,
// 'posts_per_page' => -1,
// 'ignore_sticky_posts' => true
// ));
// $posts = $posts_query->posts;
if ($posts) {
foreach ($posts as $post) {
if ($ancestors) { // -> we need to verify ancestors recursively
$parent_name = array_pop($ancestors);
$parent = $this->query_post($parent_name, $post_types, $ancestors);
// check if parent match and there is no specific translation...
if ($parent && $post->post_parent == $parent->ID) {
// Post found
if ($this->is_post_type_translatable($post->post_type) && get_post_meta($post->ID, $this->get_prefix() . 'post_name', true)) {
// But there is a specific translation for this post
$this->canonical = false;
}
return $post;
}
} else {
// Post found
if ($this->is_post_type_translatable($post->post_type) && get_post_meta($post->ID, $this->get_prefix() . 'post_name', true)) {
// But there is a specific translation for this post
$this->canonical = false;
}
return $post;
}
}
foreach ($posts as $post) {
$this->canonical = false;
// no ancestor matched but lets return this one
return $post;
}
} else {
// Nothing found. -> Search in other languages...
$post = $wpdb->get_row( $wpdb->prepare(
"SELECT post.* FROM $wpdb->posts AS post
INNER JOIN $wpdb->postmeta AS meta ON (post.ID = meta.post_id)
WHERE post.post_type IN ($post_type_strings) AND (meta_key IN ('".implode("post_name', '", esc_sql(array_map(array($this, 'create_prefix'), $this->get_language_column('post_name'))))."post_name') AND meta.meta_value = %s)",
$post_name
));
// teacher, do I need to check ancestors?
if ($post) {
// Post found in wrong language.
$this->canonical = false;
return $post;
}
}
return false;
}
/**
* Find original term based on query vars info.
*
* @from 1.0
*
* @param string $slug
* @param string|array $taxonomy
*/
public function query_taxonomy($slug, $taxonomies) {
global $wpdb;
$taxonomy_string = is_array($taxonomies) ? "'".implode("','", esc_sql($taxonomies))."'" : "'".esc_sql($taxonomies)."'";
$translation_slug = $this->get_prefix() . 'slug';
$term_ids = $wpdb->get_col( $wpdb->prepare(
"SELECT term_id FROM $wpdb->termmeta WHERE meta_key = %s AND meta_value = %s",
$translation_slug,
$slug
));
if ($term_ids) {
// Translations found but we're not sure about taxonomy
$term = $wpdb->get_row(
"SELECT t.term_id, t.slug, tt.taxonomy, tt.parent FROM $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
WHERE tt.taxonomy IN ($taxonomy_string)
AND t.term_id IN (".implode(",", array_map('intval', $term_ids)).")"
);
return $term;
// $terms = get_terms(array(
// 'taxonomy' => $taxonomies,
// 'hide_empty' => false,
// 'include' => $term_ids,
// 'language' => false
// ));
}
// -> no translated term for this slug
$term = $wpdb->get_row( $wpdb->prepare(
"SELECT t.term_id, t.slug, tt.taxonomy, tt.parent FROM $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
WHERE tt.taxonomy IN ($taxonomy_string)
AND t.slug = %s",
$slug
));
// $terms = get_terms(array(
// 'taxonomy' => $taxonomies,
// 'hide_empty' => false,
// 'slug' => $slug,
// 'language' => false
// ));
if ($term) {
if ($this->is_taxonomy_translatable($term->taxonomy) && get_term_meta($term->term_id, $this->get_prefix() . 'slug', true)) {
// -> But there is a specific translation for this term
$this->canonical = false;
}
return $term;
} else {
// Nothing found. -> Search in other languages...
$language_slugs = $this->get_language_column('post_name');
$term_ids = $wpdb->get_col( $wpdb->prepare(
"SELECT term_id FROM $wpdb->termmeta WHERE meta_key IN ('_" . implode("slug','", esc_sql(array_map(array($this, 'create_prefix'), $language_slugs))) . "slug') AND meta_value = %s",
$slug
));
if ($term_ids) {
$term = $wpdb->get_row(
"SELECT t.term_id, t.slug, tt.taxonomy, tt.parent FROM $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
WHERE tt.taxonomy IN ($taxonomy_string)
AND t.term_id IN (".implode(",", array_map('intval', $term_ids)).")"
);
if ($term) {
// Term found in wrong language.
$this->canonical = false;
return $term;
}
}
}
return false;
}
/**
* Add language slug in login url
*
* Filter for 'login_url', 'logout_url', 'lostpassword_url', 'register_url'
*
* @from 1.2
*/
public function translate_login_url($login_url){
if ($this->has_language()) {
$login_url = add_query_arg(array($this->language_query_var => $this->get_language()->post_name), $login_url);
}
return $login_url;
}
/**
* Add language input in login forms
*
* Hook for 'login_form', 'lostpassword_form', 'resetpass_form', 'register_form'
*
* @from 1.2
*/
public function translate_login_form() {
echo '<input type="hidden" name="'.$this->language_query_var.'" value="'.$this->get_language()->post_name.'"/>';
}
/**