forked from maximeschoeni/sublanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-current.php
1661 lines (1173 loc) · 41 KB
/
class-current.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
/**
* Base class for Sublanguage_site in front-end and Sublanguage_admin in admin.
*/
class Sublanguage_current extends Sublanguage_core {
/**
* @from 1.2
*
* @var boolean
*/
var $disable_translate_home_url = false;
/**
* @from 1.4.6
*
* @var array
*/
var $term_sort_cache;
/**
* @from 1.4.6
*
* @var array
*/
var $post_sort_cache;
/**
* @var Array
*
* Used to save original $wp_rewrite->extra_permastructs values
*
* @from 1.5
*/
var $original_permastructs;
/**
* Used when translating search query
*
* @var string
*
* @from 2.0
*/
private $search_sql;
/**
* @var array
*
* Gutenberg markup
*
* @from 2.5
*/
var $gutenberg_language_tag = '<!-- wp:sublanguage/language-manager {"current_language":"%s"} /-->';
var $gutenberg_language_reg = '<!-- wp:sublanguage/language-manager \{"current_language":"([^"]+)"\} /-->';
/**
* @var object WP_Post language
*
* Language to edit from Gutenberg
*
* @from 2.5
*/
var $edit_language;
/**
* Register all filters needed for admin and front-end
*
* @from 1.4.7
*/
public function load() {
add_action('init', array($this, 'register_languages')); // @from 2.5 moved from admin-ui
add_filter('parse_query', array($this, 'parse_query'));
add_filter('get_object_terms', array($this, 'filter_get_object_terms'), 10, 4);
add_filter('get_term', array($this, 'translate_get_term'), 10, 2); // hard translate term
add_filter('get_terms', array($this, 'translate_get_terms'), 10, 3); // hard translate terms
add_filter('get_the_terms', array($this, 'translate_post_terms'), 10, 3);
add_filter('list_cats', array($this, 'translate_term_name'), 10, 2);
add_filter('the_posts', array($this, 'translate_the_posts'), 10, 2);
add_filter('get_pages', array($this, 'translate_the_pages'), 10, 2);
add_filter('sublanguage_translate_post_field', array($this, 'translate_post_field_custom'), 10, 5);
add_filter('sublanguage_translate_term_field', array($this, 'translate_term_field_custom'), 10, 6);
add_filter('sublanguage_query_add_language', array($this, 'query_add_language'));
add_action('widgets_init', array($this, 'register_widget'));
// Gutenberg -> @from 2.5
add_action('init', array($this, 'register_translation_meta'));
add_action('registered_post_type', array($this, 'rest_register_post_type'));
add_filter('rest_prepare_revision', array($this, 'rest_prepare_post'), 10, 3);
}
/**
* Register language post-type
*
* @hook 'init'
*
* @from 2.5
*/
public function register_languages() {
register_post_type($this->language_post_type, array(
'labels' => array(
'name' => __( 'Languages', 'sublanguage' ),
'singular_name' => __( 'Language', 'sublanguage' ),
'menu_name' => __( 'Languages', 'sublanguage' ),
'name_admin_bar' => __( 'Languages', 'sublanguage' ),
'add_new' => __( 'Add language', 'sublanguage' ),
'add_new_item' => __( 'Add language', 'sublanguage' ),
'new_item' => __( 'New language', 'sublanguage' ),
'edit_item' => __( 'Edit language', 'sublanguage' ),
'view_item' => __( 'View language', 'sublanguage' ),
'all_items' => __( 'Languages', 'sublanguage' ),
'search_items' => __( 'Search languages', 'sublanguage' ),
'parent_item_colon' => __( 'Parent language:', 'sublanguage' ),
'not_found' => __( 'No language found.', 'sublanguage' ),
'not_found_in_trash' => __( 'No language found in Trash.', 'sublanguage' )
),
'public' => false,
'publicly_queryable' => false,
'show_ui' => true,
//'show_in_menu' => true,
'show_in_menu' => true,
'query_var' => false,
'rewrite' => false,
'capabilities' => array(
'edit_post' => 'edit_language',
'edit_posts' => 'edit_languages',
'edit_others_posts' => 'edit_other_languages',
'publish_posts' => 'publish_languages',
'read_post' => 'read_language',
'read_private_posts' => 'read_private_languages',
'delete_post' => 'delete_language'
),
'map_meta_cap' => true,
'has_archive' => false,
'hierarchical' => false,
'supports' => array('title', 'slug', 'page-attributes') ,
'menu_icon' => 'dashicons-translation',
'can_export' => false
));
}
/**
* Register meta for REST
*
* @from 2.5
*/
public function register_translation_meta() {
foreach ($this->get_languages() as $language) {
// also need to register main language!
foreach ($this->fields as $field) {
register_meta('post', $this->get_prefix($language) . $field, array(
'type' => 'string',
'auth_callback' => '__return_true',
'single' => true,
'show_in_rest' => true
));
}
}
}
/**
* Register post types for Parse meta for REST
*
* @filter 'registered_post_type'
* @from 2.5
*/
public function rest_register_post_type($post_type) {
if ($this->is_post_type_translatable($post_type)) {
add_filter('rest_pre_insert_' . $post_type, array($this, 'rest_pre_insert_post'), 10, 2);
add_filter('rest_prepare_' . $post_type, array($this, 'rest_prepare_post'), 10, 3);
$post_type_object = get_post_type_object($post_type);
if (empty($post_type_object->template)) {
$post_type_object->template = array();
}
$post_type_object->template[] = array('sublanguage/language-manager', array(
'current_language' => $this->get_language()
));
}
}
/**
* Parse meta for REST
*
* @filter "rest_pre_insert_{$this->post_type}"
* @from 2.5
*/
public function rest_pre_insert_post($prepared_post, $request) {
$meta = $request->get_param('meta');
if (!$meta) {
$meta = array();
}
if (preg_match('#'.$this->gutenberg_language_reg.'#', $prepared_post->post_content, $matches)) {
$this->edit_language = $this->find_language($matches[1], 'post_name');
$prepared_post->post_content = str_replace($matches[0], '', $prepared_post->post_content);
} else {
$this->edit_language = $this->get_language();
}
$this->current_language = $this->get_main_language(); // now data are parsed: use main language
if (isset($prepared_post->post_content)) $post_content = $prepared_post->post_content;
if (isset($prepared_post->post_excerpt)) $post_excerpt = $prepared_post->post_excerpt;
if (isset($prepared_post->post_title)) $post_title = $prepared_post->post_title;
if (isset($prepared_post->post_name)) $post_name = $prepared_post->post_name;
foreach ($this->get_languages() as $language) {
$content_field = $this->get_prefix($language) . 'post_content';
$excerpt_field = $this->get_prefix($language) . 'post_excerpt';
$title_field = $this->get_prefix($language) . 'post_title';
$name_field = $this->get_prefix($language) . 'post_name';
if (preg_match('#'.$this->gutenberg_language_reg.'#', $content_field, $matches)) {
$content_field = str_replace($matches[0], '', $content_field);
}
if ($this->is_sub($language)) {
if ($language === $this->edit_language) {
if (isset($post_content)) $meta[$content_field] = $post_content;
if (isset($post_excerpt)) $meta[$excerpt_field] = $post_excerpt;
if (isset($post_title)) $meta[$title_field] = $post_title;
if (isset($post_name)) $meta[$name_field] = $post_name;
}
} else {
if ($language !== $this->edit_language) {
if (isset($meta[$content_field])) $prepared_post->post_content = $meta[$content_field];
else unset($prepared_post->post_content);
if (isset($meta[$excerpt_field])) $prepared_post->post_excerpt = $meta[$excerpt_field];
else unset($prepared_post->post_excerpt);
if (isset($meta[$title_field])) $prepared_post->post_title = $meta[$title_field];
else unset($prepared_post->post_title);
if (isset($meta[$name_field])) $prepared_post->post_name = $meta[$name_field];
else unset($prepared_post->post_name);
}
// do not save main language in meta
unset($meta[$content_field]);
unset($meta[$excerpt_field]);
unset($meta[$title_field]);
unset($meta[$name_field]);
}
}
$request->set_param('meta', $meta);
return $prepared_post;
}
/**
* Filters the post data for a response (REST).
*
* @filter "rest_prepare_{$this->post_type}"
*
* @from 2.5
*/
public function rest_prepare_post($response, $post, $request) {
global $wpdb;
$meta = $request->get_param('meta');
$data = $response->get_data();
$post_content = $post->post_content;
$post_excerpt = $post->post_excerpt;
$post_title = $post->post_title;
$post_name = $post->post_name;
if (empty($this->edit_language)) {
$this->edit_language = $this->get_language();
}
foreach ($this->get_languages() as $language) {
$content_field = $this->get_prefix($language) . 'post_content';
$excerpt_field = $this->get_prefix($language) . 'post_excerpt';
$title_field = $this->get_prefix($language) . 'post_title';
$name_field = $this->get_prefix($language) . 'post_name';
if ($this->is_sub($language)) {
if ($language === $this->edit_language) {
$data['content']['raw'] = isset($meta[$content_field]) ? $meta[$content_field] : get_post_meta($post->ID, $content_field, true);
$data['content']['rendered'] = post_password_required( $post ) ? '' : apply_filters( 'the_content', $data['content']['raw'] );
$data['excerpt']['raw'] = isset($meta[$excerpt_field]) ? $meta[$excerpt_field] : get_post_meta($post->ID, $excerpt_field, true);
$data['excerpt']['rendered'] = apply_filters( 'the_excerpt', $data['excerpt']['raw'] );
$data['title']['raw'] = isset($meta[$title_field]) ? $meta[$title_field] : get_post_meta($post->ID, $title_field, true);
$data['title']['rendered'] = apply_filters( 'the_title', $data['title']['raw'], $post->ID );
$data['slug'] = isset($meta[$name_field]) ? $meta[$name_field] : get_post_meta($post->ID, $name_field, true);
}
} else {
if ($this->is_sub()) {
// get untranslated post
$post = $wpdb->get_row($wpdb->prepare("SELECT ID, post_content, post_excerpt, post_title, post_name FROM $wpdb->posts WHERE ID = %d", $post->ID));
}
$data['meta'][$content_field] = $post->post_content;
$data['meta'][$excerpt_field] = $post->post_excerpt;
$data['meta'][$title_field] = $post->post_title;
$data['meta'][$name_field] = $post->post_name;
}
}
if (preg_match('#'.$this->gutenberg_language_reg.'#', $data['content']['raw'], $matches)) {
$data['content']['raw'] = str_replace($matches[0], '', $data['content']['raw']);
}
$data['content']['raw'] .= sprintf($this->gutenberg_language_tag, $this->edit_language->post_name);
$response->set_data($data);
return $response;
}
/**
* Helper for parse_query(). Check if query is to be translated
*
* @param array $query_vars.
*
* @from 2.0
*/
public function is_query_translatable($query_vars) {
$post_type = isset($query_vars['post_type']) ? $query_vars['post_type'] : 'post';
if ($post_type === 'any') {
return true; // lets pretend it is...
} else if (is_string($post_type)) {
return $this->is_post_type_translatable($post_type);
} else if (is_array($post_type)) {
return array_filter($post_type, array($this, 'is_post_type_translatable')); // at least one is
}
}
/**
* Handle search query
*
* @hook for 'parse_query'
*
* @from 2.2
* @from 2.5, added 'sublanguage_search_meta' filter
*/
public function parse_query($wp_query) {
if ($this->is_query_translatable($wp_query->query_vars)) {
$language = isset($wp_query->query_vars['sublanguage']) ? $this->find_language($wp_query->query_vars['sublanguage']) : null;
if (isset($wp_query->query_vars['s']) && $wp_query->query_vars['s']) { // query_vars['s'] is empty string by default
$search_meta_fields = array();
if ($this->is_sub($language)) {
$search_meta_fields = array(
$this->get_prefix($language) . 'post_title',
$this->get_prefix($language) . 'post_content',
$this->get_prefix($language) . 'post_excerpt'
);
}
/**
* Filter meta keys searcheable for translation
*
* @from 2.5
*
* @param array $search_meta_fields. Array of custom field keys to search into
* @param WP_Query object $query
* @param Post object $language
* @param Sublanguage_current object $this
*/
$search_meta_fields = apply_filters('sublanguage_search_meta', $search_meta_fields, $wp_query, $language, $this);
if ($search_meta_fields) {
$wp_query->query_vars['sublanguage_search_meta'] = $search_meta_fields;
$wp_query->query_vars['sublanguage_search_alias'] = 'postmeta_search';
add_filter('posts_join_request', array($this, 'meta_search_join'), 10, 2);
add_filter('posts_search', array($this, 'meta_search'), 10, 2);
add_filter('posts_distinct_request', array($this, 'meta_search_distinct'), 10, 2);
}
}
/**
* Hook called on parsing a query that needs translation
*
* @from 2.0
*
* @param WP_Query object $query
* @param Sublanguage_current object $this
*/
do_action('sublanguage_parse_query', $wp_query, $language, $this);
}
}
/**
* Catch search query
*
* filter for 'posts_search'
*
* @from 2.0
*/
public function catch_search($search, $wp_query) {
if (!empty($wp_query->query_vars['sublanguage_search_deep'])) { // -> will search in original language as well... quite hacky!
$this->search_sql = $search;
add_filter('get_meta_sql', array($this, 'append_search_meta'));
}
return '';
}
/**
* Append translations data to search query
*
* filter for 'get_meta_sql'
*
* @from 2.0
*/
public function append_search_meta($sql) {
if (isset($this->search_sql)) {
$sql['where'] = " AND ((1=1 {$sql['where']}) OR (1=1 {$this->search_sql}))";
unset($this->search_sql);
}
return $sql;
}
/**
* translate posts
*
* @filter 'the_posts'
*
* @from 1.1
* @from 2.5 perform update_post_caches before translation
*/
public function translate_the_posts($posts, $wp_query) {
if (isset($wp_query->query_vars[$this->language_query_var])) {
if (!$wp_query->query_vars[$this->language_query_var]) { // false -> do not translate
// Documented below
return apply_filters('sublanguage_translate_the_posts', $posts, $posts, null, $wp_query, $this);
} else { // language slug or ID -> find language
$language = $this->find_language($wp_query->query_vars[$this->language_query_var]);
}
} else { // language not set -> use current
$language = null;
}
if ($this->is_sub($language) && $posts) {
// normally update_post_caches() is performed after 'the_posts' filter trigger.
update_post_caches($posts, $posts[0]->post_type, $wp_query->query_vars['update_post_term_cache'], true);
$wp_query->query_vars['cache_results'] = false; // prevent further caching
/**
* Filter the posts after translation
*
* @from 2.0
*
* @param array of object WP_Post $translated posts.
* @param array of object WP_Post $original posts.
* @param object WP_Post $language.
* @param object WP_Query $wp_query.
* @param object Sublanguage_core $this.
*/
$posts = apply_filters('sublanguage_translate_the_posts', $this->translate_posts($posts, $language), $posts, $language, $wp_query, $this);
}
return $posts;
}
/**
* translate posts
*
* @filter 'get_pages'
*
* @from 2.0
*/
public function translate_the_pages($posts, $r) {
if ($this->is_sub()) {
$posts = $this->translate_posts($posts);
}
return $posts;
}
/**
* Translate posts
*
* @from 2.0
*
* @param object WP_post $post
* @param object language
*/
public function translate_posts($posts, $language = null) {
$new_posts = array();
foreach ($posts as $post) {
/**
* Filter the post after translation
*
* @from 2.0
*
* @param object WP_Post $translated post.
* @param object WP_Post $original post.
* @param object WP_Post $language.
* @param object Sublanguage_core $this.
*/
$new_posts[] = apply_filters('sublanguage_translate_the_post', $this->translate_post($post, $language), $post, $language, $this);
}
return $new_posts;
}
/**
* Translate post. This function is overrided on front-end !
*
* @from 2.0
*
* @param object WP_post $post
* @param object language
*/
public function translate_post($post, $language = null) {
if (empty($language)) {
$language = $this->get_language();
}
if ($this->is_sub($language) && $this->is_post_type_translatable($post->post_type) && empty($post->sublanguage)) {
foreach ($this->fields as $field) {
$post->$field = $this->translate_post_field($post, $field, $language);
}
$post->sublanguage = true;
}
return $post;
}
/**
* Translate post meta. Public API
*
* filter for 'sublanguage_translate_post_meta'
*
* @param string $original Value to translate.
* @param object WP_Post $post Term object.
* @param string $meta_key Meta key name.
* @param bool $single Single meta value.
* @param object WP_Post $language Language. Optional.
* @param mixed language id, slug or anything
* @param string $by Field to search language by. Accepts 'post_name', 'post_title', 'post_content'. Optional.
*
* @from 2.0 changed params
* @from 1.1
*/
public function translate_custom_post_meta($original, $post, $meta_key, $single = false, $language = null, $by = null) {
$language = $this->find_language($language);
return $this->translate_post_meta($post, $meta_key, $single, $language, $original);
}
/**
* Append language slug to home url
* Filter for 'home_url'
*
* @from 1.0
*/
public function translate_home_url($url, $path, $orig_scheme, $blog_id) {
$language = $this->get_language();
if (!$this->disable_translate_home_url
&& $language
&& $this->get_option('default')
&& ($this->get_option('show_slug') || !$this->is_default())) {
if (get_option('permalink_structure')) {
$url = rtrim(substr($url, 0, strlen($url) - strlen($path)), '/') . '/' . $language->post_name . '/' . ltrim($path, '/');
} else {
$url = add_query_arg( array('language' => $language->post_name), $url);
}
}
return $url;
}
/**
* Translate title to current language
* Filter for 'the_title'
*
* @from 1.0
*/
public function translate_post_title($title, $id) {
$post = get_post($id);
if ($post && $this->is_sub() && $this->is_post_type_translatable($post->post_type) && empty($post->sublanguage)) {
$title = $this->translate_post_field($post, 'post_title', null, $title);
}
return $title;
}
/**
* Translate content to current language
* Filter for 'the_content'
*
* @from 1.0
* @from 2.5 check wether content corresponds to global post ('the_content' filter may be used outside of The Loop or when global $post does not match)
*/
public function translate_post_content($content) {
global $post;
if ($post && $post->post_content === $content && $this->is_sub() && $this->is_post_type_translatable($post->post_type) && empty($post->sublanguage)) {
$content = $this->translate_post_field($post, 'post_content', null, $content);
}
return $content;
}
/**
* Translate excerpt to current language (the_excerpt() and the_content() behave very differently!)
* @filter for 'get_the_excerpt'
*
* @from 1.0
* @from 2.5 Introduce $post parameter (@since wp 4.5)
*/
public function translate_post_excerpt($excerpt, $post = null) {
if ($post && $this->is_sub() && $this->is_post_type_translatable($post->post_type) && empty($post->sublanguage)) {
$excerpt = $this->translate_post_field($post, 'post_excerpt', null, $excerpt);
}
return $excerpt;
}
/**
* Translate page title in wp_title()
* Filter for 'single_post_title'
*
* @from 1.0
*/
public function translate_single_post_title($title, $post) {
if ($post && $this->is_sub() && $this->is_post_type_translatable($post->post_type) && empty($post->sublanguage)) {
$title = $this->translate_post_field($post, 'post_title', null, $title);
}
return $title;
}
/**
* Public API
* Filter for 'sublanguage_translate_post_field'
*
* @from 1.0
*
* @param string $original Original value to translate
* @param object WP_Post $post Post to translate field.
* @param string $field Field name. Accepts 'post_content', 'post_title', 'post_name', 'post_excerpt'
* @param mixed $language Language object, id or anything. Optional.
* @param string $by Field to search language by. Accepts 'post_name', 'post_title', 'post_content'. Optional.
* @return string
*/
public function translate_post_field_custom($original, $post, $field, $language = null, $by = null) {
if ($this->is_post_type_translatable($post->post_type)) {
$language = $this->find_language($language, $by);
return $this->translate_post_field($post, $field, $language, $original);
}
return $original;
}
/**
* Translate term field. Public API
* Filter for 'sublanguage_translate_term_field'
*
* @param string $original Original value
* @param object WP_Term $term Term object
* @param string $taxonomy Taxonomy
* @param string $field. Field to translate ('name', 'slug' or 'description')
* @param mixed $language Language object, id or anything. Optional.
* @param string $by Field to search language by. Accepts 'post_name', 'post_title', 'post_content'. Optional.
*
* @from 1.4.6
*/
public function translate_term_field_custom($original, $term, $taxonomy, $field, $language = null, $by = null) {
if ($this->is_taxonomy_translatable($taxonomy)) {
$language = $this->find_language($language, $by);
if ($language) {
return $this->translate_term_field($term, $taxonomy, $field, $language, $original);
}
}
return $original;
}
/**
* Public API
* Filter for 'sublanguage_has_post_translation'
*
* @from 2.5
*
* @param boolean $false Value to be filtered. Should be false.
* @param object WP_Post $post Post to translate field.
* @param string|mixed $field Field name. Accepts 'post_content', 'post_title', 'post_name', 'post_excerpt', an array of those, or null
* @param object WP_Post $language Language
*
* @return string
*/
public function filter_has_post_translation($false, $post = null, $field = null, $language = null) {
$language = $this->find_language($language);
return (bool) $post && $this->has_post_translation($post, $field, $language);
}
/**
* Translate term name
* Filter for 'list_cats'
*
* @from 1.0
*/
public function translate_term_name($name, $term = null) {
if (!isset($term)) { // looks like there are 2 differents list_cats filters
return $name;
}
return $this->translate_term_field($term, $term->taxonomy, 'name', null, $name);
}
/**
* Translate term name
* Filter for 'single_cat_title', single_tag_title, single_term_title
* In single_term_title()
*
* @from 1.0
*/
public function filter_single_term_title($title) {
$term = get_queried_object();
return $this->translate_term_field($term, $term->taxonomy, 'name', null, $title);
}
/**
* Filter post terms
*
* @filter 'get_the_terms'
*
* @from 1.0
*/
public function translate_post_terms($terms, $post_id, $taxonomy) {
if ($this->is_sub() && $this->is_taxonomy_translatable($taxonomy)) {
foreach ($terms as $term) {
$this->translate_term($term);
}
}
return $terms;
}
/**
* Hard translate term
* @filter 'get_term'
*
* @from 1.2
*/
public function translate_get_term($term, $taxonomy) {
if ($this->is_sub() && $this->is_taxonomy_translatable($taxonomy)) {
$this->translate_term($term);
}
return $term;
}
/**
* Filter post terms. Hard translate
*
* @filter 'get_terms'
*
* @from 1.1
*/
public function translate_get_terms($terms, $taxonomies, $args) {
if (isset($args[$this->language_query_var])) {
if (!$args[$this->language_query_var]) { // false -> do not translate
// Documented below
return apply_filters('sublanguage_translate_the_terms', $terms, $terms, null, $args, $this);
} else { // language slug or ID -> find language
$language = $this->find_language($args[$this->language_query_var]);
}
} else { // language not set -> use current
$language = null;
}
if ($this->is_sub($language)) {
if (isset($args['fields']) && $args['fields'] == 'names') { // -> Added in 1.4.4
$terms = array(); // -> restart query
unset($args['fields']);
$results = get_terms($taxonomies, $args);
foreach ($results as $term) {
$terms[] = $this->translate_term_field($term, $term->taxonomy, 'name');
}
return $terms;
}
if (empty($args['fields']) || $args['fields'] == 'all' || $args['fields'] == 'all_with_object_id') {
/**
* Filter the terms after translation
*
* @from 2.0
*
* @param array of object WP_Term $translated terms.
* @param array of object WP_Term $original terms.
* @param object WP_Post $language.
* @param object $args.
* @param object Sublanguage_core $this.
*/
$terms = apply_filters('sublanguage_translate_the_terms', $this->translate_terms($terms, $language), $terms, $language, $args, $this);
}
}
return $terms;