PageRenderTime 75ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 2ms

/htdocs/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php

https://bitbucket.org/dkrzos/phc
PHP | 7536 lines | 6118 code | 977 blank | 441 comment | 1579 complexity | 2e8344fa7eabc35e73b9aac072ac0559 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. class SitePress{
  3. private $settings;
  4. private $active_languages = array();
  5. private $this_lang;
  6. private $wp_query;
  7. private $admin_language = null;
  8. private $user_preferences = array();
  9. public $queries = array();
  10. function __construct(){
  11. global $wpdb, $pagenow;
  12. $this->settings = get_option('icl_sitepress_settings');
  13. if(is_null($pagenow) && is_multisite()){
  14. include ICL_PLUGIN_PATH . '/inc/hacks/vars-php-multisite.php';
  15. }
  16. if(false != $this->settings){
  17. $this->verify_settings();
  18. }
  19. if(isset($_GET['icl_action'])){
  20. require_once ABSPATH . WPINC . '/pluggable.php';
  21. if($_GET['icl_action']=='reminder_popup'){
  22. add_action('init', array($this, 'reminders_popup'));
  23. }
  24. elseif($_GET['icl_action']=='dismiss_help'){
  25. $this->settings['dont_show_help_admin_notice'] = true;
  26. $this->save_settings();
  27. }elseif($_GET['icl_action']=='dbdump' ){
  28. include_once ICL_PLUGIN_PATH . '/inc/functions-troubleshooting.php';
  29. add_action('init', 'icl_troubleshooting_dumpdb');
  30. }
  31. }
  32. if(isset($_GET['page']) && $_GET['page']== ICL_PLUGIN_FOLDER . '/menu/troubleshooting.php' && isset($_GET['debug_action'])){
  33. ob_start();
  34. }
  35. if(isset($_REQUEST['icl_ajx_action'])){
  36. add_action('init', array($this, 'ajax_setup'));
  37. }
  38. add_action('admin_footer', array($this, 'icl_nonces'));
  39. // Process post requests
  40. if(!empty($_POST)){
  41. add_action('init', array($this,'process_forms'));
  42. }
  43. add_action('plugins_loaded', array($this,'init'), 1);
  44. add_action('plugins_loaded', array($this,'initialize_cache'), 0);
  45. add_action('admin_print_scripts', array($this,'js_scripts_setup'));
  46. add_action('admin_print_styles', array($this,'css_setup'));
  47. // Administration menus
  48. add_action('admin_menu', array($this, 'administration_menu'));
  49. add_action('init', array($this,'plugin_localization'));
  50. if($this->settings['existing_content_language_verified']){
  51. // Post/page language box
  52. if($pagenow == 'post.php' || $pagenow == 'post-new.php'){
  53. add_action('admin_head', array($this,'post_edit_language_options'));
  54. }
  55. // Post/page save actions
  56. add_action('save_post', array($this,'save_post_actions'), 10, 2);
  57. //add_action('update_post_metadata', array($this,'pre_update_post_meta'), 10, 5);
  58. add_action('updated_post_meta', array($this,'update_post_meta'), 100, 4);
  59. add_action('added_post_meta', array($this,'update_post_meta'), 100, 4);
  60. add_action('updated_postmeta', array($this,'update_post_meta'), 100, 4); // ajax
  61. add_action('added_postmeta', array($this,'update_post_meta'), 100, 4); // ajax
  62. add_action('delete_postmeta', array($this,'delete_post_meta')); // ajax
  63. //add_filter('get_post_metadata', array($this,'get_post_metadata_filter'), 10, 4);
  64. // filter user taxonomy input
  65. add_filter('pre_post_tax_input', array($this, 'validate_taxonomy_input'));
  66. // Post/page delete actions
  67. add_action('delete_post', array($this,'delete_post_actions'));
  68. add_action('wp_trash_post', array($this,'trash_post_actions'));
  69. add_action('untrashed_post', array($this,'untrashed_post_actions'));
  70. add_filter('posts_join', array($this,'posts_join_filter'));
  71. add_filter('posts_where', array($this,'posts_where_filter'));
  72. add_filter('comment_feed_join', array($this,'comment_feed_join'));
  73. add_filter('comments_clauses', array($this, 'comments_clauses'), 10, 2);
  74. // Allow us to filter the Query before vars before the posts query is being built and executed
  75. add_filter('pre_get_posts', array($this, 'pre_get_posts'));
  76. // show untranslated posts
  77. if(!is_admin() && isset($this->settings['show_untranslated_blog_posts']) &&
  78. $this->settings['show_untranslated_blog_posts'] && $this->get_current_language() != $this->get_default_language()){
  79. add_filter('the_posts', array($this, 'the_posts'));
  80. }
  81. if($pagenow == 'edit.php'){
  82. add_action('admin_footer', array($this,'language_filter'));
  83. }
  84. add_filter('get_pages', array($this, 'exclude_other_language_pages2'));
  85. add_filter('wp_dropdown_pages', array($this, 'wp_dropdown_pages'));
  86. // posts and pages links filters
  87. add_filter('post_link', array($this, 'permalink_filter'),1,2);
  88. add_filter('post_type_link', array($this, 'permalink_filter'),1,2);
  89. add_filter('page_link', array($this, 'permalink_filter'),1,2);
  90. if(version_compare(preg_replace('#-RC[0-9]+(-[0-9]+)?$#', '', $GLOBALS['wp_version']), '3.1', '<')){
  91. add_filter('category_link', array($this, 'category_permalink_filter'),1,2);
  92. add_filter('tag_link', array($this, 'tax_permalink_filter'),1,2);
  93. }
  94. add_filter('term_link', array($this, 'tax_permalink_filter'),1,2);
  95. add_filter('get_comment_link', array($this, 'get_comment_link_filter'));
  96. add_action('create_term', array($this, 'create_term'),1, 2);
  97. add_action('edit_term', array($this, 'create_term'),1, 2);
  98. add_action('delete_term', array($this, 'delete_term'),1,3);
  99. add_action('get_term', array($this, 'get_term_filter'),1,2);
  100. add_filter('get_terms_args', array($this, 'get_terms_args_filter'));
  101. // filters terms by language
  102. if(version_compare($GLOBALS['wp_version'], '3.1', '>=')){
  103. add_filter('terms_clauses', array($this, 'terms_clauses'));
  104. }else{
  105. add_filter('list_terms_exclusions', array($this, 'exclude_other_terms'),1,2);
  106. }
  107. // allow adding terms with the same name in different languages
  108. add_filter("pre_term_name", array($this, 'pre_term_name'), 1, 2);
  109. // allow adding categories with the same name in different languages
  110. add_action('admin_init', array($this, 'pre_save_category'));
  111. // custom hook for adding the language selector to the template
  112. add_action('icl_language_selector', array($this, 'language_selector'));
  113. // front end js
  114. add_action('wp_head', array($this, 'front_end_js'));
  115. add_action('wp_head', array($this,'rtl_fix'));
  116. add_action('admin_print_styles', array($this,'rtl_fix'));
  117. add_action('restrict_manage_posts', array($this, 'restrict_manage_posts'));
  118. add_filter('get_edit_post_link', array($this, 'get_edit_post_link'), 1, 3);
  119. add_filter('get_edit_term_link', array($this, 'get_edit_term_link'), 1, 4);
  120. // short circuit get default category
  121. add_filter('pre_option_default_category', array($this, 'pre_option_default_category'));
  122. add_filter('update_option_default_category', array($this, 'update_option_default_category'), 1, 2);
  123. add_filter('the_category', array($this,'the_category_name_filter'));
  124. add_filter('get_terms', array($this,'get_terms_filter'));
  125. add_filter('get_the_terms', array($this, 'get_the_terms_filter'), 10, 3);
  126. add_filter('single_cat_title', array($this,'the_category_name_filter'));
  127. add_filter('term_links-category', array($this,'the_category_name_filter'));
  128. add_filter('term_links-post_tag', array($this,'the_category_name_filter'));
  129. add_filter('tags_to_edit', array($this,'the_category_name_filter'));
  130. add_filter('single_tag_title', array($this,'the_category_name_filter'));
  131. // adjacent posts links
  132. add_filter('get_previous_post_join', array($this,'get_adjacent_post_join'));
  133. add_filter('get_next_post_join', array($this,'get_adjacent_post_join'));
  134. add_filter('get_previous_post_where', array($this,'get_adjacent_post_where'));
  135. add_filter('get_next_post_where', array($this,'get_adjacent_post_where'));
  136. // feeds links
  137. add_filter('feed_link', array($this,'feed_link'));
  138. // commenting links
  139. add_filter('post_comments_feed_link', array($this,'post_comments_feed_link'));
  140. add_filter('trackback_url', array($this,'trackback_url'));
  141. add_filter('user_trailingslashit', array($this,'user_trailingslashit'),1, 2);
  142. // date based archives
  143. add_filter('year_link', array($this,'archives_link'));
  144. add_filter('month_link', array($this,'archives_link'));
  145. add_filter('day_link', array($this,'archives_link'));
  146. add_filter('getarchives_join', array($this,'getarchives_join'));
  147. add_filter('getarchives_where', array($this,'getarchives_where'));
  148. add_filter('pre_option_home', array($this,'pre_option_home'));
  149. if (!is_admin()) {
  150. add_filter('attachment_link', array($this, 'attachment_link_filter'), 10, 2);
  151. }
  152. // Filter custom type archive link (since WP 3.1)
  153. add_filter('post_type_archive_link', array($this,'post_type_archive_link_filter'), 10, 2);
  154. add_filter('author_link', array($this,'author_link'));
  155. add_filter('wp_unique_post_slug', array($this, 'wp_unique_post_slug'), 100, 5);
  156. add_filter('home_url', array($this, 'home_url'), 1, 4) ;
  157. // language negotiation
  158. add_action('query_vars', array($this,'query_vars'));
  159. //
  160. add_filter('language_attributes', array($this, 'language_attributes'));
  161. add_action('locale', array($this, 'locale'));
  162. if(isset($_GET['____icl_validate_domain'])){ echo '<!--'.get_option('home').'-->'; exit; }
  163. add_filter('pre_option_page_on_front', array($this,'pre_option_page_on_front'));
  164. add_filter('pre_option_page_for_posts', array($this,'pre_option_page_for_posts'));
  165. add_filter('option_sticky_posts', array($this,'option_sticky_posts'));
  166. add_filter('request', array($this,'request_filter'));
  167. add_action('wp_head', array($this,'set_wp_query'));
  168. add_action('show_user_profile', array($this, 'show_user_options'));
  169. add_action('personal_options_update', array($this, 'save_user_options'));
  170. // column with links to translations (or add translation) - low priority
  171. add_action('init', array($this,'configure_custom_column'), 1010); // accommodate Types init@999
  172. // adjust queried categories and tags ids according to the language
  173. if($this->settings['auto_adjust_ids']){
  174. add_action('parse_query', array($this, 'parse_query'));
  175. add_action('wp_list_pages_excludes', array($this, 'adjust_wp_list_pages_excludes'));
  176. if(!is_admin()){
  177. add_filter('get_term', array($this,'get_term_adjust_id'), 1, 1);
  178. add_filter('category_link', array($this,'category_link_adjust_id'), 1, 2);
  179. add_filter('get_terms', array($this,'get_terms_adjust_ids'), 1, 3);
  180. add_filter('get_pages', array($this,'get_pages_adjust_ids'), 1, 2);
  181. }
  182. }
  183. if(!is_admin()){
  184. add_action('wp_head', array($this, 'meta_generator_tag'));
  185. }
  186. require_once ICL_PLUGIN_PATH . '/inc/wp-nav-menus/iclNavMenu.class.php';
  187. $iclNavMenu = new iclNavMenu;
  188. if(is_admin() || defined('XMLRPC_REQUEST') || preg_match('#wp-comments-post\.php$#', $_SERVER['REQUEST_URI'])){
  189. global $iclTranslationManagement, $ICL_Pro_Translation;
  190. $iclTranslationManagement = new TranslationManagement;
  191. $ICL_Pro_Translation = new ICL_Pro_Translation();
  192. }
  193. add_action('wp_login', array($this, 'reset_admin_language_cookie'));
  194. add_action('template_redirect', array($this, 'setup_canonical_urls'), 100);
  195. add_action('init', array($this, '_taxonomy_languages_menu'), 99); //allow hooking in
  196. if($this->settings['seo']['head_langs']){
  197. add_action('wp_head', array($this, 'head_langs'));
  198. }
  199. } //end if the initial language is set - existing_content_language_verified
  200. add_action('wp_dashboard_setup', array($this, 'dashboard_widget_setup'));
  201. if(is_admin() && $pagenow == 'index.php'){
  202. add_action('icl_dashboard_widget_notices', array($this, 'print_translatable_custom_content_status'));
  203. }
  204. add_filter('core_version_check_locale', array($this, 'wp_upgrade_locale'));
  205. if($pagenow == 'post.php' && isset($_REQUEST['action']) && $_REQUEST['action']=='edit' && isset($_GET['post'])){
  206. add_action('init', '_icl_trash_restore_prompt');
  207. }
  208. add_action('init', array($this, 'js_load'), 2); // enqueue scripts - higher priority
  209. }
  210. function init(){
  211. global $wpdb;
  212. $this->get_user_preferences();
  213. // // default value for theme_localization_type OR
  214. // reset theme_localization_type if string translation was on (theme_localization_type was set to 2) and then it was deactivated
  215. /*
  216. if(
  217. !isset($this->settings['theme_localization_type']) ||
  218. isset($this->settings['theme_localization_type']) && $this->settings['theme_localization_type'] == 1 && !defined('WPML_ST_VERSION')
  219. ){
  220. if(!defined('WPML_DOING_UPGRADE')){
  221. $this->settings['theme_localization_type'] = 2;
  222. }
  223. }
  224. */
  225. $this->set_admin_language();
  226. //configure callbacks for plugin menu pages
  227. if(defined('WP_ADMIN') && isset($_GET['page']) && 0 === strpos($_GET['page'],basename(ICL_PLUGIN_PATH).'/')){
  228. add_action('icl_menu_footer', array($this, 'menu_footer'));
  229. }
  230. if($this->settings['existing_content_language_verified']){
  231. if(defined('WP_ADMIN')){
  232. if(isset($_GET['lang'])){
  233. $this->this_lang = rtrim(strip_tags($_GET['lang']),'/');
  234. $al = $this->get_active_languages();
  235. $al['all'] = true;
  236. if(empty($al[$this->this_lang])){
  237. $this->this_lang = $this->get_default_language();
  238. }
  239. // force default language for string translation
  240. // we also make sure it's not saved in the cookie
  241. }elseif(isset($_GET['page']) &&
  242. ((defined('WPML_ST_FOLDER') && $_GET['page'] == WPML_ST_FOLDER . '/menu/string-translation.php') ||
  243. (defined('WPML_TM_FOLDER') && $_GET['page'] == WPML_TM_FOLDER . '/menu/translations-queue.php'))
  244. ){
  245. $this->this_lang = $this->get_default_language();
  246. }elseif(defined('DOING_AJAX')) {
  247. $al = $this->get_active_languages();
  248. if(isset($_POST['lang']) && isset($al[$_POST['lang']])){
  249. $this->this_lang = $_POST['lang'];
  250. }else{
  251. $this->this_lang = $this->get_language_cookie();
  252. }
  253. }elseif($lang = $this->get_admin_language_cookie()){
  254. $this->this_lang = $lang;
  255. }else{
  256. $this->this_lang = $this->get_default_language();
  257. }
  258. if((isset($_GET['admin_bar']) && $_GET['admin_bar']==1) && (!isset($_GET['page']) || !defined('WPML_ST_FOLDER') || $_GET['page'] != WPML_ST_FOLDER . '/menu/string-translation.php')){
  259. $this->set_admin_language_cookie();
  260. }
  261. }else{
  262. $al = $this->get_active_languages();
  263. foreach($al as $l){
  264. $active_languages[] = $l['code'];
  265. }
  266. $active_languages[] = 'all';
  267. $s = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on'?'s':'';
  268. $request = 'http' . $s . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  269. $home = get_option('home');
  270. if($s){
  271. $home = preg_replace('#^http://#', 'https://', $home);
  272. }
  273. $url_parts = parse_url($home);
  274. $blog_path = !empty($url_parts['path'])?$url_parts['path']:'';
  275. switch($this->settings['language_negotiation_type']){
  276. case 1:
  277. $path = str_replace($home,'',$request);
  278. $parts = explode('?', $path);
  279. $path = $parts[0];
  280. $exp = explode('/',trim($path,'/'));
  281. if(in_array($exp[0], $active_languages)){
  282. $this->this_lang = $exp[0];
  283. // before hijiking the SERVER[REQUEST_URI]
  284. // override the canonical_redirect action
  285. // keep a copy of the original request uri
  286. remove_action('template_redirect', 'redirect_canonical');
  287. global $_icl_server_request_uri;
  288. $_icl_server_request_uri = $_SERVER['REQUEST_URI'];
  289. add_action('template_redirect', 'icl_redirect_canonical_wrapper', 11);
  290. function icl_redirect_canonical_wrapper(){
  291. global $_icl_server_request_uri, $wp_query;
  292. $requested_url = ( !empty($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
  293. $requested_url .= $_SERVER['HTTP_HOST'];
  294. $requested_url .= $_icl_server_request_uri;
  295. redirect_canonical($requested_url);
  296. /*
  297. if($wp_query->query_vars['error'] == '404'){
  298. $wp_query->is_404 = true;
  299. $template = get_404_template();
  300. include($template);
  301. exit;
  302. }
  303. */
  304. }
  305. //
  306. //deal with situations when template files need to be called directly
  307. add_action('template_redirect', array($this, '_allow_calling_template_file_directly'));
  308. //$_SERVER['REQUEST_URI'] = preg_replace('@^'. $blog_path . '/' . $this->this_lang.'@i', $blog_path ,$_SERVER['REQUEST_URI']);
  309. // Check for special case of www.example.com/fr where the / is missing on the end
  310. $parts = parse_url($_SERVER['REQUEST_URI']);
  311. if(strlen($parts['path']) == 0){
  312. $_SERVER['REQUEST_URI'] = '/' . $_SERVER['REQUEST_URI'];
  313. }
  314. }else{
  315. $this->this_lang = $this->get_default_language();
  316. }
  317. break;
  318. case 2:
  319. $exp = explode('.', $_SERVER['HTTP_HOST']);
  320. $__l = array_search('http' . $s . '://' . $_SERVER['HTTP_HOST'] . $blog_path, $this->settings['language_domains']);
  321. $this->this_lang = $__l?$__l:$this->get_default_language();
  322. if(defined('ICL_USE_MULTIPLE_DOMAIN_LOGIN') && ICL_USE_MULTIPLE_DOMAIN_LOGIN){
  323. include ICL_PLUGIN_PATH . '/modules/multiple-domains-login.php';
  324. }
  325. add_filter('allowed_redirect_hosts', array($this, 'allowed_redirect_hosts'));
  326. break;
  327. case 3:
  328. default:
  329. if(isset($_GET['lang'])){
  330. $this->this_lang = preg_replace("/[^0-9a-zA-Z-]/i", '',strip_tags($_GET['lang']));
  331. // set the language based on the content id - for short links
  332. }elseif(isset($_GET['page_id'])){
  333. $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']));
  334. }elseif(isset($_GET['p'])){
  335. $post_type = $wpdb->get_var($wpdb->prepare("SELECT post_type FROM {$wpdb->posts} WHERE ID=%d", $_GET['p']));
  336. $this->this_lang = $wpdb->get_var($wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d",
  337. 'post_' . $post_type, $_GET['p']));
  338. }elseif(isset($_GET['cat_ID'])){
  339. $cat_tax_id = $wpdb->get_var($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy=%s",
  340. $_GET['cat_ID'], 'category'));
  341. $this->this_lang = $wpdb->get_var($wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations
  342. WHERE element_type='tax_category' AND element_id=%d", $cat_tax_id));
  343. }elseif(isset($_GET['tag'])){
  344. $tag_tax_id = $wpdb->get_var($wpdb->prepare("
  345. SELECT x.term_taxonomy_id FROM {$wpdb->term_taxonomy} x JOIN {$wpdb->terms} t ON t.term_id = x.term_id
  346. WHERE t.slug=%s AND x.taxonomy='post_tag'",
  347. $_GET['tag']));
  348. $this->this_lang = $wpdb->get_var($wpdb->prepare("SELECT language_code FROM {$wpdb->prefix}icl_translations
  349. WHERE element_type='tax_post_tag' AND element_id=%d", $tag_tax_id));
  350. }
  351. //
  352. if(!isset($_GET['lang']) && ($this->this_lang && $this->this_lang != $this->get_default_language())){
  353. if(!isset($GLOBALS['wp_rewrite'])){
  354. require_once ABSPATH . WPINC . '/rewrite.php';
  355. $GLOBALS['wp_rewrite'] = new WP_Rewrite();
  356. }
  357. define('ICL_DOING_REDIRECT', true);
  358. if(isset($_GET['page_id'])){
  359. wp_redirect(get_page_link($_GET['page_id']), '301');
  360. exit;
  361. }elseif(isset($_GET['p'])){
  362. wp_redirect(get_permalink($_GET['p']), '301');
  363. exit;
  364. }elseif(isset($_GET['cat_ID'])){
  365. wp_redirect(get_term_link( intval($_GET['cat_ID']), 'category' ));
  366. exit;
  367. }elseif(isset($_GET['tag'])){
  368. wp_redirect(get_term_link( $_GET['tag'], 'post_tag' ));
  369. exit;
  370. }else{
  371. global $wp_taxonomies;
  372. if(isset($this->settings['taxonomies_sync_option'])){
  373. $taxs = array_keys((array)$this->settings['taxonomies_sync_option']);
  374. foreach($taxs as $t){
  375. if(isset($_GET[$t])){
  376. $term_obj = $wpdb->get_row($wpdb->prepare(
  377. "SELECT * FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON t.term_id = x.term_id
  378. WHERE t.slug=%s AND x.taxonomy=%s"
  379. , $_GET[$t], $t));
  380. $term_link = get_term_link( $term_obj, $t );
  381. $term_link = str_replace('&amp;', '&', $term_link); // fix
  382. if($term_link && !is_wp_error($term_link)){
  383. wp_redirect($term_link);
  384. exit;
  385. }
  386. }
  387. }
  388. }
  389. }
  390. }
  391. if(empty($this->this_lang)){
  392. $this->this_lang = $this->get_default_language();
  393. }
  394. }
  395. // allow forcing the current language when it can't be decoded from the URL
  396. $this->this_lang = apply_filters('icl_set_current_language', $this->this_lang);
  397. }
  398. //reorder active language to put 'this_lang' in front
  399. foreach($this->active_languages as $k=>$al){
  400. if($al['code']==$this->this_lang){
  401. unset($this->active_languages[$k]);
  402. $this->active_languages = array_merge(array($k=>$al), $this->active_languages);
  403. }
  404. }
  405. //if($this->settings['language_negotiation_type']==3){
  406. // fix pagenum links for when using the language as a parameter
  407. // add_filter('get_pagenum_link', array($this,'get_pagenum_link_filter'));
  408. //}
  409. // filter some queries
  410. add_filter('query', array($this, 'filter_queries'));
  411. if( $this->settings['language_negotiation_type']==1 && $this->get_current_language()!=$this->get_default_language()){
  412. add_filter('option_rewrite_rules', array($this, 'rewrite_rules_filter'));
  413. }
  414. $this->set_language_cookie();
  415. if(is_admin() &&
  416. (!isset($_GET['page']) || !defined('WPML_ST_FOLDER') || $_GET['page'] != WPML_ST_FOLDER . '/menu/string-translation.php') &&
  417. (!isset($_GET['page']) || !defined('WPML_TM_FOLDER') || $_GET['page'] != WPML_TM_FOLDER . '/menu/translations-queue.php')
  418. ){
  419. if(!$this->is_rtl() && version_compare($GLOBALS['wp_version'], '3.3', '>')){
  420. add_action('admin_notices', 'wpml_set_admin_language_switcher_place', 100);
  421. function wpml_set_admin_language_switcher_place(){
  422. echo '<br clear="all" />';
  423. }
  424. }
  425. add_action('in_admin_header', array($this, 'admin_language_switcher'));
  426. }
  427. if(!is_admin() && defined('DISQUS_VERSION')) include ICL_PLUGIN_PATH . '/modules/disqus.php';
  428. }
  429. if($this->is_rtl()){
  430. $GLOBALS['text_direction'] = 'rtl';
  431. }
  432. // Automatic redirect
  433. if(!is_admin() && !empty($this->settings['automatic_redirect'])){
  434. add_action('template_redirect', array($this, 'automatic_redirect'));
  435. }
  436. if(is_admin() && empty($this->settings['dont_show_help_admin_notice'])){
  437. if(count($this->get_active_languages()) < 2){
  438. add_action('admin_notices', array($this, 'help_admin_notice'));
  439. }
  440. }
  441. $short_v = implode('.', array_slice(explode('.', ICL_SITEPRESS_VERSION), 0, 3));
  442. if(is_admin() && (!isset($this->settings['hide_upgrade_notice']) || $this->settings['hide_upgrade_notice'] != $short_v)){
  443. add_action('admin_notices', array($this, 'upgrade_notice'));
  444. }
  445. if(is_admin() && current_user_can('manage_options')){
  446. if($this->icl_account_configured()) {
  447. add_action('admin_notices', array($this, 'icl_reminders'));
  448. }
  449. }
  450. require ICL_PLUGIN_PATH . '/inc/template-constants.php';
  451. if(defined('WPML_LOAD_API_SUPPORT')){
  452. require ICL_PLUGIN_PATH . '/inc/wpml-api.php';
  453. }
  454. add_action('wp_footer', array($this, 'display_wpml_footer'),20);
  455. if(defined('XMLRPC_REQUEST') && XMLRPC_REQUEST){
  456. add_action('xmlrpc_call', array($this, 'xmlrpc_call_actions'));
  457. add_filter('xmlrpc_methods',array($this, 'xmlrpc_methods'));
  458. }
  459. if(defined('WPML_TM_VERSION') && is_admin()){
  460. require ICL_PLUGIN_PATH . '/inc/quote.php';
  461. }
  462. }
  463. function setup(){
  464. return !empty($this->settings['setup_complete']);
  465. }
  466. function automatic_redirect(){
  467. $save_cookie = false;
  468. if(empty($_COOKIE['_icl_visitor_lang'])){
  469. //No cookie found
  470. $lang = $this->get_current_language();
  471. $langs = @explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  472. foreach($langs as $lang){
  473. $lang = preg_replace('#q=(.+)$#', '', $lang);
  474. $lang = str_replace('-', '_', $lang); // normalize
  475. if(!in_array($lang, array_keys($this->get_active_languages()))){
  476. $lang = substr($lang, 0, strpos($lang, '_'));
  477. }
  478. if(in_array($lang, array_keys($this->get_active_languages()))){
  479. //'Got lang from browser: %s', $lang
  480. $save_cookie = true;
  481. break;
  482. }
  483. }
  484. }else{
  485. //'Cookie found: %s', $_COOKIE['_icl_visitor_lang']
  486. if($_COOKIE['_icl_visitor_lang'] != $this->get_current_language()){
  487. //'User changed language: %s', $this->get_current_language())
  488. $lang = $this->get_current_language();
  489. $save_cookie = true;
  490. }else{
  491. //'Using cookie lang: %s', $_COOKIE['_icl_visitor_lang']
  492. $lang = $_COOKIE['_icl_visitor_lang'];
  493. }
  494. }
  495. if($save_cookie){
  496. $cookie_domain = defined('COOKIE_DOMAIN') ? COOKIE_DOMAIN : $_SERVER['HTTP_HOST'];
  497. $cookie_path = defined('COOKIEPATH') ? COOKIEPATH : '/';
  498. $s = setcookie('_icl_visitor_lang', $lang, time()+3600 * intval($this->settings['remember_language']), $cookie_path, $cookie_domain);
  499. //'Saving cookie: %s', $lang
  500. }
  501. if($lang != $this->get_current_language()){
  502. //'Doing redirect from %s to %s', $this->get_current_language(), $lang
  503. if($this->settings['automatic_redirect'] == 1){
  504. $args['skip_missing'] = 1;
  505. }else{
  506. $args['skip_missing'] = 0;
  507. }
  508. $languages = $this->get_ls_languages($args);
  509. if(isset($languages[$lang])){
  510. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  511. header("Expires: Sat, 16 Aug 1980 05:00:00 GMT"); // Date in the past
  512. if(false !== strpos($_SERVER['HTTP_USER_AGENT'], 'Safari')){
  513. $http_code = 307;
  514. }else{
  515. $http_code = 300;
  516. }
  517. wp_redirect($languages[$lang]['url'], $http_code);
  518. exit;
  519. }
  520. }
  521. //'No redirect needed.'
  522. }
  523. function ajax_setup(){
  524. require ICL_PLUGIN_PATH . '/ajax.php';
  525. }
  526. function configure_custom_column(){
  527. global $pagenow, $wp_post_types;
  528. $pagenow_ = '';
  529. $is_ajax = false;
  530. if($pagenow == 'admin-ajax.php'){
  531. if(
  532. isset($_POST['action']) && $_POST['action']=='inline-save' ||
  533. isset($_GET['action']) && $_GET['action'] == 'fetch-list'
  534. ){
  535. $is_ajax = true;
  536. }
  537. }
  538. if(($pagenow == 'edit.php' || $pagenow_ == 'edit-pages.php' || $is_ajax)){
  539. $post_type = isset($_REQUEST['post_type']) ? $_REQUEST['post_type'] : 'post';
  540. switch($post_type){
  541. case 'post': case 'page':
  542. add_filter('manage_'.$post_type.'s_columns',array($this,'add_posts_management_column'));
  543. if(isset($_GET['post_type']) && $_GET['post_type']=='page'){
  544. add_action('manage_'.$post_type.'s_custom_column',array($this,'add_content_for_posts_management_column'));
  545. }
  546. add_action('manage_posts_custom_column',array($this,'add_content_for_posts_management_column'));
  547. break;
  548. default:
  549. if(in_array($post_type, array_keys($this->get_translatable_documents()))){
  550. add_filter('manage_'.$post_type.'_posts_columns',array($this,'add_posts_management_column'));
  551. if($wp_post_types[$post_type]->hierarchical){
  552. add_action('manage_pages_custom_column',array($this,'add_content_for_posts_management_column'));
  553. add_action('manage_posts_custom_column',array($this,'add_content_for_posts_management_column')); // add this too - for more types plugin
  554. }else{
  555. add_action('manage_posts_custom_column',array($this,'add_content_for_posts_management_column'));
  556. }
  557. }
  558. }
  559. add_action('admin_print_scripts', array($this, '__set_posts_management_column_width'));
  560. }
  561. }
  562. function _taxonomy_languages_menu(){
  563. // tags language selection
  564. global $pagenow, $wpdb;
  565. if($pagenow == 'edit-tags.php'){
  566. // handle case of the tax edit page (after a taxonomy has been added)
  567. // needs to redirect back to
  568. if(isset($_GET['trid']) && isset($_GET['source_lang'])){
  569. $translations = $this->get_element_translations($_GET['trid'], 'tax_' . $_GET['taxonomy']);
  570. if(isset($translations[$_GET['lang']])){
  571. wp_redirect(get_edit_term_link($translations[$_GET['lang']]->term_id, $_GET['taxonomy']));
  572. exit;
  573. }else{
  574. add_action( 'admin_notices', array($this, '_tax_adding') );
  575. }
  576. }
  577. $taxonomy = isset($_GET['taxonomy']) ? $wpdb->escape($_GET['taxonomy']) : 'post_tag';
  578. if($this->is_translated_taxonomy($taxonomy)){
  579. add_action('admin_print_scripts-edit-tags.php', array($this,'js_scripts_tags'));
  580. if($taxonomy == 'category'){
  581. add_action('edit_category_form', array($this, 'edit_term_form'));
  582. }else{
  583. add_action('add_tag_form', array($this, 'edit_term_form'));
  584. add_action('edit_tag_form', array($this, 'edit_term_form'));
  585. }
  586. add_action('admin_footer', array($this,'terms_language_filter'));
  587. add_filter('wp_dropdown_cats', array($this, 'wp_dropdown_cats_select_parent'));
  588. }
  589. }
  590. }
  591. function _tax_adding(){
  592. $translations = $this->get_element_translations($_GET['trid'], 'tax_' . $_GET['taxonomy']);
  593. if(!empty($translations) && isset($translations[$_GET['source_lang']]->name)){
  594. $tax_name = apply_filters('the_category', $translations[$_GET['source_lang']]->name);
  595. echo '<div id="icl_tax_adding_notice" class="updated fade"><p>'. sprintf(__('Adding translation for: %s.', 'sitepress'), $tax_name). '</p></div>';
  596. }
  597. }
  598. function the_posts($posts){
  599. global $wpdb, $wp_query;
  600. $db = debug_backtrace();
  601. $custom_wp_query = isset($db[3]['object']) ? $db[3]['object'] : false;
  602. //exceptions
  603. if(
  604. ($this->get_current_language() == $this->get_default_language()) // original language
  605. || ($wp_query != $custom_wp_query) // called by a custom query
  606. || (!$custom_wp_query->is_posts_page && !$custom_wp_query->is_home) // not the blog posts page
  607. || $wp_query->is_singular //is singular
  608. || !empty($custom_wp_query->query_vars['category__not_in'])
  609. //|| !empty($custom_wp_query->query_vars['category__in'])
  610. //|| !empty($custom_wp_query->query_vars['category__and'])
  611. || !empty($custom_wp_query->query_vars['tag__not_in'])
  612. || !empty($custom_wp_query->query_vars['post__in'])
  613. || !empty($custom_wp_query->query_vars['post__not_in'])
  614. || !empty($custom_wp_query->query_vars['post_parent'])
  615. ){
  616. //$wp_query->query_vars = $this->wp_query->query_vars;
  617. return $posts;
  618. }
  619. // get the posts in the default language instead
  620. $this_lang = $this->this_lang;
  621. $this->this_lang = $this->get_default_language();
  622. remove_filter('the_posts', array($this, 'the_posts'));
  623. $custom_wp_query->query_vars['suppress_filters'] = 0;
  624. if(isset($custom_wp_query->query_vars['pagename']) && !empty($custom_wp_query->query_vars['pagename'])){
  625. if (isset($custom_wp_query->queried_object_id) && !empty($custom_wp_query->queried_object_id)) {
  626. $page_id = $custom_wp_query->queried_object_id;
  627. } else {
  628. // urlencode added for languages that have urlencoded post_name field value
  629. $custom_wp_query->query_vars['pagename'] = urlencode($custom_wp_query->query_vars['pagename']);
  630. $page_id = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_name='{$custom_wp_query->query_vars['pagename']}' AND post_type='page'");
  631. }
  632. if($page_id){
  633. $tr_page_id = icl_object_id($page_id, 'page', false, $this->get_default_language());
  634. if($tr_page_id){
  635. $custom_wp_query->query_vars['pagename'] = $wpdb->get_var("SELECT post_name FROM {$wpdb->posts} WHERE ID={$tr_page_id}");
  636. }
  637. }
  638. }
  639. // look for posts without translations
  640. if($posts){
  641. foreach($posts as $p){
  642. $pids[] = $p->ID;
  643. }
  644. $trids = $wpdb->get_col("
  645. SELECT trid
  646. FROM {$wpdb->prefix}icl_translations
  647. WHERE element_type='post_post' AND element_id IN (".join(',', $pids).") AND language_code = '".$this_lang."'");
  648. if(!empty($trids)){
  649. $posts_not_translated = $wpdb->get_col("
  650. SELECT element_id, COUNT(language_code) AS c
  651. FROM {$wpdb->prefix}icl_translations
  652. WHERE trid IN (".join(',', $trids).") GROUP BY trid HAVING c = 1
  653. ");
  654. if(!empty($posts_not_translated)){
  655. $GLOBALS['__icl_the_posts_posts_not_translated'] = $posts_not_translated;
  656. add_filter('posts_where', array($this, '_posts_untranslated_extra_posts_where'), 99);
  657. }
  658. }
  659. }
  660. //fix page for posts
  661. unset($custom_wp_query->query_vars['page_id']); unset($custom_wp_query->query_vars['p']);
  662. $my_query = new WP_Query($custom_wp_query->query_vars);
  663. add_filter('the_posts', array($this, 'the_posts'));
  664. $this->this_lang = $this_lang;
  665. // create a map of the translated posts
  666. foreach($posts as $post){
  667. $trans_posts[$post->ID] = $post;
  668. }
  669. // loop original posts
  670. foreach($my_query->posts as $k=>$post){ // loop posts in the default language
  671. $trid = $this->get_element_trid($post->ID);
  672. $translations = $this->get_element_translations($trid); // get translations
  673. if(isset($translations[$this->get_current_language()])){ // if there is a translation in the current language
  674. if(isset($trans_posts[$translations[$this->get_current_language()]->element_id])){ //check the map of translated posts
  675. $my_query->posts[$k] = $trans_posts[$translations[$this->get_current_language()]->element_id];
  676. }else{ // check if the translated post exists in the database still
  677. $_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));
  678. if(!empty($_post)){
  679. $_post = sanitize_post($_post);
  680. $my_query->posts[$k] = $_post;
  681. }else{
  682. $my_query->posts[$k]->original_language = true;
  683. }
  684. }
  685. }else{
  686. $my_query->posts[$k]->original_language = true;
  687. }
  688. }
  689. if($custom_wp_query == $wp_query){
  690. $wp_query->max_num_pages = $my_query->max_num_pages;
  691. }
  692. $posts = $my_query->posts;
  693. unset($GLOBALS['__icl_the_posts_posts_not_translated']);
  694. remove_filter('posts_where', array($this, '_posts_untranslated_extra_posts_where'), 99);
  695. return $posts;
  696. }
  697. function _posts_untranslated_extra_posts_where($where){
  698. global $wpdb;
  699. $where .= ' OR ' . $wpdb->posts . '.ID IN (' . join(',', $GLOBALS['__icl_the_posts_posts_not_translated']) . ')';
  700. return $where;
  701. }
  702. function initialize_cache(){
  703. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  704. $this->icl_translations_cache = new icl_cache();
  705. $this->icl_locale_cache = new icl_cache('locale', true);
  706. $this->icl_flag_cache = new icl_cache('flags', true);
  707. $this->icl_language_name_cache = new icl_cache('language_name', true);
  708. $this->icl_term_taxonomy_cache = new icl_cache();
  709. }
  710. function set_admin_language(){
  711. global $wpdb, $current_user;
  712. if(is_null($current_user) && function_exists('wp_get_current_user')){
  713. $u = wp_get_current_user();
  714. if(isset($u->ID) && $u->ID > 0){
  715. $current_user = $u;
  716. }else{
  717. return $this->get_default_language();
  718. }
  719. }
  720. $active_languages = array_keys($wpdb->get_col("SELECT code FROM {$wpdb->prefix}icl_languages WHERE active=1")); //don't use method get_active_language()
  721. if(!empty($current_user->data->ID)){
  722. $this->admin_language = $this->get_user_admin_language($current_user->data->ID);
  723. }
  724. if($this->admin_language != '' && !in_array($this->admin_language, $active_languages)){
  725. delete_user_meta($current_user->data->ID,'icl_admin_language');
  726. }
  727. if(empty($this->settings['admin_default_language']) || !in_array($this->settings['admin_default_language'], $active_languages)){
  728. $this->settings['admin_default_language'] = '_default_';
  729. $this->save_settings();
  730. }
  731. if(!$this->admin_language){
  732. $this->admin_language = $this->settings['admin_default_language'];
  733. }
  734. if($this->admin_language == '_default_' && $this->get_default_language()){
  735. $this->admin_language = $this->get_default_language();
  736. }
  737. }
  738. function get_admin_language(){
  739. return $this->admin_language;
  740. }
  741. function get_user_admin_language($user_id) {
  742. static $lang = array();
  743. if (!isset($lang[$user_id])) {
  744. $lang[$user_id] = get_user_meta($user_id,'icl_admin_language',true);
  745. if(empty($lang[$user_id])){
  746. if(isset($this->settings['admin_default_language'])){
  747. $lang[$user_id] = $this->settings['admin_default_language'];
  748. }
  749. if(empty($lang[$user_id]) || '_default_' == $lang[$user_id]){
  750. $lang[$user_id] = $this->get_admin_language();
  751. }
  752. }
  753. }
  754. return $lang[$user_id];
  755. }
  756. function administration_menu(){
  757. if (1 < count($this->get_active_languages())) {
  758. $main_page = apply_filters('icl_menu_main_page', basename(ICL_PLUGIN_PATH).'/menu/languages.php');
  759. add_menu_page(__('WPML','sitepress'), __('WPML','sitepress'), 'manage_options',
  760. $main_page, null, ICL_PLUGIN_URL . '/res/img/icon16.png');
  761. do_action('icl_wpml_top_menu_added');
  762. add_submenu_page($main_page,
  763. __('Languages','sitepress'), __('Languages','sitepress'),
  764. 'manage_options', basename(ICL_PLUGIN_PATH).'/menu/languages.php');
  765. add_submenu_page($main_page,
  766. __('Theme and plugins localization','sitepress'), __('Theme and plugins localization','sitepress'),
  767. 'manage_options', basename(ICL_PLUGIN_PATH).'/menu/theme-localization.php');
  768. if(!defined('WPML_TM_VERSION')){
  769. add_submenu_page($main_page,
  770. __('Translation options','sitepress'), __('Translation options','sitepress'),
  771. 'manage_options', basename(ICL_PLUGIN_PATH).'/menu/translation-options.php');
  772. }
  773. /*
  774. add_submenu_page($main_page,
  775. __('Comments translation','sitepress'), __('Comments translation','sitepress'),
  776. 'manage_options', basename(ICL_PLUGIN_PATH).'/menu/comments-translation.php');
  777. */
  778. } else {
  779. $main_page = basename(ICL_PLUGIN_PATH).'/menu/languages.php';
  780. add_menu_page(__('WPML','sitepress'), __('WPML','sitepress'), 'manage_options',
  781. $main_page,null, ICL_PLUGIN_URL . '/res/img/icon16.png');
  782. add_submenu_page($main_page,
  783. __('Languages','sitepress'), __('Languages','sitepress'),
  784. 'manage_options', basename(ICL_PLUGIN_PATH).'/menu/languages.php');
  785. }
  786. if(isset($_GET['page']) && $_GET['page'] == basename(ICL_PLUGIN_PATH).'/menu/troubleshooting.php'){
  787. add_submenu_page($main_page,
  788. __('Troubleshooting','sitepress'), __('Troubleshooting','sitepress'),
  789. 'manage_options', basename(ICL_PLUGIN_PATH).'/menu/troubleshooting.php');
  790. }
  791. //$alert = '&nbsp;<img width="12" height="12" style="margin-bottom:-2px;" src="'.ICL_PLUGIN_URL.'/res/img/alert.png" />';
  792. add_submenu_page($main_page,
  793. __('Support','sitepress'), __('Support','sitepress'), 'manage_options', ICL_PLUGIN_FOLDER . '/menu/support.php');
  794. }
  795. function save_settings($settings=null){
  796. if(!is_null($settings)){
  797. foreach($settings as $k=>$v){
  798. if(is_array($v)){
  799. foreach($v as $k2=>$v2){
  800. $this->settings[$k][$k2] = $v2;
  801. }
  802. }else{
  803. $this->settings[$k] = $v;
  804. }
  805. }
  806. }
  807. if(!empty($this->settings)){
  808. update_option('icl_sitepress_settings', $this->settings);
  809. }
  810. do_action('icl_save_settings', $settings);
  811. }
  812. function get_settings(){
  813. return $this->settings;
  814. }
  815. function get_option($option_name){
  816. return isset($this->settings[$option_name]) ? $this->settings[$option_name] : null;
  817. }
  818. function verify_settings(){
  819. $default_settings = array(
  820. 'interview_translators' => 1,
  821. 'existing_content_language_verified' => 0,
  822. 'language_negotiation_type' => 3,
  823. 'theme_localization_type' => 1,
  824. 'icl_lso_header' => 0,
  825. 'icl_lso_link_empty' => 0,
  826. 'icl_lso_flags' => 0,
  827. 'icl_lso_native_lang' => 1,
  828. 'icl_lso_display_lang' => 1,
  829. 'sync_page_ordering' => 1,
  830. 'sync_page_parent' => 1,
  831. 'sync_page_template' => 1,
  832. 'sync_ping_status' => 1,
  833. 'sync_comment_status' => 1,
  834. 'sync_sticky_flag' => 1,
  835. 'sync_private_flag' => 1,
  836. 'sync_post_format' => 1,
  837. 'sync_delete' => 0,
  838. 'sync_post_taxonomies' => 1,
  839. 'sync_post_date' => 0,
  840. 'sync_taxonomy_parents' => 0,
  841. 'translation_pickup_method' => 0,
  842. 'notify_complete' => 1,
  843. 'translated_document_status' => 1,
  844. 'remote_management' => 0,
  845. 'auto_adjust_ids' => 1,
  846. 'alert_delay' => 0,
  847. 'promote_wpml' => 0,
  848. 'troubleshooting_options' => array('http_communication' => 1),
  849. 'automat…

Large files files are truncated, but you can click here to view the full file