-
Notifications
You must be signed in to change notification settings - Fork 1
/
sitepress.class.php
5495 lines (4884 loc) · 279 KB
/
sitepress.class.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 SitePress{
private $settings;
private $active_languages = array();
private $this_lang;
private $wp_query;
private $admin_language = null;
function __construct(){
global $wpdb;
$this->settings = get_option('icl_sitepress_settings');
if(false != $this->settings){
$this->verify_settings();
}
if(isset($_GET['icl_action'])){
require_once ABSPATH . WPINC . '/pluggable.php';
if($_GET['icl_action']=='reminder_popup'){
add_action('init', array($this, 'reminders_popup'));
}
elseif($_GET['icl_action']=='dismiss_help'){
$this->settings['dont_show_help_admin_nonotice'] = true;
$this->save_settings();
}elseif($_GET['icl_action']=='dbdump'){
include_once ICL_PLUGIN_PATH . '/inc/functions-troubleshooting.php';
icl_troubleshooting_dumpdb();
exit;
}
}
if(isset($_GET['page']) && $_GET['page']== ICL_PLUGIN_FOLDER . '/menu/troubleshooting.php' && isset($_GET['debug_action'])){
ob_start();
}
if(isset($_REQUEST['icl_ajx_action'])){
add_action('init', array($this, 'ajax_setup'));
}
add_action('plugins_loaded', array($this,'init'), 1);
add_action('plugins_loaded', array($this,'initialize_cache'), 0);
// Administration menus
add_action('admin_menu', array($this, 'administration_menu'));
// Process post requests
if(!empty($_POST)){
add_action('init', array($this,'process_forms'));
}
add_action('init', array($this,'plugin_localization'));
if($this->settings['existing_content_language_verified']){
// Post/page language box
add_action('admin_head', array($this,'post_edit_language_options'));
// Post/page save actions
add_action('save_post', array($this,'save_post_actions'));
// Post/page delete actions
add_action('delete_post', array($this,'delete_post_actions'));
add_action('trash_post', array($this,'trash_post_actions'));
add_action('untrashed_post', array($this,'untrashed_post_actions'));
add_filter('posts_join', array($this,'posts_join_filter'));
add_filter('posts_where', array($this,'posts_where_filter'));
add_filter('comment_feed_join', array($this,'comment_feed_join'));
// show untranslated posts
if(!is_admin() && $this->settings['show_untranslated_blog_posts'] && $this->get_current_language() != $this->get_default_language()){
add_filter('the_posts', array($this, 'the_posts'));
}
$this->queries = array();
global $pagenow;
/* preWP3 compatibility - start */
if(ICL_PRE_WP3){
if($pagenow == 'edit.php'){
add_action('restrict_manage_posts', array($this,'language_filter'));
}elseif($pagenow == 'edit-pages.php'){
add_action('admin_footer', array($this,'language_filter'));
}
}else{
/* preWP3 compatibility - end */
if($pagenow == 'edit.php'){
add_action('admin_footer', array($this,'language_filter'));
//add_action('restrict_manage_posts', array($this,'language_filter'));
}
}
add_filter('get_pages', array($this, 'exclude_other_language_pages2'));
add_filter('wp_dropdown_pages', array($this, 'wp_dropdown_pages'));
// posts and pages links filters
add_filter('post_link', array($this, 'permalink_filter'),1,2);
add_filter('post_type_link', array($this, 'permalink_filter'),1,2);
add_filter('page_link', array($this, 'permalink_filter'),1,2);
add_filter('category_link', array($this, 'category_permalink_filter'),1,2);
add_filter('tag_link', array($this, 'tax_permalink_filter'),1,2);
add_filter('term_link', array($this, 'tax_permalink_filter'),1,2);
add_filter('get_comment_link', array($this, 'get_comment_link_filter'));
add_action('create_term', array($this, 'create_term'),1, 2);
add_action('edit_term', array($this, 'create_term'),1, 2);
add_action('delete_term', array($this, 'delete_term'),1,3);
// filters terms by language
add_filter('list_terms_exclusions', array($this, 'exclude_other_terms'),1,2);
// allow adding terms with the same name in different languages
add_filter("pre_term_name", array($this, 'pre_term_name'), 1, 2);
// allow adding categories with the same name in different languages
add_action('admin_init', array($this, 'pre_save_category'));
// category language selection
// add_action('edit_category', array($this, 'create_term'),1, 2);
/* preWP3 compatibility - start */
if(ICL_PRE_WP3 && $pagenow == 'categories.php'){
add_action('admin_print_scripts-categories.php', array($this,'js_scripts_categories'));
add_action('edit_category_form', array($this, 'edit_term_form'));
add_action('admin_footer', array($this,'terms_language_filter'));
}
/* preWP3 compatibility - end */
// tags language selection
if($pagenow == 'edit-tags.php'){
$taxonomy = isset($_GET['taxonomy']) ? $wpdb->escape($_GET['taxonomy']) : 'post_tag';
if($this->is_translated_taxonomy($taxonomy)){
add_action('admin_print_scripts-edit-tags.php', array($this,'js_scripts_tags'));
if($taxonomy == 'category'){
add_action('edit_category_form', array($this, 'edit_term_form'));
}else{
add_action('add_tag_form', array($this, 'edit_term_form'));
add_action('edit_tag_form', array($this, 'edit_term_form'));
}
add_action('admin_footer', array($this,'terms_language_filter'));
add_filter('wp_dropdown_cats', array($this, 'wp_dropdown_cats_select_parent'));
}
}
// custom hook for adding the language selector to the template
add_action('icl_language_selector', array($this, 'language_selector'));
// front end js
add_action('wp_head', array($this, 'front_end_js'));
add_action('wp_head', array($this,'rtl_fix'));
add_action('admin_print_styles', array($this,'rtl_fix'));
add_action('restrict_manage_posts', array($this, 'restrict_manage_posts'));
/* preWP3 compatibility - start */
if(ICL_PRE_WP3){
add_action('admin_print_scripts-edit-pages.php', array($this,'restrict_manage_pages'));
}
/* preWP3 compatibility - endif */
add_filter('get_edit_post_link', array($this, 'get_edit_post_link'), 1, 3);
// short circuit get default category
add_filter('pre_option_default_category', array($this, 'pre_option_default_category'));
add_filter('update_option_default_category', array($this, 'update_option_default_category'), 1, 2);
add_filter('the_category', array($this,'the_category_name_filter'));
add_filter('get_terms', array($this,'get_terms_filter'));
add_filter('single_cat_title', array($this,'the_category_name_filter'));
add_filter('term_links-category', array($this,'the_category_name_filter'));
add_filter('term_links-post_tag', array($this,'the_category_name_filter'));
add_filter('tags_to_edit', array($this,'the_category_name_filter'));
add_filter('single_tag_title', array($this,'the_category_name_filter'));
// adiacent posts links
add_filter('get_previous_post_join', array($this,'get_adiacent_post_join'));
add_filter('get_next_post_join', array($this,'get_adiacent_post_join'));
add_filter('get_previous_post_where', array($this,'get_adiacent_post_where'));
add_filter('get_next_post_where', array($this,'get_adiacent_post_where'));
// feeds links
add_filter('feed_link', array($this,'feed_link'));
// commenting links
add_filter('post_comments_feed_link', array($this,'post_comments_feed_link'));
add_filter('trackback_url', array($this,'trackback_url'));
add_filter('user_trailingslashit', array($this,'user_trailingslashit'),1, 2);
// date based archives
add_filter('year_link', array($this,'archives_link'));
add_filter('month_link', array($this,'archives_link'));
add_filter('day_link', array($this,'archives_link'));
add_filter('getarchives_join', array($this,'getarchives_join'));
add_filter('getarchives_where', array($this,'getarchives_where'));
add_filter('pre_option_home', array($this,'pre_option_home'));
/* preWP3 compatibility - start */
if(ICL_PRE_WP3){
// author template
add_filter('author_link', array($this,'convert_url'));
}
/* preWP3 compatibility - end */
add_filter('author_link', array($this,'author_link'));
add_filter('home_url', array($this, 'home_url'), 1, 4) ;
// language negotiation
add_action('query_vars', array($this,'query_vars'));
//
add_filter('language_attributes', array($this, 'language_attributes'));
add_action('locale', array($this, 'locale'));
if(isset($_GET['____icl_validate_domain'])){ echo '<!--'.get_option('home').'-->'; exit; }
add_filter('pre_option_page_on_front', array($this,'pre_option_page_on_front'));
add_filter('pre_option_page_for_posts', array($this,'pre_option_page_for_posts'));
add_filter('option_sticky_posts', array($this,'option_sticky_posts'));
add_filter('request', array($this,'request_filter'));
add_action('wp_head', array($this,'set_wp_query'));
add_action('show_user_profile', array($this, 'show_user_options'));
add_action('personal_options_update', array($this, 'save_user_options'));
// column with links to translations (or add translation) - low priority
add_action('init', array($this,'configure_custom_column'), 15);
// adjust queried categories and tags ids according to the language
if($this->settings['auto_adjust_ids']){
add_action('parse_query', array($this, 'parse_query'));
add_action('wp_list_pages_excludes', array($this, 'adjust_wp_list_pages_excludes'));
if(!is_admin()){
add_filter('get_term', array($this,'get_term_adjust_id'), 1, 1);
add_filter('category_link', array($this,'category_link_adjust_id'), 1, 2);
add_filter('get_terms', array($this,'get_terms_adjust_ids'), 1, 3);
add_filter('get_pages', array($this,'get_pages_adjust_ids'), 1, 2);
}
}
if(!is_admin()){
add_action('wp_head', array($this, 'meta_generator_tag'));
}
if(!ICL_PRE_WP3){
require_once ICL_PLUGIN_PATH . '/inc/wp-nav-menus/iclNavMenu.class.php';
$iclNavMenu = new iclNavMenu;
}
if(is_admin() || defined('XMLRPC_REQUEST')){
global $iclTranslationManagement;
$iclTranslationManagement = new TranslationManagement;
}
} //end if the initial language is set - existing_content_language_verified
add_action('wp_dashboard_setup', array($this, 'dashboard_widget_setup'));
if(isset($pagenow) && is_admin() && $pagenow == 'index.php'){
add_action('icl_dashboard_widget_notices', array($this, 'print_translatable_custom_content_status'));
if(trim(get_option('permalink_structure'), '/') == '%postname%'){
add_action('icl_dashboard_widget_notices', array($this, 'warn_permalink_structure'));
}
}
if(isset($pagenow) && $pagenow == 'options-permalink.php' && trim(get_option('permalink_structure'), '/') == '%postname%'){
add_action('admin_notices', array($this, 'warn_permalink_structure'));
}
}
function init(){
global $wpdb;
$this->set_admin_language();
//configure callbacks for plugin menu pages
if(defined('WP_ADMIN') && isset($_GET['page']) && 0 === strpos($_GET['page'],basename(ICL_PLUGIN_PATH).'/')){
add_action('icl_menu_footer', array($this, 'menu_footer'));
}
if($this->settings['existing_content_language_verified']){
if(defined('WP_ADMIN')){
if(isset($_GET['lang'])){
$this->this_lang = rtrim($_GET['lang'],'/');
}else{
$this->this_lang = $this->get_default_language();
}
}else{
$al = $this->get_active_languages();
foreach($al as $l){
$active_languages[] = $l['code'];
}
$active_languages[] = 'all';
$s = $_SERVER['HTTPS']=='on'?'s':'';
$request = 'http' . $s . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$home = get_option('home');
$url_parts = parse_url($home);
$blog_path = $url_parts['path']?$url_parts['path']:'';
switch($this->settings['language_negotiation_type']){
case 1:
$path = str_replace($home,'',$request);
$parts = explode('?', $path);
$path = $parts[0];
$exp = explode('/',trim($path,'/'));
if(in_array($exp[0], $active_languages)){
$this->this_lang = $exp[0];
// before hijiking the SERVER[REQUEST_URI]
// override the canonical_redirect action
// keep a copy of the original request uri
remove_action('template_redirect', 'redirect_canonical');
global $_icl_server_request_uri;
$_icl_server_request_uri = $_SERVER['REQUEST_URI'];
add_action('template_redirect', 'icl_redirect_canonical_wrapper', 11);
function icl_redirect_canonical_wrapper(){
global $_icl_server_request_uri, $wp_query;
$requested_url = ( !empty($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
$requested_url .= $_SERVER['HTTP_HOST'];
$requested_url .= $_icl_server_request_uri;
redirect_canonical($requested_url);
/*
if($wp_query->query_vars['error'] == '404'){
$wp_query->is_404 = true;
$template = get_404_template();
include($template);
exit;
}
*/
}
//
//deal with situations when template files need to be called directly
add_action('template_redirect', array($this, '_allow_calling_template_file_directly'));
//$_SERVER['REQUEST_URI'] = preg_replace('@^'. $blog_path . '/' . $this->this_lang.'@i', $blog_path ,$_SERVER['REQUEST_URI']);
// Check for special case of www.example.com/fr where the / is missing on the end
$parts = parse_url($_SERVER['REQUEST_URI']);
if(strlen($parts['path']) == 0){
$_SERVER['REQUEST_URI'] = '/' . $_SERVER['REQUEST_URI'];
}
}else{
$this->this_lang = $this->get_default_language();
}
break;
case 2:
$exp = explode('.', $_SERVER['HTTP_HOST']);
$__l = array_search('http' . $s . '://' . $_SERVER['HTTP_HOST'] . $blog_path, $this->settings['language_domains']);
$this->this_lang = $__l?$__l:$this->get_default_language();
if(defined('ICL_USE_MULTIPLE_DOMAIN_LOGIN') && ICL_USE_MULTIPLE_DOMAIN_LOGIN){
include ICL_PLUGIN_PATH . '/modules/multiple-domains-login.php';
}
break;
case 3:
default:
if(isset($_GET['lang'])){
$this->this_lang = preg_replace("/[^0-9a-zA-Z-]/i", '',$_GET['lang']);
// set the language based on the content id - for short links
}elseif(isset($_GET['page_id'])){
$this->this_lang = $wpdb->get_var($wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_type='post_page' AND element_id=%d", $_GET['page_id']));
}elseif(isset($_GET['p'])){
$post_type = $wpdb->get_var($wpdb->prepare("SELECT post_type FROM {$wpdb->posts} WHERE ID=%d", $_GET['p']));
$this->this_lang = $wpdb->get_var($wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d",
'post_' . $post_type, $_GET['p']));
}elseif(isset($_GET['cat_ID'])){
$cat_tax_id = $wpdb->get_var($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy=%s",
$_GET['cat_ID'], 'category'));
$this->this_lang = $wpdb->get_var($wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations
WHERE element_type='tax_category' AND element_id=%d", $cat_tax_id));
}elseif(isset($_GET['tag'])){
$tag_tax_id = $wpdb->get_var($wpdb->prepare("
SELECT x.term_taxonomy_id FROM {$wpdb->term_taxonomy} x JOIN {$wpdb->terms} t ON t.term_id = x.term_id
WHERE t.slug='%s' AND x.taxonomy='%s'",
$_GET['tag'], 'post_tag'));
$this->this_lang = $wpdb->get_var($wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations
WHERE element_type='tax_post_tag' AND element_id=%d", $tag_tax_id));
}
//
if(!isset($_GET['lang']) && ($this->this_lang != $this->get_default_language())){
if(!isset($GLOBALS['wp_rewrite'])){
require_once ABSPATH . WPINC . '/rewrite.php';
$GLOBALS['wp_rewrite'] = new WP_Rewrite();
}
define('ICL_DOING_REDIRECT', true);
if(isset($_GET['page_id'])){
wp_redirect(get_page_link($_GET['page_id']), '301');
exit;
}elseif(isset($_GET['p'])){
wp_redirect(get_permalink($_GET['p']), '301');
exit;
}elseif(isset($_GET['cat_ID'])){
wp_redirect(get_term_link( intval($_GET['cat_ID']), 'category' ));
exit;
}elseif(isset($_GET['tag'])){
wp_redirect(get_term_link( $_GET['tag'], 'post_tag' ));
exit;
}else{
global $wp_taxonomies;
$taxs = array_keys((array)$this->settings['taxonomies_sync_option']);
foreach($taxs as $t){
if(isset($_GET[$t])){
$term_obj = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON t.term_id = x.term_id
WHERE t.slug=%s AND x.taxonomy=%s"
, $_GET[$t], $t));
$term_link = get_term_link( $term_obj, $t );
$term_link = str_replace('&', '&', $term_link); // fix
if($term_link && !is_wp_error($term_link)){
wp_redirect($term_link);
exit;
}
}
}
}
}
if(empty($this->this_lang)){
$this->this_lang = $this->get_default_language();
}
}
// allow forcing the current language when it can't be decoded from the URL
$this->this_lang = apply_filters('icl_set_current_language', $this->this_lang);
}
//reorder active language to put 'this_lang' in front
foreach($this->active_languages as $k=>$al){
if($al['code']==$this->this_lang){
unset($this->active_languages[$k]);
$this->active_languages = array_merge(array($k=>$al), $this->active_languages);
}
}
//if($this->settings['language_negotiation_type']==3){
// fix pagenum links for when using the language as a parameter
// add_filter('get_pagenum_link', array($this,'get_pagenum_link_filter'));
//}
// filter some queries
add_filter('query', array($this, 'filter_queries'));
if( $this->settings['language_negotiation_type']==1 && $this->get_current_language()!=$this->get_default_language()){
add_filter('option_rewrite_rules', array($this, 'rewrite_rules_filter'));
if(version_compare($GLOBALS['wp_version'], '2.8.4', '<=')){
add_filter('transient_rewrite_rules', array($this, 'rewrite_rules_filter'));
}
}
$this->set_language_cookie();
}
if(empty($this->settings['dont_show_help_admin_notice'])){
if(count($this->get_active_languages()) < 2){
add_action('admin_notices', array($this, 'help_admin_notice'));
}
}
$short_v = implode('.', array_slice(explode('.', ICL_SITEPRESS_VERSION), 0, 3));
if($this->settings['hide_upgrade_notice'] != $short_v){
add_action('admin_notices', array($this, 'upgrade_notice'));
}
if ($this->icl_account_configured()) {
add_action('admin_notices', array($this, 'icl_reminders'));
}
require ICL_PLUGIN_PATH . '/inc/template-constants.php';
if(defined('WPML_LOAD_API_SUPPORT')){
require ICL_PLUGIN_PATH . '/inc/wpml-api.php';
}
add_action('wp_footer', array($this, 'display_wpml_footer'),20);
if(defined('XMLRPC_REQUEST') && XMLRPC_REQUEST){
add_action('xmlrpc_call', array($this, 'xmlrpc_call_actions'));
add_filter('xmlrpc_methods',array($this, 'xmlrpc_methods'));
}
}
function ajax_setup(){
require ICL_PLUGIN_PATH . '/ajax.php';
}
function configure_custom_column(){
global $pagenow, $wp_post_types;
/* preWP3 compatibility - start */
if(ICL_PRE_WP3 && $pagenow == 'edit-pages.php'){
$pagenow_ = $pagenow;
$_REQUEST['post_type'] = 'page';
}
/* preWP3 compatibility - end */
if(($pagenow == 'edit.php' || $pagenow_ == 'edit-pages.php' || ($pagenow == 'admin-ajax.php' && $_POST['action']=='inline-save'))
&& !$this->settings['hide_translation_controls_on_posts_lists']){
$post_type = isset($_REQUEST['post_type']) ? $_REQUEST['post_type'] : 'post';
switch($post_type){
case 'post': case 'page':
add_filter('manage_'.$post_type.'s_columns',array($this,'add_posts_management_column'));
if(ICL_PRE_WP3 && $pagenow == 'edit-pages.php' || version_compare($GLOBALS['wp_version'], '3.1', '<') && $_GET['post_type']=='page'){
add_action('manage_'.$post_type.'s_custom_column',array($this,'add_content_for_posts_management_column'));
}
add_action('manage_posts_custom_column',array($this,'add_content_for_posts_management_column'));
break;
default:
if($this->settings['custom_posts_sync_option'][$post_type] == 1){
add_filter('manage_'.$post_type.'_posts_columns',array($this,'add_posts_management_column'));
if($wp_post_types[$post_type]->hierarchical){
add_action('manage_pages_custom_column',array($this,'add_content_for_posts_management_column'));
add_action('manage_posts_custom_column',array($this,'add_content_for_posts_management_column')); // add this too - for more types plugin
}else{
add_action('manage_posts_custom_column',array($this,'add_content_for_posts_management_column'));
}
}
}
add_action('admin_print_scripts', array($this, '__set_posts_management_column_width'));
}
}
function the_posts($posts){
global $wpdb, $wp_query;
$db = debug_backtrace();
$custom_wp_query = $db[3]['object'];
//exceptions
if(
($this->get_current_language() == $this->get_default_language()) // original language
|| ($wp_query != $custom_wp_query) // called by a custom query
|| (!$custom_wp_query->is_posts_page && !$custom_wp_query->is_home) // not the blog posts page
|| $wp_query->is_singular //is singular
|| !empty($custom_wp_query->query_vars['category__not_in'])
//|| !empty($custom_wp_query->query_vars['category__in'])
//|| !empty($custom_wp_query->query_vars['category__and'])
|| !empty($custom_wp_query->query_vars['tag__not_in'])
|| !empty($custom_wp_query->query_vars['post__in'])
|| !empty($custom_wp_query->query_vars['post__not_in'])
|| !empty($custom_wp_query->query_vars['post_parent'])
){
//$wp_query->query_vars = $this->wp_query->query_vars;
return $posts;
}
// get the posts in the default language instead
$this_lang = $this->this_lang;
$this->this_lang = $this->get_default_language();
remove_filter('the_posts', array($this, 'the_posts'));
$custom_wp_query->query_vars['suppress_filters'] = 0;
if(isset($custom_wp_query->query_vars['pagename']) && !empty($custom_wp_query->query_vars['pagename'])){
if (isset($custom_wp_query->queried_object_id) && !empty($custom_wp_query->queried_object_id)) {
$page_id = $custom_wp_query->queried_object_id;
} else {
// urlencode added for languages that have urlencoded post_name field value
$custom_wp_query->query_vars['pagename'] = urlencode($custom_wp_query->query_vars['pagename']);
$page_id = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_name='{$custom_wp_query->query_vars['pagename']}' AND post_type='page'");
}
if($page_id){
$tr_page_id = icl_object_id($page_id, 'page', false, $this->get_default_language());
if($tr_page_id){
$custom_wp_query->query_vars['pagename'] = $wpdb->get_var("SELECT post_name FROM {$wpdb->posts} WHERE ID={$tr_page_id}");
}
}
}
// look for posts without translations
if($posts){
foreach($posts as $p){
$pids[] = $p->ID;
}
$trids = $wpdb->get_col("
SELECT trid
FROM {$wpdb->prefix}icl_translations
WHERE element_type='post_post' AND element_id IN (".join(',', $pids).") AND language_code = '".$this_lang."'");
$posts_not_translated = $wpdb->get_col("
SELECT element_id, COUNT(language_code) AS c
FROM {$wpdb->prefix}icl_translations
WHERE trid IN (".join(',', $trids).") GROUP BY trid HAVING c = 1
");
if($posts_not_translated){
$GLOBALS['__icl_the_posts_posts_not_translated'] = $posts_not_translated;
add_filter('posts_where', array($this, '_posts_untranslated_extra_posts_where'), 99);
}
}
//fix page for posts
unset($custom_wp_query->query_vars['page_id']); unset($custom_wp_query->query_vars['p']);
$my_query = new WP_Query($custom_wp_query->query_vars);
add_filter('the_posts', array($this, 'the_posts'));
$this->this_lang = $this_lang;
// create a map of the translated posts
foreach($posts as $post){
$trans_posts[$post->ID] = $post;
}
// loop original posts
foreach($my_query->posts as $k=>$post){ // loop posts in the default language
$trid = $this->get_element_trid($post->ID);
$translations = $this->get_element_translations($trid); // get translations
if(isset($translations[$this->get_current_language()])){ // if there is a translation in the current language
if(isset($trans_posts[$translations[$this->get_current_language()]->element_id])){ //check the map of translated posts
$my_query->posts[$k] = $trans_posts[$translations[$this->get_current_language()]->element_id];
}else{ // check if the translated post exists in the database still
$_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d AND post_status='publish' LIMIT 1", $translations[$this->get_current_language()]->element_id));
if(!empty($_post)){
$_post = sanitize_post($_post);
$my_query->posts[$k] = $_post;
}else{
$my_query->posts[$k]->original_language = true;
}
}
}else{
$my_query->posts[$k]->original_language = true;
}
}
if($custom_wp_query == $wp_query){
$wp_query->max_num_pages = $my_query->max_num_pages;
}
$posts = $my_query->posts;
unset($GLOBALS['__icl_the_posts_posts_not_translated']);
remove_filter('posts_where', array($this, '_posts_untranslated_extra_posts_where'), 99);
return $posts;
}
function _posts_untranslated_extra_posts_where($where){
global $wpdb;
$where .= ' OR ' . $wpdb->posts . '.ID IN (' . join(',', $GLOBALS['__icl_the_posts_posts_not_translated']) . ')';
return $where;
}
function initialize_cache(){
require_once ICL_PLUGIN_PATH . '/inc/cache.php';
$this->icl_translations_cache = new icl_cache();
$this->icl_locale_cache = new icl_cache('locale', true);
$this->icl_flag_cache = new icl_cache('flags', true);
$this->icl_language_name_cache = new icl_cache('language_name', true);
$this->icl_term_taxonomy_cache = new icl_cache();
$this->icl_cms_nav_offsite_url_cache = new icl_cache('cms_nav_offsite_url', true);
}
function set_admin_language(){
global $wpdb, $current_user;
if(is_null($current_user) && function_exists('wp_get_current_user')){
$u = wp_get_current_user();
if($u->ID > 0){
$current_user = $u;
}
}
$active_languages = array_keys($wpdb->get_col("SELECT code FROM {$wpdb->prefix}icl_languages WHERE active=1")); //don't use method get_active_language()
$this->admin_language = $this->get_user_admin_language($current_user->data->ID);
if($this->admin_language != '' && !in_array($this->admin_language, $active_languages)){
delete_usermeta($current_user->data->ID,'icl_admin_language');
}
if(!in_array($this->settings['admin_default_language'], $active_languages) || is_null($this->settings['admin_default_language'])){
$this->settings['admin_default_language'] = '_default_';
$this->save_settings();
}
if(!$this->admin_language){
$this->admin_language = $this->settings['admin_default_language'];
}
if($this->admin_language == '_default_' && $this->get_default_language()){
$this->admin_language = $this->get_default_language();
}
}
function get_admin_language(){
return $this->admin_language;
}
function get_user_admin_language($user_id) {
static $lang = null;
if ($lang === null) {
$lang = get_user_meta($user_id,'icl_admin_language',true);
}
return $lang;
}
function administration_menu(){
add_action('admin_print_scripts', array($this,'js_scripts_setup'));
add_action('admin_print_styles', array($this,'css_setup'));
if (1 < count($this->get_active_languages())) {
$main_page = 'translation-management.php';
add_menu_page(__('WPML','sitepress'), __('WPML','sitepress'), 'manage_options', basename(ICL_PLUGIN_PATH).'/menu/translation-management.php',null, ICL_PLUGIN_URL . '/res/img/icon16.png');
add_submenu_page(basename(ICL_PLUGIN_PATH).'/menu/' . $main_page, __('Translation Management','sitepress'), __('Translation Management','sitepress'),
'manage_options', basename(ICL_PLUGIN_PATH).'/menu/translation-management.php');
add_submenu_page(basename(ICL_PLUGIN_PATH).'/menu/' . $main_page, __('Languages','sitepress'), __('Languages','sitepress'),
'manage_options', basename(ICL_PLUGIN_PATH).'/menu/languages.php');
add_submenu_page(basename(ICL_PLUGIN_PATH).'/menu/' . $main_page, __('Theme and plugins localization','sitepress'), __('Theme and plugins localization','sitepress'),
'manage_options', basename(ICL_PLUGIN_PATH).'/menu/theme-localization.php');
icl_st_administration_menu();
add_submenu_page(basename(ICL_PLUGIN_PATH).'/menu/' . $main_page, __('Comments translation','sitepress'), __('Comments translation','sitepress'),
'manage_options', basename(ICL_PLUGIN_PATH).'/menu/comments-translation.php');
} else {
$main_page = 'languages.php';
add_menu_page(__('WPML','sitepress'), __('WPML','sitepress'), 'manage_options', basename(ICL_PLUGIN_PATH).'/menu/languages.php',null, ICL_PLUGIN_URL . '/res/img/icon16.png');
add_submenu_page(basename(ICL_PLUGIN_PATH).'/menu/' . $main_page, __('Languages','sitepress'), __('Languages','sitepress'),
'manage_options', basename(ICL_PLUGIN_PATH).'/menu/languages.php');
}
if($this->settings['modules']['cms-navigation']['enabled']){
add_submenu_page(basename(ICL_PLUGIN_PATH).'/menu/' . $main_page, __('Navigation','sitepress'), __('Navigation','sitepress'),
'manage_options', basename(ICL_PLUGIN_PATH).'/menu/navigation.php');
}
if($this->settings['modules']['absolute-links']['enabled']){
add_submenu_page(basename(ICL_PLUGIN_PATH).'/menu/' . $main_page, __('Sticky links','sitepress'), __('Sticky links','sitepress'),
'manage_options', basename(ICL_PLUGIN_PATH).'/menu/absolute-links.php');
}
if(isset($_GET['page']) && $_GET['page'] == basename(ICL_PLUGIN_PATH).'/menu/troubleshooting.php'){
add_submenu_page(basename(ICL_PLUGIN_PATH).'/menu/' . $main_page, __('Troubleshooting','sitepress'), __('Troubleshooting','sitepress'),
'manage_options', basename(ICL_PLUGIN_PATH).'/menu/troubleshooting.php');
}
$alert = ' <img width="12" height="12" style="margin-bottom:-2px;" src="'.ICL_PLUGIN_URL.'/res/img/alert.png" />';
add_submenu_page(basename(ICL_PLUGIN_PATH).'/menu/' . $main_page, __('Compatibility packages','sitepress'), __('Compatibility packages','sitepress').$alert,
'manage_options', basename(ICL_PLUGIN_PATH).'/menu/compatibility-packages.php');
add_submenu_page(basename(ICL_PLUGIN_PATH).'/menu/' . $main_page, __('Support','sitepress'), __('Support','sitepress'), 'manage_options', basename(ICL_PLUGIN_PATH).'/menu/support.php');
}
function save_settings($settings=null){
if(!is_null($settings)){
foreach($settings as $k=>$v){
if(is_array($v)){
foreach($v as $k2=>$v2){
$this->settings[$k][$k2] = $v2;
}
}else{
$this->settings[$k] = $v;
}
}
}
update_option('icl_sitepress_settings', $this->settings);
do_action('icl_save_settings', $settings);
}
function get_settings(){
return $this->settings;
}
function verify_settings(){
$default_settings = array(
'interview_translators' => 1,
'existing_content_language_verified' => 0,
'language_negotiation_type' => 3,
'icl_lso_header' => 0,
'icl_lso_link_empty' => 0,
'icl_lso_flags' => 0,
'icl_lso_native_lang' => 1,
'icl_lso_display_lang' => 1,
'sync_page_ordering' => 1,
'sync_page_parent' => 1,
'sync_page_template' => 1,
'sync_ping_status' => 1,
'sync_comment_status' => 1,
'sync_sticky_flag' => 1,
'sync_private_flag' => 1,
'sync_delete' => 0,
'translation_pickup_method' => 0,
'notify_complete' => 1,
'translated_document_status' => 1,
'remote_management' => 0,
'auto_adjust_ids' => 1,
'alert_delay' => 0,
'modules' => array(
'absolute-links' => array('enabled'=>0, 'sticky_links_widgets'=>0, 'sticky_links_strings'=>0),
'cms-navigation'=>array('enabled'=>0, 'breadcrumbs_separator'=>' » ')
),
'promote_wpml' => 1,
'troubleshooting_options' => array('http_communication' => 1)
);
//congigured for three levels
$update_settings = false;
foreach($default_settings as $key => $value){
if(is_array($value)){
foreach($value as $k2 => $v2){
if(is_array($v2)){
foreach($v2 as $k3 => $v3){
if(!isset($this->settings[$key][$k2][$k3])){
$this->settings[$key][$k2][$k3] = $v3;
$update_settings = true;
}
}
}else{
if(!isset($this->settings[$key][$k2])){
$this->settings[$key][$k2] = $v2;
$update_settings = true;
}
}
}
}else{
if(!isset($this->settings[$key])){
$this->settings[$key] = $value;
$update_settings = true;
}
}
}
if($update_settings){
$this->save_settings();
}
}
function _validate_language_per_directory($language_code){
if(!class_exists('WP_Http')) include_once ABSPATH . WPINC . '/class-http.php';
$client = new WP_Http();
if(false === strpos($_POST['url'],'?')){$url_glue='?';}else{$url_glue='&';}
$response = $client->request(get_option('home') . '/' . $language_code .'/' . $url_glue . '____icl_validate_domain=1', array('timeout'=>15, 'decompress'=>false));
return (!is_wp_error($response) && ($response['response']['code']=='200') && ($response['body'] == '<!--'.get_option('home').'-->'));
}
function save_language_pairs() {
// clear existing languages
$lang_pairs = $this->settings['language_pairs'];
if (is_array($lang_pairs)) {
foreach ($lang_pairs as $from => $to) {
$lang_pairs[$from] = array();
}
}
// get the from languages
$from_languages = array();
foreach($_POST as $k=>$v){
if(0 === strpos($k,'icl_lng_from_')){
$f = str_replace('icl_lng_from_','',$k);
$from_languages[] = $f;
}
}
foreach($_POST as $k=>$v){
if(0 !== strpos($k,'icl_lng_')) continue;
if(0 === strpos($k,'icl_lng_to')){
$t = str_replace('icl_lng_to_','',$k);
$exp = explode('_',$t);
if (in_array($exp[0], $from_languages)){
$lang_pairs[$exp[0]][$exp[1]] = 1;
}
}
}
$iclsettings['language_pairs'] = $lang_pairs;
$this->save_settings($iclsettings);
}
function get_active_languages($refresh = false){
global $wpdb;
if($refresh || !$this->active_languages){
if(defined('WP_ADMIN') && $this->admin_language){
$in_language = $this->admin_language;
}else{
$in_language = $this->get_current_language()?$this->get_current_language():$this->get_default_language();
}
if (isset($this->icl_language_name_cache)) {
$res = $this->icl_language_name_cache->get('in_language_'.$in_language);
} else {
$res = null;
}
if (!$res) {
$res = $wpdb->get_results("
SELECT l.id, code, english_name, active, lt.name AS display_name
FROM {$wpdb->prefix}icl_languages l
JOIN {$wpdb->prefix}icl_languages_translations lt ON l.code=lt.language_code
WHERE
active=1 AND lt.display_language_code = '{$in_language}'
ORDER BY major DESC, english_name ASC", ARRAY_A);
if (isset($this->icl_language_name_cache)) {
$this->icl_language_name_cache->set('in_language_'.$in_language, $res);
}
}
$languages = array();
if($res){
foreach($res as $r){
$languages[$r['code']] = $r;
}
}
if (isset($this->icl_language_name_cache)) {
$res = $this->icl_language_name_cache->get('languages_'.$languages);
} else {
$res = null;
}
if (!$res) {
$res = $wpdb->get_results("
SELECT language_code, name
FROM {$wpdb->prefix}icl_languages_translations
WHERE language_code IN ('".join("','",array_keys($languages))."') AND language_code = display_language_code
");
if (isset($this->icl_language_name_cache)) {
$this->icl_language_name_cache->set('languages_'.$languages, $res);
}
}
foreach($res as $row){
$languages[$row->language_code]['native_name'] = $row->name;
}
$this->active_languages = $languages;
}
// hide languages for front end
global $current_user;
if(!is_admin() && !empty($this->settings['hidden_languages'])
&& is_array($this->settings['hidden_languages']) && !get_user_meta($current_user->data->ID, 'icl_show_hidden_languages', true)){
foreach($this->settings['hidden_languages'] as $l){
unset($this->active_languages[$l]);
}
}
return $this->active_languages;
}
function set_active_languages($arr){
global $wpdb;
if(!empty($arr)){
foreach($arr as $code){
$tmp[] = mysql_real_escape_string(trim($code));
}
// set the locale
$current_active_languages = (array)$wpdb->get_col("SELECT code FROM {$wpdb->prefix}icl_languages WHERE active = 1");
$new_languages = array_diff($tmp, $current_active_languages);
if(!empty($new_languages)){
foreach($new_languages as $code){
$default_locale = $wpdb->get_var("SELECT default_locale FROM {$wpdb->prefix}icl_languages WHERE code='{$code}'");
if($default_locale){
if($wpdb->get_var("SELECT code FROM {$wpdb->prefix}icl_locale_map WHERE code='{$code}'")){
$wpdb->update($wpdb->prefix.'icl_locale_map', array('locale'=>$default_locale), array('code'=>$code));
}else{
$wpdb->insert($wpdb->prefix.'icl_locale_map', array('code'=>$code, 'locale'=>$default_locale));
}
}
}
}
$codes = '(\'' . join('\',\'',$tmp) . '\')';
$wpdb->update($wpdb->prefix.'icl_languages', array('active'=>0), array('active'=>'1'));
$wpdb->query("UPDATE {$wpdb->prefix}icl_languages SET active=1 WHERE code IN {$codes}");
$this->icl_language_name_cache->clear();
}
$res = $wpdb->get_results("
SELECT code, english_name, active, lt.name AS display_name
FROM {$wpdb->prefix}icl_languages l
JOIN {$wpdb->prefix}icl_languages_translations lt ON l.code=lt.language_code
WHERE
active=1 AND lt.display_language_code = '{$this->get_default_language()}'
ORDER BY major DESC, english_name ASC", ARRAY_A);
$languages = array();
foreach($res as $r){
$languages[] = $r;
}
$this->active_languages = $languages;
return true;
}
function get_languages($lang=false){
global $wpdb;
if(!$lang){
$lang = $this->get_default_language();
}
$res = $wpdb->get_results("