PageRenderTime 91ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 2ms

/Dossier wordpress/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php

https://github.com/amo23/PFE
PHP | 10691 lines | 8473 code | 1550 blank | 668 comment | 2434 complexity | 221a93d39a7e34d370cbd8525238d229 MD5 | raw file

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

  1. <?php
  2. /**
  3. * Main SitePress Class
  4. *
  5. * @package wpml-core
  6. */
  7. class SitePress
  8. {
  9. private $settings;
  10. private $active_languages = array();
  11. private $this_lang;
  12. private $wp_query;
  13. private $admin_language = null;
  14. private $user_preferences = array();
  15. private $current_user;
  16. public $queries = array();
  17. /**
  18. * @var icl_cache
  19. */
  20. public $icl_translations_cache;
  21. /**
  22. * @var icl_cache
  23. */
  24. public $icl_locale_cache;
  25. /**
  26. * @var icl_cache
  27. */
  28. public $icl_flag_cache;
  29. /**
  30. * @var icl_cache
  31. */
  32. public $icl_language_name_cache;
  33. /**
  34. * @var icl_cache
  35. */
  36. public $icl_term_taxonomy_cache;
  37. function __construct()
  38. {
  39. global $wpdb, $pagenow;
  40. $this->settings = get_option( 'icl_sitepress_settings' );
  41. //TODO: To remove in WPML 3.5
  42. //@since 3.1
  43. if(is_admin() && !$this->get_setting('icl_capabilities_verified')) {
  44. icl_enable_capabilities();
  45. $this->settings = get_option( 'icl_sitepress_settings' );
  46. }
  47. // set up current user early
  48. // no authentication
  49. if ( defined( 'LOGGED_IN_COOKIE' ) && isset( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
  50. list( $username ) = explode( '|', $_COOKIE[ LOGGED_IN_COOKIE ] );
  51. $user_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->users} WHERE user_login = %s", array($username) ) );
  52. } else {
  53. $user_id = 0;
  54. }
  55. $this->current_user = new WP_User( $user_id );
  56. if ( is_null( $pagenow ) && is_multisite() ) {
  57. include ICL_PLUGIN_PATH . '/inc/hacks/vars-php-multisite.php';
  58. }
  59. if ( false != $this->settings ) {
  60. $this->verify_settings();
  61. }
  62. if ( isset( $_GET[ 'icl_action' ] ) ) {
  63. require_once ABSPATH . WPINC . '/pluggable.php';
  64. if ( $_GET[ 'icl_action' ] == 'reminder_popup' ) {
  65. add_action( 'init', array( $this, 'reminders_popup' ) );
  66. } elseif ( $_GET[ 'icl_action' ] == 'dismiss_help' ) {
  67. $this->settings[ 'dont_show_help_admin_notice' ] = true;
  68. $this->save_settings();
  69. }
  70. }
  71. if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == ICL_PLUGIN_FOLDER . '/menu/troubleshooting.php' && isset( $_GET[ 'debug_action' ] ) ) {
  72. ob_start();
  73. }
  74. if ( isset( $_REQUEST[ 'icl_ajx_action' ] ) ) {
  75. add_action( 'init', array( $this, 'ajax_setup' ), 15 );
  76. }
  77. add_action( 'admin_footer', array( $this, 'icl_nonces' ) );
  78. // Process post requests
  79. if ( !empty( $_POST ) ) {
  80. add_action( 'init', array( $this, 'process_forms' ) );
  81. }
  82. add_action( 'plugins_loaded', array( $this, 'initialize_cache' ), 0 );
  83. add_action( 'plugins_loaded', array( $this, 'init' ), 1 );
  84. add_action( 'init', array( $this, 'on_wp_init' ), 1 );
  85. add_action( 'admin_print_scripts', array( $this, 'js_scripts_setup' ) );
  86. add_action( 'admin_print_styles', array( $this, 'css_setup' ) );
  87. // Administration menus
  88. add_action( 'admin_menu', array( $this, 'administration_menu' ) );
  89. add_action( 'admin_menu', array( $this, 'administration_menu2' ), 30 );
  90. add_action( 'init', array( $this, 'plugin_localization' ) );
  91. //add_filter('tag_template', array($this, 'load_taxonomy_template'));
  92. if ( $this->settings[ 'existing_content_language_verified' ] ) {
  93. // Post/page language box
  94. if ( $pagenow == 'post.php' || $pagenow == 'post-new.php' || $pagenow == 'edit.php' ) {
  95. add_action( 'admin_head', array( $this, 'post_edit_language_options' ) );
  96. }
  97. //For when it will be possible to add custom bulk actions
  98. //add_action( 'bulk_actions-edit-post', array( $this, 'bulk_actions' ) );
  99. // Post/page save actions
  100. add_action( 'save_post', array( $this, 'save_post_actions' ), 10, 2 );
  101. //post/page delete taxonomy
  102. add_action( 'deleted_term_relationships', array( $this, 'deleted_term_relationships' ), 10, 2 );
  103. add_action( 'updated_post_meta', array( $this, 'update_post_meta' ), 100, 4 );
  104. add_action( 'added_post_meta', array( $this, 'update_post_meta' ), 100, 4 );
  105. add_action( 'updated_postmeta', array( $this, 'update_post_meta' ), 100, 4 ); // ajax
  106. add_action( 'added_postmeta', array( $this, 'update_post_meta' ), 100, 4 ); // ajax
  107. add_action( 'delete_postmeta', array( $this, 'delete_post_meta' ) ); // ajax
  108. // filter user taxonomy input
  109. add_filter( 'pre_post_tax_input', array( $this, 'validate_taxonomy_input' ) );
  110. // Post/page delete actions
  111. // add_action('delete_post', array($this,'delete_post_actions'));
  112. add_action( 'before_delete_post', array( $this, 'before_delete_post_actions' ) );
  113. add_action( 'deleted_post', array( $this, 'deleted_post_actions' ) );
  114. add_action( 'wp_trash_post', array( $this, 'trash_post_actions' ) );
  115. add_action( 'untrashed_post', array( $this, 'untrashed_post_actions' ) );
  116. add_filter( 'posts_join', array( $this, 'posts_join_filter' ), 10, 2 );
  117. add_filter( 'posts_where', array( $this, 'posts_where_filter' ), 10, 2 );
  118. add_filter( 'comment_feed_join', array( $this, 'comment_feed_join' ) );
  119. add_filter( 'comments_clauses', array( $this, 'comments_clauses' ), 10, 2 );
  120. // Allow us to filter the Query vars before the posts query is being built and executed
  121. add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
  122. add_action( 'loop_start', array( $this, 'loop_start' ), 10 );
  123. add_filter( 'the_posts', array( $this, 'the_posts' ), 10 );
  124. add_filter( 'get_pages', array( $this, 'get_pages' ), 100, 2 );
  125. if ( $pagenow == 'edit.php' ) {
  126. add_action( 'admin_footer', array( $this, 'language_filter' ) );
  127. }
  128. add_filter( 'get_pages', array( $this, 'exclude_other_language_pages2' ) );
  129. add_filter( 'wp_dropdown_pages', array( $this, 'wp_dropdown_pages' ) );
  130. // posts and pages links filters
  131. add_filter( 'post_link', array( $this, 'permalink_filter' ), 1, 2 );
  132. add_filter( 'post_type_link', array( $this, 'permalink_filter' ), 1, 2 );
  133. add_filter( 'page_link', array( $this, 'permalink_filter' ), 1, 2 );
  134. add_filter( 'get_comment_link', array( $this, 'get_comment_link_filter' ) );
  135. //Taxonomies
  136. if ( version_compare( preg_replace( '#-RC[0-9]+(-[0-9]+)?$#', '', $GLOBALS[ 'wp_version' ] ), '3.1', '<' ) ) {
  137. add_filter( 'category_link', array( $this, 'category_permalink_filter' ), 1, 2 );
  138. add_filter( 'tag_link', array( $this, 'tax_permalink_filter' ), 1, 2 );
  139. }
  140. add_filter( 'term_link', array( $this, 'tax_permalink_filter' ), 1, 2 );
  141. add_action( 'create_term', array( $this, 'create_term' ), 1, 2 );
  142. add_action( 'edit_term', array( $this, 'create_term' ), 1, 2 );
  143. add_action( 'delete_term', array( $this, 'delete_term' ), 1, 3 );
  144. add_action( 'get_term', array( $this, 'get_term_filter' ), 1, 2 );
  145. add_filter( 'get_terms_args', array( $this, 'get_terms_args_filter' ) );
  146. // filters terms by language
  147. add_filter( 'terms_clauses', array( $this, 'terms_clauses' ), 10, 4 );
  148. add_filter( 'list_terms_exclusions', array( $this, 'exclude_other_terms' ), 1, 2 );
  149. // allow adding terms with the same name in different languages
  150. add_filter( "pre_term_name", array( $this, 'pre_term_name' ), 1, 2 );
  151. // allow adding categories with the same name in different languages
  152. add_action( 'admin_init', array( $this, 'pre_save_category' ) );
  153. //Hooking to 'option_{taxonomy}_children' to translate taxonomy children
  154. // global $wp_taxonomies;
  155. // foreach ( $wp_taxonomies as $tax_key => $tax ) {
  156. // if ( $this->is_translated_taxonomy( $tax_key ) && is_taxonomy_hierarchical($tax_key) ) {
  157. // add_filter("option_{$tax_key}_children", array($this, 'option_taxonomy_children'), 10 );
  158. // add_filter("pre_update_option_{$tax_key}_children", array($this, 'pre_update_option_taxonomy_children'), 10, 2 );
  159. // }
  160. // }
  161. add_filter( 'get_edit_term_link', array( $this, 'get_edit_term_link' ), 1, 4 );
  162. // short circuit get default category
  163. add_filter( 'pre_option_default_category', array( $this, 'pre_option_default_category' ) );
  164. add_filter( 'update_option_default_category', array( $this, 'update_option_default_category' ), 1, 2 );
  165. add_filter( 'the_category', array( $this, 'the_category_name_filter' ) );
  166. add_filter( 'get_terms', array( $this, 'get_terms_filter' ) );
  167. add_filter( 'get_the_terms', array( $this, 'get_the_terms_filter' ), 10, 3 );
  168. add_filter( 'single_cat_title', array( $this, 'the_category_name_filter' ) );
  169. add_filter( 'term_links-category', array( $this, 'the_category_name_filter' ) );
  170. add_filter( 'term_links-post_tag', array( $this, 'the_category_name_filter' ) );
  171. add_filter( 'tags_to_edit', array( $this, 'the_category_name_filter' ) );
  172. add_filter( 'single_tag_title', array( $this, 'the_category_name_filter' ) );
  173. // custom hook for adding the language selector to the template
  174. add_action( 'icl_language_selector', array( $this, 'language_selector' ) );
  175. // front end js
  176. add_action( 'wp_head', array( $this, 'front_end_js' ) );
  177. add_action( 'wp_head', array( $this, 'rtl_fix' ) );
  178. add_action( 'admin_print_styles', array( $this, 'rtl_fix' ) );
  179. add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) );
  180. add_filter( 'get_edit_post_link', array( $this, 'get_edit_post_link' ), 1, 3 );
  181. // adjacent posts links
  182. add_filter( 'get_previous_post_join', array( $this, 'get_adjacent_post_join' ) );
  183. add_filter( 'get_next_post_join', array( $this, 'get_adjacent_post_join' ) );
  184. add_filter( 'get_previous_post_where', array( $this, 'get_adjacent_post_where' ) );
  185. add_filter( 'get_next_post_where', array( $this, 'get_adjacent_post_where' ) );
  186. // feeds links
  187. add_filter( 'feed_link', array( $this, 'feed_link' ) );
  188. // commenting links
  189. add_filter( 'post_comments_feed_link', array( $this, 'post_comments_feed_link' ) );
  190. add_filter( 'trackback_url', array( $this, 'trackback_url' ) );
  191. add_filter( 'user_trailingslashit', array( $this, 'user_trailingslashit' ), 1, 2 );
  192. // date based archives
  193. add_filter( 'year_link', array( $this, 'archives_link' ) );
  194. add_filter( 'month_link', array( $this, 'archives_link' ) );
  195. add_filter( 'day_link', array( $this, 'archives_link' ) );
  196. add_filter( 'getarchives_join', array( $this, 'getarchives_join' ) );
  197. add_filter( 'getarchives_where', array( $this, 'getarchives_where' ) );
  198. add_filter( 'pre_option_home', array( $this, 'pre_option_home' ) );
  199. if ( !is_admin() ) {
  200. add_filter( 'attachment_link', array( $this, 'attachment_link_filter' ), 10, 2 );
  201. }
  202. // Filter custom type archive link (since WP 3.1)
  203. add_filter( 'post_type_archive_link', array( $this, 'post_type_archive_link_filter' ), 10, 2 );
  204. add_filter( 'author_link', array( $this, 'author_link' ) );
  205. add_filter( 'wp_unique_post_slug', array( $this, 'wp_unique_post_slug' ), 100, 5 );
  206. add_filter( 'home_url', array( $this, 'home_url' ), 1, 4 );
  207. // language negotiation
  208. add_action( 'query_vars', array( $this, 'query_vars' ) );
  209. add_filter( 'language_attributes', array( $this, 'language_attributes' ) );
  210. add_action( 'locale', array( $this, 'locale' ) );
  211. if ( isset( $_GET[ '____icl_validate_domain' ] ) ) {
  212. echo '<!--' . get_home_url() . '-->';
  213. exit;
  214. }
  215. add_filter( 'pre_option_page_on_front', array( $this, 'pre_option_page_on_front' ) );
  216. add_filter( 'pre_option_page_for_posts', array( $this, 'pre_option_page_for_posts' ) );
  217. add_filter( 'option_sticky_posts', array( $this, 'option_sticky_posts' ) );
  218. add_filter( 'request', array( $this, 'request_filter' ) );
  219. add_action( 'wp_head', array( $this, 'set_wp_query' ) );
  220. add_action( 'show_user_profile', array( $this, 'show_user_options' ) );
  221. add_action( 'personal_options_update', array( $this, 'save_user_options' ) );
  222. // column with links to translations (or add translation) - low priority
  223. add_action( 'init', array( $this, 'configure_custom_column' ), 1010 ); // accommodate Types init@999
  224. // adjust queried categories and tags ids according to the language
  225. if ( $this->settings[ 'auto_adjust_ids' ] ) {
  226. add_action( 'parse_query', array( $this, 'parse_query' ) );
  227. add_action( 'wp_list_pages_excludes', array( $this, 'adjust_wp_list_pages_excludes' ) );
  228. if ( !is_admin() ) {
  229. add_filter( 'get_term', array( $this, 'get_term_adjust_id' ), 1, 1 );
  230. add_filter( 'category_link', array( $this, 'category_link_adjust_id' ), 1, 2 );
  231. add_filter( 'get_terms', array( $this, 'get_terms_adjust_ids' ), 1, 3 );
  232. add_filter( 'get_pages', array( $this, 'get_pages_adjust_ids' ), 1, 2 );
  233. }
  234. }
  235. if ( !is_admin() ) {
  236. add_action( 'wp_head', array( $this, 'meta_generator_tag' ) );
  237. }
  238. require_once ICL_PLUGIN_PATH . '/inc/wp-nav-menus/iclNavMenu.class.php';
  239. new iclNavMenu;
  240. if ( is_admin() || defined( 'XMLRPC_REQUEST' ) || preg_match( '#wp-comments-post\.php$#', $_SERVER[ 'REQUEST_URI' ] ) ) {
  241. global $iclTranslationManagement, $ICL_Pro_Translation;
  242. $iclTranslationManagement = new TranslationManagement;
  243. $ICL_Pro_Translation = new ICL_Pro_Translation();
  244. }
  245. add_action( 'wp_login', array( $this, 'reset_admin_language_cookie' ) );
  246. if ( $this->settings[ 'seo' ][ 'canonicalization_duplicates' ] ) {
  247. add_action( 'template_redirect', array( $this, 'setup_canonical_urls' ), 100 );
  248. }
  249. add_filter( 'taxonomy_template', array($this, 'slug_template') );
  250. add_filter( 'category_template', array($this, 'slug_template') );
  251. add_action( 'init', array( $this, '_taxonomy_languages_menu' ), 99 ); //allow hooking in
  252. if ( $this->settings[ 'seo' ][ 'head_langs' ] ) {
  253. add_action( 'wp_head', array( $this, 'head_langs' ) );
  254. }
  255. } //end if the initial language is set - existing_content_language_verified
  256. add_action( 'wp_dashboard_setup', array( $this, 'dashboard_widget_setup' ) );
  257. if ( is_admin() && $pagenow == 'index.php' ) {
  258. add_action( 'icl_dashboard_widget_notices', array( $this, 'print_translatable_custom_content_status' ) );
  259. }
  260. add_filter( 'core_version_check_locale', array( $this, 'wp_upgrade_locale' ) );
  261. if ( $pagenow == 'post.php' && isset( $_REQUEST[ 'action' ] ) && $_REQUEST[ 'action' ] == 'edit' && isset( $_GET[ 'post' ] ) ) {
  262. add_action( 'init', '_icl_trash_restore_prompt' );
  263. }
  264. add_action( 'init', array( $this, 'js_load' ), 2 ); // enqueue scripts - higher priority
  265. add_filter( 'get_pagenum_link', array( $this, 'get_pagenum_link' ) );
  266. add_action('switch_blog', array($this, 'init_settings'));
  267. add_filter('url_to_postid', array($this, 'url_to_postid'));
  268. }
  269. function get_pagenum_link( $url ) {
  270. // fix cases like that in url:
  271. // lang=pl%2F%3Flang%3Dpl
  272. // lang=pl/?lang=pl
  273. $current_language = $this->get_current_language();
  274. $find[ ] = 'lang=' . $current_language . '%2F%3Flang%3D' . $current_language;
  275. $find[ ] = 'lang=' . $current_language . '/?lang=' . $current_language;
  276. $replace = 'lang=' . $current_language;
  277. $url = str_replace( $find, $replace, $url );
  278. // fix cases like that:
  279. // ?lang=pl/page/3/?lang=pl
  280. $pattern = '/(\?lang=' . $current_language . ')(\/page\/\d+)/';
  281. $url = preg_replace( $pattern, '$2', $url );
  282. return $url;
  283. }
  284. function init()
  285. {
  286. global $wpdb;
  287. $this->get_user_preferences();
  288. $this->set_admin_language();
  289. $default_language = $this->get_default_language();
  290. // default value for theme_localization_type OR
  291. // reset theme_localization_type if string translation was on (theme_localization_type was set to 2) and then it was deactivated
  292. if ( !isset( $this->settings[ 'theme_localization_type' ] ) || ( $this->settings[ 'theme_localization_type' ] == 1 && !defined( 'WPML_ST_VERSION' ) && !defined( 'WPML_DOING_UPGRADE' ) ) ) {
  293. global $sitepress_settings;
  294. $this->settings[ 'theme_localization_type' ] = $sitepress_settings[ 'theme_localization_type' ] = 2;
  295. }
  296. //configure callbacks for plugin menu pages
  297. if ( defined( 'WP_ADMIN' ) && isset( $_GET[ 'page' ] ) && 0 === strpos( $_GET[ 'page' ], basename( ICL_PLUGIN_PATH ) . '/' ) ) {
  298. add_action( 'icl_menu_footer', array( $this, 'menu_footer' ) );
  299. }
  300. //Run only if existing content language has been verified, and is front-end or settings are not corrupted
  301. if (!empty( $this->settings[ 'existing_content_language_verified' ] ) && (!is_admin() || SitePress::check_settings_integrity()) ) {
  302. if ( $this->settings[ 'language_negotiation_type' ] == 1 && $this->settings[ 'urls' ][ 'directory_for_default_language' ] && $this->settings[ 'urls' ][ 'show_on_root' ] == 'page' ) {
  303. include ICL_PLUGIN_PATH . '/inc/home-url-functions.php';
  304. }
  305. if ($this->settings[ 'language_negotiation_type' ] == 2) {
  306. add_filter( 'allowed_redirect_hosts', array( $this, 'allowed_redirect_hosts' ) );
  307. }
  308. if ( defined( 'WP_ADMIN' ) ) {
  309. if ( $this->settings[ 'language_negotiation_type' ] == 2 ) {
  310. //Login and Logout
  311. add_filter( 'login_url', array( $this, 'convert_url' ) );
  312. add_filter( 'logout_url', array( $this, 'convert_url' ) );
  313. add_filter( 'site_url', array( $this, 'convert_url' ) );
  314. }
  315. if (isset($_GET['post']) && is_numeric($_GET['post'])) {
  316. $post_type = get_post_type($_GET['post']);
  317. }
  318. if ( isset( $_GET[ 'lang' ] ) ) {
  319. $this->this_lang = rtrim( strip_tags( $_GET[ 'lang' ] ), '/' );
  320. $al = $this->get_active_languages();
  321. $al[ 'all' ] = true;
  322. if ( empty( $al[ $this->this_lang ] ) ) {
  323. $this->this_lang = $default_language;
  324. }
  325. // force default language for string translation
  326. // we also make sure it's not saved in the cookie
  327. } elseif ( isset( $_GET[ 'page' ] ) && ( ( defined( 'WPML_ST_FOLDER' ) && $_GET[ 'page' ] == WPML_ST_FOLDER . '/menu/string-translation.php' ) || ( defined( 'WPML_TM_FOLDER' ) && $_GET[ 'page' ] == WPML_TM_FOLDER . '/menu/translations-queue.php' ) )
  328. ) {
  329. $this->this_lang = $default_language;
  330. } elseif ( wpml_is_ajax() ) {
  331. $al = $this->get_active_languages();
  332. if ( isset( $_POST[ 'lang' ] ) && isset( $al[ $_POST[ 'lang' ] ] ) ) {
  333. $this->this_lang = $_POST[ 'lang' ];
  334. } else {
  335. $this->this_lang = $this->get_language_cookie();
  336. }
  337. } elseif ( $lang = $this->get_admin_language_cookie() ) {
  338. $this->this_lang = $lang;
  339. } elseif ( isset($_POST['action']) && $_POST['action'] == 'editpost' && isset($_POST['icl_post_language'])) {
  340. $this->this_lang = $_POST['icl_post_language'];
  341. } elseif ( isset($_GET['post']) && is_numeric($_GET['post']) && isset($post_type) && $this->is_translated_post_type($post_type)) {
  342. $this->this_lang = $this->get_language_for_element($_GET['post'], 'post_' . $post_type);
  343. } else {
  344. $this->this_lang = $default_language;
  345. }
  346. 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' ) ) {
  347. $this->set_admin_language_cookie();
  348. }
  349. } else {
  350. $al = $this->get_active_languages();
  351. foreach ( $al as $l ) {
  352. $active_languages[ ] = $l[ 'code' ];
  353. }
  354. $active_languages[ ] = 'all';
  355. $s = isset( $_SERVER[ 'HTTPS' ] ) && $_SERVER[ 'HTTPS' ] == 'on' ? 's' : '';
  356. $home = get_home_url();
  357. if ( $s ) {
  358. $home = preg_replace( '#^http://#', 'https://', $home );
  359. }
  360. $url_parts = parse_url( $home );
  361. $non_default_port = ( isset( $url_parts[ 'port' ] ) && $url_parts[ 'port' ] != 80 ) ? ':' . $url_parts[ 'port' ] : '';
  362. $request = 'http' . $s . '://' . $this->get_server_host_name() . $_SERVER[ 'REQUEST_URI' ];
  363. $blog_path = ! empty( $url_parts[ 'path' ] ) ? $url_parts[ 'path' ] : '';
  364. switch ( $this->settings[ 'language_negotiation_type' ] ) {
  365. case 1:
  366. $path = str_replace( $home, '', $request );
  367. $parts = explode( '?', $path );
  368. $path = $parts[ 0 ];
  369. $exp = explode( '/', trim( $path, '/' ) );
  370. $language_part = $exp[ 0 ];
  371. if ( in_array( $language_part, $active_languages ) ) {
  372. $this->this_lang = $exp[ 0 ];
  373. // before hijacking the SERVER[REQUEST_URI]
  374. // override the canonical_redirect action
  375. // keep a copy of the original request uri
  376. remove_action( 'template_redirect', 'redirect_canonical' );
  377. global $_icl_server_request_uri;
  378. $_icl_server_request_uri = $_SERVER[ 'REQUEST_URI' ];
  379. add_action( 'template_redirect', array($this,'icl_redirect_canonical_wrapper'), 0 );
  380. //deal with situations when template files need to be called directly
  381. add_action( 'template_redirect', array( $this, '_allow_calling_template_file_directly' ) );
  382. //$_SERVER['REQUEST_URI'] = preg_replace('@^'. $blog_path . '/' . $this->this_lang.'@i', $blog_path ,$_SERVER['REQUEST_URI']);
  383. // Check for special case of www.example.com/fr where the / is missing on the end
  384. $parts = parse_url( $_SERVER[ 'REQUEST_URI' ] );
  385. if ( strlen( $parts[ 'path' ] ) == 0 ) {
  386. $_SERVER[ 'REQUEST_URI' ] = '/' . $_SERVER[ 'REQUEST_URI' ];
  387. }
  388. } else {
  389. $this->this_lang = $default_language;
  390. }
  391. if ( !trim( $path, '/' ) && $this->settings[ 'urls' ][ 'directory_for_default_language' ] ) {
  392. if ( $this->settings[ 'urls' ][ 'show_on_root' ] == 'html_file' ) {
  393. // html file
  394. if ( false === strpos( $this->settings[ 'urls' ][ 'root_html_file_path' ], '/' ) ) {
  395. $html_file = ABSPATH . $this->settings[ 'urls' ][ 'root_html_file_path' ];
  396. } else {
  397. $html_file = $this->settings[ 'urls' ][ 'root_html_file_path' ];
  398. }
  399. include $html_file;
  400. exit;
  401. } else {
  402. //page
  403. if ( !trim( $path, '/' ) ) {
  404. wpml_home_url_setup_root_page();
  405. }
  406. }
  407. }
  408. break;
  409. case 2:
  410. $this->this_lang = $default_language;
  411. foreach( $this->settings[ 'language_domains' ] as $language_code => $domain ){
  412. if( rtrim( $this->get_server_host_name() . $blog_path, '/' ) == rtrim( preg_replace( '@^https?://@', '', $domain ), '/' ) ){
  413. $this->this_lang = $language_code;
  414. break;
  415. }
  416. }
  417. if ( defined( 'ICL_USE_MULTIPLE_DOMAIN_LOGIN' ) && ICL_USE_MULTIPLE_DOMAIN_LOGIN ) {
  418. include ICL_PLUGIN_PATH . '/modules/multiple-domains-login.php';
  419. }
  420. add_filter( 'site_url', array( $this, 'convert_url' ) );
  421. break;
  422. case 3:
  423. default:
  424. if ( isset( $_GET[ 'lang' ] ) ) {
  425. $this->this_lang = preg_replace( "/[^0-9a-zA-Z-]/i", '', strip_tags( $_GET[ 'lang' ] ) );
  426. // set the language based on the content id - for short links
  427. } elseif ( isset( $_GET[ 'page_id' ] ) ) {
  428. $language_code_prepared = $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_type='post_page' AND element_id=%d", $_GET[ 'page_id' ] );
  429. $this->this_lang = $wpdb->get_var( $language_code_prepared );
  430. } elseif ( isset( $_GET[ 'p' ] ) ) {
  431. $post_type_prepared = $wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE ID=%d", $_GET[ 'p' ] );
  432. $post_type = $wpdb->get_var( $post_type_prepared );
  433. $language_code_prepared = $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d", 'post_' . $post_type, $_GET[ 'p' ] );
  434. $this->this_lang = $wpdb->get_var( $language_code_prepared );
  435. } elseif ( isset( $_GET[ 'cat_ID' ] ) ) {
  436. $cat_tax_id_prepared = $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy=%s", $_GET[ 'cat_ID' ], 'category' );
  437. $cat_tax_id = $wpdb->get_var( $cat_tax_id_prepared );
  438. $language_code_prepared = $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_type='tax_category' AND element_id=%d", $cat_tax_id );
  439. $this->this_lang = $wpdb->get_var( $language_code_prepared );
  440. } elseif ( isset( $_GET[ 'tag' ] ) ) {
  441. $tag_tax_id_prepared = $wpdb->prepare( "
  442. SELECT x.term_taxonomy_id FROM {$wpdb->term_taxonomy} x JOIN {$wpdb->terms} t ON t.term_id = x.term_id
  443. WHERE t.slug=%s AND x.taxonomy='post_tag'", $_GET[ 'tag' ] );
  444. $tag_tax_id = $wpdb->get_var( $tag_tax_id_prepared );
  445. $language_code_prepared = $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations
  446. WHERE element_type='tax_post_tag' AND element_id=%d", $tag_tax_id );
  447. $this->this_lang = $wpdb->get_var( $language_code_prepared );
  448. }
  449. //
  450. if ( !isset( $_GET[ 'lang' ] ) && ( $this->this_lang && $this->this_lang != $default_language ) ) {
  451. if ( !isset( $GLOBALS[ 'wp_rewrite' ] ) ) {
  452. require_once ABSPATH . WPINC . '/rewrite.php';
  453. $GLOBALS[ 'wp_rewrite' ] = new WP_Rewrite();
  454. }
  455. define( 'ICL_DOING_REDIRECT', true );
  456. if ( isset( $_GET[ 'page_id' ] ) ) {
  457. wp_redirect( get_page_link( $_GET[ 'page_id' ] ), '301' );
  458. exit;
  459. } elseif ( isset( $_GET[ 'p' ] ) ) {
  460. wp_redirect( get_permalink( $_GET[ 'p' ] ), '301' );
  461. exit;
  462. } elseif ( isset( $_GET[ 'cat_ID' ] ) ) {
  463. wp_redirect( get_term_link( intval( $_GET[ 'cat_ID' ] ), 'category' ) );
  464. exit;
  465. } elseif ( isset( $_GET[ 'tag' ] ) ) {
  466. wp_redirect( get_term_link( $_GET[ 'tag' ], 'post_tag' ) );
  467. exit;
  468. } else {
  469. if ( isset( $this->settings[ 'taxonomies_sync_option' ] ) ) {
  470. $taxs = array_keys( (array)$this->settings[ 'taxonomies_sync_option' ] );
  471. foreach ( $taxs as $t ) {
  472. if ( isset( $_GET[ $t ] ) ) {
  473. $term_obj = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON t.term_id = x.term_id
  474. WHERE t.slug=%s AND x.taxonomy=%s", $_GET[ $t ], $t ) );
  475. $term_link = get_term_link( $term_obj, $t );
  476. $term_link = str_replace( '&amp;', '&', $term_link ); // fix
  477. if ( $term_link && !is_wp_error( $term_link ) ) {
  478. wp_redirect( $term_link );
  479. exit;
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }
  486. if ( empty( $this->this_lang ) ) {
  487. $this->this_lang = $default_language;
  488. }
  489. }
  490. // allow forcing the current language when it can't be decoded from the URL
  491. $this->this_lang = apply_filters( 'icl_set_current_language', $this->this_lang );
  492. }
  493. //reorder active language to put 'this_lang' in front
  494. foreach ( $this->active_languages as $k => $al ) {
  495. if ( $al[ 'code' ] == $this->this_lang ) {
  496. unset( $this->active_languages[ $k ] );
  497. $this->active_languages = array_merge( array( $k => $al ), $this->active_languages );
  498. }
  499. }
  500. // filter some queries
  501. add_filter( 'query', array( $this, 'filter_queries' ) );
  502. add_filter( 'option_rewrite_rules', array( $this, 'rewrite_rules_filter' ) );
  503. $this->set_language_cookie();
  504. if ( is_admin() && ( !isset( $_GET[ 'page' ] ) || !defined( 'WPML_ST_FOLDER' ) || $_GET[ 'page' ] != WPML_ST_FOLDER . '/menu/string-translation.php' ) && ( !isset( $_GET[ 'page' ] ) || !defined( 'WPML_TM_FOLDER' ) || $_GET[ 'page' ] != WPML_TM_FOLDER . '/menu/translations-queue.php' )
  505. ) {
  506. if ( version_compare( $GLOBALS[ 'wp_version' ], '3.3', '<' ) ) {
  507. // Legacy code for admin language switcher
  508. if ( !$this->is_rtl() && version_compare( $GLOBALS[ 'wp_version' ], '3.3', '>' ) ) {
  509. add_action( 'admin_notices', 'wpml_set_admin_language_switcher_place', 100 );
  510. add_action( 'network_admin_notices', 'wpml_set_admin_language_switcher_place', 100 );
  511. add_action( 'user_admin_notices', 'wpml_set_admin_language_switcher_place', 100 );
  512. function wpml_set_admin_language_switcher_place()
  513. {
  514. echo '<br clear="all" />';
  515. }
  516. }
  517. add_action( 'in_admin_header', array( $this, 'admin_language_switcher_legacy' ) );
  518. } else {
  519. // Admin language switcher goes to the WP admin bar
  520. add_action( 'wp_before_admin_bar_render', array( $this, 'admin_language_switcher' ) );
  521. }
  522. }
  523. if ( !is_admin() && defined( 'DISQUS_VERSION' ) ) {
  524. include ICL_PLUGIN_PATH . '/modules/disqus.php';
  525. }
  526. }
  527. /*
  528. * If user perform bulk taxonomy deletion when displaying non-default
  529. * language taxonomies, after deletion should stay with same language
  530. */
  531. if ( is_admin() &&
  532. isset($_POST['_wp_http_referer'])
  533. && false !== strpos($_POST['_wp_http_referer'], 'edit-tags.php')
  534. && !empty($_POST['delete_tags'])
  535. && is_array($_POST['delete_tags'])
  536. && !empty($_GET['lang'])
  537. && (
  538. $_POST['action'] == 'delete' || $_POST['action2'] == 'delete'
  539. ) ) {
  540. add_filter('wp_redirect', array($this, 'preserve_lang_param_after_bulk_category_delete'));
  541. }
  542. if ( $this->is_rtl() ) {
  543. $GLOBALS[ 'text_direction' ] = 'rtl';
  544. }
  545. if (!wpml_is_ajax() && is_admin() && empty( $this->settings[ 'dont_show_help_admin_notice' ] ) ) {
  546. if ( !$this->get_setting('setup_wizard_step') ) {
  547. if(SitePress::check_settings_integrity()) {
  548. add_action( 'admin_notices', array( $this, 'help_admin_notice' ) );
  549. }
  550. }
  551. }
  552. $short_v = implode( '.', array_slice( explode( '.', ICL_SITEPRESS_VERSION ), 0, 3 ) );
  553. if ( is_admin() && ( !isset( $this->settings[ 'hide_upgrade_notice' ] ) || $this->settings[ 'hide_upgrade_notice' ] != $short_v ) ) {
  554. add_action( 'admin_notices', array( $this, 'upgrade_notice' ) );
  555. }
  556. require ICL_PLUGIN_PATH . '/inc/template-constants.php';
  557. if ( defined( 'WPML_LOAD_API_SUPPORT' ) ) {
  558. require ICL_PLUGIN_PATH . '/inc/wpml-api.php';
  559. }
  560. add_action( 'wp_footer', array( $this, 'display_wpml_footer' ), 20 );
  561. if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
  562. add_action( 'xmlrpc_call', array( $this, 'xmlrpc_call_actions' ) );
  563. add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
  564. }
  565. if ( defined( 'WPML_TM_VERSION' ) && is_admin() ) {
  566. require ICL_PLUGIN_PATH . '/inc/quote.php';
  567. }
  568. add_action( 'init', array( $this, 'set_up_language_selector' ) );
  569. global $pagenow;
  570. if ( $pagenow == 'admin-ajax.php' ) {
  571. if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'wpml_tt_show_terms') {
  572. $default_language = $this->get_default_language();
  573. $this->switch_lang($default_language, true);
  574. }
  575. }
  576. // Disable the Admin language switcher when in Taxonomy Translation page
  577. // If the page uses AJAX and the language must be forced to default, please use the
  578. // if ( $pagenow == 'admin-ajax.php' )above
  579. if ( is_admin() && isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == ICL_PLUGIN_FOLDER . '/menu/taxonomy-translation.php' ) {
  580. $this->switch_lang($default_language, true);
  581. add_action( 'init', array( $this, 'remove_admin_language_switcher'));
  582. }
  583. //Code to run when reactivating the plugin
  584. $recently_activated = $this->get_setting('just_reactivated');
  585. if($recently_activated) {
  586. add_action( 'init', array( $this, 'rebuild_language_information' ), 1000 );
  587. }
  588. }
  589. function icl_redirect_canonical_wrapper()
  590. {
  591. global $_icl_server_request_uri;
  592. $requested_url = ( !empty( $_SERVER[ 'HTTPS' ] ) && strtolower( $_SERVER[ 'HTTPS' ] ) == 'on' ) ? 'https://' : 'http://';
  593. $requested_url .= $this->get_server_host_name();
  594. $requested_url .= $_icl_server_request_uri;
  595. redirect_canonical( $requested_url );
  596. }
  597. /**
  598. * If user perform bulk taxonomy deletion when displaying non-default
  599. * language taxonomies, after deletion should stay with same language
  600. *
  601. * @param string $location Url where browser will redirect
  602. * @return string Url where browser will redirect
  603. */
  604. function preserve_lang_param_after_bulk_category_delete($location) {
  605. if (empty($_GET['lang'])) {
  606. return $location;
  607. }
  608. $location = add_query_arg( 'lang', $_GET['lang'], $location );
  609. return $location;
  610. }
  611. function remove_admin_language_switcher() {
  612. remove_action( 'wp_before_admin_bar_render', array( $this, 'admin_language_switcher' ) );
  613. }
  614. function rebuild_language_information() {
  615. $this->set_setting('just_reactivated', 0);
  616. $this->save_settings();
  617. global $iclTranslationManagement;
  618. if ( isset( $iclTranslationManagement ) ) {
  619. $iclTranslationManagement->add_missing_language_information();
  620. }
  621. }
  622. function on_wp_init()
  623. {
  624. if ( is_admin() && current_user_can( 'manage_options' ) ) {
  625. if ( $this->icl_account_configured() ) {
  626. add_action( 'admin_notices', array( $this, 'icl_reminders' ) );
  627. }
  628. }
  629. include ICL_PLUGIN_PATH . '/inc/translation-management/taxonomy-translation.php';
  630. }
  631. function setup()
  632. {
  633. $setup_complete = $this->get_setting('setup_complete');
  634. if(!$setup_complete) {
  635. $this->set_setting('setup_complete', false);
  636. }
  637. return $setup_complete;
  638. }
  639. function get_current_user()
  640. {
  641. global $current_user;
  642. if ( did_action( 'set_current_user' ) ) {
  643. return $current_user;
  644. } else {
  645. return $this->current_user; // created early / no authentication
  646. }
  647. }
  648. function ajax_setup()
  649. {
  650. require ICL_PLUGIN_PATH . '/ajax.php';
  651. }
  652. function configure_custom_column()
  653. {
  654. global $pagenow, $wp_post_types;
  655. $pagenow_ = '';
  656. $is_ajax = false;
  657. if ( $pagenow == 'admin-ajax.php' ) {
  658. if ( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'inline-save' || isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'fetch-list'
  659. ) {
  660. $is_ajax = true;
  661. }
  662. }
  663. if ( ( $pagenow == 'edit.php' || $pagenow_ == 'edit-pages.php' || $is_ajax ) ) {
  664. $post_type = isset( $_REQUEST[ 'post_type' ] ) ? $_REQUEST[ 'post_type' ] : 'post';
  665. switch ( $post_type ) {
  666. case 'post':
  667. case 'page':
  668. add_filter( 'manage_' . $post_type . 's_columns', array( $this, 'add_posts_management_column' ) );
  669. if ( isset( $_GET[ 'post_type' ] ) && $_GET[ 'post_type' ] == 'page' ) {
  670. add_action( 'manage_' . $post_type . 's_custom_column', array( $this, 'add_content_for_posts_management_column' ) );
  671. }
  672. add_action( 'manage_posts_custom_column', array( $this, 'add_content_for_posts_management_column' ) );
  673. break;
  674. default:
  675. if ( in_array( $post_type, array_keys( $this->get_translatable_documents() ) ) ) {
  676. add_filter( 'manage_' . $post_type . '_posts_columns', array( $this, 'add_posts_management_column' ) );
  677. if ( $wp_post_types[ $post_type ]->hierarchical ) {
  678. add_action( 'manage_pages_custom_column', array( $this, 'add_content_for_posts_management_column' ) );
  679. add_action( 'manage_posts_custom_column', array( $this, 'add_content_for_posts_management_column' ) ); // add this too - for more types plugin
  680. } else {
  681. add_action( 'manage_posts_custom_column', array( $this, 'add_content_for_posts_management_column' ) );
  682. }
  683. }
  684. }
  685. add_action( 'admin_print_scripts', array( $this, '__set_posts_management_column_width' ) );
  686. }
  687. }
  688. function _taxonomy_languages_menu()
  689. {
  690. // tags language selection
  691. global $pagenow;
  692. if ( $pagenow == 'edit-tags.php' ) {
  693. // handle case of the tax edit page (after a taxonomy has been added)
  694. // needs to redirect back to
  695. if ( isset( $_GET[ 'trid' ] ) && isset( $_GET[ 'source_lang' ] ) ) {
  696. $translations = $this->get_element_translations( $_GET[ 'trid' ], 'tax_' . $_GET[ 'taxonomy' ] );
  697. if ( isset( $translations[ $_GET[ 'lang' ] ] ) ) {
  698. wp_redirect( get_edit_term_link( $translations[ $_GET[ 'lang' ] ]->term_id, $_GET[ 'taxonomy' ] ) );
  699. exit;
  700. } else {
  701. add_action( 'admin_notices', array( $this, '_tax_adding' ) );
  702. }
  703. }
  704. $taxonomy = isset( $_GET[ 'taxonomy' ] ) ? esc_sql( $_GET[ 'taxonomy' ] ) : 'post_tag';
  705. if ( $this->is_translated_taxonomy( $taxonomy ) ) {
  706. add_action( 'admin_print_scripts-edit-tags.php', array( $this, 'js_scripts_tags' ) );
  707. if ( $taxonomy == 'category' ) {
  708. add_action( 'edit_category_form', array( $this, 'edit_term_form' ) );
  709. } else {
  710. add_action( 'add_tag_form', array( $this, 'edit_term_form' ) );
  711. add_action( 'edit_tag_form', array( $this, 'edit_term_form' ) );
  712. }
  713. add_action( 'admin_footer', array( $this, 'terms_language_filter' ) );
  714. add_filter( 'wp_dropdown_cats', array( $this, 'wp_dropdown_cats_select_parent' ) );
  715. }
  716. }
  717. }
  718. function _tax_adding()
  719. {
  720. $translations = $this->get_element_translations( $_GET[ 'trid' ], 'tax_' . $_GET[ 'taxonomy' ] );
  721. if ( !empty( $translations ) && isset( $translations[ $_GET[ 'source_lang' ] ]->name ) ) {
  722. $tax_name = apply_filters( 'the_category', $translations[ $_GET[ 'source_lang' ] ]->name );
  723. echo '<div id="icl_tax_adding_notice" class="updated fade"><p>' . sprintf( __( 'Adding translation for: %s.', 'sitepress' ), $tax_name ) . '</p></div>';
  724. }
  725. }
  726. /**
  727. * @param WP_Query $query
  728. */
  729. function loop_start($query) {
  730. if($query->post_count) {
  731. $this->cache_translations($query->posts);
  732. }
  733. }
  734. /**
  735. * Cache translated posts
  736. *
  737. * @param $posts
  738. */
  739. function cache_translations($posts) {
  740. global $wpdb, $wp_query, $sitepress;
  741. static $last_query=false;
  742. if ( isset( $sitepress ) && isset( $wp_query ) && $wp_query->is_main_query() ) {
  743. if($last_query == $wp_query->query_vars_hash) return;
  744. $sticky_posts_ids = get_option( 'sticky_posts' );
  745. if ( $sticky_posts_ids ) {
  746. if ( count( $sticky_posts_ids ) == 1 ) {
  747. $sticky_posts_prepared = $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE ID = %d", array( $sticky_posts_ids[0] ) );
  748. } else {
  749. $sticky_posts_prepared = "SELECT * FROM {$wpdb->posts} WHERE ID IN (" . implode( ',', array_filter( $sticky_posts_ids ) ) . ")";
  750. }
  751. $sticky_posts = $wpdb->get_results( $sticky_posts_prepared );
  752. $posts_objects = array_map( 'get_post', $sticky_posts );
  753. if ( !$posts ) {
  754. $posts = $posts_objects;
  755. } else {
  756. $posts = array_merge( $posts, $posts_objects );
  757. //Remove duplicates
  758. $posts = array_map( "unserialize", array_unique( array_map( "serialize", $posts ) ) );
  759. }
  760. }
  761. if ( $posts ) {
  762. $terms = array();
  763. //Query specific cache
  764. $cache_key = $wp_query->query_vars_hash;
  765. $cache_group = 'wp_query:posts_translations';
  766. $cached_posts_translations = wp_cache_get( $cache_key, $cache_group );
  767. if ( !$cached_posts_translations ) {
  768. $post_types = array();
  769. foreach ( $posts as $post ) {
  770. $post_types[ $post->post_type ][] = $post->ID;
  771. }
  772. $trids = array();
  773. if ( $post_types ) {
  774. $trid_cache_group = 'element_trid';
  775. foreach ( $post_types as $post_type => $posts_ids ) {
  776. $element_type = 'post_' . $post_type;
  777. $s_post_type_ids = join( ',', array_filter($posts_ids) );
  778. $trids_prepared = $wpdb->prepare( "SELECT trid, element_id, language_code, source_language_code FROM {$wpdb->prefix}icl_translations WHERE element_id IN (" . $s_post_type_ids . ") AND element_type=%s GROUP BY trid", array( $element_type ) );
  779. $post_type_trids_data = $wpdb->get_results( $trids_prepared );
  780. foreach($post_type_trids_data as $post_type_trid_data) {
  781. $element_id = $post_type_trid_data->element_id;
  782. $trid_cache_key = $element_id . ':post_' . $post_type;
  783. $trid = wp_cache_get($trid_cache_key, $trid_cache_group);
  784. if(!$trid) {
  785. $trid = $post_type_trid_data->trid;
  786. $trids[] = $trid;
  787. wp_cache_add($trid_cache_key, $trid, $trid_cache_group);
  788. }
  789. if($trid) {
  790. $element_language_details_cache_group = 'element_language_details';
  791. $element_language_details = wp_cache_get($trid_cache_key, $element_language_details_cache_group);
  792. if(!$element_language_details) {
  793. $details = new stdClass();
  794. $details->trid = $trid;
  795. $details->language_code = $post_type_trid_data->language_code;
  796. $details->source_language_code = $post_type_trid_data->source_language_code;
  797. wp_cache_add($trid_cache_key, $details, $element_language_details_cache_group);
  798. }
  799. }
  800. //Deal with taxonomies
  801. //$_taxonomies = get_post_taxonomies($element_id);
  802. $_taxonomies= get_post_taxonomies($element_id);
  803. foreach($_taxonomies as $_taxonomy) {
  804. if($sitepress->is_translated_taxonomy($_taxonomy)) {
  805. $_terms = wp_get_post_terms($element_id, $_taxonomy);
  806. foreach($_terms as $_term) {
  807. $terms[$_term->taxonomy][] = $_term->term_id;
  808. }
  809. }
  810. }
  811. }
  812. }
  813. }
  814. if ( $trids ) {
  815. if(count($trids)==1) {
  816. $posts_translations_prepared = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}icl_translations WHERE trid = %d ", array($trids[0]) );
  817. }else {
  818. $posts_translations_prepared = "SELECT * FROM {$wpdb->prefix}icl_translations WHERE trid IN (" . join( ',', array_filter($trids) ) . ") ";
  819. }
  820. $posts_translations = $wpdb->get_results( $posts_translations_prepared );
  821. $post_ids = array();
  822. foreach($posts_translations as $posts_translation) {
  823. $post_ids[] = $posts_translation->element_id;
  824. }
  825. $posts_data = wp_cache_get($cache_key, 'wp_query:posts');
  826. if(!$posts_data && $post_ids) {
  827. $posts_prepared = "SELECT * FROM {$wpdb->posts} WHERE ID IN (" . join( ',', array_filter($post_ids) ) . ") ";
  828. $posts_data = $wpdb->get_results( $posts_prepared );
  829. wp_cache_set($cache_key, $posts_data, 'wp_query:posts');
  830. }
  831. if ( $posts_data ) {
  832. foreach($posts_data as $post) {
  833. $_post = wp_cache_get( $post->ID, 'posts' );
  834. if ( ! $_post ) {
  835. $_post = $post;
  836. $_post = sanitize_post( $_post, 'raw' );
  837. wp_cache_add( $_post->ID, $_post, 'posts' );
  838. }
  839. }
  840. }
  841. }
  842. if ( $terms ) {
  843. $cache_group = 'element_language_details';
  844. foreach ( $terms as $taxonomy => $term_ids ) {
  845. $element_type = 'tax_' . $taxonomy;
  846. $terms_translations_prepared = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type = %s AND element_id IN (" . join( ',', $term_ids ) . ")", array( $element_type ) );
  847. $terms_translations = $wpdb->get_results( $terms_translations_prepared );
  848. foreach ( $terms_translations as $terms_translation ) {
  849. $cache_key = $terms_translation->element_id . ':' . $terms_translation->element_type;
  850. $cached_details = wp_cache_get( $cache_key, $cache_group );
  851. if ( !$cached_details ) {
  852. wp_cache_set( $cache_key, $terms_translation, $cache_group );
  853. }
  854. $icl_object_id_cache_group = 'icl_object_id';
  855. $icl_object_id_cache_key_args = array( $terms_translation->element_id, $taxonomy, false, $terms_translation->language_code );
  856. $icl_object_id_cache_key = implode( ':', array_filter( $icl_object_id_cache_key_args ) );
  857. $icl_object_id = wp_cache_get( $cache_key, $cache_group );
  858. if ( !$icl_object_id ) {
  859. wp_cache_set( $icl_object_id_cache_key, $terms_translation->element_id, $icl_object_id_cache_group );
  860. }
  861. $icl_object_id_cache_key_args = array( $terms_translation->element_id, $taxonomy, true, $terms_translation->language_code );
  862. $icl_object_id_cache_key = implode( ':', array_filter( $icl_object_id_cache_key_args ) );
  863. $icl_object_id = wp_cache_get( $cache_key, $cache_group );
  864. if ( !$icl_object_id ) {
  865. wp_cache_set( $icl_object_id_cache_key, $terms_translation->element_id, $icl_object_id_cache_group );
  866. }
  867. }
  868. }
  869. }
  870. }
  871. }
  872. $last_query = $wp_query->query_vars_hash;
  873. }
  874. }
  875. function the_posts( $posts ) {
  876. if ( !is_admin() && isset( $this->settings[ 'show_untranslated_blog_posts' ] ) && $this->settings[ 'show_untranslated_blog_posts' ] && $this->get_current_language() != $this->get_default_language() ) {
  877. // show untranslated posts
  878. global $wpdb, $wp_query;
  879. $default_language = $this->get_default_language();
  880. $current_language = $this->get_current_language();
  881. $debug_backtrace = $this->get_backtrace(4, true); //Limit to first 4 stack frames, since 3 is the highest index we use
  882. /** @var $custom_wp_query WP_Query */
  883. $custom_wp_query = isset( $debug_backtrace[ 3 ][ 'object' ] ) ? $debug_backtrace[ 3 ][ 'object' ] : false;
  884. //exceptions
  885. if ( ( $current_language == $default_language )
  886. // original language
  887. ||
  888. ( $wp_query != $custom_wp_query )
  889. // called by a custom query
  890. ||
  891. ( !$custom_wp_query->is_posts_page && !$custom_wp_query->is_home )
  892. // not the blog posts page
  893. ||
  894. $wp_query->is_singular
  895. //is singular
  896. ||
  897. !empty( $custom_wp_query->query_vars[ 'category__not_in' ] )
  898. //|| !empty($custom_wp_query->query_vars['category__in'])
  899. //|| !empty($custom_wp_query->query_vars['category__and'])
  900. ||
  901. !empty( $custom_wp_query->query_vars[ 'tag__not_in' ] ) ||
  902. !empty( $custom_wp_query->query_vars[ 'post__in' ] ) ||
  903. !empty( $custom_wp_query->query_vars[ 'post__not_in' ] ) ||
  904. !empty( $custom_wp_query->query_vars[ 'post_parent' ] )
  905. ) {
  906. return $posts;
  907. }
  908. // get the posts in the default language instead
  909. $this_lang = $this->this_lang;
  910. $this->this_lang = $default_language;
  911. remove_filter( 'the_posts', array( $this, 'the_posts' ) );
  912. $custom_wp_query->query_vars[ 'suppress_filters' ] = 0;
  913. if ( isset( $custom_wp_query->query_vars[ 'pagename' ] ) && !empty( $custom_wp_query->query_vars[ 'pagename' ] ) ) {
  914. if ( isset( $custom_wp_query->queried_object_id ) && !empty( $custom_wp_query->queried_object_id ) ) {
  915. $page_id = $custom_wp_query->queried_object_id;
  916. } else {
  917. // urlencode added for languages that have urlencoded post_name field value
  918. $custom_wp_query->query_vars[ 'pagename' ] = urlencode( $custom_wp_query->query_vars[ 'pagename' ] );
  919. $page_id = $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_name='{$custom_wp_query->query_vars['pagename']}' AND post_type='page'" );
  920. }
  921. if ( $page_id ) {
  922. $tr_page_id = icl_object_id( $page_id, 'page', false, $default_language );
  923. if ( $tr_page_id ) {
  924. $custom_wp_query->query_vars[ 'pagename' ] = $wpdb->get_var( "SELECT post_name FROM {$wpdb->posts} WHERE ID={$tr_page_id}" );
  925. }
  926. }
  927. }
  928. // look for posts without translations
  929. if ( $posts ) {
  930. $pids = false;
  931. foreach ( $posts as $p ) {
  932. $pids[ ] = $p->ID;
  933. }
  934. if ( $pids ) {
  935. $trids = $wpdb->get_col( "
  936. SELECT trid
  937. FROM {$wpdb->prefix}icl_translations
  938. WHERE element_type='post_post' AND element_id IN (" . join( ',', $pids ) . ") AND language_code = '" . $this_lang . "'" );
  939. if ( !empty( $trids ) ) {
  940. $posts_not_translated = $wpdb->get_col( "
  941. SELECT element_id, COUNT(language_code) AS c
  942. FROM {$wpdb->prefix}icl_translations
  943. WHERE trid IN (" . join( ',', $trids ) . ") GROUP BY trid HAVING c = 1
  944. " );
  945. if ( !empty( $posts_not_translated ) ) {
  946. $GLOBALS[ '__icl_the_posts_posts_not_translated' ] = $posts_not_translated;
  947. add_filter( 'posts_where', array( $this, '_posts_untranslated_extra_posts_where' ), 99 );
  948. }
  949. }
  950. }
  951. }
  952. //fix page for posts
  953. unset( $custom_wp_query->query_vars[ 'pagename' ] );
  954. unset( $custom_wp_query->query_vars[ 'page_id' ] );
  955. unset( $custom_wp_query->query_vars[ 'p' ] );
  956. $my_query = new WP_Query( $custom_wp_query->query_vars );
  957. add_filter( 'the_posts', array( $this, 'the_posts' ) );
  958. $this->this_lang = $this_lang;
  959. // create a map of the translated posts
  960. foreach ( $posts as $post ) {
  961. $trans_posts[ $post->ID ] = $post;
  962. }
  963. // loop original posts
  964. foreach ( $my_query->posts as $k => $post ) { // loop posts in the default language
  965. $trid = $this->get_element_trid( $post->ID );
  966. $translations = $this->get_element_translations( $trid ); // get translations
  967. if ( isset( $translations[ $current_language ] ) ) { // if there is a translation in the current language
  968. if ( isset( $trans_posts[ $translations[ $current_language ]->element_id ] ) ) { //check the map of translated posts
  969. $my_query->posts[ $k ] = $trans_posts[ $translations[ $current_language ]->element_id ];
  970. } else { // check if the translated post exists in the database still
  971. $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d AND post_status='publish' LIMIT 1", $translations[ $current_language ]->element_id ) );
  972. if ( !empty( $_post ) ) {
  973. $_post = sanitize_post( $_post );
  974. $my_query->posts[ $k ] = $_post;
  975. } else {
  976. $my_query->posts[ $k ]->original_language = true;
  977. }
  978. }
  979. } else {
  980. $my_query->posts[ $k ]->original_language = true;
  981. }
  982. }
  983. if ( $custom_wp_query == $wp_query ) {
  984. $wp_query->max_num_pages = $my_query->max_num_pages;
  985. }
  986. $posts = $my_query->posts;
  987. unset( $GLOBALS[ '__icl_the_posts_posts_not_translated' ] );
  988. remove_filter( 'posts_where', array( $this, '_posts_untranslated_extra_posts_where' ), 99 );
  989. }
  990. // cache translated posts
  991. $this->cache_translations($posts);
  992. return $posts;
  993. }
  994. function get_pages($pages, $r) {
  995. $this->cache_translations($pages);
  996. return $pages;
  997. }
  998. function _posts_untranslated_extra_posts_where( $where )
  999. {
  1000. global $wpdb;
  1001. $where .= ' OR ' . $wpdb->posts . '.ID IN (' . join( ',', $GLOBALS[ '__icl_the_posts_posts_not_translated' ] ) . ')';
  1002. return $where;
  1003. }
  1004. function initialize_cache()
  1005. {
  1006. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  1007. $this->icl_translations_cache = new icl_cache();
  1008. $this->icl_locale_cache = new icl_cache( 'locale', true );
  1009. $this->icl_flag_cache = new icl_cache( 'flags', true );
  1010. $this->icl_language_name_cache = new icl_cache( 'language_name', true );
  1011. $this->icl_term_taxonomy_cache = new icl_cache();
  1012. }
  1013. function set_admin_language()
  1014. {
  1015. global $wpdb;
  1016. $default_language = $this->get_default_language();
  1017. $found = false;
  1018. $cache_key = "active_languages";
  1019. $cache_group = "set_admin_lang";
  1020. $active_languages = wp_cache_get($cach

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