forked from alexandergross/sitepress-multilingual-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitepress.class.php
executable file
·4368 lines (3692 loc) · 159 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
/**
* Main SitePress Class
*
* @package wpml-core
*/
class SitePress extends WPML_WPDB_User implements
IWPML_Current_Language,
IWPML_Taxonomy_State
{
/** @var WPML_Taxonomy_Translation */
private $taxonomy_translation;
private $template_real_path;
/** @var WPML_Post_Translation $post_translation */
private $post_translation;
/** @var WPML_Terms_Translations $term_translation */
private $term_translation;
/** @var WPML_Post_Duplication $post_duplication */
private $post_duplication;
/** @var WPML_Term_Actions $term_actions */
private $term_actions;
/** @var WPML_Admin_Scripts_Setup $scripts_handler */
private $scripts_handler;
/** @var WPML_Set_Language $language_setter */
private $language_setter;
/** @var WPML_Term_Query_Filter $term_query_filter */
private $term_query_filter;
/** @var array $settings */
private $settings;
private $active_languages = array();
private $_admin_notices = array();
private $this_lang;
private $wp_query;
private $admin_language;
private $user_preferences = array();
/** @var WPML_WP_API $wp_api */
private $wp_api;
/** @var WPML_Records $records */
private $records;
/** @var @var int $loaded_blog_id */
private $loaded_blog_id;
/**
* @var string $original_language caches the initial language when calling
* \SitePress::switch_lang() for the first time.
*/
private $original_language;
/**
* @var string $original_language_cookie caches the initial language value
* in the user's cookie when calling \SitePress::switch_lang() for the
* first time.
*/
private $original_language_cookie;
/** @var WPML_Locale $locale_utils */
public $locale_utils;
public $footer_preview = false;
/**
* @var icl_cache
*/
public $icl_translations_cache;
/**
* @var WPML_Flags
*/
private $flags;
/**
* @var icl_cache
*/
public $icl_language_name_cache;
/**
* @var icl_cache
*/
public $icl_term_taxonomy_cache;
private $wpml_helper;
/**
* @var array $current_request_data - Use to store temporary information during the current request
*/
private $current_request_data = array();
function __construct() {
do_action('wpml_before_startup');
/** @var array $sitepress_settings */
global $pagenow, $sitepress_settings, $wpdb, $wpml_post_translations, $locale, $wpml_term_translations;
$this->wpml_helper = new WPML_Helper( $wpdb );
parent::__construct( $wpdb );
$this->locale_utils = new WPML_Locale( $wpdb, $this, $locale );
$sitepress_settings = get_option( 'icl_sitepress_settings' );
$this->settings = &$sitepress_settings;
$this->post_translation = &$wpml_post_translations;
$this->term_translation = &$wpml_term_translations;
//@since 3.1
if(is_admin() && !$this->get_setting('icl_capabilities_verified')) {
icl_enable_capabilities();
$sitepress_settings = get_option('icl_sitepress_settings');
}
if ( null === $pagenow && is_multisite() ) {
include WPML_PLUGIN_PATH . '/inc/hacks/vars-php-multisite.php';
}
if ( $this->settings ) {
$this->verify_settings();
}
if ( isset( $_GET['page'], $_GET['debug_action'] ) && WPML_PLUGIN_FOLDER . '/menu/troubleshooting.php' === $_GET['page'] ) {
ob_start();
}
if ( isset( $_REQUEST[ 'icl_ajx_action' ] ) ) {
add_action( 'init', array( $this, 'ajax_setup' ), 15 );
}
// Process post requests
if ( !empty( $_POST ) ) {
add_action( 'init', array( $this, 'process_forms' ) );
}
$this->initialize_cache( );
$flags_factory = new WPML_Flags_Factory( $wpdb );
$this->flags = $flags_factory->create();
add_action( 'plugins_loaded', array( $this, 'plugin_localization' ), - PHP_INT_MAX );
add_action( 'plugins_loaded', array( $this, 'init' ), 1 );
add_action( 'wp_loaded', array( $this, 'maybe_set_this_lang' ) );
add_action( 'switch_blog', array( $this, 'init_settings' ), 10, 1 );
// Administration menus
add_action( 'admin_menu', array( $this, 'administration_menu' ) );
if ( $this->get_setting('existing_content_language_verified') && ( $this->get_setting('setup_complete') || ( !empty($_GET[ 'page' ]) && $this->get_setting('setup_wizard_step') > 1 && $_GET[ 'page' ] == WPML_PLUGIN_FOLDER . '/menu/languages.php' ) ) ) {
// Post/page language box
add_filter( 'comment_feed_join', array( $this, 'comment_feed_join' ) );
add_filter( 'comments_clauses', array( $this, 'comments_clauses' ), 10, 2 );
// Allow us to filter the Query vars before the posts query is being built and executed
add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
if ( $pagenow === 'edit.php' ) {
add_action( 'quick_edit_custom_box', array( 'WPML_Terms_Translations', 'quick_edit_terms_removal' ), 10, 2 );
}
add_filter( 'get_pages', array( $this, 'exclude_other_language_pages2' ), 10, 2 );
add_filter( 'wp_dropdown_pages', array( $this, 'wp_dropdown_pages' ) );
add_filter( 'get_comment_link', array( $this, 'get_comment_link_filter' ) );
$this->set_term_filters_and_hooks();
add_action( 'parse_query', array( $this, 'parse_query' ) );
// AJAX Actions for the post edit screen
add_action( 'wp_ajax_wpml_save_term', array( 'WPML_Post_Edit_Ajax', 'wpml_save_term_action' ) );
add_action( 'wp_ajax_wpml_switch_post_language', array( 'WPML_Post_Edit_Ajax', 'wpml_switch_post_language' ) );
add_action( 'wp_ajax_wpml_get_default_lang', array( 'WPML_Post_Edit_Ajax', 'wpml_get_default_lang' ) );
//AJAX Actions for the taxonomy translation screen
add_action( 'wp_ajax_wpml_get_terms_and_labels_for_taxonomy_table', array( 'WPML_Taxonomy_Translation_Table_Display', 'wpml_get_terms_and_labels_for_taxonomy_table' ) );
// Ajax Action for the updating of term names on the troubleshooting page
add_action( 'wp_ajax_wpml_update_term_names_troubleshoot', array( 'WPML_Troubleshooting_Terms_Menu', 'wpml_update_term_names_troubleshoot' ) );
add_action( 'wp_ajax_wpml_generate_term_slug', array( $this->get_term_actions_helper(), 'generate_unique_term_slug_ajax_handler' ) );
// 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 );
// front end js
add_action( 'admin_enqueue_scripts', 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' ) );
// 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 );
add_filter( 'pre_option_home', array( $this, 'pre_option_home' ) );
if ( !is_admin() ) {
add_filter( 'attachment_link', array( $this, 'attachment_link_filter' ), 10, 2 );
}
// Filter custom type archive link (since WP 3.1)
add_filter( 'post_type_archive_link', array( $this, 'post_type_archive_link_filter' ), 10, 2 );
add_filter( 'author_link', array( $this, 'author_link' ) );
// language negotiation
add_action( 'query_vars', array( $this, 'query_vars' ) );
add_filter( 'language_attributes', array( $this, 'language_attributes' ) );
add_filter( 'locale', array( $this, 'locale_filter' ), 10, 1 );
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( 'pre_option_sticky_posts', array( $this, 'option_sticky_posts' ), 10, 2 );
add_filter( 'trashed_post', array( $this, 'fix_trashed_front_or_posts_page_settings' ) );
add_filter( 'delete_post', array( $this, 'fix_trashed_front_or_posts_page_settings' ) );
add_action( 'wp', array( $this, 'set_wp_query' ) );
add_action( 'personal_options_update', array( $this, 'save_user_options' ) );
add_action( 'edit_user_profile_update', array( $this, 'save_user_options' ) );
if ( !is_admin() ) {
add_action( 'wp_head', array( $this, 'meta_generator_tag' ) );
}
if ( $this->is_setup_complete() ) {
$icl_nav_menu = new WPML_Nav_Menu( $this, $wpdb, $wpml_post_translations, $wpml_term_translations );
$icl_nav_menu->init_hooks();
}
add_action( 'wp_login', array( $this, 'reset_admin_language_cookie' ) );
$this->handle_head_hreflang();
/**
* add extra debug information
*/
add_filter( 'icl_get_extra_debug_info', array( $this, 'add_extra_debug_info' ) );
} //end if the initial language is set - existing_content_language_verified
add_filter( 'core_version_check_locale', array( $this, 'wp_upgrade_locale' ) );
if ( $pagenow === 'post.php' && isset( $_REQUEST['action'], $_GET['post'] ) && $_REQUEST['action'] === 'edit' ) {
add_action( 'init', '_icl_trash_restore_prompt' );
}
add_action( 'admin_enqueue_scripts', array( $this, 'js_load' ), 2 ); // enqueue scripts - higher priority
add_action( 'wp_enqueue_scripts', array( $this, 'js_load' ), 2 ); // enqueue scripts - higher priority
add_filter('url_to_postid', array($this, 'url_to_postid'));
//cron job to update WPML config index file from CDN
$xml_config_log_factory = new WPML_XML_Config_Log_Factory();
$log = $xml_config_log_factory->create_log();
if ( $this->is_setup_complete() ) {
$xml_config_log_notice = $xml_config_log_factory->create_notice();
$xml_config_log_notice->add_hooks();
}
$wpml_config_update_integrator = new WPML_Config_Update_Integrator( $log );
$wpml_config_update_integrator->add_hooks();
add_action('core_upgrade_preamble', array($this, 'update_index_screen'));
add_filter('get_search_form', array($this, 'get_search_form_filter'));
$this->api_hooks();
add_action('wpml_loaded', array($this, 'load_dependencies'), 10000);
do_action('wpml_after_startup');
}
/**
* @since 3.2
*/
public function api_hooks() {
/**
* @deprecated in favour of lowercased namespaces
*/
add_filter( 'WPML_get_setting', array( $this, 'filter_get_setting' ), 10, 2 );
/**
* @deprecated in favour of lowercased namespaces
*/
add_filter( 'WPML_get_current_language', array( $this, 'get_current_language' ), 10, 0 );
/**
* @deprecated in favour of lowercased namespaces
*/
add_filter( 'WPML_get_user_admin_language', array( $this, 'get_user_admin_language_filter' ), 10, 2 );
/**
* @deprecated in favour of lowercased namespaces
*/
add_filter( 'WPML_is_admin_action_from_referer', array( $this, 'check_if_admin_action_from_referer' ), 10, 0 );
/**
* @deprecated in favour of lowercased namespaces
*/
add_filter( 'WPML_current_user', array( $this, 'get_current_user' ), 10, 0 );
add_filter( 'wpml_get_setting', array( $this, 'filter_get_setting' ), 10, 2 );
add_action( 'wpml_set_setting', array( $this, 'action_set_setting' ), 10, 3 );
add_filter( 'wpml_get_language_cookie', array( $this, 'get_language_cookie' ), 10, 0 );
add_filter( 'wpml_current_language', array( $this, 'get_current_language' ), 10, 0 );
add_filter( 'wpml_get_user_admin_language', array( $this, 'get_user_admin_language_filter' ), 10, 2 );
add_filter( 'wpml_is_admin_action_from_referer', array( $this, 'check_if_admin_action_from_referer' ), 10, 0 );
add_filter( 'wpml_current_user', array( $this, 'get_current_user' ), 10, 0 );
add_filter( 'wpml_new_post_source_id', array ( $this, 'get_new_post_source_id'), 10, 1);
/**
* @use \SitePress::get_translatable_documents_filter
*/
add_filter( 'wpml_translatable_documents', array( $this, 'get_translatable_documents_filter' ), 10, 2 );
add_filter( 'wpml_is_translated_post_type', array( $this, 'is_translated_post_type_filter' ), 10, 2 );
add_filter( 'wpml_is_display_as_translated_post_type', array( $this, 'is_display_as_translated_post_type_filter' ), 10, 2 );
add_filter( 'wpml_is_translated_taxonomy', array( $this, 'is_translated_taxonomy_filter' ), 10, 2 );
/**
* @deprecated it has a wrong hook tag
* @since 3.2
*/
add_filter( 'wpml_get_element_translations_filter', array( $this, 'get_element_translations_filter' ), 10, 6 );
/**
* @deprecated it has a wrong hook tag
* @since 3.2
*/
add_filter( 'wpml_get_element_translations', array( $this, 'get_element_translations_filter' ), 10, 6 );
add_filter( 'wpml_is_original_content', array( $this, 'is_original_content_filter'), 10, 3 );
add_filter( 'wpml_original_element_id', array( $this, 'get_original_element_id_filter'), 10, 3 );
add_filter( 'wpml_element_trid', array( $this, 'get_element_trid_filter'), 10, 3 );
add_filter( 'wpml_is_rtl', array( $this, 'is_rtl' ) );
add_filter( 'wpml_home_url', 'wpml_get_home_url_filter', 10 );
add_filter( 'wpml_active_languages', 'wpml_get_active_languages_filter', 10, 2 );
add_filter( 'wpml_display_language_names', 'wpml_display_language_names_filter', 10, 5 );
add_filter( 'wpml_display_single_language_name', array($this, 'get_display_single_language_name_filter'), 10, 2 );
add_filter( 'wpml_element_link', 'wpml_link_to_element_filter', 10, 7 );
add_filter( 'wpml_object_id', 'wpml_object_id_filter', 10, 4 );
add_filter( 'wpml_translated_language_name', 'wpml_translated_language_name_filter', 10, 3 );
add_filter( 'wpml_default_language', 'wpml_get_default_language_filter', 10, 1);
add_filter( 'wpml_post_language_details', 'wpml_get_language_information', 10, 2 );
add_action( 'wpml_add_language_form_field', 'wpml_add_language_form_field_action' );
add_shortcode( 'wpml_language_form_field', 'wpml_language_form_field_shortcode' );
add_filter( 'wpml_element_translation_type', 'wpml_get_element_translation_type_filter', 10, 3 );
add_filter( 'wpml_element_has_translations', 'wpml_element_has_translations_filter', 10, 3 );
add_filter( 'wpml_content_translations', 'wpml_get_content_translations_filter', 10, 3 );
add_filter( 'wpml_master_post_from_duplicate', 'wpml_get_master_post_from_duplicate_filter' );
add_filter( 'wpml_post_duplicates', 'wpml_get_post_duplicates_filter' );
add_filter( 'wpml_element_type', 'wpml_element_type_filter' );
add_filter( 'wpml_setting', 'wpml_get_setting_filter', 10, 3 );
add_filter( 'wpml_sub_setting', 'wpml_get_sub_setting_filter', 10, 4 );
add_filter( 'wpml_language_is_active', 'wpml_language_is_active_filter', 10, 2 );
add_action( 'wpml_admin_make_post_duplicates', 'wpml_admin_make_post_duplicates_action', 10, 1 );
/**
* @deprecated This actions will be removed in future releases.
*/
add_action( 'wpml_make_post_duplicates', 'wpml_make_post_duplicates_action', 10, 1 );
add_filter( 'wpml_element_language_details', 'wpml_element_language_details_filter', 10, 2 );
add_action( 'wpml_set_element_language_details', array($this, 'set_element_language_details_action'), 10, 1 );
add_filter( 'wpml_element_language_code', 'wpml_element_language_code_filter', 10, 2 );
add_filter( 'wpml_elements_without_translations', 'wpml_elements_without_translations_filter', 10, 2 );
add_action( 'wpml_switch_language', 'wpml_switch_language_action', 10, 1 );
}
function init() {
do_action('wpml_before_init');
$this->locale_utils->init();
$this->maybe_set_this_lang();
if ( function_exists( 'w3tc_add_action' ) ) {
w3tc_add_action( 'w3tc_object_cache_key', 'w3tc_translate_cache_key_filter' );
}
$this->get_user_preferences();
$this->set_admin_language();
// default value for theme_localization_type OR
// reset theme_localization_type if string translation was on (theme_localization_type was set to 2) and then it was deactivated
global $sitepress_settings;
$theme_localization_type = new WPML_Theme_Localization_Type( $this );
if ( ! isset( $this->settings['theme_localization_type'] ) || ( $theme_localization_type->is_st_type() && ! defined( 'WPML_ST_VERSION' ) && ! defined( 'WPML_DOING_UPGRADE' ) ) ) {
$sitepress_settings['theme_localization_type_previous'] = $sitepress_settings['theme_localization_type'];
$this->settings['theme_localization_type'] = $sitepress_settings['theme_localization_type'] = WPML_Theme_Localization_Type::USE_MO_FILES;
} else if ( defined( 'WPML_ST_VERSION' ) && isset( $sitepress_settings['theme_localization_type_previous'] ) ) {
$this->settings['theme_localization_type'] = $sitepress_settings['theme_localization_type'] = $sitepress_settings['theme_localization_type_previous'];
unset( $sitepress_settings['theme_localization_type_previous'] );
}
$theme_localization_type->add_hooks();
//Run only if existing content language has been verified, and is front-end or settings are not corrupted
if ( $this->get_setting( 'existing_content_language_verified' ) ) {
add_action( 'wpml_verify_post_translations', array(
$this,
'verify_post_translations_action'
), 10, 1 );
if ( 2 === (int) $this->get_setting( 'language_negotiation_type' ) ) {
add_filter( 'allowed_redirect_hosts', array( $this, 'allowed_redirect_hosts' ) );
}
$this->move_current_language_to_the_top();
add_filter( 'mod_rewrite_rules', array( $this, 'rewrite_rules_filter' ), 10 ,1 );
if ( is_admin() && $this->get_setting( 'setup_complete' ) ) {
new WPML_Custom_Columns_Hooks( $this->wpdb, $this );
if ( ! $this->get_wp_api()->is_translation_queue_page() && ! $this->get_wp_api()->is_string_translation_page() ) {
// Admin language switcher goes to the WP admin bar
if ( apply_filters( 'wpml_show_admin_language_switcher', true ) ) {
add_action( 'wp_before_admin_bar_render', array( $this, 'admin_language_switcher' ) );
} else {
$this->set_this_lang( 'all' );
}
}
}
if ( defined( 'DISQUS_VERSION' ) && ! is_admin() ) {
include WPML_PLUGIN_PATH . '/modules/disqus.php';
}
}
if ( $this->is_rtl() ) {
$GLOBALS[ 'text_direction' ] = 'rtl';
}
if ( ! wpml_is_ajax() && is_admin() && ! $this->get_setting( 'dont_show_help_admin_notice' ) ) {
WPML_Troubleshooting_Terms_Menu::display_terms_with_suffix_admin_notice();
if ( !$this->get_setting( 'setup_wizard_step' )
&& strpos( filter_input( INPUT_GET, 'page', FILTER_SANITIZE_URL ), 'menu/languages.php' ) === false
) {
add_action( 'admin_notices', array( $this, 'help_admin_notice' ) );
}
}
$short_v = implode( '.', array_slice( explode( '.', ICL_SITEPRESS_VERSION ), 0, 3 ) );
if ( is_admin() && ( ! isset( $this->settings['hide_upgrade_notice'] ) || $this->get_setting( 'hide_upgrade_notice' ) !== $short_v ) ) {
add_action( 'admin_notices', array( $this, 'upgrade_notice' ) );
}
require WPML_PLUGIN_PATH . '/inc/template-constants.php';
if ( defined( 'WPML_LOAD_API_SUPPORT' ) ) {
require WPML_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' ) );
}
global $pagenow;
// set language to default and remove language switcher when in Taxonomy Translation page
// If the page uses AJAX and the language must be forced to default, please use the
// if ( $pagenow == 'admin-ajax.php' )above
if ( is_admin()
&& ( $this->is_taxonomy_related_page()
|| $this->is_saving_taxonomy_labels() )
) {
$this->switch_to_admin_language();
add_action( 'init', array( $this, 'remove_admin_language_switcher' ) );
}
/* Posts and new inline created terms, can only be saved in an active language.
* Also the content of the post-new.php should always be filtered for one specific
* active language, so to display the correct taxonomy terms for selection.
*/
if ( $pagenow === 'post-new.php' ) {
if ( ! $this->is_active_language( $this->get_current_language() ) ) {
$this->switch_to_admin_language();
}
}
//Code to run when reactivating the plugin
$recently_activated = $this->get_setting('just_reactivated');
if($recently_activated) {
add_action( 'init', array( $this, 'rebuild_language_information' ), 1000 );
}
if ( is_admin() ) {
$mo_file_search = new WPML_MO_File_Search( $this );
add_action('after_switch_theme', array($mo_file_search, 'reload_theme_dirs'));
}
do_action('wpml_after_init');
do_action('wpml_loaded', $this);
if ( isset( $_GET['page'] )
&& WPML_PLUGIN_FOLDER . '/menu/taxonomy-translation.php' === $_GET['page']
&& is_admin()
) {
$this->taxonomy_translation = new WPML_Taxonomy_Translation( '', array(), new WPML_UI_Screen_Options_Factory( $this ) );
}
if ( WPML_LANGUAGE_NEGOTIATION_TYPE_DOMAIN === (int) $this->get_setting( 'language_negotiation_type' )
&& $this->get_setting( 'language_per_domain_sso_enabled', false )
) {
$sso = new WPML_Language_Per_Domain_SSO( $this );
$sso->init_hooks();
}
}
/**
* Sets the current language in \SitePress::$this_lang, redirects if
* frontend requests point to incomplete or incorrect urls, un-sets the
* $_GET['lang'] and $_GET['admin_bar'] values so that upload.php is able to
* enqueue 'media-grid' correctly without url parameters breaking its
* functionality.
*/
public function maybe_set_this_lang() {
/** @var WPML_Request $wpml_request_handler */
global $wpml_request_handler, $pagenow, $wpml_language_resolution, $mode;
if ( ! defined( 'WP_ADMIN' ) && isset( $_SERVER['HTTP_HOST'] ) && did_action( 'init' ) ) {
require_once WPML_PLUGIN_PATH . '/inc/request-handling/redirection/wpml-frontend-redirection.php';
/** @var WPML_Frontend_Request $wpml_request_handler */
$redirect_helper = _wpml_get_redirect_helper();
$redirection = new WPML_Frontend_Redirection( $this,
$wpml_request_handler, $redirect_helper,
$wpml_language_resolution );
$this->set_this_lang( $redirection->maybe_redirect() );
} else {
$this->set_this_lang( $wpml_request_handler->get_requested_lang() );
}
$wpml_request_handler->set_language_cookie( $this->this_lang );
if ( $pagenow === 'upload.php' && isset( $mode ) && 'grid' === $mode ) {
$_GET['lang'] = null;
$_GET['admin_bar'] = null;
}
}
function load_dependencies() {
do_action( 'wpml_load_dependencies' );
}
/**
* Sets up all term/taxonomy actions for use outside Translations Management or the Post Edit screen
*/
function set_term_filters_and_hooks(){
// The delete filter only ensures the synchronizing of delete actions between translations of a term.
add_action( 'delete_term', array( $this, 'delete_term' ), 1, 3 );
add_action( 'set_object_terms', array( 'WPML_Terms_Translations', 'set_object_terms_action' ), 10, 6 );
add_action( 'created_term_translation', array( 'WPML_Terms_Translations', 'sync_ttid_action' ), 10, 3 );
// filters terms by language for the term/tag-box autoselect
if ( ( isset( $_GET['action'] ) && 'ajax-tag-search' === $_GET['action'] ) || ( isset( $_POST['action'] ) && 'get-tagcloud' === $_POST['action'] ) ) {
add_filter( 'get_terms', array( 'WPML_Terms_Translations', 'get_terms_filter' ), 10, 2 );
}
add_filter( 'terms_clauses', array( $this, 'terms_clauses' ), 10, 4 );
add_action( 'create_term', array( $this, 'create_term' ), 1, 3 );
add_action( 'edit_term', array( $this, 'create_term' ), 1, 3 );
add_filter( 'get_terms_args', array( $this, 'get_terms_args_filter' ), 10, 2 );
add_filter( 'get_edit_term_link', array( $this, 'get_edit_term_link' ), 1, 4 );
add_action( 'deleted_term_relationships', array( $this, 'deleted_term_relationships' ), 10, 2 );
add_action('wp_ajax_icl_repair_broken_type_and_language_assignments', 'icl_repair_broken_type_and_language_assignments');
// adjust queried categories and tags ids according to the language
if ( (bool) $this->get_setting('auto_adjust_ids' ) ) {
add_action( 'wp_list_pages_excludes', array( $this, 'adjust_wp_list_pages_excludes' ) );
if ( ! $this->get_wp_api()->is_admin()
|| $this->get_wp_api()->constant( 'DOING_AJAX' )
) {
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_pages', array( $this, 'get_pages_adjust_ids' ), 1, 2 );
}
}
add_action( 'clean_term_cache', array( $this, 'clear_elements_cache' ), 10, 2 );
}
function remove_admin_language_switcher() {
remove_action( 'wp_before_admin_bar_render', array( $this, 'admin_language_switcher' ) );
}
function rebuild_language_information() {
$this->set_setting('just_reactivated', 0);
$this->save_settings();
/** @var TranslationManagement $iclTranslationManagement */
global $iclTranslationManagement;
if ( $iclTranslationManagement ) {
$iclTranslationManagement->add_missing_language_information();
}
}
function setup()
{
$setup_complete = $this->get_setting('setup_complete');
if(!$setup_complete) {
$this->set_setting('setup_complete', false);
}
return $setup_complete;
}
public function user_lang_by_authcookie() {
global $current_user;
if ( !isset( $current_user ) ) {
$username = '';
if ( function_exists ( 'wp_parse_auth_cookie' ) ) {
$cookie_data = wp_parse_auth_cookie ();
$username = isset( $cookie_data[ 'username' ] ) ? $cookie_data[ 'username' ] : null;
}
$user_obj = new WP_User( null, $username );
} else {
$user_obj = $current_user;
}
$user_id = isset( $user_obj->ID ) ? $user_obj->ID : 0;
$user_lang = $this->get_user_admin_language ( $user_id );
$user_lang = $user_lang ? $user_lang : $this->get_current_language ();
return $user_lang;
}
function get_current_user() {
global $current_user;
return $current_user !== null ? $current_user : new WP_User();
}
function ajax_setup()
{
require WPML_PLUGIN_PATH . '/ajax.php';
}
function check_if_admin_action_from_referer() {
$referer = isset( $_SERVER[ 'HTTP_REFERER' ] ) ? $_SERVER[ 'HTTP_REFERER' ] : '';
return strpos ( $referer, strtolower ( admin_url () ) ) === 0;
}
/**
* Check translation mangement column screen option.
*
* @param string $post_type Current post type.
*
* @return bool
*/
public function show_management_column_content( $post_type ) {
$custom_columns = new WPML_Custom_Columns( $this );
return $custom_columns->show_management_column_content( $post_type );
}
function initialize_cache() {
require_once WPML_PLUGIN_PATH . '/inc/cache.php';
}
/**
* @return icl_cache
*/
function get_translations_cache() {
if ( ! isset( $this->icl_translations_cache ) ) {
$this->icl_translations_cache = new icl_cache();
}
return $this->icl_translations_cache;
}
/**
* @return icl_cache
*/
function get_language_name_cache() {
if ( ! isset( $this->icl_language_name_cache ) ) {
$this->icl_language_name_cache = new icl_cache( 'language_name', true );
}
return $this->icl_language_name_cache;
}
public function set_admin_language( $admin_language = false ) {
$default_language = $this->get_default_language ();
$this->admin_language = $admin_language ? $admin_language : $this->user_lang_by_authcookie ();
$lang_codes = array_keys ( $this->get_languages () );
if ( (bool) $this->admin_language === true && ! in_array( $this->admin_language, $lang_codes, true ) ) {
delete_user_meta ( $this->get_current_user ()->ID, 'icl_admin_language' );
}
if ( empty( $this->settings[ 'admin_default_language' ] ) || !in_array (
$this->settings[ 'admin_default_language' ], array_merge( $lang_codes, array( '_default_' ) ), true
)
) {
$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_' && $default_language ) {
$this->admin_language = $default_language;
}
}
function get_admin_language() {
$current_user = $this->get_current_user();
if (
( ! empty( $current_user->ID ) && $this->get_wp_api()->get_user_meta( $current_user->ID,
'icl_admin_language_for_edit',
true ) && $this->is_post_edit_screen() )
|| $this->is_wpml_switch_language_triggered()
) {
$admin_language = $this->get_current_language();
} else {
$admin_language = $this->user_lang_by_authcookie();
}
return $admin_language;
}
public function is_wpml_switch_language_triggered() {
return isset( $GLOBALS['icl_language_switched'] ) ? true : false ;
}
/**
* @return bool
*/
function is_post_edit_screen() {
global $pagenow;
$action = isset( $_GET['action'] ) ? $_GET['action'] : "";
return $pagenow === 'post-new.php' || ( $pagenow === 'post.php' && ( 0 === strcmp( $action, 'edit' ) ) );
}
function get_user_admin_language_filter( $value, $user_id ) {
return $this->get_user_admin_language( $user_id );
}
function get_user_admin_language( $user_id, $reload = false ) {
$user_admin_language = new WPML_User_Admin_Language( $this );
return $user_admin_language->get( $user_id, $reload );
}
/**
* @todo rename this method, has it has nothing to do with the menus
*/
function administration_menu() {
if ( ! $this->is_setup_complete() ) {
$this->check_and_display_missing_records_notice();
}
}
private function check_and_display_missing_records_notice() {
if ( ! $this->is_troubleshooting_page() && ! SitePress_Setup::languages_table_is_complete() ) {
$troubleshooting_url = admin_url( 'admin.php?page=' . WPML_PLUGIN_FOLDER . '/menu/troubleshooting.php' );
$troubleshooting_link = '<a href="'
. $troubleshooting_url
. '" title="'
. esc_attr( __( 'Troubleshooting',
'sitepress' ) )
. '">'
. __( 'Troubleshooting', 'sitepress' )
. '</a>';
$message = '';
$message .= __( 'WPML is missing some records in the languages tables and it cannot fully work until this issue is fixed.',
'sitepress' );
$message .= '<br />';
$message .= sprintf( __( 'Please go to the %s page and click on "%s" to fix this problem.',
'sitepress' ),
$troubleshooting_link,
__( 'Clear language information and repopulate languages',
'sitepress' ) );
$message .= '<br />';
$message .= '<br />';
$message .= __( 'This warning will disappear once this issue is fixed.', 'sitepress' );
ICL_AdminNotifier::removeMessage( 'setup-incomplete' );
ICL_AdminNotifier::addMessage( 'setup-incomplete', $message, 'error', false, false, false, 'setup', true );
ICL_AdminNotifier::displayMessages( 'setup' );
}
}
function taxonomy_translation_page() {
$this->taxonomy_translation->render();
}
/**
* @param int|string $blog_id
*/
function init_settings( $blog_id ) {
$blog_id = (int) $blog_id;
if ( ! isset( $this->loaded_blog_id ) || $this->loaded_blog_id != $blog_id ) {
$this->loaded_blog_id = $blog_id;
$this->settings = get_option( 'icl_sitepress_settings' );
$default_lang_code = isset( $this->settings[ 'default_language' ] ) ? $this->settings[ 'default_language' ] : false;
load_wpml_url_converter( $this->settings, false, $default_lang_code );
}
}
/**
* @param array|null $settings
*/
function save_settings( $settings = null ) {
if ( 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;
}
}
}
if ( ! empty( $this->settings ) ) {
update_option( 'icl_sitepress_settings', $this->settings );
}
do_action( 'icl_save_settings', $settings );
}
/**
* @since 3.1
*/
function get_settings() {
return $this->settings;
}
function filter_get_setting($value, $key) {
return $this->get_setting($key, $value);
}
/**
* @param string $key
* @param mixed|bool $default
*
* @since 3.1
*
* @return bool|mixed
*/
function get_setting( $key, $default = false ) {
return wpml_get_setting_filter( $default, $key );
}
function action_set_setting($key, $value, $save_now) {
$this->set_setting($key, $value, $save_now);
}
/**
* @param string $key
* @param mixed $value
* @param bool $save_now Immediately update the settings record in the DB
*
* @since 3.1
*
* @return bool Always True. If `$save_now === true`, it returns the result of `update_option`
*/
function set_setting($key, $value, $save_now = false) {
return icl_set_setting($key, $value, $save_now);
}
function get_user_preferences() {
if ( ! isset( $this->user_preferences ) || ! $this->user_preferences ) {
$this->user_preferences = get_user_meta( $this->get_current_user()->ID, '_icl_preferences', true );
}
if ( (is_array( $this->user_preferences) && $this->user_preferences == array(0 => false)) || !$this->user_preferences ) {
$this->user_preferences = array();
}
if ( ! is_array( $this->user_preferences ) ) {
$this->user_preferences = (array) $this->user_preferences;
}
return $this->user_preferences;
}
function set_user_preferences($value) {
$this->user_preferences = $value;
}
function save_user_preferences()
{
update_user_meta( $this->get_current_user()->ID, '_icl_preferences', $this->user_preferences );
}
function get_option( $option_name )
{
return isset( $this->settings[ $option_name ] ) ? $this->settings[ $option_name ] : null;
}
function verify_settings() {
$verify_settings = new WPML_Verify_SitePress_Settings( $this->get_wp_api() );
list( $this->settings, $update_settings ) = $verify_settings->verify( $this->settings );
if ( $update_settings ) {
$this->save_settings();
}
}
/**
* @param bool $refresh
* @param bool $major_first
* @param string $order_by
*
* @return array
*/
function get_active_languages( $refresh = false, $major_first = false, $order_by = 'english_name' ) {
/** @var WPML_Request $wpml_request_handler */
global $wpml_request_handler;
$in_language = defined( 'WP_ADMIN' ) && $this->admin_language ? $this->admin_language : null;
$in_language = $in_language === null ? $this->get_current_language() : $in_language;
$in_language = $in_language ? $in_language : $this->get_default_language();
$active_languages = $this->get_languages( $in_language, true, $refresh, $major_first, $order_by );
$active_languages = isset( $active_languages[ $in_language ] ) ? $active_languages : $this->get_languages( $in_language, true, true, $major_first, $order_by );
$active_languages = $active_languages ? $active_languages : array();
$this->active_languages = $wpml_request_handler->show_hidden() ? $active_languages : array_diff_key( $active_languages, array_fill_keys( $this->get_setting( 'hidden_languages', array() ), 1 ) );
return $this->active_languages;
}
/**
* Returns an input array of languages, that are in the form of associative arrays,
* ordered by the user-chosen language order
*
* @param array[] $languages
*
* @return array[]
*/
function order_languages( $languages ) {
$ordered_languages = array();
if ( is_array( $this->settings[ 'languages_order' ] ) ) {
foreach ( $this->settings[ 'languages_order' ] as $code ) {
if ( isset( $languages[ $code ] ) ) {
$ordered_languages[ $code ] = $languages[ $code ];
unset( $languages[ $code ] );
}
}
} else {
// initial save
$iclsettings[ 'languages_order' ] = array_keys( $languages );
$this->save_settings( $iclsettings );
}
if ( ! empty( $languages ) ) {
foreach ( $languages as $code => $lang ) {
$ordered_languages[ $code ] = $lang;
}
}
return $ordered_languages;
}
/**
* @param $lang_code
* Checks if a given language code belongs to a currently active language.
* @return bool
*/
function is_active_language( $lang_code ) {
$result = false;
$active_languages = $this->get_active_languages();
foreach ( $active_languages as $lang ) {
if ( $lang_code == $lang[ 'code' ] ) {
$result = true;
break;
}
}
return $result;
}
public function get_languages( $lang = false, $active_only = false, $refresh = false, $major_first = false, $order_by = 'english_name') {
$res = false;
if ( !$lang ) {
$lang = $this->get_default_language();
}
if ( $active_only && ! $refresh ) {
$res = $this->get_language_name_cache()->get( 'in_language_' . $lang . '_' . $major_first . '_' . $order_by );
}
if ( ! $res && ! $active_only && ! $refresh ) {
$res = $this->get_language_name_cache()->get( 'all_language_' . $lang . '_' . $major_first . '_' . $order_by );
}
if ( ! $res ) {
$setup_instance = wpml_get_setup_instance();
$res = $setup_instance->refresh_active_lang_cache( $lang, $active_only, $major_first, $order_by );
}
return $res;
}
function get_language_details( $code ) {
if ( $this->get_wp_api()->is_admin() ) {
$dcode = $this->admin_language;
} else {
$dcode = $code;
}