PageRenderTime 135ms CodeModel.GetById 20ms RepoModel.GetById 1ms 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
  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($cache_key, $cache_group, false, $found);
  1021. if(!$found) {
  1022. //don't use method get_active_language()
  1023. $active_languages_col = $wpdb->get_col( "SELECT code FROM {$wpdb->prefix}icl_languages WHERE active=1" );
  1024. $active_languages = array_keys( $active_languages_col );
  1025. wp_cache_set($cache_key, $active_languages, $cache_group);
  1026. }
  1027. if ( !empty( $this->get_current_user()->ID ) ) {
  1028. $this->admin_language = $this->get_user_admin_language( $this->get_current_user()->ID );
  1029. }
  1030. if ( $this->admin_language != '' && !in_array( $this->admin_language, $active_languages ) ) {
  1031. delete_user_meta( $this->get_current_user()->ID, 'icl_admin_language' );
  1032. }
  1033. if ( empty( $this->settings[ 'admin_default_language' ] ) || !in_array( $this->settings[ 'admin_default_language' ], $active_languages ) ) {
  1034. $this->settings[ 'admin_default_language' ] = '_default_';
  1035. $this->save_settings();
  1036. }
  1037. if ( !$this->admin_language ) {
  1038. $this->admin_language = $this->settings[ 'admin_default_language' ];
  1039. }
  1040. if ( $this->admin_language == '_default_' && $default_language ) {
  1041. $this->admin_language = $default_language;
  1042. }
  1043. }
  1044. function get_admin_language()
  1045. {
  1046. $current_user = $this->get_current_user();
  1047. if ( isset( $current_user->ID ) && get_user_meta( $current_user->ID, 'icl_admin_language_for_edit', true ) && icl_is_post_edit() ) {
  1048. $admin_language = $this->get_current_language();
  1049. } else {
  1050. $admin_language = $this->admin_language;
  1051. }
  1052. return $admin_language;
  1053. }
  1054. function get_user_admin_language( $user_id )
  1055. {
  1056. static $lang = array();
  1057. if ( !isset( $lang[ $user_id ] ) ) {
  1058. $lang[ $user_id ] = get_user_meta( $user_id, 'icl_admin_language', true );
  1059. if ( empty( $lang[ $user_id ] ) ) {
  1060. if ( isset( $this->settings[ 'admin_default_language' ] ) ) {
  1061. $lang[ $user_id ] = $this->settings[ 'admin_default_language' ];
  1062. }
  1063. if ( empty( $lang[ $user_id ] ) || '_default_' == $lang[ $user_id ] ) {
  1064. $lang[ $user_id ] = $this->get_admin_language();
  1065. }
  1066. }
  1067. }
  1068. return $lang[ $user_id ];
  1069. }
  1070. function administration_menu()
  1071. {
  1072. if(!SitePress::check_settings_integrity()) return;
  1073. ICL_AdminNotifier::removeMessage( 'setup-incomplete' );
  1074. $main_page = apply_filters( 'icl_menu_main_page', basename( ICL_PLUGIN_PATH ) . '/menu/languages.php' );
  1075. if ( SitePress_Setup::setup_complete() ) {
  1076. add_menu_page( __( 'WPML', 'sitepress' ), __( 'WPML', 'sitepress' ), 'wpml_manage_languages', $main_page, null, ICL_PLUGIN_URL . '/res/img/icon16.png' );
  1077. add_submenu_page( $main_page, __( 'Languages', 'sitepress' ), __( 'Languages', 'sitepress' ), 'wpml_manage_languages', basename( ICL_PLUGIN_PATH ) . '/menu/languages.php' );
  1078. //By Gen, moved Translation management after language, because problems with permissions
  1079. do_action( 'icl_wpml_top_menu_added' );
  1080. add_submenu_page( $main_page, __( 'Theme and plugins localization', 'sitepress' ), __( 'Theme and plugins localization', 'sitepress' ), 'wpml_manage_theme_and_plugin_localization', basename( ICL_PLUGIN_PATH ) . '/menu/theme-localization.php' );
  1081. if ( !defined( 'WPML_TM_VERSION' ) ) {
  1082. add_submenu_page( $main_page, __( 'Translation options', 'sitepress' ), __( 'Translation options', 'sitepress' ), 'wpml_manage_translation_options', basename( ICL_PLUGIN_PATH ) . '/menu/translation-options.php' );
  1083. }
  1084. } else {
  1085. $main_page = basename( ICL_PLUGIN_PATH ) . '/menu/languages.php';
  1086. add_menu_page( __( 'WPML', 'sitepress' ), __( 'WPML', 'sitepress' ), 'manage_options', $main_page, null, ICL_PLUGIN_URL . '/res/img/icon16.png' );
  1087. add_submenu_page( $main_page, __( 'Languages', 'sitepress' ), __( 'Languages', 'sitepress' ), 'wpml_manage_languages', $main_page );
  1088. if ((!isset($_REQUEST['page']) || $_REQUEST['page']!=ICL_PLUGIN_FOLDER . '/menu/troubleshooting.php') && !SitePress_Setup::languages_table_is_complete() ) {
  1089. $troubleshooting_url = admin_url( 'admin.php?page=' . ICL_PLUGIN_FOLDER . '/menu/troubleshooting.php' );
  1090. $troubleshooting_link = '<a href="' . $troubleshooting_url . '" title="' . esc_attr( __( 'Troubleshooting', 'sitepress' ) ) . '">' . __( 'Troubleshooting', 'sitepress' ) . '</a>';
  1091. $message = '';
  1092. $message .= __( 'WPML is missing some records in the languages tables and it cannot fully work until this issue is fixed.', 'sitepress' );
  1093. $message .= '<br />';
  1094. $message .= sprintf( __( 'Please go to the %s page and click on %s to fix this problem.', 'sitepress' ), $troubleshooting_link, __( 'Fix languages tables', 'sitepress' ) );
  1095. $message .= '<br />';
  1096. $message .= '<br />';
  1097. $message .= __( 'This warning will disappear once this issue is issue is fixed.', 'sitepress' );
  1098. ICL_AdminNotifier::removeMessage( 'setup-incomplete' );
  1099. ICL_AdminNotifier::addMessage( 'setup-incomplete', $message, 'error', false, false, false, 'setup', true );
  1100. ICL_AdminNotifier::displayMessages( 'setup' );
  1101. }
  1102. }
  1103. add_submenu_page( $main_page, __( 'Support', 'sitepress' ), __( 'Support', 'sitepress' ), 'wpml_manage_support', ICL_PLUGIN_FOLDER . '/menu/support.php' );
  1104. $this->troubleshooting_menu(ICL_PLUGIN_FOLDER . '/menu/support.php');
  1105. $this->debug_information_menu(ICL_PLUGIN_FOLDER . '/menu/support.php');
  1106. }
  1107. private function troubleshooting_menu( $main_page ) {
  1108. $submenu_slug = basename( ICL_PLUGIN_PATH ) . '/menu/troubleshooting.php';
  1109. //if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == $submenu_slug ) {
  1110. add_submenu_page( $main_page, __( 'Troubleshooting', 'sitepress' ), __( 'Troubleshooting', 'sitepress' ), 'wpml_manage_troubleshooting', $submenu_slug );
  1111. return $submenu_slug;
  1112. //}
  1113. }
  1114. private function debug_information_menu( $main_page ) {
  1115. $submenu_slug = basename( ICL_PLUGIN_PATH ) . '/menu/debug-information.php';
  1116. add_submenu_page( $main_page, __( 'Debug information', 'sitepress' ), __( 'Debug information', 'sitepress' ), 'wpml_manage_troubleshooting', $submenu_slug );
  1117. return $submenu_slug;
  1118. }
  1119. // lower priority
  1120. function administration_menu2()
  1121. {
  1122. if(!SitePress::check_settings_integrity()) return;
  1123. $main_page = apply_filters( 'icl_menu_main_page', ICL_PLUGIN_FOLDER . '/menu/languages.php' );
  1124. if ( $this->setup() ) {
  1125. add_submenu_page( $main_page, __( 'Taxonomy Translation', 'sitepress' ), __( 'Taxonomy Translation', 'sitepress' ), 'wpml_manage_taxonomy_translation', ICL_PLUGIN_FOLDER . '/menu/taxonomy-translation.php' );
  1126. }
  1127. }
  1128. public function init_settings() {
  1129. if ($this->get_setting('setup_wizard_step') == 3 && $this->get_setting('setup_complete') && !ms_is_switched()) {
  1130. global $sitepress_settings;
  1131. $this->settings = get_option( 'icl_sitepress_settings' );
  1132. $sitepress_settings = $this->settings;
  1133. }
  1134. }
  1135. function save_settings( $settings = null )
  1136. {
  1137. global $sitepress_settings;
  1138. if ( !is_null( $settings ) ) {
  1139. foreach ( $settings as $k => $v ) {
  1140. if ( is_array( $v ) ) {
  1141. foreach ( $v as $k2 => $v2 ) {
  1142. $this->settings[ $k ][ $k2 ] = $v2;
  1143. }
  1144. } else {
  1145. $this->settings[ $k ] = $v;
  1146. }
  1147. }
  1148. }
  1149. if ( !empty( $this->settings ) ) {
  1150. update_option( 'icl_sitepress_settings', $this->settings );
  1151. $sitepress_settings = $this->settings;
  1152. }
  1153. do_action( 'icl_save_settings', $settings );
  1154. }
  1155. private function check_settings() {
  1156. if ( !isset( $this->settings ) ) {
  1157. $this->settings = $this->init_settings();
  1158. }
  1159. }
  1160. /**
  1161. * @since 3.1
  1162. */
  1163. function get_settings()
  1164. {
  1165. $this->check_settings();
  1166. return $this->settings;
  1167. }
  1168. /**
  1169. * @param string $key
  1170. * @param mixed|bool $default
  1171. *
  1172. * @since 3.1
  1173. *
  1174. * @return bool|mixed
  1175. */
  1176. function get_setting($key, $default = false)
  1177. {
  1178. $this->check_settings();
  1179. return isset($this->settings[$key]) ? $this->settings[$key] : $default;
  1180. }
  1181. /**
  1182. * @param string $key
  1183. * @param mixed $value
  1184. * @param bool $save_now Immediately update the settings record in the DB
  1185. *
  1186. * @since 3.1
  1187. */
  1188. function set_setting($key, $value, $save_now = false)
  1189. {
  1190. $this->check_settings();
  1191. $this->settings[$key] = $value;
  1192. if($save_now) {
  1193. $this->save_settings();
  1194. }
  1195. }
  1196. function get_user_preferences()
  1197. {
  1198. if ( empty( $this->user_preferences ) ) {
  1199. $this->user_preferences = get_user_meta( $this->get_current_user()->ID, '_icl_preferences', true );
  1200. }
  1201. return $this->user_preferences;
  1202. }
  1203. function set_user_preferences($value) {
  1204. $this->user_preferences = $value;
  1205. }
  1206. function save_user_preferences()
  1207. {
  1208. update_user_meta( $this->get_current_user()->ID, '_icl_preferences', $this->user_preferences );
  1209. }
  1210. function get_option( $option_name )
  1211. {
  1212. return isset( $this->settings[ $option_name ] ) ? $this->settings[ $option_name ] : null;
  1213. }
  1214. function verify_settings()
  1215. {
  1216. $default_settings = array(
  1217. 'interview_translators' => 1,
  1218. 'existing_content_language_verified' => 0,
  1219. 'language_negotiation_type' => 3,
  1220. 'theme_localization_type' => 1,
  1221. 'icl_lso_header' => 0,
  1222. 'icl_lso_link_empty' => 0,
  1223. 'icl_lso_flags' => 0,
  1224. 'icl_lso_native_lang' => 1,
  1225. 'icl_lso_display_lang' => 1,
  1226. 'sync_page_ordering' => 1,
  1227. 'sync_page_parent' => 1,
  1228. 'sync_page_template' => 1,
  1229. 'sync_ping_status' => 1,
  1230. 'sync_comment_status' => 1,
  1231. 'sync_sticky_flag' => 1,
  1232. 'sync_private_flag' => 1,
  1233. 'sync_post_format' => 1,
  1234. 'sync_delete' => 0,
  1235. 'sync_delete_tax' => 0,
  1236. 'sync_post_taxonomies' => 1,
  1237. 'sync_post_date' => 0,
  1238. 'sync_taxonomy_parents' => 0,
  1239. 'translation_pickup_method' => 0,
  1240. 'notify_complete' => 1,
  1241. 'translated_document_status' => 1,
  1242. 'remote_management' => 0,
  1243. 'auto_adjust_ids' => 1,
  1244. 'alert_delay' => 0,
  1245. 'promote_wpml' => 0,
  1246. 'troubleshooting_options' => array( 'http_communication' => 1 ),
  1247. 'automatic_redirect' => 0,
  1248. 'remember_language' => 24,
  1249. 'icl_lang_sel_type' => 'dropdown',
  1250. 'icl_lang_sel_stype' => 'classic',
  1251. 'icl_lang_sel_orientation' => 'vertical',
  1252. 'icl_lang_sel_copy_parameters' => '',
  1253. 'icl_widget_title_show' => 1,
  1254. 'translated_document_page_url' => 'auto-generate',
  1255. 'sync_comments_on_duplicates ' => 0,
  1256. 'seo' => array( 'head_langs' => 1, 'canonicalization_duplicates' => 1 ),
  1257. 'posts_slug_translation' => array( 'on' => 0 ),
  1258. 'languages_order' => '',
  1259. 'urls' => array( 'directory_for_default_language' => 0, 'show_on_root' => '', 'root_html_file_path' => '', 'root_page' => 0, 'hide_language_switchers' => 1 )
  1260. );
  1261. //configured for three levels
  1262. $update_settings = false;
  1263. foreach ( $default_settings as $key => $value ) {
  1264. if ( is_array( $value ) ) {
  1265. foreach ( $value as $k2 => $v2 ) {
  1266. if ( is_array( $v2 ) ) {
  1267. foreach ( $v2 as $k3 => $v3 ) {
  1268. if ( !isset( $this->settings[ $key ][ $k2 ][ $k3 ] ) ) {
  1269. $this->settings[ $key ][ $k2 ][ $k3 ] = $v3;
  1270. $update_settings = true;
  1271. }
  1272. }
  1273. } else {
  1274. if ( !isset( $this->settings[ $key ][ $k2 ] ) ) {
  1275. $this->settings[ $key ][ $k2 ] = $v2;
  1276. $update_settings = true;
  1277. }
  1278. }
  1279. }
  1280. } else {
  1281. if ( !isset( $this->settings[ $key ] ) ) {
  1282. $this->settings[ $key ] = $value;
  1283. $update_settings = true;
  1284. }
  1285. }
  1286. }
  1287. if ( $update_settings ) {
  1288. $this->save_settings();
  1289. }
  1290. }
  1291. function _validate_language_per_directory( $language_code )
  1292. {
  1293. if ( !class_exists( 'WP_Http' ) ) {
  1294. include_once ABSPATH . WPINC . '/class-http.php';
  1295. }
  1296. $client = new WP_Http();
  1297. if ( false === @strpos( $_POST[ 'url' ], '?' ) ) {
  1298. $url_glue = '?';
  1299. } else {
  1300. $url_glue = '&';
  1301. }
  1302. $response = $client->request( get_home_url() . '/' . $language_code . '/' . $url_glue . '____icl_validate_domain=1', array( 'timeout' => 15, 'decompress' => false ) );
  1303. return ( !is_wp_error( $response ) && ( $response[ 'response' ][ 'code' ] == '200' ) && ( $response[ 'body' ] == '<!--' . get_home_url() . '-->' ) );
  1304. }
  1305. function save_language_pairs()
  1306. {
  1307. // clear existing languages
  1308. $lang_pairs = $this->settings[ 'language_pairs' ];
  1309. if ( is_array( $lang_pairs ) ) {
  1310. foreach ( $lang_pairs as $from => $to ) {
  1311. $lang_pairs[ $from ] = array();
  1312. }
  1313. }
  1314. // get the from languages
  1315. $from_languages = array();
  1316. foreach ( $_POST as $k => $v ) {
  1317. if ( 0 === strpos( $k, 'icl_lng_from_' ) ) {
  1318. $f = str_replace( 'icl_lng_from_', '', $k );
  1319. $from_languages[ ] = $f;
  1320. }
  1321. }
  1322. foreach ( $_POST as $k => $v ) {
  1323. if ( 0 !== strpos( $k, 'icl_lng_' ) ) {
  1324. continue;
  1325. }
  1326. if ( 0 === strpos( $k, 'icl_lng_to' ) ) {
  1327. $t = str_replace( 'icl_lng_to_', '', $k );
  1328. $exp = explode( '_', $t );
  1329. if ( in_array( $exp[ 0 ], $from_languages ) ) {
  1330. $lang_pairs[ $exp[ 0 ] ][ $exp[ 1 ] ] = 1;
  1331. }
  1332. }
  1333. }
  1334. $iclsettings[ 'language_pairs' ] = $lang_pairs;
  1335. $this->save_settings( $iclsettings );
  1336. }
  1337. function get_active_languages( $refresh = false )
  1338. {
  1339. global $wpdb, $current_user;
  1340. if ( $refresh || !isset($this->active_languages) || !$this->active_languages ) {
  1341. $current_language = $this->get_current_language();
  1342. if ( defined( 'WP_ADMIN' ) && isset($this->admin_language) && $this->admin_language ) {
  1343. $in_language = $this->admin_language;
  1344. } else {
  1345. $in_language = $current_language ? $current_language : $this->get_default_language();
  1346. }
  1347. if ( !$refresh && isset( $this->icl_language_name_cache ) ) {
  1348. $res = $this->icl_language_name_cache->get( 'in_language_' . $in_language );
  1349. } else {
  1350. $res = null;
  1351. }
  1352. if ( !$res || !is_array( $res ) ) {
  1353. if(!$in_language) {
  1354. $in_language = 'en';
  1355. }
  1356. $res_prepared = $wpdb->prepare( "
  1357. SELECT l.id, code, english_name, active, lt.name AS display_name, l.encode_url, l.default_locale, l.tag
  1358. FROM {$wpdb->prefix}icl_languages l
  1359. JOIN {$wpdb->prefix}icl_languages_translations lt ON l.code=lt.language_code
  1360. WHERE
  1361. active=1 AND lt.display_language_code = %s
  1362. ORDER BY major DESC, english_name ASC", array($in_language));
  1363. $res = $wpdb->get_results( $res_prepared, ARRAY_A );
  1364. if ( isset( $this->icl_language_name_cache ) ) {
  1365. $this->icl_language_name_cache->set( 'in_language_' . $in_language, $res );
  1366. }
  1367. }
  1368. $languages = array();
  1369. if ( $res ) {
  1370. foreach ( $res as $r ) {
  1371. $languages[ $r[ 'code' ] ] = $r;
  1372. }
  1373. }
  1374. if ( isset( $this->icl_language_name_cache ) ) {
  1375. $res = $this->icl_language_name_cache->get( 'languages' );
  1376. } else {
  1377. $res = null;
  1378. }
  1379. //WPML setup
  1380. if ( !isset($this->settings[ 'setup_complete' ]) || empty( $this->settings[ 'setup_complete' ] ) ) {
  1381. $res = null;
  1382. }
  1383. if ( !$res && $languages ) {
  1384. $res = $wpdb->get_results( "
  1385. SELECT language_code, name
  1386. FROM {$wpdb->prefix}icl_languages_translations
  1387. WHERE language_code IN ('" . join( "','", array_keys( $languages ) ) . "') AND language_code = display_language_code
  1388. " );
  1389. if ( isset( $this->icl_language_name_cache ) ) {
  1390. $this->icl_language_name_cache->set( 'languages', $res );
  1391. }
  1392. }
  1393. if($res) {
  1394. foreach ( $res as $row ) {
  1395. $languages[ $row->language_code ][ 'native_name' ] = $row->name;
  1396. }
  1397. $this->active_languages = $languages;
  1398. }
  1399. }
  1400. // hide languages for front end
  1401. if ( !is_admin() && isset( $this->settings[ 'hidden_languages' ]) && !empty( $this->settings[ 'hidden_languages' ] ) && is_array( $this->settings[ 'hidden_languages' ] ) ) {
  1402. if ( !isset( $current_user ) || !$current_user ) {
  1403. get_currentuserinfo();
  1404. }
  1405. if ( empty( $current_user->data ) || !get_user_meta( $this->get_current_user()->ID, 'icl_show_hidden_languages', true ) ) {
  1406. foreach ( $this->settings[ 'hidden_languages' ] as $l ) {
  1407. unset( $this->active_languages[ $l ] );
  1408. }
  1409. }
  1410. }
  1411. return $this->active_languages;
  1412. }
  1413. function order_languages( $languages )
  1414. {
  1415. $ordered_languages = array();
  1416. if ( is_array( $this->settings[ 'languages_order' ] ) ) {
  1417. foreach ( $this->settings[ 'languages_order' ] as $code ) {
  1418. if ( isset( $languages[ $code ] ) ) {
  1419. $ordered_languages[ $code ] = $languages[ $code ];
  1420. unset( $languages[ $code ] );
  1421. }
  1422. }
  1423. } else {
  1424. // initial save
  1425. $iclsettings[ 'languages_order' ] = array_keys( $languages );
  1426. $this->save_settings( $iclsettings );
  1427. }
  1428. if ( !empty( $languages ) ) {
  1429. foreach ( $languages as $code => $lang ) {
  1430. $ordered_languages[ $code ] = $lang;
  1431. }
  1432. }
  1433. return $ordered_languages;
  1434. }
  1435. function set_active_languages( $arr )
  1436. {
  1437. global $wpdb;
  1438. if ( !empty( $arr ) ) {
  1439. $tmp = array();
  1440. foreach ( $arr as $code ) {
  1441. $tmp[ ] = esc_sql( trim( $code ) );
  1442. }
  1443. // set the locale
  1444. $current_active_languages = (array)$wpdb->get_col( "SELECT code FROM {$wpdb->prefix}icl_languages WHERE active = 1" );
  1445. $new_languages = array_diff( $tmp, $current_active_languages );
  1446. if ( !empty( $new_languages ) ) {
  1447. foreach ( $new_languages as $code ) {
  1448. $default_locale_prepared = $wpdb->prepare( "SELECT default_locale FROM {$wpdb->prefix}icl_languages WHERE code=%s", $code );
  1449. $default_locale = $wpdb->get_var( $default_locale_prepared );
  1450. if ( $default_locale ) {
  1451. $code_exists_prepared = $wpdb->prepare( "SELECT code FROM {$wpdb->prefix}icl_locale_map WHERE code=%s", $code );
  1452. $code_exists = $wpdb->get_var( $code_exists_prepared );
  1453. if ( $code_exists ) {
  1454. $wpdb->update( $wpdb->prefix . 'icl_locale_map', array( 'locale' => $default_locale ), array( 'code' => $code ) );
  1455. } else {
  1456. $wpdb->insert( $wpdb->prefix . 'icl_locale_map', array( 'code' => $code, 'locale' => $default_locale ) );
  1457. }
  1458. }
  1459. }
  1460. }
  1461. $codes = '(\'' . join( '\',\'', $tmp ) . '\')';
  1462. $wpdb->update( $wpdb->prefix . 'icl_languages', array( 'active' => 0 ), array( 'active' => '1' ) );
  1463. $wpdb->query( "UPDATE {$wpdb->prefix}icl_languages SET active=1 WHERE code IN {$codes}" );
  1464. $this->icl_language_name_cache->clear();
  1465. }
  1466. $res_prepared = $wpdb->prepare( "
  1467. SELECT code, english_name, active, lt.name AS display_name
  1468. FROM {$wpdb->prefix}icl_languages l
  1469. JOIN {$wpdb->prefix}icl_languages_translations lt ON l.code=lt.language_code
  1470. WHERE
  1471. active=1 AND lt.display_language_code = %s
  1472. ORDER BY major DESC, english_name ASC", $this->get_default_language() );
  1473. $res = $wpdb->get_results( $res_prepared, ARRAY_A );
  1474. $languages = array();
  1475. foreach ( $res as $r ) {
  1476. $languages[ ] = $r;
  1477. }
  1478. $this->active_languages = $languages;
  1479. return true;
  1480. }
  1481. function get_languages( $lang = false )
  1482. {
  1483. global $wpdb;
  1484. if ( !$lang ) {
  1485. $lang = $this->get_default_language();
  1486. }
  1487. $res_query = "SELECT
  1488. code, english_name, major, active, default_locale, lt.name AS display_name
  1489. FROM {$wpdb->prefix}icl_languages l
  1490. JOIN {$wpdb->prefix}icl_languages_translations lt ON l.code=lt.language_code
  1491. WHERE lt.display_language_code = %s
  1492. ORDER BY major DESC, english_name ASC";
  1493. $res_query_prepared = $wpdb->prepare($res_query, $lang );
  1494. $res = $wpdb->get_results( $res_query_prepared, ARRAY_A );
  1495. $languages = array();
  1496. foreach ( (array)$res as $r ) {
  1497. $languages[ ] = $r;
  1498. }
  1499. return $languages;
  1500. }
  1501. function get_language_details( $code )
  1502. {
  1503. global $wpdb;
  1504. if ( defined( 'WP_ADMIN' ) ) {
  1505. $dcode = $this->admin_language;
  1506. } else {
  1507. $dcode = $code;
  1508. }
  1509. if ( isset( $this->icl_language_name_cache ) ) {
  1510. $details = $this->icl_language_name_cache->get( 'language_details_' . $code . $dcode );
  1511. } else {
  1512. $details = null;
  1513. }
  1514. if ( !$details ) {
  1515. $details = $wpdb->get_row( "
  1516. SELECT
  1517. code, english_name, major, active, lt.name AS display_name
  1518. FROM {$wpdb->prefix}icl_languages l
  1519. JOIN {$wpdb->prefix}icl_languages_translations lt ON l.code=lt.language_code
  1520. WHERE lt.display_language_code = '{$dcode}' AND code='{$code}'
  1521. ORDER BY major DESC, english_name ASC", ARRAY_A );
  1522. if ( isset( $this->icl_language_name_cache ) ) {
  1523. $this->icl_language_name_cache->set( 'language_details_' . $code . $dcode, $details );
  1524. }
  1525. }
  1526. return $details;
  1527. }
  1528. function get_language_code( $english_name )
  1529. {
  1530. global $wpdb;
  1531. $code = $wpdb->get_row( "
  1532. SELECT
  1533. code
  1534. FROM {$wpdb->prefix}icl_languages
  1535. WHERE english_name = '{$english_name}'", ARRAY_A );
  1536. return $code[ 'code' ];
  1537. }
  1538. function get_icl_translator_status( &$iclsettings, $res = null )
  1539. {
  1540. if ( $res == null ) {
  1541. // check what languages we have translators for.
  1542. require_once ICL_PLUGIN_PATH . '/lib/Snoopy.class.php';
  1543. require_once ICL_PLUGIN_PATH . '/lib/xml2array.php';
  1544. require_once ICL_PLUGIN_PATH . '/lib/icl_api.php';
  1545. $icl_query = false;
  1546. if ( empty( $iclsettings[ 'site_id' ] ) ) {
  1547. // Must be for support
  1548. if ( !empty( $iclsettings[ 'support_site_id' ] ) ) {
  1549. $icl_query = new ICanLocalizeQuery( $iclsettings[ 'support_site_id' ], $iclsettings[ 'support_access_key' ] );
  1550. }
  1551. } else {
  1552. $icl_query = new ICanLocalizeQuery( $iclsettings[ 'site_id' ], $iclsettings[ 'access_key' ] );
  1553. }
  1554. if ( $icl_query === false ) {
  1555. return;
  1556. }
  1557. $res = $icl_query->get_website_details();
  1558. }
  1559. if ( isset( $res[ 'translation_languages' ][ 'translation_language' ] ) ) {
  1560. // reset $this->settings['icl_lang_status']
  1561. $iclsettings[ 'icl_lang_status' ] = array();
  1562. $translation_languages = $res[ 'translation_languages' ][ 'translation_language' ];
  1563. if ( !isset( $translation_languages[ 0 ] ) ) {
  1564. $buf = $translation_languages;
  1565. $translation_languages = array( 0 => $buf );
  1566. }
  1567. $target = array();
  1568. foreach ( $translation_languages as $lang ) {
  1569. $translators = $_tr = array();
  1570. $max_rate = false;
  1571. if ( isset( $lang[ 'translators' ] ) && !empty( $lang[ 'translators' ] ) ) {
  1572. if ( !isset( $lang[ 'translators' ][ 'translator' ][ 0 ] ) ) {
  1573. $_tr[ 0 ] = $lang[ 'translators' ][ 'translator' ];
  1574. } else {
  1575. $_tr = $lang[ 'translators' ][ 'translator' ];
  1576. }
  1577. foreach ( $_tr as $t ) {
  1578. if ( $max_rate === false || $t[ 'attr' ][ 'amount' ] > $max_rate ) {
  1579. $max_rate = $t[ 'attr' ][ 'amount' ];
  1580. }
  1581. $translators[ ] = array( 'id' => $t[ 'attr' ][ 'id' ], 'nickname' => $t[ 'attr' ][ 'nickname' ], 'contract_id' => $t[ 'attr' ][ 'contract_id' ] );
  1582. }
  1583. }
  1584. $target[ ] = array(
  1585. 'from' => $this->get_language_code( ICL_Pro_Translation::server_languages_map( $lang[ 'attr' ][ 'from_language_name' ], true ) ),
  1586. 'to' => $this->get_language_code( ICL_Pro_Translation::server_languages_map( $lang[ 'attr' ][ 'to_language_name' ], true ) ), 'have_translators' => $lang[ 'attr' ][ 'have_translators' ],
  1587. 'available_translators' => $lang[ 'attr' ][ 'available_translators' ], 'applications' => $lang[ 'attr' ][ 'applications' ], 'contract_id' => $lang[ 'attr' ][ 'contract_id' ], 'id' => $lang[ 'attr' ][ 'id' ],
  1588. 'translators' => $translators, 'max_rate' => $max_rate
  1589. );
  1590. }
  1591. $iclsettings[ 'icl_lang_status' ] = $target;
  1592. }
  1593. if ( isset( $res[ 'client' ][ 'attr' ] ) ) {
  1594. $iclsettings[ 'icl_balance' ] = $res[ 'client' ][ 'attr' ][ 'balance' ];
  1595. $iclsettings[ 'icl_anonymous_user' ] = $res[ 'client' ][ 'attr' ][ 'anon' ];
  1596. }
  1597. if ( isset( $res[ 'html_status' ][ 'value' ] ) ) {
  1598. $iclsettings[ 'icl_html_status' ] = html_entity_decode( $res[ 'html_status' ][ 'value' ] );
  1599. $iclsettings[ 'icl_html_status' ] = preg_replace_callback(
  1600. '#<a([^>]*)href="([^"]+)"([^>]*)>#i',
  1601. create_function( '$matches', 'global $sitepress; return $sitepress->create_icl_popup_link($matches[2], array(\'unload_cb\'=>\'icl_pt_reload_translation_box\'));' ),
  1602. $iclsettings[ 'icl_html_status' ]
  1603. );
  1604. }
  1605. if ( isset( $res[ 'translators_management_info' ][ 'value' ] ) ) {
  1606. $iclsettings[ 'translators_management_info' ] = html_entity_decode( $res[ 'translators_management_info' ][ 'value' ] );
  1607. $iclsettings[ 'translators_management_info' ] = preg_replace_callback(
  1608. '#<a([^>]*)href="([^"]+)"([^>]*)>#i',
  1609. create_function( '$matches', 'global $sitepress; return $sitepress->create_icl_popup_link($matches[2], array(\'unload_cb\'=>\'icl_pt_reload_translation_box\'));' ),
  1610. $iclsettings[ 'translators_management_info' ]
  1611. );
  1612. }
  1613. $iclsettings[ 'icl_support_ticket_id' ] = @intval( $res[ 'attr' ][ 'support_ticket_id' ] );
  1614. }
  1615. function get_language_status_text( $from_lang, $to_lang, $pop_close_cb = false )
  1616. {
  1617. $pop_args = array( 'title' => 'ICanLocalize' );
  1618. if ( $pop_close_cb ) {
  1619. $pop_args[ 'unload_cb' ] = $pop_close_cb;
  1620. }
  1621. $lang_status = !empty( $this->settings[ 'icl_lang_status' ] ) ? $this->settings[ 'icl_lang_status' ] : array();
  1622. foreach ( $lang_status as $lang ) {
  1623. if ( $from_lang == $lang[ 'from' ] && $to_lang == $lang[ 'to' ] ) {
  1624. if ( isset( $lang[ 'available_translators' ] ) ) {
  1625. if ( !$lang[ 'available_translators' ] ) {
  1626. if ( $this->settings[ 'icl_support_ticket_id' ] == '' ) {
  1627. // No translators available on icanlocalize for this language pair.
  1628. $response = sprintf( __( '- (No translators available - please %sprovide more information about your site%s)', 'sitepress' ), $this->create_icl_popup_link( ICL_API_ENDPOINT . '/websites/' . $this->settings[ 'site_id' ] . '/explain?after=refresh_langs', $pop_args ), '</a>' );
  1629. } else {
  1630. $response = sprintf( __( '- (No translators available - %scheck progress%s)', 'sitepress' ), $this->create_icl_popup_link( ICL_API_ENDPOINT . '/support/show/' . $this->settings[ 'icl_support_ticket_id' ] . '?after=refresh_langs', $pop_args ), '</a>' );
  1631. }
  1632. } else {
  1633. if ( !$lang[ 'applications' ] ) {
  1634. // No translators have applied for this language pair.
  1635. $pop_args[ 'class' ] = 'icl_hot_link';
  1636. $response = ' | ' . $this->create_icl_popup_link( "@select-translators;{$from_lang};{$to_lang}@", $pop_args ) . __( 'Select translators', 'sitepress' ) . '</a>';
  1637. } else {
  1638. if ( !$lang[ 'have_translators' ] ) {
  1639. // translators have applied but none selected yet
  1640. $pop_args[ 'class' ] = 'icl_hot_link';
  1641. $response = ' | ' . $this->create_icl_popup_link( "@select-translators;{$from_lang};{$to_lang}@", $pop_args ) . __( 'Select translators', 'sitepress' ) . '</a>';
  1642. } else {
  1643. // there are translators ready to translate
  1644. $translators = array();
  1645. if ( is_array( $lang[ 'translators' ] ) ) {
  1646. foreach ( $lang[ 'translators' ] as $translator ) {
  1647. $link = $this->create_icl_popup_link( ICL_API_ENDPOINT . '/websites/' . $this->settings[ 'site_id' ] . '/website_translation_offers/' . $lang[ 'id' ] . '/website_translation_contracts/' . $translator[ 'contract_id' ], $pop_args );
  1648. $translators[ ] = $link . esc_html( $translator[ 'nickname' ] ) . '</a>';
  1649. }
  1650. }
  1651. $response = ' | ' . $this->create_icl_popup_link( "@select-translators;{$from_lang};{$to_lang}@", $pop_args ) . __( 'Select translators', 'sitepress' ) . '</a>';
  1652. $response .= ' | ' . sprintf( __( 'Communicate with %s', 'sitepress' ), join( ', ', $translators ) );
  1653. }
  1654. }
  1655. }
  1656. return $response;
  1657. }
  1658. break;
  1659. }
  1660. }
  1661. $pop_args[ 'class' ] = 'icl_hot_link';
  1662. $response = ' | ' . $this->create_icl_popup_link( "@select-translators;{$from_lang};{$to_lang}@", $pop_args ) . __( 'Select translators', 'sitepress' ) . '</a>';
  1663. // no status found
  1664. return $response;
  1665. }
  1666. function are_waiting_for_translators( $from_lang )
  1667. {
  1668. $lang_status = $this->settings[ 'icl_lang_status' ];
  1669. if ( $lang_status && $this->icl_account_configured() ) {
  1670. foreach ( $lang_status as $lang ) {
  1671. if ( $from_lang == $lang[ 'from' ] ) {
  1672. if ( isset( $lang[ 'available_translators' ] ) ) {
  1673. if ( $lang[ 'available_translators' ] && !$lang[ 'applications' ] ) {
  1674. return true;
  1675. }
  1676. }
  1677. }
  1678. }
  1679. }
  1680. return false;
  1681. }
  1682. function get_default_language()
  1683. {
  1684. return isset( $this->settings[ 'default_language' ] ) ? $this->settings[ 'default_language' ] : false;
  1685. }
  1686. function get_current_language()
  1687. {
  1688. return apply_filters( 'icl_current_language', $this->this_lang );
  1689. }
  1690. function get_strings_language()
  1691. {
  1692. return isset( $this->settings[ 'st' ][ 'strings_language' ] ) ? $this->settings[ 'st' ][ 'strings_language' ] : false;
  1693. }
  1694. function switch_lang( $code = null, $cookie_lang = false )
  1695. {
  1696. static $original_language, $original_language_cookie;
  1697. if ( is_null( $original_language ) ) {
  1698. $original_language = $this->get_current_language();
  1699. }
  1700. if ( is_null( $code ) ) {
  1701. $this->this_lang = $original_language;
  1702. $this->admin_language = $original_language;
  1703. // restore cookie language if case
  1704. if ( !empty( $original_language_cookie ) ) {
  1705. $this->update_language_cookie($original_language_cookie);
  1706. $original_language_cookie = false;
  1707. }
  1708. } else {
  1709. if ( $code == 'all' || in_array( $code, array_keys( $this->get_active_languages() ) ) ) {
  1710. $this->this_lang = $code;
  1711. $this->admin_language = $code;
  1712. }
  1713. // override cookie language
  1714. if ( $cookie_lang ) {
  1715. $original_language_cookie = $this->get_language_cookie();
  1716. $this->update_language_cookie($code);
  1717. }
  1718. }
  1719. }
  1720. function set_default_language( $code )
  1721. {
  1722. $previous_default = $this->get_setting('default_language');
  1723. $this->set_setting( 'default_language', $code);
  1724. $this->save_settings();
  1725. do_action('icl_after_set_default_language',$code, $previous_default);
  1726. // change WP locale
  1727. $locale = $this->get_locale( $code );
  1728. if ( $locale ) {
  1729. update_option( 'WPLANG', $locale );
  1730. }
  1731. if ( $code != 'en' && !file_exists( ABSPATH . LANGDIR . '/' . $locale . '.mo' ) ) {
  1732. return 1; //locale not installed
  1733. }
  1734. return true;
  1735. }
  1736. function get_icl_translation_enabled( $lang = null, $langto = null )
  1737. {
  1738. if ( !is_null( $lang ) ) {
  1739. if ( !is_null( $langto ) ) {
  1740. return $this->settings[ 'language_pairs' ][ $lang ][ $langto ];
  1741. } else {
  1742. return !empty( $this->settings[ 'language_pairs' ][ $lang ] );
  1743. }
  1744. } else {
  1745. return isset( $this->settings[ 'enable_icl_translations' ] ) ? $this->settings[ 'enable_icl_translations' ] : false;
  1746. }
  1747. }
  1748. function set_icl_translation_enabled()
  1749. {
  1750. $iclsettings[ 'translation_enabled' ] = true;
  1751. $this->save_settings( $iclsettings );
  1752. }
  1753. function icl_account_reqs()
  1754. {
  1755. $errors = array();
  1756. if ( !$this->get_icl_translation_enabled() ) {
  1757. $errors[ ] = __( 'Professional translation not enabled', 'sitepress' );
  1758. }
  1759. return $errors;
  1760. }
  1761. function icl_account_configured()
  1762. {
  1763. return isset( $this->settings[ 'site_id' ] ) && $this->settings[ 'site_id' ] && isset( $this->settings[ 'access_key' ] ) && $this->settings[ 'access_key' ];
  1764. }
  1765. function icl_support_configured()
  1766. {
  1767. return isset( $this->settings[ 'support_site_id' ] ) && isset( $this->settings[ 'support_access_key' ] ) && $this->settings[ 'support_site_id' ] && $this->settings[ 'support_access_key' ];
  1768. }
  1769. function reminders_popup()
  1770. {
  1771. include ICL_PLUGIN_PATH . '/modules/icl-translation/icl-reminder-popup.php';
  1772. exit;
  1773. }
  1774. function create_icl_popup_link( $link, $args = array(), $just_url = false, $support_mode = false )
  1775. {
  1776. // defaults
  1777. /** @var $id int */
  1778. /** @var $class string */
  1779. $defaults = array(
  1780. 'title' => null, 'class' => '', 'id' => '', 'ar' => 0, // auto_resize
  1781. 'unload_cb' => false, // onunload callback
  1782. );
  1783. extract( $defaults );
  1784. extract( $args, EXTR_OVERWRITE );
  1785. if ( !empty( $ar ) ) {
  1786. $auto_resize = '&amp;auto_resize=1';
  1787. } else {
  1788. $auto_resize = '';
  1789. }
  1790. $unload_cb = isset( $unload_cb ) ? '&amp;unload_cb=' . $unload_cb : '';
  1791. $url_glue = false !== strpos( $link, '?' ) ? '&' : '?';
  1792. $link .= $url_glue . 'compact=1';
  1793. if ( isset( $this->settings[ 'access_key' ] ) || isset( $this->settings[ 'support_access_key' ] ) ) {
  1794. if ( $support_mode && isset( $this->settings[ 'support_access_key' ] ) ) {
  1795. $link .= '&accesskey=' . $this->settings[ 'support_access_key' ];
  1796. } elseif ( isset( $this->settings[ 'access_key' ] ) ) {
  1797. $link .= '&accesskey=' . $this->settings[ 'access_key' ];
  1798. }
  1799. }
  1800. if ( !empty( $id ) ) {
  1801. $id = ' id="' . $id . '"';
  1802. }
  1803. if ( isset( $title ) && !$just_url ) {
  1804. return '<a class="icl_thickbox ' . $class . '" title="' . $title . '" href="admin.php?page=' . ICL_PLUGIN_FOLDER . "/menu/languages.php&amp;icl_action=reminder_popup{$auto_resize}{$unload_cb}&amp;target=" . urlencode( $link ) . '"' . $id . '>';
  1805. } else {
  1806. if ( !$just_url ) {
  1807. return '<a class="icl_thickbox ' . $class . '" href="admin.php?page=' . ICL_PLUGIN_FOLDER . "/menu/languages.php&amp;icl_action=reminder_popup{$auto_resize}{$unload_cb}&amp;target=" . urlencode( $link ) . '"' . $id . '>';
  1808. } else {
  1809. return 'admin.php?page=' . ICL_PLUGIN_FOLDER . "/menu/languages.php&amp;icl_action=reminder_popup{$auto_resize}{$unload_cb}&amp;target=" . urlencode( $link );
  1810. }
  1811. }
  1812. }
  1813. function js_scripts_setup()
  1814. {
  1815. //TODO: move javascript to external resource (use wp_localize_script() to pass arguments)
  1816. global $pagenow, $wpdb;
  1817. $default_language = $this->get_default_language();
  1818. $current_language = $this->get_current_language();
  1819. if ( isset( $_GET[ 'page' ] ) ) {
  1820. $page = basename( $_GET[ 'page' ] );
  1821. $page_basename = str_replace( '.php', '', $page );
  1822. } else {
  1823. $page_basename = false;
  1824. }
  1825. $icl_ajax_url_root = rtrim(get_site_url(),'/');
  1826. if(defined('FORCE_SSL_ADMIN') && FORCE_SSL_ADMIN) {
  1827. $icl_ajax_url_root = str_replace('http://', 'https://', $icl_ajax_url_root);
  1828. }
  1829. $icl_ajax_url = $icl_ajax_url_root . '/wp-admin/admin.php?page=' . ICL_PLUGIN_FOLDER . '/menu/languages.php';
  1830. ?>
  1831. <script type="text/javascript">
  1832. // <![CDATA[
  1833. var icl_ajx_url;
  1834. icl_ajx_url = '<?php echo $icl_ajax_url; ?>';
  1835. var icl_ajx_saved = '<?php echo icl_js_escape( __('Data saved','sitepress')); ?>';
  1836. var icl_ajx_error = '<?php echo icl_js_escape( __('Error: data not saved','sitepress')); ?>';
  1837. var icl_default_mark = '<?php echo icl_js_escape(__('default','sitepress')); ?>';
  1838. var icl_this_lang = '<?php echo $this->this_lang ?>';
  1839. var icl_ajxloaderimg_src = '<?php echo ICL_PLUGIN_URL ?>/res/img/ajax-loader.gif';
  1840. var icl_cat_adder_msg = '<?php echo icl_js_escape(sprintf(__('To add categories that already exist in other languages go to the <a%s>category management page</a>','sitepress'), ' href="'.admin_url('edit-tags.php?taxonomy=category').'"'));?>';
  1841. // ]]>
  1842. <?php if(!$this->get_setting('ajx_health_checked')): ?>
  1843. addLoadEvent(function () {
  1844. jQuery.ajax({type: "POST", url: icl_ajx_url, data: "icl_ajx_action=health_check", error: function (msg) {
  1845. var icl_initial_language = jQuery('#icl_initial_language');
  1846. if (icl_initial_language.length) {
  1847. icl_initial_language.find('input').attr('disabled', 'disabled');
  1848. }
  1849. jQuery('.wrap').prepend('<div class="error"><p><?php
  1850. echo icl_js_escape(sprintf(__("WPML can't run normally. There is an installation or server configuration problem. %sShow details%s",'sitepress'),
  1851. '<a href="#" onclick="jQuery(this).parent().next().slideToggle()">', '</a>'));
  1852. ?></p><p style="display:none"><?php echo icl_js_escape(__('AJAX Error:', 'sitepress'))?> ' + msg.statusText + ' [' + msg.status + ']<br />URL:' + icl_ajx_url + '</p></div>');
  1853. }});
  1854. });
  1855. <?php endif; ?>
  1856. </script>
  1857. <?php
  1858. if ( 'options-reading.php' == $pagenow ) {
  1859. list( $warn_home, $warn_posts ) = $this->verify_home_and_blog_pages_translations();
  1860. if ( $warn_home || $warn_posts ) {
  1861. ?>
  1862. <script type="text/javascript">
  1863. addLoadEvent(function () {
  1864. jQuery('input[name="show_on_front"]').parent().parent().parent().parent().append('<?php echo str_replace("'","\\'",$warn_home . $warn_posts); ?>');
  1865. });
  1866. </script>
  1867. <?php
  1868. }
  1869. }
  1870. // display correct links on the posts by status break down
  1871. // also fix links to category and tag pages
  1872. if ( ( 'edit.php' == $pagenow || 'edit-pages.php' == $pagenow || 'categories.php' == $pagenow || 'edit-tags.php' == $pagenow ) && $current_language != $default_language ) {
  1873. ?>
  1874. <script type="text/javascript">
  1875. addLoadEvent(function () {
  1876. jQuery('.subsubsub li a').each(function () {
  1877. var h = jQuery(this).attr('href');
  1878. var urlg;
  1879. if (-1 == h.indexOf('?')) urlg = '?'; else urlg = '&';
  1880. jQuery(this).attr('href', h + urlg + 'lang=<?php echo $current_language?>');
  1881. });
  1882. jQuery('.column-categories a, .column-tags a, .column-posts a').each(function () {
  1883. jQuery(this).attr('href', jQuery(this).attr('href') + '&lang=<?php echo $current_language?>');
  1884. });
  1885. });
  1886. </script>
  1887. <?php
  1888. }
  1889. if ( 'edit-tags.php' == $pagenow ) {
  1890. ?>
  1891. <script type="text/javascript">
  1892. addLoadEvent(function () {
  1893. var edit_tag = jQuery('#edittag');
  1894. if (edit_tag.find('[name="_wp_original_http_referer"]').length && edit_tag.find('[name="_wp_http_referer"]').length) {
  1895. edit_tag.find('[name="_wp_original_http_referer"]').val('<?php
  1896. $post_type = isset($_GET['post_type']) ? '&post_type=' . esc_html($_GET['post_type']) : '';
  1897. echo admin_url('edit-tags.php?taxonomy=' . esc_js($_GET['taxonomy']) . '&lang='.$current_language.'&message=3'.$post_type) ?>');
  1898. }
  1899. });
  1900. </script>
  1901. <?php
  1902. }
  1903. if ( 'post-new.php' == $pagenow ) {
  1904. if ( isset( $_GET[ 'trid' ] ) ) {
  1905. $translations = $wpdb->get_col( $wpdb->prepare( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d", $_GET[ 'trid' ] ) );
  1906. remove_filter( 'option_sticky_posts', array( $this, 'option_sticky_posts' ) ); // remove filter used to get language relevant stickies. get them all
  1907. $sticky_posts = get_option( 'sticky_posts' );
  1908. add_filter( 'option_sticky_posts', array( $this, 'option_sticky_posts' ) ); // add filter back
  1909. $is_sticky = false;
  1910. foreach ( $translations as $t ) {
  1911. if ( in_array( $t, $sticky_posts ) ) {
  1912. $is_sticky = true;
  1913. break;
  1914. }
  1915. }
  1916. if ( isset( $_GET[ 'trid' ] ) && ( $this->settings[ 'sync_ping_status' ] || $this->settings[ 'sync_comment_status' ] ) ) {
  1917. $res = $wpdb->get_row( $wpdb->prepare( "SELECT comment_status, ping_status FROM {$wpdb->prefix}icl_translations t
  1918. JOIN {$wpdb->posts} p ON t.element_id = p.ID WHERE t.trid=%d", $_GET[ 'trid' ] ) ); ?>
  1919. <script type="text/javascript">addLoadEvent(function () {
  1920. var comment_status = jQuery('#comment_status');
  1921. var ping_status = jQuery('#ping_status');
  1922. <?php if($this->settings['sync_comment_status']): ?>
  1923. <?php if($res->comment_status == 'open'): ?>
  1924. comment_status.attr('checked', 'checked');
  1925. <?php else: ?>
  1926. comment_status.removeAttr('checked');
  1927. <?php endif; ?>
  1928. <?php endif; ?>
  1929. <?php if($this->settings['sync_ping_status']): ?>
  1930. <?php if($res->ping_status == 'open'): ?>
  1931. ping_status.attr('checked', 'checked');
  1932. <?php else: ?>
  1933. ping_status.removeAttr('checked');
  1934. <?php endif; ?>
  1935. <?php endif; ?>
  1936. });</script><?php
  1937. }
  1938. if ( isset( $_GET[ 'trid' ] ) && $this->settings[ 'sync_private_flag' ] ) {
  1939. if ( 'private' == $wpdb->get_var( $wpdb->prepare( "
  1940. SELECT p.post_status FROM {$wpdb->prefix}icl_translations t
  1941. JOIN {$wpdb->posts} p ON t.element_id = p.ID
  1942. WHERE t.trid=%d AND t.element_type='post_post'
  1943. ", $_GET[ 'trid' ] ) )
  1944. ) {
  1945. ?>
  1946. <script type="text/javascript">addLoadEvent(function () {
  1947. jQuery('#visibility-radio-private').attr('checked', 'checked');
  1948. jQuery('#post-visibility-display').html('<?php echo icl_js_escape(__('Private', 'sitepress')); ?>');
  1949. });
  1950. </script><?php
  1951. }
  1952. }
  1953. if ( isset( $_GET[ 'trid' ] ) && $this->settings[ 'sync_post_taxonomies' ] ) {
  1954. $post_type = isset( $_GET[ 'post_type' ] ) ? $_GET[ 'post_type' ] : 'post';
  1955. $source_lang = isset( $_GET[ 'source_lang' ] ) ? $_GET[ 'source_lang' ] : $default_language;
  1956. $translatable_taxs = $this->get_translatable_taxonomies( true, $post_type );
  1957. $all_taxs = get_object_taxonomies( $post_type );
  1958. $translations = $this->get_element_translations( $_GET[ 'trid' ], 'post_' . $post_type );
  1959. $js = array();
  1960. if ( !empty( $all_taxs ) ) {
  1961. foreach ( $all_taxs as $tax ) {
  1962. $tax_detail = get_taxonomy( $tax );
  1963. $terms = get_the_terms( $translations[ $source_lang ]->element_id, $tax );
  1964. $term_names = array();
  1965. if ( $terms ) {
  1966. foreach ( $terms as $term ) {
  1967. if ( $tax_detail->hierarchical ) {
  1968. if ( in_array( $tax, $translatable_taxs ) ) {
  1969. $term_id = icl_object_id( $term->term_id, $tax, false );
  1970. } else {
  1971. $term_id = $term->term_id;
  1972. }
  1973. $js[ ] = "jQuery('#in-" . $tax . "-" . $term_id . "').attr('checked', 'checked');";
  1974. } else {
  1975. if ( in_array( $tax, $translatable_taxs ) ) {
  1976. $term_id = icl_object_id( $term->term_id, $tax, false );
  1977. if ( $term_id ) {
  1978. $term = get_term_by( 'id', $term_id, $tax );
  1979. $term_names[ ] = esc_js( $term->name );
  1980. }
  1981. } else {
  1982. $term_names[ ] = esc_js( $term->name );
  1983. }
  1984. }
  1985. }
  1986. }
  1987. if ( $term_names ) {
  1988. $js[ ] = "jQuery('#{$tax} .taghint').css('visibility','hidden');";
  1989. $js[ ] = "jQuery('#new-tag-{$tax}').val('" . join( ', ', $term_names ) . "');";
  1990. }
  1991. }
  1992. }
  1993. if ( $js ) {
  1994. echo '<script type="text/javascript">';
  1995. echo PHP_EOL . '// <![CDATA[' . PHP_EOL;
  1996. echo 'addLoadEvent(function(){' . PHP_EOL;
  1997. echo join( PHP_EOL, $js );
  1998. echo PHP_EOL . 'jQuery().ready(function() {
  1999. jQuery(".tagadd").click();
  2000. jQuery(\'html, body\').prop({scrollTop:0});
  2001. jQuery(\'#title\').focus();
  2002. });' . PHP_EOL;
  2003. echo PHP_EOL . '});' . PHP_EOL;
  2004. echo PHP_EOL . '// ]]>' . PHP_EOL;
  2005. echo '</script>';
  2006. }
  2007. }
  2008. // sync custom fields
  2009. if ( !empty( $this->settings[ 'translation-management' ] ) ) {
  2010. foreach ( (array)$this->settings[ 'translation-management' ][ 'custom_fields_translation' ] as $key => $sync_opt ) {
  2011. if ( $sync_opt == 1 ) {
  2012. $copied_cf[ ] = $key;
  2013. }
  2014. }
  2015. }
  2016. if ( !empty( $copied_cf ) ) {
  2017. $source_lang = isset( $_GET[ 'source_lang' ] ) ? $_GET[ 'source_lang' ] : $default_language;
  2018. $lang_details = $this->get_language_details( $source_lang );
  2019. $original_custom = get_post_custom( $translations[ $source_lang ]->element_id );
  2020. $copied_cf = array_intersect( $copied_cf, array_keys( $original_custom ) );
  2021. $copied_cf = apply_filters( 'icl_custom_fields_to_be_copied', $copied_cf, $translations[ $source_lang ]->element_id );
  2022. if ( !empty( $copied_cf ) && ( empty( $this->user_preferences[ 'notices' ] ) || empty( $this->user_preferences[ 'notices' ][ 'hide_custom_fields_copy' ] ) ) ) {
  2023. $ccf_note = '<img src="' . ICL_PLUGIN_URL . '/res/img/alert.png" alt="Notice" width="16" height="16" style="margin-right:8px" />';
  2024. $ccf_note .= '<a class="icl_user_notice_hide" href="#hide_custom_fields_copy" style="float:right;margin-left:20px;">' . __( 'Never show this.', 'sitepress' ) . '</a>';
  2025. $ccf_note .= wp_nonce_field( 'save_user_preferences_nonce', '_icl_nonce_sup', false, false );
  2026. $ccf_note .= sprintf( __( 'WPML will copy %s from %s when you save this post.', 'sitepress' ), '<i><strong>' . join( '</strong>, <strong>', $copied_cf ) . '</strong></i>', $lang_details[ 'display_name' ] );
  2027. $this->admin_notices( $ccf_note, 'error' );
  2028. }
  2029. }
  2030. }
  2031. ?>
  2032. <?php if ( !empty( $is_sticky ) && $this->settings[ 'sync_sticky_flag' ] ): ?>
  2033. <script type="text/javascript">
  2034. addLoadEvent(
  2035. function () {
  2036. jQuery('#sticky').attr('checked', 'checked');
  2037. var post_visibility_display = jQuery('#post-visibility-display');
  2038. post_visibility_display.html(post_visibility_display.html() + ', <?php echo icl_js_escape(__('Sticky', 'sitepress')) ?>');
  2039. });
  2040. </script>
  2041. <?php endif; ?>
  2042. <?php
  2043. }
  2044. if ( 'page-new.php' == $pagenow || ( 'post-new.php' == $pagenow && isset( $_GET[ 'post_type' ] ) ) ) {
  2045. if ( isset( $_GET[ 'trid' ] ) && ( $this->settings[ 'sync_page_template' ] || $this->settings[ 'sync_page_ordering' ] ) ) {
  2046. $res = $wpdb->get_row( $wpdb->prepare( "
  2047. SELECT p.ID, p.menu_order FROM {$wpdb->prefix}icl_translations t
  2048. JOIN {$wpdb->posts} p ON t.element_id = p.ID
  2049. WHERE t.trid=%d AND p.post_type=%s AND t.element_type=%s
  2050. ", $_GET[ 'trid' ], $_GET[ 'post_type' ], 'post_' . $_GET[ 'post_type' ] ) );
  2051. if ( $this->settings[ 'sync_page_ordering' ] ) {
  2052. $menu_order = $res->menu_order;
  2053. } else {
  2054. $menu_order = false;
  2055. }
  2056. if ( $this->settings[ 'sync_page_template' ] ) {
  2057. $page_template = get_post_meta( $res->ID, '_wp_page_template', true );
  2058. } else {
  2059. $page_template = false;
  2060. }
  2061. if ( $menu_order || $page_template ) {
  2062. ?>
  2063. <script type="text/javascript">addLoadEvent(function () { <?php
  2064. if($menu_order){ ?>
  2065. jQuery('#menu_order').val(<?php echo $menu_order ?>);
  2066. <?php }
  2067. if($page_template && 'default' != $page_template){ ?>
  2068. jQuery('#page_template').val('<?php echo $page_template ?>');
  2069. <?php }
  2070. ?>
  2071. });</script><?php
  2072. }
  2073. }
  2074. } elseif ( 'edit-comments.php' == $pagenow || 'index.php' == $pagenow || 'post.php' == $pagenow ) {
  2075. wp_enqueue_script( 'sitepress-' . $page_basename, ICL_PLUGIN_URL . '/res/js/comments-translation.js', array(), ICL_SITEPRESS_VERSION );
  2076. }
  2077. // sync post dates
  2078. if ( icl_is_post_edit() ) {
  2079. // @since 3.1.5
  2080. // Enqueing 'wp-jquery-ui-dialog', just in case it doesn't get automatically enqueued
  2081. wp_enqueue_style( 'wp-jquery-ui-dialog' );
  2082. wp_enqueue_style( 'sitepress-post-edit', ICL_PLUGIN_URL . '/res/css/post-edit.css', array( ), ICL_SITEPRESS_VERSION );
  2083. wp_enqueue_script( 'sitepress-post-edit', ICL_PLUGIN_URL . '/res/js/post-edit.js', array( 'jquery-ui-dialog', 'jquery-ui-autocomplete', 'autosave' ), ICL_SITEPRESS_VERSION );
  2084. if ( $this->settings[ 'sync_post_date' ] ) {
  2085. $post_type = isset( $_GET[ 'post_type' ] ) ? $_GET[ 'post_type' ] : 'post';
  2086. if ( isset( $_GET[ 'trid' ] ) ) {
  2087. $trid = intval( $_GET[ 'trid' ] );
  2088. } else {
  2089. $post_id = @intval( $_GET[ 'post' ] );
  2090. $trid = $this->get_element_trid( $post_id, 'post_' . $post_type );
  2091. }
  2092. $translations = $this->get_element_translations( $trid, 'post_' . $post_type );
  2093. if ( !empty( $translations ) && isset( $translations[ $current_language ] ) && !$translations[ $current_language ]->original ) {
  2094. $source_lang = isset( $_GET[ 'source_lang' ] ) ? $_GET[ 'source_lang' ] : $default_language;
  2095. if ( isset( $translations[ $source_lang ] ) ) {
  2096. $original_date = $wpdb->get_var( $wpdb->prepare( "SELECT post_date FROM {$wpdb->posts} WHERE ID=%d", $translations[ $source_lang ]->element_id ) );
  2097. $exp = explode( ' ', $original_date );
  2098. list( $aa, $mm, $jj ) = explode( '-', $exp[ 0 ] );
  2099. list( $hh, $mn, $ss ) = explode( ':', $exp[ 1 ] );
  2100. ?>
  2101. <script type="text/javascript">
  2102. addLoadEvent(
  2103. function () {
  2104. jQuery('#aa').val('<?php echo $aa ?>').attr('readonly', 'readonly');
  2105. jQuery('#mm').val('<?php echo $mm ?>').attr('readonly', 'readonly');
  2106. jQuery('#jj').val('<?php echo $jj ?>').attr('readonly', 'readonly');
  2107. jQuery('#hh').val('<?php echo $hh ?>').attr('readonly', 'readonly');
  2108. jQuery('#mn').val('<?php echo $mn ?>').attr('readonly', 'readonly');
  2109. jQuery('#ss').val('<?php echo $ss ?>').attr('readonly', 'readonly');
  2110. var timestamp = jQuery('#timestamp');
  2111. timestamp.find('b').html('<?php esc_html_e('copy from original', 'sitepress') ?>');
  2112. timestamp.next().html('<?php esc_html_e('show', 'sitepress') ?>');
  2113. });
  2114. </script>
  2115. <?php
  2116. }
  2117. }
  2118. }
  2119. }
  2120. if ( 'post-new.php' == $pagenow && isset( $_GET[ 'trid' ] ) && $this->settings[ 'sync_post_format' ] && function_exists( 'get_post_format' ) ) {
  2121. $format = get_post_format( $wpdb->get_var( $wpdb->prepare( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d and language_code=%s", $_GET[ 'trid' ], $_GET[ 'source_lang' ] ) ) );
  2122. ?>
  2123. <script type="text/javascript">
  2124. addLoadEvent(function () {
  2125. jQuery('#post-format-' + '<?php echo $format ?>').attr('checked', 'checked');
  2126. });
  2127. </script><?php
  2128. }
  2129. if ( is_admin() ) {
  2130. wp_enqueue_script( 'thickbox' );
  2131. wp_enqueue_script( 'theme-preview' );
  2132. wp_enqueue_script( 'sitepress-icl_reminders', ICL_PLUGIN_URL . '/res/js/icl_reminders.js', array(), ICL_SITEPRESS_VERSION );
  2133. }
  2134. //if('content-translation' == $page_basename) {
  2135. // wp_enqueue_script('icl-sidebar-scripts', ICL_PLUGIN_URL . '/res/js/icl_sidebar.js', array(), ICL_SITEPRESS_VERSION);
  2136. //}
  2137. if ( 'languages' == $page_basename || 'string-translation' == $page_basename ) {
  2138. wp_enqueue_script( 'colorpicker' );
  2139. wp_enqueue_script( 'jquery-ui-sortable' );
  2140. }
  2141. }
  2142. function js_load()
  2143. {
  2144. if ( is_admin() && !defined( 'DOING_AJAX' ) ) {
  2145. if ( isset( $_GET[ 'page' ] ) ) {
  2146. $page = basename( $_GET[ 'page' ] );
  2147. $page_basename = str_replace( '.php', '', $page );
  2148. } else {
  2149. $page_basename = false;
  2150. }
  2151. wp_enqueue_script( 'sitepress-scripts', ICL_PLUGIN_URL . '/res/js/scripts.js', array( 'jquery' ), ICL_SITEPRESS_VERSION );
  2152. if ( isset( $page_basename ) && file_exists( ICL_PLUGIN_PATH . '/res/js/' . $page_basename . '.js' ) ) {
  2153. $dependencies = array();
  2154. switch ( $page_basename ) {
  2155. case 'languages':
  2156. $dependencies[ ] = 'colorpicker';
  2157. break;
  2158. }
  2159. wp_enqueue_script( 'sitepress-' . $page_basename, ICL_PLUGIN_URL . '/res/js/' . $page_basename . '.js', $dependencies, ICL_SITEPRESS_VERSION );
  2160. } else {
  2161. wp_enqueue_script( 'translate-taxonomy', ICL_PLUGIN_URL . '/res/js/taxonomy-translation.js', array( 'jquery' ), ICL_SITEPRESS_VERSION );
  2162. }
  2163. if ( !wp_style_is( 'toolset-font-awesome', 'registered' ) ) { // check if styles are already registered
  2164. wp_register_style( 'toolset-font-awesome', ICL_PLUGIN_URL . '/res/css/font-awesome.min.css', null, ICL_SITEPRESS_VERSION ); // register if not
  2165. }
  2166. wp_enqueue_style( 'toolset-font-awesome' ); // enqueue styles
  2167. }
  2168. }
  2169. function front_end_js()
  2170. {
  2171. if ( defined( 'ICL_DONT_LOAD_LANGUAGES_JS' ) && ICL_DONT_LOAD_LANGUAGES_JS ) {
  2172. return;
  2173. }
  2174. wp_register_script( 'sitepress', ICL_PLUGIN_URL . '/res/js/sitepress.js', false );
  2175. wp_enqueue_script( 'sitepress' );
  2176. $vars = array(
  2177. 'current_language' => $this->this_lang, 'icl_home' => $this->language_url(),
  2178. );
  2179. wp_localize_script( 'sitepress', 'icl_vars', $vars );
  2180. }
  2181. function js_scripts_categories()
  2182. {
  2183. wp_enqueue_script( 'sitepress-categories', ICL_PLUGIN_URL . '/res/js/categories.js', array(), ICL_SITEPRESS_VERSION );
  2184. }
  2185. function js_scripts_tags()
  2186. {
  2187. wp_enqueue_script( 'sitepress-tags', ICL_PLUGIN_URL . '/res/js/tags.js', array(), ICL_SITEPRESS_VERSION );
  2188. }
  2189. function rtl_fix()
  2190. {
  2191. global $wp_styles;
  2192. if ( !empty( $wp_styles ) && $this->is_rtl() ) {
  2193. $wp_styles->text_direction = 'rtl';
  2194. }
  2195. }
  2196. function css_setup()
  2197. {
  2198. if ( isset( $_GET[ 'page' ] ) ) {
  2199. $page = basename( $_GET[ 'page' ] );
  2200. $page_basename = str_replace( '.php', '', $page );
  2201. }
  2202. wp_enqueue_style( 'sitepress-style', ICL_PLUGIN_URL . '/res/css/style.css', array(), ICL_SITEPRESS_VERSION );
  2203. if ( isset( $page_basename ) && file_exists( ICL_PLUGIN_PATH . '/res/css/' . $page_basename . '.css' ) ) {
  2204. wp_enqueue_style( 'sitepress-' . $page_basename, ICL_PLUGIN_URL . '/res/css/' . $page_basename . '.css', array(), ICL_SITEPRESS_VERSION );
  2205. }
  2206. if ( is_admin() ) {
  2207. wp_enqueue_style( 'thickbox' );
  2208. wp_enqueue_style( 'translate-taxonomy', ICL_PLUGIN_URL . '/res/css/taxonomy-translation.css', array(), ICL_SITEPRESS_VERSION );
  2209. }
  2210. }
  2211. function transfer_icl_account( $create_account_and_transfer )
  2212. {
  2213. $user = $_POST[ 'user' ];
  2214. $user[ 'site_id' ] = $this->settings[ 'site_id' ];
  2215. $user[ 'accesskey' ] = $this->settings[ 'access_key' ];
  2216. $user[ 'create_account' ] = $create_account_and_transfer ? '1' : '0';
  2217. $icl_query = new ICanLocalizeQuery();
  2218. list( $success, $access_key ) = $icl_query->transfer_account( $user );
  2219. if ( $success ) {
  2220. $this->settings[ 'access_key' ] = $access_key;
  2221. // set the support data the same.
  2222. $this->settings[ 'support_access_key' ] = $access_key;
  2223. $this->save_settings();
  2224. return true;
  2225. } else {
  2226. $_POST[ 'icl_form_errors' ] = $access_key;
  2227. return false;
  2228. }
  2229. }
  2230. function process_forms()
  2231. {
  2232. global $wpdb;
  2233. require_once ICL_PLUGIN_PATH . '/lib/Snoopy.class.php';
  2234. require_once ICL_PLUGIN_PATH . '/lib/xml2array.php';
  2235. require_once ICL_PLUGIN_PATH . '/lib/icl_api.php';
  2236. if ( isset( $_POST[ 'icl_post_action' ] ) ) {
  2237. switch ( $_POST[ 'icl_post_action' ] ) {
  2238. case 'save_theme_localization':
  2239. $locales = array();
  2240. foreach ( $_POST as $k => $v ) {
  2241. if ( 0 !== strpos( $k, 'locale_file_name_' ) || !trim( $v ) ) {
  2242. continue;
  2243. }
  2244. $locales[ str_replace( 'locale_file_name_', '', $k ) ] = $v;
  2245. }
  2246. if ( !empty( $locales ) ) {
  2247. $this->set_locale_file_names( $locales );
  2248. }
  2249. break;
  2250. }
  2251. return;
  2252. }
  2253. $create_account = isset( $_POST[ 'icl_create_account_nonce' ] ) && $_POST[ 'icl_create_account_nonce' ] == wp_create_nonce( 'icl_create_account' );
  2254. $create_account_and_transfer = isset( $_POST[ 'icl_create_account_and_transfer_nonce' ] ) && $_POST[ 'icl_create_account_and_transfer_nonce' ] == wp_create_nonce( 'icl_create_account_and_transfer' );
  2255. $config_account = isset( $_POST[ 'icl_configure_account_nonce' ] ) && $_POST[ 'icl_configure_account_nonce' ] == wp_create_nonce( 'icl_configure_account' );
  2256. $create_support_account = isset( $_POST[ 'icl_create_support_account_nonce' ] ) && $_POST[ 'icl_create_support_account_nonce' ] == wp_create_nonce( 'icl_create_support_account' );
  2257. $config_support_account = isset( $_POST[ 'icl_configure_support_account_nonce' ] ) && $_POST[ 'icl_configure_support_account_nonce' ] == wp_create_nonce( 'icl_configure_support_account' );
  2258. $use_existing_account = isset( $_POST[ 'icl_use_account_nonce' ] ) && $_POST[ 'icl_use_account_nonce' ] == wp_create_nonce( 'icl_use_account' );
  2259. $transfer_to_account = isset( $_POST[ 'icl_transfer_account_nonce' ] ) && $_POST[ 'icl_transfer_account_nonce' ] == wp_create_nonce( 'icl_transfer_account' );
  2260. if ( $create_account || $config_account || $create_support_account || $config_support_account ) {
  2261. if ( isset( $_POST[ 'icl_content_trans_setup_back_2' ] ) ) {
  2262. // back button in wizard mode.
  2263. $this->settings[ 'content_translation_setup_wizard_step' ] = 2;
  2264. $this->save_settings();
  2265. } else {
  2266. $user = $_POST[ 'user' ];
  2267. $user[ 'create_account' ] = ( isset( $_POST[ 'icl_create_account_nonce' ] ) || isset( $_POST[ 'icl_create_support_account_nonce' ] ) ) ? 1 : 0;
  2268. $user[ 'platform_kind' ] = 2;
  2269. $user[ 'cms_kind' ] = 1;
  2270. $user[ 'blogid' ] = $wpdb->blogid ? $wpdb->blogid : 1;
  2271. $user[ 'url' ] = get_home_url();
  2272. $user[ 'title' ] = get_option( 'blogname' );
  2273. $user[ 'description' ] = $this->settings[ 'icl_site_description' ];
  2274. $user[ 'is_verified' ] = 1;
  2275. if ( $user[ 'create_account' ] && defined( 'ICL_AFFILIATE_ID' ) && defined( 'ICL_AFFILIATE_KEY' ) ) {
  2276. $user[ 'affiliate_id' ] = ICL_AFFILIATE_ID;
  2277. $user[ 'affiliate_key' ] = ICL_AFFILIATE_KEY;
  2278. }
  2279. $user[ 'interview_translators' ] = $this->settings[ 'interview_translators' ];
  2280. $user[ 'project_kind' ] = $this->settings[ 'website_kind' ];
  2281. /*
  2282. if(is_null($user['project_kind']) || $user['project_kind']==''){
  2283. $_POST['icl_form_errors'] = __('Please select the kind of website','sitepress');
  2284. return;
  2285. }
  2286. */
  2287. $user[ 'pickup_type' ] = intval( $this->settings[ 'translation_pickup_method' ] );
  2288. $notifications = 0;
  2289. if ( $this->settings[ 'icl_notify_complete' ] ) {
  2290. $notifications += 1;
  2291. }
  2292. if ( $this->settings[ 'alert_delay' ] ) {
  2293. $notifications += 2;
  2294. }
  2295. $user[ 'notifications' ] = $notifications;
  2296. // prepare language pairs
  2297. $pay_per_use = $this->settings[ 'translator_choice' ] == 1;
  2298. $language_pairs = $this->settings[ 'language_pairs' ];
  2299. $lang_pairs = array();
  2300. if ( isset( $language_pairs ) ) {
  2301. foreach ( $language_pairs as $k => $v ) {
  2302. $english_fr = $wpdb->get_var( "SELECT english_name FROM {$wpdb->prefix}icl_languages WHERE code='{$k}' " );
  2303. $pay_per_use_increment = 0;
  2304. foreach ( $v as $k => $v ) {
  2305. $pay_per_use_increment++;
  2306. $english_to = $wpdb->get_var( "SELECT english_name FROM {$wpdb->prefix}icl_languages WHERE code='{$k}' " );
  2307. $lang_pairs[ 'from_language' . $pay_per_use_increment ] = ICL_Pro_Translation::server_languages_map( $english_fr );
  2308. $lang_pairs[ 'to_language' . $pay_per_use_increment ] = ICL_Pro_Translation::server_languages_map( $english_to );
  2309. if ( $pay_per_use ) {
  2310. $lang_pairs[ 'pay_per_use' . $pay_per_use_increment ] = 1;
  2311. }
  2312. }
  2313. }
  2314. }
  2315. $icl_query = new ICanLocalizeQuery();
  2316. list( $site_id, $access_key ) = $icl_query->createAccount( array_merge( $user, $lang_pairs ) );
  2317. if ( !$site_id ) {
  2318. $user[ 'pickup_type' ] = ICL_PRO_TRANSLATION_PICKUP_POLLING;
  2319. list( $site_id, $access_key ) = $icl_query->createAccount( array_merge( $user, $lang_pairs ) );
  2320. }
  2321. if ( !$site_id ) {
  2322. if ( $access_key ) {
  2323. $_POST[ 'icl_form_errors' ] = $access_key;
  2324. } else {
  2325. $_POST[ 'icl_form_errors' ] = __( 'An unknown error has occurred when communicating with the ICanLocalize server. Please try again.', 'sitepress' );
  2326. // We will force the next try to be http.
  2327. update_option( '_force_mp_post_http', 1 );
  2328. }
  2329. } else {
  2330. if ( $create_account || $config_account ) {
  2331. $iclsettings[ 'site_id' ] = $site_id;
  2332. $iclsettings[ 'access_key' ] = $access_key;
  2333. $iclsettings[ 'icl_account_email' ] = $user[ 'email' ];
  2334. // set the support data the same.
  2335. $iclsettings[ 'support_site_id' ] = $site_id;
  2336. $iclsettings[ 'support_access_key' ] = $access_key;
  2337. $iclsettings[ 'support_icl_account_email' ] = $user[ 'email' ];
  2338. } else {
  2339. $iclsettings[ 'support_site_id' ] = $site_id;
  2340. $iclsettings[ 'support_access_key' ] = $access_key;
  2341. $iclsettings[ 'support_icl_account_email' ] = $user[ 'email' ];
  2342. }
  2343. if ( isset( $user[ 'pickup_type' ] ) && $user[ 'pickup_type' ] == ICL_PRO_TRANSLATION_PICKUP_POLLING ) {
  2344. $iclsettings[ 'translation_pickup_method' ] = ICL_PRO_TRANSLATION_PICKUP_POLLING;
  2345. }
  2346. $this->save_settings( $iclsettings );
  2347. if ( $user[ 'create_account' ] == 1 ) {
  2348. $_POST[ 'icl_form_success' ] = __( 'A project on ICanLocalize has been created.', 'sitepress' ) . '<br />';
  2349. } else {
  2350. $_POST[ 'icl_form_success' ] = __( 'Project added', 'sitepress' );
  2351. }
  2352. $this->get_icl_translator_status( $iclsettings );
  2353. $this->save_settings( $iclsettings );
  2354. }
  2355. if ( !$create_support_account && !$config_support_account && intval( $site_id ) > 0 && $access_key && $this->settings[ 'content_translation_setup_complete' ] == 0 && $this->settings[ 'content_translation_setup_wizard_step' ] == 3 && !isset( $_POST[ 'icl_form_errors' ] ) ) {
  2356. // we are running the wizard, so we can finish it now.
  2357. $this->settings[ 'content_translation_setup_complete' ] = 1;
  2358. $this->settings[ 'content_translation_setup_wizard_step' ] = 0;
  2359. $this->save_settings();
  2360. }
  2361. }
  2362. } elseif ( $use_existing_account || $transfer_to_account || $create_account_and_transfer ) {
  2363. if ( isset( $_POST[ 'icl_content_trans_setup_back_2' ] ) ) {
  2364. // back button in wizard mode.
  2365. $this->settings[ 'content_translation_setup_wizard_step' ] = 2;
  2366. $this->save_settings();
  2367. } else {
  2368. if ( $transfer_to_account ) {
  2369. $_POST[ 'user' ][ 'email' ] = $_POST[ 'user' ][ 'email2' ];
  2370. }
  2371. // we will be using the support account for the icl_account
  2372. $this->settings[ 'site_id' ] = $this->settings[ 'support_site_id' ];
  2373. $this->settings[ 'access_key' ] = $this->settings[ 'support_access_key' ];
  2374. $this->settings[ 'icl_account_email' ] = $this->settings[ 'support_icl_account_email' ];
  2375. $this->save_settings();
  2376. if ( $transfer_to_account || $create_account_and_transfer ) {
  2377. if ( !$this->transfer_icl_account( $create_account_and_transfer ) ) {
  2378. return;
  2379. }
  2380. }
  2381. // we are running the wizard, so we can finish it now.
  2382. $this->settings[ 'content_translation_setup_complete' ] = 1;
  2383. $this->settings[ 'content_translation_setup_wizard_step' ] = 0;
  2384. $this->save_settings();
  2385. $iclsettings[ 'site_id' ] = $this->settings[ 'site_id' ];
  2386. $iclsettings[ 'access_key' ] = $this->settings[ 'access_key' ];
  2387. $this->get_icl_translator_status( $iclsettings );
  2388. $this->save_settings( $iclsettings );
  2389. }
  2390. } elseif ( isset( $_POST[ 'icl_initial_languagenonce' ] ) && $_POST[ 'icl_initial_languagenonce' ] == wp_create_nonce( 'icl_initial_language' ) ) {
  2391. $this->prepopulate_translations( $_POST[ 'icl_initial_language_code' ] );
  2392. $wpdb->update( $wpdb->prefix . 'icl_languages', array( 'active' => '1' ), array( 'code' => $_POST[ 'icl_initial_language_code' ] ) );
  2393. $blog_default_cat = get_option( 'default_category' );
  2394. $blog_default_cat_tax_id = $wpdb->get_var( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id='{$blog_default_cat}' AND taxonomy='category'" );
  2395. if ( isset( $_POST[ 'save_one_language' ] ) ) {
  2396. $this->settings[ 'setup_wizard_step' ] = 0;
  2397. $this->settings[ 'setup_complete' ] = 1;
  2398. } else {
  2399. $this->settings[ 'setup_wizard_step' ] = 2;
  2400. }
  2401. $this->settings[ 'default_categories' ] = array( $_POST[ 'icl_initial_language_code' ] => $blog_default_cat_tax_id );
  2402. $this->settings[ 'existing_content_language_verified' ] = 1;
  2403. $this->settings[ 'default_language' ] = $_POST[ 'icl_initial_language_code' ];
  2404. $this->settings[ 'admin_default_language' ] = $this->admin_language = $_POST[ 'icl_initial_language_code' ];
  2405. // set the locale in the icl_locale_map (if it's not set)
  2406. $q = "SELECT code FROM {$wpdb->prefix}icl_locale_map WHERE code=%s";
  2407. $q_prepared = $wpdb->prepare($q, $_POST['icl_initial_language_code']);
  2408. if ( !$wpdb->get_var( $q_prepared ) ) {
  2409. $q = "SELECT default_locale FROM {$wpdb->prefix}icl_languages WHERE code=%s";
  2410. $q_prepared = $wpdb->prepare($q, $_POST['icl_initial_language_code']);
  2411. $default_locale = $wpdb->get_var( $q_prepared );
  2412. if ( $default_locale ) {
  2413. $wpdb->insert( $wpdb->prefix . 'icl_locale_map', array( 'code' => $_POST[ 'icl_initial_language_code' ], 'locale' => $default_locale ) );
  2414. }
  2415. }
  2416. $this->save_settings();
  2417. global $sitepress_settings;
  2418. $sitepress_settings = $this->settings;
  2419. $this->get_active_languages( true ); //refresh active languages list
  2420. do_action( 'icl_initial_language_set' );
  2421. } elseif ( isset( $_POST[ 'icl_language_pairs_formnounce' ] ) && $_POST[ 'icl_language_pairs_formnounce' ] == wp_create_nonce( 'icl_language_pairs_form' ) ) {
  2422. $this->save_language_pairs();
  2423. $this->settings[ 'content_translation_languages_setup' ] = 1;
  2424. // Move onto the site description page
  2425. $this->settings[ 'content_translation_setup_wizard_step' ] = 2;
  2426. $this->settings[ 'website_kind' ] = 2;
  2427. $this->settings[ 'interview_translators' ] = 1;
  2428. $this->save_settings();
  2429. } elseif ( isset( $_POST[ 'icl_site_description_wizardnounce' ] ) && $_POST[ 'icl_site_description_wizardnounce' ] == wp_create_nonce( 'icl_site_description_wizard' ) ) {
  2430. if ( isset( $_POST[ 'icl_content_trans_setup_back_2' ] ) ) {
  2431. // back button.
  2432. $this->settings[ 'content_translation_languages_setup' ] = 0;
  2433. $this->settings[ 'content_translation_setup_wizard_step' ] = 1;
  2434. $this->save_settings();
  2435. } elseif ( isset( $_POST[ 'icl_content_trans_setup_next_2' ] ) || isset( $_POST[ 'icl_content_trans_setup_next_2_enter' ] ) ) {
  2436. // next button.
  2437. $description = $_POST[ 'icl_description' ];
  2438. if ( $description == "" ) {
  2439. $_POST[ 'icl_form_errors' ] = __( 'Please provide a short description of the website so that translators know what background is required from them.', 'sitepress' );
  2440. } else {
  2441. $this->settings[ 'icl_site_description' ] = $description;
  2442. $this->settings[ 'content_translation_setup_wizard_step' ] = 3;
  2443. $this->save_settings();
  2444. }
  2445. }
  2446. }
  2447. }
  2448. function prepopulate_translations( $lang )
  2449. {
  2450. global $wpdb;
  2451. if ( !empty( $this->settings[ 'existing_content_language_verified' ] ) ) {
  2452. return;
  2453. }
  2454. $this->icl_translations_cache->clear();
  2455. // case of icl_sitepress_settings accidentally lost
  2456. // if there's at least one translation do not initialize the languages for elements
  2457. $one_translation = $wpdb->get_var( $wpdb->prepare( "SELECT translation_id FROM {$wpdb->prefix}icl_translations WHERE language_code<>%s", $lang ) );
  2458. if ( $one_translation ) {
  2459. return;
  2460. }
  2461. $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}icl_translations" );
  2462. $wpdb->query( $wpdb->prepare("
  2463. INSERT INTO {$wpdb->prefix}icl_translations(element_type, element_id, trid, language_code, source_language_code)
  2464. SELECT CONCAT('post_',post_type), ID, ID, %s, NULL FROM {$wpdb->posts} WHERE post_status IN ('draft', 'publish','schedule','future','private', 'pending')
  2465. ", $lang) );
  2466. $maxtrid = 1 + $wpdb->get_var( "SELECT MAX(trid) FROM {$wpdb->prefix}icl_translations" );
  2467. global $wp_taxonomies;
  2468. $taxonomies = array_keys( (array)$wp_taxonomies );
  2469. foreach ( $taxonomies as $tax ) {
  2470. $wpdb->query( $wpdb->prepare("
  2471. INSERT INTO {$wpdb->prefix}icl_translations(element_type, element_id, trid, language_code, source_language_code)
  2472. SELECT 'tax_" . $tax . "', term_taxonomy_id, {$maxtrid}+term_taxonomy_id, %s, NULL FROM {$wpdb->term_taxonomy} WHERE taxonomy = %s
  2473. ", $lang, $tax) );
  2474. $maxtrid = 1 + $wpdb->get_var( "SELECT MAX(trid) FROM {$wpdb->prefix}icl_translations" );
  2475. }
  2476. $wpdb->query( $wpdb->prepare( "
  2477. INSERT INTO {$wpdb->prefix}icl_translations(element_type, element_id, trid, language_code, source_language_code)
  2478. SELECT 'comment', comment_ID, {$maxtrid}+comment_ID, %s, NULL FROM {$wpdb->comments}
  2479. ", $lang) );
  2480. }
  2481. function post_edit_language_options()
  2482. {
  2483. global $post, $iclTranslationManagement, $post_new_file, $post_type_object;
  2484. if(!isset($post)) return;
  2485. if ( !function_exists( 'post_type_supports' ) || post_type_supports( $post->post_type, 'editor' ) ) {
  2486. add_action( 'icl_post_languages_options_after', array( $this, 'copy_from_original' ) );
  2487. }
  2488. if ( current_user_can( 'manage_options' ) ) {
  2489. add_meta_box( 'icl_div_config', __( 'Multilingual Content Setup', 'sitepress' ), array( $this, 'meta_box_config' ), $post->post_type, 'normal', 'low' );
  2490. }
  2491. if ( isset( $_POST[ 'icl_action' ] ) && $_POST[ 'icl_action' ] == 'icl_mcs_inline' ) {
  2492. if ( !in_array( $_POST[ 'custom_post' ], array( 'post', 'page' ) ) ) {
  2493. $iclsettings[ 'custom_posts_sync_option' ][ $_POST[ 'custom_post' ] ] = @intval( $_POST[ 'translate' ] );
  2494. if ( @intval( $_POST[ 'translate' ] ) ) {
  2495. $this->verify_post_translations( $_POST[ 'custom_post' ] );
  2496. }
  2497. }
  2498. if ( !empty( $_POST[ 'custom_taxs_off' ] ) ) {
  2499. foreach ( $_POST[ 'custom_taxs_off' ] as $off ) {
  2500. $iclsettings[ 'taxonomies_sync_option' ][ $off ] = 0;
  2501. }
  2502. }
  2503. if ( !empty( $_POST[ 'custom_taxs_on' ] ) ) {
  2504. foreach ( $_POST[ 'custom_taxs_on' ] as $on ) {
  2505. $iclsettings[ 'taxonomies_sync_option' ][ $on ] = 1;
  2506. $this->verify_taxonomy_translations( $on );
  2507. }
  2508. }
  2509. if ( !empty( $_POST[ 'cfnames' ] ) ) {
  2510. foreach ( $_POST[ 'cfnames' ] as $k => $v ) {
  2511. $custom_field_name = base64_decode( $v );
  2512. $iclTranslationManagement->settings[ 'custom_fields_translation' ][ $custom_field_name ] = @intval( $_POST[ 'cfvals' ][ $k ] );
  2513. $iclTranslationManagement->save_settings();
  2514. // sync the custom fields for the current post
  2515. if ( 1 == @intval( $_POST[ 'cfvals' ][ $k ] ) ) {
  2516. $trid = $this->get_element_trid( $_POST[ 'post_id' ], 'post_' . $_POST[ 'custom_post' ] );
  2517. $translations = $this->get_element_translations( $trid, 'post_' . $_POST[ 'custom_post' ] );
  2518. // determine original post id
  2519. $original_post_id = false;
  2520. foreach ( $translations as $t ) {
  2521. if ( $t->original ) {
  2522. $original_post_id = $t->element_id;
  2523. break;
  2524. }
  2525. }
  2526. // get a list of $custom_field_name values that the original document has
  2527. $custom_fields_copy = get_post_meta( $original_post_id, $custom_field_name );
  2528. foreach ( $translations as $t ) {
  2529. if ( $t->original ) {
  2530. continue;
  2531. }
  2532. // if none, then attempt to delete any that the tranlations would have
  2533. if ( empty( $custom_fields_copy ) ) {
  2534. delete_post_meta( $t->element_id, $custom_field_name );
  2535. } else {
  2536. // get a list of $custom_field_name values that the translated document has
  2537. $translation_cfs = get_post_meta( $t->element_id, $custom_field_name );
  2538. // see what elements have been deleted in the original document
  2539. $deleted_fields = $translation_cfs;
  2540. foreach ( $custom_fields_copy as $cfc ) {
  2541. $tc_key = array_search( $cfc, $translation_cfs );
  2542. if ( $tc_key !== false ) {
  2543. unset( $deleted_fields[ $tc_key ] );
  2544. }
  2545. }
  2546. if ( !empty( $deleted_fields ) ) {
  2547. foreach ( $deleted_fields as $meta_value ) {
  2548. delete_post_meta( $t->element_id, $custom_field_name, $meta_value );
  2549. }
  2550. }
  2551. // update each custom field in the translated document
  2552. foreach ( $custom_fields_copy as $meta_value ) {
  2553. if ( !in_array( $meta_value, $translation_cfs ) ) {
  2554. // if it doesn't exist, add
  2555. add_post_meta( $t->element_id, $custom_field_name, $meta_value );
  2556. }
  2557. }
  2558. }
  2559. }
  2560. }
  2561. }
  2562. }
  2563. if ( !empty( $iclsettings ) ) {
  2564. $this->save_settings( $iclsettings );
  2565. }
  2566. }
  2567. $post_types = array_keys( $this->get_translatable_documents() );
  2568. if ( in_array( $post->post_type, $post_types ) ) {
  2569. add_meta_box( 'icl_div', __( 'Language', 'sitepress' ), array( $this, 'meta_box' ), $post->post_type, 'side', 'high' );
  2570. }
  2571. //Fix the "Add new" button adding the language argument, so to create new content in the same language
  2572. if(isset($post_new_file) && isset($post_type_object) && $this->is_translated_post_type($post_type_object->name)) {
  2573. $post_language = $this->get_language_for_element($post->ID, 'post_' . $post_type_object->name);
  2574. $post_new_file = add_query_arg(array('lang' => $post_language), $post_new_file);
  2575. }
  2576. }
  2577. /**
  2578. * @param int $el_id
  2579. * @param string $el_type
  2580. * @param int $trid
  2581. * @param string $language_code
  2582. * @param null|string $src_language_code
  2583. * @param bool $check_duplicates
  2584. *
  2585. * @return bool|int|null|string
  2586. */
  2587. function set_element_language_details( $el_id, $el_type = 'post_post', $trid, $language_code, $src_language_code = null, $check_duplicates = true )
  2588. {
  2589. global $wpdb;
  2590. // Do not set language for the front page, if defined
  2591. $is_root_page = isset( $sitepress_settings[ 'urls' ][ 'root_page' ] ) && $sitepress_settings[ 'urls' ][ 'root_page' ] == $el_id;
  2592. if ( $is_root_page ) {
  2593. $trid = $this->get_element_trid($el_id, $el_type);
  2594. if($trid) {
  2595. $this->delete_element_translation($trid, $el_type);
  2596. }
  2597. return false;
  2598. }
  2599. // special case for posts and taxonomies
  2600. // check if a different record exists for the same ID
  2601. // if it exists don't save the new element and get out
  2602. if ( $check_duplicates && $el_id ) {
  2603. $exp = explode( '_', $el_type );
  2604. $_type = $exp[ 0 ];
  2605. if ( in_array( $_type, array( 'post', 'tax' ) ) ) {
  2606. $_el_exists = $wpdb->get_var( "
  2607. SELECT translation_id FROM {$wpdb->prefix}icl_translations
  2608. WHERE element_id={$el_id} AND element_type <> '{$el_type}' AND element_type LIKE '{$_type}\\_%'" );
  2609. if ( $_el_exists ) {
  2610. trigger_error( 'Element ID already exists with a different type', E_USER_NOTICE );
  2611. return false;
  2612. }
  2613. }
  2614. }
  2615. if ( $trid ) { // it's a translation of an existing element
  2616. // check whether we have an orphan translation - the same trid and language but a different element id
  2617. $translation_id = $wpdb->get_var( "
  2618. SELECT translation_id FROM {$wpdb->prefix}icl_translations
  2619. WHERE trid = '{$trid}'
  2620. AND language_code = '{$language_code}'
  2621. AND element_id <> '{$el_id}'
  2622. " );
  2623. if ( $translation_id ) {
  2624. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}icl_translations WHERE translation_id=%d", $translation_id ) );
  2625. $this->icl_translations_cache->clear();
  2626. }
  2627. if ( !is_null( $el_id ) && $translation_id = $wpdb->get_var( "SELECT translation_id FROM {$wpdb->prefix}icl_translations
  2628. WHERE element_type='{$el_type}' AND element_id='{$el_id}' AND trid='{$trid}' AND element_id IS NOT NULL" )
  2629. ) {
  2630. //case of language change
  2631. $wpdb->update( $wpdb->prefix . 'icl_translations', array( 'language_code' => $language_code ), array( 'translation_id' => $translation_id ) );
  2632. } elseif ( !is_null( $el_id ) && $translation_id = $wpdb->get_var( "SELECT translation_id FROM {$wpdb->prefix}icl_translations
  2633. WHERE element_type='{$el_type}' AND element_id='{$el_id}' AND element_id IS NOT NULL " )
  2634. ) {
  2635. //case of changing the "translation of"
  2636. if ( empty( $src_language_code ) ) {
  2637. $src_language_code = $wpdb->get_var( "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE trid={$trid} AND source_language_code IS NULL" );
  2638. }
  2639. $wpdb->update( $wpdb->prefix . 'icl_translations', array( 'trid' => $trid, 'language_code' => $language_code, 'source_language_code' => $src_language_code ), array( 'element_type' => $el_type, 'element_id' => $el_id ) );
  2640. $this->icl_translations_cache->clear();
  2641. } elseif ( $translation_id = $wpdb->get_var( $wpdb->prepare( "
  2642. SELECT translation_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND language_code='%s' AND element_id IS NULL", $trid, $language_code ) )
  2643. ) {
  2644. $wpdb->update( $wpdb->prefix . 'icl_translations', array( 'element_id' => $el_id ), array( 'translation_id' => $translation_id ) );
  2645. } else {
  2646. //get source
  2647. if ( empty( $src_language_code ) ) {
  2648. $src_language_code = $wpdb->get_var( "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE trid={$trid} AND source_language_code IS NULL" );
  2649. }
  2650. // case of adding a new language
  2651. $new = array(
  2652. 'trid' => $trid, 'element_type' => $el_type, 'language_code' => $language_code, 'source_language_code' => $src_language_code
  2653. );
  2654. if ( $el_id ) {
  2655. $new[ 'element_id' ] = $el_id;
  2656. }
  2657. $wpdb->insert( $wpdb->prefix . 'icl_translations', $new );
  2658. $translation_id = $wpdb->insert_id;
  2659. $this->icl_translations_cache->clear();
  2660. }
  2661. } else { // it's a new element or we are removing it from a trid
  2662. if ( $translation_id = $wpdb->get_var( "
  2663. SELECT translation_id
  2664. FROM {$wpdb->prefix}icl_translations WHERE element_type='{$el_type}' AND element_id='{$el_id}' AND element_id IS NOT NULL" )
  2665. ) {
  2666. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}icl_translations WHERE translation_id=%d", $translation_id ) );
  2667. $this->icl_translations_cache->clear();
  2668. }
  2669. $trid = 1 + $wpdb->get_var( "SELECT MAX(trid) FROM {$wpdb->prefix}icl_translations" );
  2670. $new = array(
  2671. 'trid' => $trid, 'element_type' => $el_type, 'language_code' => $language_code
  2672. );
  2673. if ( $el_id ) {
  2674. $new[ 'element_id' ] = $el_id;
  2675. }
  2676. $wpdb->insert( $wpdb->prefix . 'icl_translations', $new );
  2677. $translation_id = $wpdb->insert_id;
  2678. }
  2679. return $translation_id;
  2680. }
  2681. function delete_element_translation( $trid, $el_type, $language_code = false )
  2682. {
  2683. global $wpdb;
  2684. $trid = intval( $trid );
  2685. $el_type = esc_sql( $el_type );
  2686. $where = '';
  2687. $delete_args = array($trid, $el_type);
  2688. if ( $language_code ) {
  2689. $where .= " AND language_code=%s";
  2690. $delete_args[] = $language_code;
  2691. }
  2692. $delete_sql = "DELETE FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND element_type=%s" . $where;
  2693. $delete_sql_prepared = $wpdb->prepare($delete_sql, $delete_args);
  2694. $wpdb->query( $delete_sql_prepared );
  2695. $this->icl_translations_cache->clear();
  2696. }
  2697. function get_element_language_details( $el_id, $el_type = 'post_post' )
  2698. {
  2699. $cache_key = $el_id . ':' . $el_type;
  2700. $cache_group = 'element_language_details';
  2701. $cached_details = wp_cache_get($cache_key, $cache_group);
  2702. if($cached_details) return $cached_details;
  2703. global $wpdb;
  2704. static $pre_load_done = false;
  2705. if ( !$pre_load_done && !ICL_DISABLE_CACHE ) {
  2706. // search previous queries for a group of posts
  2707. foreach ( $this->queries as $query ) {
  2708. $pos = strstr( $query, 'post_id IN (' );
  2709. if ( $pos !== false ) {
  2710. $group = substr( $pos, 10 );
  2711. $group = substr( $group, 0, strpos( $group, ')' ) + 1 );
  2712. $query = "SELECT element_id, trid, language_code, source_language_code
  2713. FROM {$wpdb->prefix}icl_translations
  2714. WHERE element_id IN {$group} AND element_type=%s";
  2715. $query_prepared = $wpdb->prepare($query, $el_type );
  2716. $ret = $wpdb->get_results( $query_prepared );
  2717. foreach ( $ret as $details ) {
  2718. if ( isset( $this->icl_translations_cache ) ) {
  2719. $this->icl_translations_cache->set( $details->element_id . $el_type, $details );
  2720. }
  2721. }
  2722. // get the taxonomy for the posts for later use
  2723. // categories first
  2724. $query = "SELECT DISTINCT(tr.term_taxonomy_id), tt.term_id, tt.taxonomy, icl.trid, icl.language_code, icl.source_language_code
  2725. FROM {$wpdb->prefix}term_relationships as tr
  2726. LEFT JOIN {$wpdb->prefix}term_taxonomy AS tt
  2727. ON tr.term_taxonomy_id = tt.term_taxonomy_id
  2728. LEFT JOIN {$wpdb->prefix}icl_translations as icl ON tr.term_taxonomy_id = icl.element_id
  2729. WHERE tr.object_id IN {$group}
  2730. AND (icl.element_type='tax_category' and tt.taxonomy='category')
  2731. ";
  2732. $query .= "UNION
  2733. ";
  2734. $query .= "SELECT DISTINCT(tr.term_taxonomy_id), tt.term_id, tt.taxonomy, icl.trid, icl.language_code, icl.source_language_code
  2735. FROM {$wpdb->prefix}term_relationships as tr
  2736. LEFT JOIN {$wpdb->prefix}term_taxonomy AS tt
  2737. ON tr.term_taxonomy_id = tt.term_taxonomy_id
  2738. LEFT JOIN {$wpdb->prefix}icl_translations as icl ON tr.term_taxonomy_id = icl.element_id
  2739. WHERE tr.object_id IN {$group}
  2740. AND (icl.element_type='tax_post_tag' and tt.taxonomy='post_tag')";
  2741. global $wp_taxonomies;
  2742. $custom_taxonomies = array_diff( array_keys( $wp_taxonomies ), array( 'post_tag', 'category', 'link_category' ) );
  2743. if ( !empty( $custom_taxonomies ) ) {
  2744. foreach ( $custom_taxonomies as $tax ) {
  2745. $query .= " UNION
  2746. SELECT DISTINCT(tr.term_taxonomy_id), tt.term_id, tt.taxonomy, icl.trid, icl.language_code, icl.source_language_code
  2747. FROM {$wpdb->prefix}term_relationships as tr
  2748. LEFT JOIN {$wpdb->prefix}term_taxonomy AS tt
  2749. ON tr.term_taxonomy_id = tt.term_taxonomy_id
  2750. LEFT JOIN {$wpdb->prefix}icl_translations as icl ON tr.term_taxonomy_id = icl.element_id
  2751. WHERE tr.object_id IN {$group}
  2752. AND (icl.element_type='tax_{$tax}' and tt.taxonomy='{$tax}')";
  2753. }
  2754. }
  2755. $ret = $wpdb->get_results( $query );
  2756. foreach ( $ret as $details ) {
  2757. // save language details
  2758. $lang_details = new stdClass();
  2759. $lang_details->trid = $details->trid;
  2760. $lang_details->language_code = $details->language_code;
  2761. $lang_details->source_language_code = $details->source_language_code;
  2762. if ( isset( $this->icl_translations_cache ) ) {
  2763. $this->icl_translations_cache->set( $details->term_taxonomy_id . 'tax_' . $details->taxonomy, $lang_details );
  2764. // save the term taxonomy
  2765. $this->icl_term_taxonomy_cache->set( 'category_' . $details->term_id, $details->term_taxonomy_id );
  2766. }
  2767. }
  2768. break;
  2769. }
  2770. }
  2771. $pre_load_done = true;
  2772. }
  2773. if ( isset( $this->icl_translations_cache ) && $this->icl_translations_cache->has_key( $el_id . $el_type ) ) {
  2774. return $this->icl_translations_cache->get( $el_id . $el_type );
  2775. }
  2776. $details_prepared_sql = $wpdb->prepare( "
  2777. SELECT trid, language_code, source_language_code
  2778. FROM {$wpdb->prefix}icl_translations
  2779. WHERE element_id=%d AND element_type=%s", array( $el_id, $el_type ) );
  2780. $details = $wpdb->get_row( $details_prepared_sql );
  2781. if ( isset( $this->icl_translations_cache ) ) {
  2782. $this->icl_translations_cache->set( $el_id . $el_type, $details );
  2783. }
  2784. wp_cache_add($cache_key, $details, $cache_group);
  2785. return $details;
  2786. }
  2787. //if set option "When deleting a taxonomy (category, tag or custom), delete translations as well"
  2788. //when editing post and deleting tag or category or custom taxonomy in original language then delete this taxonomy in other languages
  2789. function deleted_term_relationships( $object_id, $delete_terms )
  2790. {
  2791. global $wpdb;
  2792. if ( $this->settings[ 'sync_post_taxonomies' ] ) {
  2793. $post = get_post( $object_id );
  2794. $trid = $this->get_element_trid( $object_id, 'post_' . $post->post_type );
  2795. if ( $trid ) {
  2796. $translations = $this->get_element_translations( $trid, 'post_' . $post->post_type );
  2797. foreach ( $translations as $translation ) {
  2798. if ( $translation->original == 1 && $translation->element_id == $object_id ) {
  2799. $taxonomies = get_object_taxonomies( $post->post_type );
  2800. foreach ( $taxonomies as $taxonomy ) {
  2801. foreach ( $delete_terms as $delete_term ) {
  2802. $trid = $this->get_element_trid( $delete_term, 'tax_' . $taxonomy );
  2803. if ( $trid ) {
  2804. $tags = $this->get_element_translations( $trid, 'tax_' . $taxonomy );
  2805. foreach ( $tags as $tag ) {
  2806. if ( !$tag->original && isset( $translations[ $tag->language_code ] ) ) {
  2807. $translated_post = $translations[ $tag->language_code ];
  2808. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = $tag->element_id", $translated_post->element_id ) );
  2809. }
  2810. }
  2811. }
  2812. }
  2813. }
  2814. }
  2815. }
  2816. }
  2817. }
  2818. }
  2819. function save_post_actions( $pidd, $post )
  2820. {
  2821. global $wpdb;
  2822. wp_defer_term_counting( true );
  2823. list( $post_type, $post_status ) = $wpdb->get_row( "SELECT post_type, post_status FROM {$wpdb->posts} WHERE ID = " . $pidd, ARRAY_N );
  2824. // exceptions
  2825. if ( !$this->is_translated_post_type( $post_type ) || (isset( $post) && $post->post_status == "auto-draft" ) || isset( $_POST[ 'autosave' ] ) || isset( $_POST[ 'skip_sitepress_actions' ] ) || ( isset( $_POST[ 'post_ID' ] ) && $_POST[ 'post_ID' ] != $pidd ) || ( isset( $_POST[ 'post_type' ] ) && $_POST[ 'post_type' ] == 'revision' ) || $post_type == 'revision' || get_post_meta( $pidd, '_wp_trash_meta_status', true ) || ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'untrash' )) {
  2826. wp_defer_term_counting( false );
  2827. return;
  2828. }
  2829. $default_language = $this->get_default_language();
  2830. if(!isset($post) && $pidd) {
  2831. $post = get_post($pidd);
  2832. }
  2833. // exception for auto-drafts - setting the right language
  2834. // allow post arguments to be passed via wp_insert_post directly and not be expected on $_POST exclusively
  2835. $post_vars = (array)$_POST;
  2836. foreach ( (array)$post as $k => $v ) {
  2837. $post_vars[ $k ] = $v;
  2838. }
  2839. if ( !isset( $post_vars[ 'post_type' ] ) ) {
  2840. $post_vars[ 'post_type' ] = $post_type;
  2841. }
  2842. $element_type = 'post_' . $post_type;
  2843. $language_code = false;
  2844. if ( isset( $post_vars[ 'action' ] ) && $post_vars[ 'action' ] == 'post-quickpress-publish' ) {
  2845. $post_id = $pidd;
  2846. $language_code = $default_language;
  2847. } elseif ( isset( $_GET[ 'bulk_edit' ] ) ) {
  2848. $post_id = $pidd;
  2849. } else {
  2850. $post_id = isset( $post_vars[ 'post_ID' ] ) ? $post_vars[ 'post_ID' ] : $pidd; //latter case for XML-RPC publishing
  2851. if ( isset( $post_vars[ 'icl_post_language' ] ) ) {
  2852. $language_code = $post_vars[ 'icl_post_language' ];
  2853. } elseif ( isset( $_GET[ 'lang' ] ) ) {
  2854. $language_code = $_GET[ 'lang' ];
  2855. } elseif ( $_ldet = $this->get_element_language_details( $post_id, $element_type ) ) {
  2856. $language_code = $_ldet->language_code;
  2857. } elseif(isset($this->this_lang)) {
  2858. $language_code = $this->this_lang;
  2859. } else {
  2860. $language_code = $default_language; //latter case for XML-RPC publishing
  2861. }
  2862. }
  2863. $source_language = $default_language;
  2864. if ( isset( $post_vars[ 'action' ] ) && $post_vars[ 'action' ] == 'inline-save' || isset( $_GET[ 'bulk_edit' ] ) || isset( $_GET[ 'doing_wp_cron' ] ) || ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'untrash' ) ) {
  2865. $res = $this->get_element_language_details($post_id, 'post_' . $post->post_type);
  2866. if(!isset($res) || !$res) return;
  2867. $language_code = $res->language_code;
  2868. $trid = $res->trid;
  2869. } else {
  2870. if ( isset( $post_vars[ 'icl_trid' ] ) ) {
  2871. $trid = @intval( $post_vars[ 'icl_trid' ] );
  2872. } elseif ( isset( $_GET[ 'trid' ] ) ) {
  2873. $trid = @intval( $_GET[ 'trid' ] );
  2874. } else {
  2875. $trid = $this->get_element_trid( $post_id, 'post_' . $post->post_type );
  2876. }
  2877. // see if we have a "translation of" setting.
  2878. if ( isset( $post_vars[ 'icl_translation_of' ] ) ) {
  2879. if ( is_numeric( $post_vars[ 'icl_translation_of' ] ) ) {
  2880. $translation_of_data_prepared = $wpdb->prepare( "SELECT trid, language_code FROM {$wpdb->prefix}icl_translations WHERE element_id=%d AND element_type=%s", $post_vars[ 'icl_translation_of' ], 'post_' . $post->post_type );
  2881. list($trid, $source_language) = $wpdb->get_row( $translation_of_data_prepared, 'ARRAY_N' );
  2882. } else {
  2883. if ( empty( $post_vars[ 'icl_trid' ] ) ) {
  2884. $trid = null;
  2885. }
  2886. }
  2887. }
  2888. }
  2889. if ( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'inline-save' ) {
  2890. $trid = $this->get_element_trid( $post_id, 'post_' . $post_type );
  2891. } else if ( isset( $post_vars[ 'icl_translation_of' ] ) && $post_vars[ 'icl_translation_of' ] == 'none' ) {
  2892. $trid = null;
  2893. }
  2894. if (!$trid) {
  2895. $trid_from_referrer = $this->get_trid_from_referer();
  2896. if ($trid_from_referrer) {
  2897. $trid = $trid_from_referrer;
  2898. }
  2899. }
  2900. // set trid if front-end translation creating
  2901. $trid = apply_filters( 'wpml_save_post_trid_value', $trid, $post_status );
  2902. // set post language if front-end translation creating
  2903. $language_code = apply_filters( 'wpml_save_post_lang', $language_code );
  2904. $translation_id = $this->set_element_language_details( $post_id, $element_type, $trid, $language_code, $source_language );
  2905. //get trid of $translation_id
  2906. $translated_id_trid = $wpdb->get_var( $wpdb->prepare( "SELECT trid FROM {$wpdb->prefix}icl_translations WHERE translation_id=%d", array( $translation_id) ) );
  2907. //get all translations
  2908. $translated_element_id = $wpdb->get_col( $wpdb->prepare( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d", $translated_id_trid ) );
  2909. if ( !in_array( $post_type, array( 'post', 'page' ) ) && !$this->is_translated_post_type( $post_type ) ) {
  2910. wp_defer_term_counting( false );
  2911. return;
  2912. }
  2913. // synchronize the page order for translations
  2914. if ( $trid && $this->settings[ 'sync_page_ordering' ] && $translated_element_id && is_array($translated_element_id)) {
  2915. $menu_order = esc_sql( $post_vars[ 'menu_order' ] );
  2916. $wpdb->query( "UPDATE {$wpdb->posts} SET menu_order={$menu_order} WHERE ID IN (" . join( ',', $translated_element_id ) . ")" );
  2917. }
  2918. // synchronize the page parent for translations
  2919. if ( $trid && $this->settings[ 'sync_page_parent' ] ) {
  2920. $original_id = $this->get_original_element_id($post_id, 'post_' . $post_vars[ 'post_type' ]);
  2921. if ($original_id == $post_id) {
  2922. $translations = $this->get_element_translations( $trid, 'post_' . $post_vars[ 'post_type' ] );
  2923. foreach ( $translations as $target_lang => $target_details ) {
  2924. if ( $target_lang != $language_code ) {
  2925. if ( $target_details->element_id ) {
  2926. $this->fix_translated_parent( $post_id, $target_details->element_id, $target_lang);
  2927. }
  2928. }
  2929. }
  2930. }
  2931. }
  2932. // synchronize the page template
  2933. if ( isset( $post_vars[ 'page_template' ] ) && $trid && $post_vars[ 'post_type' ] == 'page' && $this->settings[ 'sync_page_template' ] ) {
  2934. if ( $translated_element_id && is_array($translated_element_id) ) {
  2935. foreach ( $translated_element_id as $tp ) {
  2936. if ( $tp != $post_id ) {
  2937. update_post_meta( $tp, '_wp_page_template', $post_vars[ 'page_template' ] );
  2938. }
  2939. }
  2940. }
  2941. }
  2942. // synchronize comment and ping status
  2943. if ( $trid && ( $this->settings[ 'sync_ping_status' ] || $this->settings[ 'sync_comment_status' ] ) ) {
  2944. $arr = array();
  2945. if ( $this->settings[ 'sync_comment_status' ] ) {
  2946. $arr[ 'comment_status' ] = $post_vars[ 'comment_status' ];
  2947. }
  2948. if ( $this->settings[ 'sync_ping_status' ] ) {
  2949. $arr[ 'ping_status' ] = $post_vars[ 'ping_status' ];
  2950. }
  2951. if ( !empty( $arr ) && $translated_element_id && is_array($translated_element_id) ) {
  2952. foreach ( $translated_element_id as $tp ) {
  2953. if ( $tp != $post_id ) {
  2954. $wpdb->update( $wpdb->posts, $arr, array( 'ID' => $tp ) );
  2955. }
  2956. }
  2957. }
  2958. }
  2959. // copy custom fields from original
  2960. $translations = $this->get_element_translations( $trid, 'post_' . $post_vars[ 'post_type' ] );
  2961. if ( !empty( $translations ) ) {
  2962. $original_post_id = false;
  2963. foreach ( $translations as $t ) {
  2964. if ( $t->original ) {
  2965. $original_post_id = $t->element_id;
  2966. break;
  2967. }
  2968. }
  2969. // this runs only for translated documents
  2970. if ( $post_id != $original_post_id ) {
  2971. $this->copy_custom_fields( $original_post_id, $post_id );
  2972. } else {
  2973. foreach ( $translations as $t ) {
  2974. if ( $original_post_id != $t->element_id ) {
  2975. $this->copy_custom_fields( $original_post_id, $t->element_id );
  2976. }
  2977. }
  2978. }
  2979. }
  2980. //sync posts stickiness
  2981. if ( isset( $post_vars[ 'post_type' ] ) && $post_vars[ 'post_type' ] == 'post' && isset( $post_vars[ 'action' ] ) && $post_vars[ 'action' ] != 'post-quickpress-publish' && $this->settings[ 'sync_sticky_flag' ] ) { //not for quick press
  2982. remove_filter( 'option_sticky_posts', array( $this, 'option_sticky_posts' ) ); // remove filter used to get language relevant stickies. get them all
  2983. $sticky_posts = get_option( 'sticky_posts' );
  2984. add_filter( 'option_sticky_posts', array( $this, 'option_sticky_posts' ) ); // add filter back
  2985. // get ids of the translations
  2986. if ( $trid ) {
  2987. $translations = $wpdb->get_col( $wpdb->prepare( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d", $trid ) );
  2988. } else {
  2989. $translations = array();
  2990. }
  2991. if ( isset( $post_vars[ 'sticky' ] ) && $post_vars[ 'sticky' ] == 'sticky' ) {
  2992. $sticky_posts = array_unique( array_merge( $sticky_posts, $translations ) );
  2993. } else {
  2994. //makes sure translations are not set to sticky if this posts switched from sticky to not-sticky
  2995. $sticky_posts = array_diff( $sticky_posts, $translations );
  2996. }
  2997. update_option( 'sticky_posts', $sticky_posts );
  2998. }
  2999. //sync private flag
  3000. if ( $this->settings[ 'sync_private_flag' ] ) {
  3001. if ( $post_status == 'private' && ( empty( $post_vars[ 'original_post_status' ] ) || $post_vars[ 'original_post_status' ] != 'private' ) ) {
  3002. if ( $translated_element_id && is_array($translated_element_id) ) {
  3003. foreach ( $translated_element_id as $tp ) {
  3004. if ( $tp != $post_id ) {
  3005. $wpdb->update( $wpdb->posts, array( 'post_status' => 'private' ), array( 'ID' => $tp ) );
  3006. }
  3007. }
  3008. }
  3009. } elseif ( $post_status != 'private' && isset( $post_vars[ 'original_post_status' ] ) && $post_vars[ 'original_post_status' ] == 'private' ) {
  3010. if ( $translated_element_id && is_array($translated_element_id) ) {
  3011. foreach ( $translated_element_id as $tp ) {
  3012. if ( $tp != $post_id ) {
  3013. $wpdb->update( $wpdb->posts, array( 'post_status' => $post_status ), array( 'ID' => $tp ) );
  3014. }
  3015. }
  3016. }
  3017. }
  3018. }
  3019. //sync post format
  3020. if ( $this->settings[ 'sync_post_format' ] && function_exists( 'set_post_format' ) ) {
  3021. $format = get_post_format( $post_id );
  3022. if ( $translated_element_id && is_array($translated_element_id) ) {
  3023. foreach ( $translated_element_id as $tp ) {
  3024. if ( $tp != $post_id ) {
  3025. set_post_format( $tp, $format );
  3026. }
  3027. }
  3028. }
  3029. }
  3030. // sync taxonomies (ONE WAY)
  3031. if ( !empty( $this->settings[ 'sync_post_taxonomies' ] ) && $language_code == $default_language ) {
  3032. $translatable_taxs = $this->get_translatable_taxonomies( true, $post_vars[ 'post_type' ] );
  3033. $all_taxs = get_object_taxonomies( $post_vars[ 'post_type' ] );
  3034. if ( !empty( $all_taxs ) ) {
  3035. $translations = $this->get_element_translations( $trid, 'post_' . $post_vars[ 'post_type' ] );
  3036. foreach ( $all_taxs as $tt ) {
  3037. $terms = get_the_terms( $post_id, $tt );
  3038. if ( !empty( $terms ) ) {
  3039. foreach ( $translations as $target_lang => $translation ) {
  3040. if ( $target_lang != $language_code ) {
  3041. $tax_sync = array();
  3042. foreach ( $terms as $term ) {
  3043. if ( in_array( $tt, $translatable_taxs ) ) {
  3044. $term_id = icl_object_id( $term->term_id, $tt, false, $target_lang );
  3045. } else {
  3046. $term_id = $term->term_id;
  3047. }
  3048. if ( $term_id ) {
  3049. $tax_sync[ ] = intval( $term_id );
  3050. }
  3051. }
  3052. //set the fourth parameter in 'true' because we need to add new terms, instead of replacing all
  3053. wp_set_object_terms( $translation->element_id, $tax_sync, $tt, true );
  3054. }
  3055. }
  3056. }
  3057. }
  3058. }
  3059. }
  3060. // sync post dates
  3061. if ( !empty( $this->settings[ 'sync_post_date' ] ) ) {
  3062. if ( $language_code == $default_language ) {
  3063. if ( $translated_element_id && is_array($translated_element_id) ) {
  3064. $post_date = $wpdb->get_var( $wpdb->prepare( "SELECT post_date FROM {$wpdb->posts} WHERE ID=%d", $post_id ) );
  3065. foreach ( $translated_element_id as $tp ) {
  3066. if ( $tp != $post_id ) {
  3067. $wpdb->update( $wpdb->posts, array( 'post_date' => $post_date, 'post_date_gmt' => get_gmt_from_date( $post_date ) ), array( 'ID' => $tp ) );
  3068. }
  3069. }
  3070. }
  3071. } else {
  3072. if ( !is_null( $trid ) ) {
  3073. $source_lang = isset( $_GET[ 'source_lang' ] ) ? $_GET[ 'source_lang' ] : $default_language;
  3074. $original_id = $wpdb->get_var( $wpdb->prepare( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND language_code=%s", $trid, $source_lang ) );
  3075. $post_date = $wpdb->get_var( $wpdb->prepare( "SELECT post_date FROM {$wpdb->posts} WHERE ID=%d", $original_id ) );
  3076. $wpdb->update( $wpdb->posts, array( 'post_date' => $post_date, 'post_date_gmt' => get_gmt_from_date( $post_date ) ), array( 'ID' => $post_id ) );
  3077. }
  3078. }
  3079. }
  3080. // new categories created inline go to the correct language
  3081. if ( isset( $post_vars[ 'post_category' ] ) && is_array( $post_vars[ 'post_category' ] ) && $post_vars[ 'action' ] != 'inline-save' && $post_vars[ 'icl_post_language' ] ) {
  3082. foreach ( $post_vars[ 'post_category' ] as $cat ) {
  3083. $ttid = $wpdb->get_var( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id={$cat} AND taxonomy='category'" );
  3084. $wpdb->update( $wpdb->prefix . 'icl_translations', array( 'language_code' => $post_vars[ 'icl_post_language' ] ), array( 'element_id' => $ttid, 'element_type' => 'tax_category' ) );
  3085. }
  3086. }
  3087. if ( isset( $post_vars[ 'icl_tn_note' ] ) ) {
  3088. update_post_meta( $post_id, '_icl_translator_note', $post_vars[ 'icl_tn_note' ] );
  3089. }
  3090. //fix guid
  3091. if ( $this->settings[ 'language_negotiation_type' ] == 2 && $this->get_current_language() != $default_language ) {
  3092. $guid = $this->convert_url( get_post_field( 'guid', $post_id ) );
  3093. $wpdb->update( $wpdb->posts, array( 'guid' => $guid ), array( 'id' => $post_id ) );
  3094. }
  3095. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  3096. icl_cache_clear( $post_vars[ 'post_type' ] . 's_per_language', true );
  3097. wp_defer_term_counting( false );
  3098. }
  3099. function get_trid_from_referer() {
  3100. if (isset($_SERVER['HTTP_REFERER'])) {
  3101. $query = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY);
  3102. parse_str($query);
  3103. if (isset($trid)) {
  3104. return $trid;
  3105. }
  3106. }
  3107. return false;
  3108. }
  3109. /**
  3110. * If post has translations and is the original, returns false
  3111. *
  3112. * @param $post WP_Post
  3113. *
  3114. * @return bool
  3115. */
  3116. function can_post_be_deleted( $post )
  3117. {
  3118. global $sitepress;
  3119. $sitepress_settings = $sitepress->get_settings();
  3120. if ( $sitepress_settings[ 'sync_delete' ] != 1 ) {
  3121. $trid = $sitepress->get_element_trid( $post->ID, 'post_' . $post->post_type );
  3122. if ( $trid ) {
  3123. $translations = $sitepress->get_element_translations( $trid, 'post_' . $post->post_type );
  3124. $original_id = false;
  3125. $can_delete = true;
  3126. foreach ( $translations as $translation ) {
  3127. //TODO: check that the post exists too
  3128. if ( $translation->language_code != $sitepress->get_default_language() ) {
  3129. $can_delete = false;
  3130. } else {
  3131. $original_id = $translation->element_id;
  3132. }
  3133. }
  3134. if ( $post->ID == $original_id && !$can_delete ) {
  3135. return false;
  3136. } else {
  3137. return true;
  3138. }
  3139. }
  3140. }
  3141. return true;
  3142. }
  3143. function wp_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_parent ) {
  3144. global $wpdb;
  3145. $cache_key_array = array( $slug, $post_id, $post_status, $post_type, $post_parent );
  3146. $cache_key = md5( serialize( $cache_key_array ) );
  3147. $cache_group = 'wp_unique_post_slug';
  3148. $cache_found = false;
  3149. $result = wp_cache_get( $cache_key, $cache_group, false, $cache_found );
  3150. if ( $cache_found ) {
  3151. return $result;
  3152. }
  3153. if ( $this->is_translated_post_type( $post_type ) && $post_id ) {
  3154. $post_prepared = $wpdb->prepare( "SELECT ID, post_status, post_name, post_title FROM {$wpdb->posts} WHERE ID=%d", $post_id );
  3155. $post = $wpdb->get_row( $post_prepared );
  3156. if ( !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
  3157. global $wp_rewrite;
  3158. $feeds = $wp_rewrite->feeds;
  3159. if ( !is_array( $feeds ) ) {
  3160. $feeds = array();
  3161. }
  3162. $post_language = $this->get_language_for_element( $post_id, 'post_' . $post_type );
  3163. if ( isset( $_POST[ 'new_slug' ] ) ) {
  3164. if ( $_POST[ 'new_slug' ] === '' ) {
  3165. $slug = sanitize_title( $_POST[ 'new_title' ], $post->ID );
  3166. } else {
  3167. $slug = sanitize_title( $_POST[ 'new_slug' ], $post->ID );
  3168. }
  3169. } elseif ( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'inline-save' ) {
  3170. $slug = sanitize_title( $_POST[ 'post_name' ], $post->ID );
  3171. } else {
  3172. $slug = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
  3173. }
  3174. $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );
  3175. if ( in_array( $post_type, $hierarchical_post_types ) ) {
  3176. // Page slugs must be unique within their own trees. Pages are in a separate
  3177. // namespace than posts so page slugs are allowed to overlap post slugs.
  3178. $post_name_check_sql = "SELECT p.post_name
  3179. FROM $wpdb->posts p
  3180. JOIN {$wpdb->prefix}icl_translations t ON p.ID = t.element_id AND t.element_type = %s
  3181. WHERE p.post_name = %s AND p.post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' )
  3182. AND p.ID != %d AND p.post_parent = %d AND t.language_code = %s LIMIT 1";
  3183. $post_name_check = $wpdb->get_var( $wpdb->prepare( $post_name_check_sql, 'post_' . $post_type, $slug, $post_id, $post_parent, $post_language ) );
  3184. if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
  3185. $suffix = 2;
  3186. do {
  3187. $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
  3188. $post_name_check = $wpdb->get_var( $wpdb->prepare( $post_name_check_sql, 'post_' . $post_type, $alt_post_name, $post_id, $post_parent, $post_language ) );
  3189. $suffix++;
  3190. } while ( $post_name_check );
  3191. $slug = $alt_post_name;
  3192. }
  3193. } else {
  3194. // Post slugs must be unique across all posts.
  3195. $post_name_check_sql = "SELECT p.post_name
  3196. FROM $wpdb->posts p
  3197. JOIN {$wpdb->prefix}icl_translations t ON p.ID = t.element_id AND t.element_type = %s
  3198. WHERE p.post_name = %s AND p.post_type = %s AND p.ID != %d AND t.language_code = %s LIMIT 1";
  3199. $post_name_check_prepared = $wpdb->prepare( $post_name_check_sql, 'post_' . $post_type, $slug, $post_type, $post_id, $post_language );
  3200. $post_name_check = $wpdb->get_var( $post_name_check_prepared );
  3201. if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
  3202. $suffix = 2;
  3203. do {
  3204. $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
  3205. $post_name_check = $wpdb->get_var( $wpdb->prepare( $post_name_check_sql, 'post_' . $post_type, $alt_post_name, $post_type, $post_id, $post_language ) );
  3206. $suffix++;
  3207. } while ( $post_name_check );
  3208. $slug = $alt_post_name;
  3209. }
  3210. }
  3211. if ( isset( $_POST[ 'new_slug' ] ) ) {
  3212. $wpdb->update( $wpdb->posts, array( 'post_name' => $slug ), array( 'ID' => $post_id ) );
  3213. }
  3214. }
  3215. }
  3216. wp_cache_set( $cache_key, $slug, $cache_group );
  3217. return $slug;
  3218. }
  3219. /** Fix parent of translation
  3220. * User changed parent for $orginal_id and we are setting proper parent for $translation_id in $language_code_translated language
  3221. * @param $original_id - id of post with changed parent
  3222. * @param $translated_id - translation of changed post
  3223. * @param $translation_language - language we are fixing
  3224. */
  3225. function fix_translated_parent( $original_id, $translated_id, $translation_language )
  3226. {
  3227. global $wpdb;
  3228. $icl_post_type = isset( $_POST[ 'post_type' ] ) ? 'post_' . $_POST[ 'post_type' ] : 'post_page';
  3229. // get parent of original page
  3230. $original_parent = $wpdb->get_var( $wpdb->prepare( "SELECT post_parent FROM {$wpdb->posts} WHERE ID = %d AND post_type = %s", array($original_id, 'page') ) );
  3231. if ( !is_null( $original_parent ) ) {
  3232. if ( $original_parent == 0){
  3233. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->posts} SET post_parent=%d WHERE ID = %d", array( 0, $translated_id ) ) );
  3234. }else {
  3235. $trid = $this->get_element_trid( $original_parent, $icl_post_type );
  3236. if ( $trid ) {
  3237. //get parent translations
  3238. $translations_of_parent = $this->get_element_translations( $trid, $icl_post_type );
  3239. if ( isset( $translations_of_parent[ $translation_language ] ) ) {
  3240. $current_translated_parent = $wpdb->get_var( $wpdb->prepare( "SELECT post_parent FROM {$wpdb->posts} WHERE ID = %d", array($translated_id) ) );
  3241. if ( !is_null( $translations_of_parent[ $translation_language ]->element_id ) && $current_translated_parent != $translations_of_parent[ $translation_language ]->element_id ) {
  3242. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->posts} SET post_parent=%d WHERE ID = %d", array( $translations_of_parent[$translation_language]->element_id, $translated_id ) ) );
  3243. }
  3244. }
  3245. }
  3246. }
  3247. }
  3248. }
  3249. /* Custom fields synchronization - START */
  3250. function _sync_custom_field( $post_id_from, $post_id_to, $meta_key )
  3251. {
  3252. global $wpdb;
  3253. $values_from_prepared = $wpdb->prepare( "SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s", array($post_id_from, $meta_key) );
  3254. $values_to_prepared = $wpdb->prepare( "SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s", array($post_id_to, $meta_key) );
  3255. $values_from = $wpdb->get_col( $values_from_prepared );
  3256. $values_to = $wpdb->get_col( $values_to_prepared );
  3257. // handle the case of 1 key - 1 value with updates in case of change
  3258. // if ( count( $values_from ) == 1 && count( $values_to ) == 1 ) {
  3259. //
  3260. // if ( $values_from[ 0 ] != $values_to[ 0 ] ) {
  3261. // $update_prepared = $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value=%s WHERE post_id=%d AND meta_key=%s", array( $values_from[ 0 ], $post_id_to, $meta_key ) );
  3262. // $wpdb->query( $update_prepared );
  3263. // }
  3264. //
  3265. // } else {
  3266. //removed
  3267. $removed = array_diff( $values_to, $values_from );
  3268. foreach ( $removed as $v ) {
  3269. $delete_prepared = $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s AND meta_value=%s", $post_id_to, $meta_key, $v );
  3270. $wpdb->query( $delete_prepared );
  3271. }
  3272. //added
  3273. $added = array_diff( $values_from, $values_to );
  3274. foreach ( $added as $v ) {
  3275. $insert_prepared = $wpdb->prepare( "INSERT INTO {$wpdb->postmeta}(post_id, meta_key, meta_value) VALUES(%d, %s, %s)", $post_id_to, $meta_key, $v );
  3276. $wpdb->query( $insert_prepared );
  3277. }
  3278. // }
  3279. }
  3280. function copy_custom_fields( $post_id_from, $post_id_to )
  3281. {
  3282. $cf_copy = array();
  3283. if ( isset( $this->settings[ 'translation-management' ][ 'custom_fields_translation' ] ) ) {
  3284. foreach ( $this->settings[ 'translation-management' ][ 'custom_fields_translation' ] as $meta_key => $option ) {
  3285. if ( $option == 1 ) {
  3286. $cf_copy[ ] = $meta_key;
  3287. }
  3288. }
  3289. }
  3290. foreach ( $cf_copy as $meta_key ) {
  3291. $meta_from = get_post_meta($post_id_from, $meta_key) ;
  3292. $meta_to = get_post_meta($post_id_to, $meta_key) ;
  3293. if($meta_from || $meta_to) {
  3294. $this->_sync_custom_field( $post_id_from, $post_id_to, $meta_key );
  3295. }
  3296. }
  3297. }
  3298. function update_post_meta( $meta_id, $object_id, $meta_key, $_meta_value )
  3299. {
  3300. return;
  3301. global $wpdb, $iclTranslationManagement;
  3302. // only handle custom fields that need to be copied
  3303. $custom_fields_translation = isset($this->settings[ 'translation-management' ][ 'custom_fields_translation' ]) ? $this->settings[ 'translation-management' ][ 'custom_fields_translation' ] : false;
  3304. if (!$custom_fields_translation || !isset( $custom_fields_translation[ $meta_key ] ) || !in_array( $custom_fields_translation[ $meta_key ], array( 1, 2 ) ) ) {
  3305. return;
  3306. }
  3307. $post = get_post( $object_id );
  3308. if ( $custom_fields_translation[ $meta_key ] == '2' ) {
  3309. $trid = $this->get_element_trid( $object_id, 'post_' . $post->post_type );
  3310. if ( $trid ) {
  3311. $translations = $this->get_element_translations( $trid, 'post_' . $post->post_type );
  3312. foreach ( $translations as $translation ) {
  3313. if ( $translation->original == 1 && $translation->element_id == $object_id ) {
  3314. $is_original = true;
  3315. break;
  3316. }
  3317. }
  3318. if ( isset( $is_original ) ) {
  3319. $md5 = $iclTranslationManagement->post_md5( $object_id );
  3320. foreach ( $translations as $translation ) {
  3321. if ( !$translation->original ) {
  3322. $emd5 = $wpdb->get_var( $wpdb->prepare( "SELECT md5 FROM {$wpdb->prefix}icl_translation_status WHERE translation_id = %d", $translation->translation_id ) );
  3323. if ( $md5 != $emd5 ) {
  3324. $translation_package = $iclTranslationManagement->create_translation_package( $object_id );
  3325. $translation_data = array(
  3326. 'translation_id' => $translation->translation_id,
  3327. 'needs_update' => 1,
  3328. 'md5' => $md5,
  3329. 'translation_package' => serialize( $translation_package )
  3330. );
  3331. list( $rid ) = $iclTranslationManagement->update_translation_status( $translation_data );
  3332. $translator_id_prepared = $wpdb->prepare( "SELECT translator_id FROM {$wpdb->prefix}icl_translation_status WHERE translation_id = %d", $translation->translation_id );
  3333. $translator_id = $wpdb->get_var( $translator_id_prepared );
  3334. $iclTranslationManagement->add_translation_job( $rid, $translator_id, $translation_package );
  3335. }
  3336. }
  3337. }
  3338. }
  3339. }
  3340. } else {
  3341. $translated_docs = $this->get_translatable_documents();
  3342. if ( !empty( $translated_docs[ $post->post_type ] ) ) {
  3343. $original_id = $this->get_original_element_id($object_id, 'post_' . $post->post_type, true, $all_statuses = true);
  3344. $trid = $this->get_element_trid( $object_id, 'post_' . $post->post_type );
  3345. if ( $trid ) {
  3346. $translations = $this->get_element_translations( $trid, 'post_' . $post->post_type, true, true );
  3347. // CASE of updating the original document (source)
  3348. if ( isset( $original_id ) ) {
  3349. if ( $object_id == $original_id ) {
  3350. foreach ( $translations as $t ) {
  3351. if ( $object_id != $t->element_id ) {
  3352. $this->_sync_custom_field( $object_id, $t->element_id, $meta_key, true );
  3353. }
  3354. }
  3355. } else { // CASE of updating the translated document (target) - don't let writing something else here
  3356. $_meta_value = get_post_meta($original_id, $meta_key);
  3357. $this->update_post_meta( $meta_id, $original_id, $meta_key, $_meta_value );
  3358. }
  3359. }
  3360. }
  3361. }
  3362. }
  3363. }
  3364. function delete_post_meta( $meta_id )
  3365. {
  3366. return;
  3367. if(!isset($this->settings[ 'translation-management' ][ 'custom_fields_translation' ])) return;
  3368. if ( !function_exists( 'get_post_meta_by_id' ) ) {
  3369. require_once ABSPATH . 'wp-admin/includes/post.php';
  3370. }
  3371. if ( is_array( $meta_id ) ) {
  3372. $meta_id = $meta_id[ 0 ];
  3373. }
  3374. $meta = get_post_meta_by_id( $meta_id );
  3375. if(!isset($this->settings[ 'translation-management' ][ 'custom_fields_translation' ][ $meta->meta_key ])) return;
  3376. $custom_fields_translation_meta = $this->settings[ 'translation-management' ][ 'custom_fields_translation' ][ $meta->meta_key ];
  3377. if ( $meta && in_array( $custom_fields_translation_meta, array( 1, 2 ) ) ) {
  3378. $post = get_post( $meta->post_id );
  3379. $translated_docs = $this->get_translatable_documents();
  3380. if ( !empty( $translated_docs[ $post->post_type ] ) ) {
  3381. $trid = $this->get_element_trid( $meta->post_id, 'post_' . $post->post_type );
  3382. if ( $trid ) {
  3383. $translations = $this->get_element_translations( $trid, 'post_' . $post->post_type );
  3384. if ( $translations ) {
  3385. foreach ( $translations as $t ) {
  3386. if ( $t->original ) {
  3387. $original_id = $t->element_id;
  3388. }
  3389. }
  3390. }
  3391. if ( isset( $original_id ) ) {
  3392. if ( $original_id == $meta->post_id ) {
  3393. foreach ( $translations as $t ) {
  3394. if ( !$t->original ) {
  3395. $this->_sync_custom_field( $meta->post_id, $t->element_id, $meta->meta_key, $custom_fields_translation_meta==1 );
  3396. }
  3397. }
  3398. } else {
  3399. $this->_sync_custom_field( $original_id, $meta->post_id, $meta->meta_key, $custom_fields_translation_meta==1 );
  3400. }
  3401. }
  3402. }
  3403. }
  3404. }
  3405. }
  3406. /* Custom fields synchronization - END */
  3407. function before_delete_post_actions( $post_id )
  3408. {
  3409. global $wpdb;
  3410. if ( !isset( $post_id ) || !$post_id ) {
  3411. $post = get_post();
  3412. $post_id = $post->ID;
  3413. } else {
  3414. $post = get_post( $post_id );
  3415. }
  3416. if ( $post == null ) {
  3417. return;
  3418. }
  3419. $post_type = $post->post_type;
  3420. //we need to save information which for which children we have to update translations after parent post delete
  3421. if ( is_post_type_hierarchical( $post_type ) && $this->settings[ 'sync_page_parent' ] && !$this->settings[ 'sync_delete' ] ) {
  3422. //get children of deleted post
  3423. $children_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_parent=%d AND post_type=%s", array($post_id, $post_type) ) );
  3424. //cache value will be used in in deleted_post_actions
  3425. wp_cache_set('children_before_delete_post_actions_'.$post_id, $children_ids, 'icl_before_delete_post_actions');
  3426. }
  3427. }
  3428. function deleted_post_actions( $post_id )
  3429. {
  3430. global $wpdb;
  3431. if ( !isset( $post_id ) || !$post_id ) {
  3432. $post = get_post();
  3433. $post_id = $post->ID;
  3434. } else {
  3435. $post = get_post( $post_id );
  3436. }
  3437. if ( $post == null ) {
  3438. return;
  3439. }
  3440. $post_type = $post->post_type;
  3441. $post_type_exceptions = array('nav_menu_item');
  3442. if(in_array($post_type, $post_type_exceptions)) return;
  3443. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  3444. icl_cache_clear( $post_type . 's_per_language', true );
  3445. $element_type = 'post_' . $post_type;
  3446. $trid = $this->get_element_trid( $post_id, $element_type );
  3447. if ( $trid ) {
  3448. $language_details = $this->get_element_language_details( $post_id, $element_type );
  3449. if ( $language_details ) {
  3450. $is_original = ! $language_details->source_language_code;
  3451. $original_language = $language_details->language_code;
  3452. $this->delete_element_translation( $trid, $element_type, $language_details->language_code );
  3453. //If we've just deleted the original and there are still translations, let's set the original to the first available translation
  3454. $post_translations = $this->get_element_translations( $trid, $element_type );
  3455. if ( $is_original && $post_translations ) {
  3456. $languages = $this->get_active_languages( true );
  3457. $languages = $this->order_languages( $languages );
  3458. $fallback_language = false;
  3459. $new_source_translation_id = false;
  3460. //Get first available languages (to keep their order)
  3461. foreach ( $languages as $language ) {
  3462. if ( $language[ 'code' ] != $original_language ) {
  3463. if ( isset( $post_translations[ $language[ 'code' ] ] ) ) {
  3464. $fallback_language = $language[ 'code' ];
  3465. $new_source_translation_id = $post_translations[ $fallback_language ]->element_id;
  3466. break;
  3467. }
  3468. }
  3469. }
  3470. foreach ( $post_translations as $post_translation ) {
  3471. $element_language_details = $this->get_element_language_details( $post_translation->element_id, $element_type );
  3472. if ( $post_translation->element_id == $new_source_translation_id ) {
  3473. $source_language = false;
  3474. } elseif ( $original_language == $element_language_details->source_language_code ) {
  3475. $source_language = $fallback_language;
  3476. } else {
  3477. $source_language = $element_language_details->source_language_code;
  3478. }
  3479. $update_data = array(
  3480. 'language_code' => $element_language_details->language_code
  3481. );
  3482. if ( $source_language ) {
  3483. $update_data[ 'source_language_code' ] = $source_language;
  3484. } else {
  3485. $update_data[ 'source_language_code' ] = null;
  3486. }
  3487. $update_where = array(
  3488. 'translation_id' => $post_translation->translation_id
  3489. );
  3490. $wpdb->update( $wpdb->prefix . 'icl_translations', $update_data, $update_where );
  3491. $_icl_lang_duplicate_of = get_post_meta( $post_translation->element_id, '_icl_lang_duplicate_of', true );
  3492. if ( $_icl_lang_duplicate_of ) {
  3493. if ( $element_language_details->language_code == $fallback_language ) {
  3494. delete_post_meta( $post_translation->element_id, '_icl_lang_duplicate_of' );
  3495. } else {
  3496. update_post_meta( $post_translation->element_id, '_icl_lang_duplicate_of', $new_source_translation_id );
  3497. }
  3498. }
  3499. }
  3500. }
  3501. }
  3502. // synchronize the page parent for translations only when we do not delete translations and only for hierarchical types
  3503. if ( is_post_type_hierarchical( $post_type ) && $this->settings[ 'sync_page_parent' ] && !$this->settings[ 'sync_delete' ] ) {
  3504. //get deleted post parent
  3505. $parent_trid = $this->get_element_trid( $post->post_parent, $element_type );
  3506. //get translations of deleted post parent
  3507. $parent_translations = $this->get_element_translations( $parent_trid, $element_type );
  3508. // get children of deleted post (stored in before_post_delete_actions)
  3509. $children_ids = wp_cache_get( 'children_before_delete_post_actions_' . $post_id, 'icl_before_delete_post_actions' );
  3510. if ( $children_ids ) {
  3511. //for each children of deleted post
  3512. foreach ( $children_ids as $child_id ) {
  3513. //get translations
  3514. $child_trid = $this->get_element_trid( $child_id, $element_type );
  3515. $child_translations = $this->get_element_translations( $child_trid, $element_type );
  3516. //for each translation of child
  3517. foreach ( $child_translations as $child_target_lang => $child_target_details ) {
  3518. //if parent translation exists and it is a translations (not child of deleted post)
  3519. if ( $child_target_details->element_id != $child_id ) {
  3520. if ( isset( $parent_translations[ $child_target_lang ]->element_id ) ) {
  3521. //set parent
  3522. $wpdb->update( $wpdb->posts, array( 'post_parent' => $parent_translations[ $child_target_lang ]->element_id ), array( 'ID' => $child_target_details->element_id ) );
  3523. } else {
  3524. $wpdb->update( $wpdb->posts, array( 'post_parent' => 0 ), array( 'ID' => $child_target_details->element_id ) );
  3525. }
  3526. }
  3527. }
  3528. }
  3529. }
  3530. // remove children of deleted post (stored in before_post_delete_actions)
  3531. wp_cache_delete( 'children_before_delete_post_actions_' . $post_id, 'icl_before_delete_post_actions' );
  3532. }
  3533. }
  3534. if ( !$this->settings[ 'sync_delete' ] ) {
  3535. return;
  3536. }
  3537. static $deleted_posts;
  3538. if ( isset( $deleted_posts[ $post_id ] ) ) {
  3539. return; // avoid infinite loop
  3540. }
  3541. if ( ( empty( $deleted_posts ) || ( is_array( $deleted_posts ) && !isset( $deleted_posts[ $post_id ] ) ) ) ) {
  3542. $translations = $this->get_element_translations( $trid, $element_type );
  3543. foreach ( $translations as $t ) {
  3544. if ( $t->element_id != $post_id ) {
  3545. $deleted_posts[ ] = $post_id;
  3546. $post_exists_sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE id = %d", $t->element_id );
  3547. $post_exists = $wpdb->get_col( $post_exists_sql );
  3548. if ( $post_exists ) {
  3549. wp_delete_post( $t->element_id );
  3550. }
  3551. }
  3552. }
  3553. }
  3554. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  3555. icl_cache_clear( $post_type . 's_per_language', true );
  3556. }
  3557. function trash_post_actions( $post_id )
  3558. {
  3559. global $wpdb;
  3560. $post_type = $wpdb->get_var( "SELECT post_type FROM {$wpdb->posts} WHERE ID={$post_id}" );
  3561. // bulk deleting
  3562. // using this to not try to delete a post that's going to be deleted anyway
  3563. static $posts_to_delete_in_bulk;
  3564. if ( is_null( $posts_to_delete_in_bulk ) ) {
  3565. $posts_to_delete_in_bulk = isset( $_GET[ 'post' ] ) && is_array( $_GET[ 'post' ] ) ? $_GET[ 'post' ] : array();
  3566. }
  3567. if ( $this->settings[ 'sync_delete' ] ) {
  3568. static $trashed_posts = array();
  3569. if ( isset( $trashed_posts[ $post_id ] ) ) {
  3570. return; // avoid infinite loop
  3571. }
  3572. $trashed_posts[ $post_id ] = $post_id;
  3573. $trid = $this->get_element_trid( $post_id, 'post_' . $post_type );
  3574. $translations = $this->get_element_translations( $trid, 'post_' . $post_type );
  3575. foreach ( $translations as $t ) {
  3576. if ( $t->element_id != $post_id && !in_array( $t->element_id, $posts_to_delete_in_bulk ) ) {
  3577. wp_trash_post( $t->element_id );
  3578. }
  3579. }
  3580. }
  3581. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  3582. icl_cache_clear( $post_type . 's_per_language', true );
  3583. }
  3584. function untrashed_post_actions( $post_id )
  3585. {
  3586. global $wpdb;
  3587. $post_type = $wpdb->get_var( "SELECT post_type FROM {$wpdb->posts} WHERE ID={$post_id}" );
  3588. if ( $this->settings[ 'sync_delete' ] ) {
  3589. static $untrashed_posts = array();
  3590. if ( isset( $untrashed_posts[ $post_id ] ) ) {
  3591. return; // avoid infinite loop
  3592. }
  3593. $untrashed_posts[ $post_id ] = $post_id;
  3594. $trid = $this->get_element_trid( $post_id, 'post_' . $post_type );
  3595. $translations = $this->get_element_translations( $trid, 'post_' . $post_type );
  3596. foreach ( $translations as $t ) {
  3597. if ( $t->element_id != $post_id ) {
  3598. wp_untrash_post( $t->element_id );
  3599. }
  3600. }
  3601. }
  3602. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  3603. icl_cache_clear( $post_type . 's_per_language', true );
  3604. }
  3605. function validate_taxonomy_input( $input )
  3606. {
  3607. global $wpdb;
  3608. static $runonce;
  3609. if ( empty( $runonce ) ) {
  3610. $post_language = isset( $_POST[ 'icl_post_language' ] ) ? $_POST[ 'icl_post_language' ] : $this->get_current_language();
  3611. if ( !empty( $input ) && is_array( $input ) ) {
  3612. foreach ( $input as $taxonomy => $values ) {
  3613. if ( is_string( $values ) ) { // only not-hierarchical
  3614. $values = array_map( 'trim', explode( ',', $values ) );
  3615. foreach ( $values as $k => $term ) {
  3616. // if the term exists in another language, apply the language suffix
  3617. $term_info = term_exists( $term, $taxonomy );
  3618. if ( $term_info ) {
  3619. $term_language = $wpdb->get_var( $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations
  3620. WHERE element_type=%s AND element_id=%d", 'tax_' . $taxonomy, $term_info[ 'term_taxonomy_id' ] ) );
  3621. if ( $term_language && $term_language != $post_language ) {
  3622. $term = $term . ' @' . $post_language;
  3623. }
  3624. }
  3625. $values[ $k ] = $term;
  3626. }
  3627. $values = join( ',', $values );
  3628. $input[ $taxonomy ] = $values;
  3629. }
  3630. }
  3631. }
  3632. $runonce = true;
  3633. }
  3634. return $input;
  3635. }
  3636. /**
  3637. * @param int $trid
  3638. * @param string $el_type Use comment, post, page, {custom post time name}, nav_menu, nav_menu_item, category, post_tag, etc. (prefixed with 'post_', 'tax_', or nothing for 'comment')
  3639. * @param bool $skip_empty
  3640. * @param bool $all_statuses
  3641. * @param bool $skip_cache
  3642. *
  3643. * @return array|bool|mixed
  3644. */
  3645. function get_element_translations( $trid, $el_type = 'post_post', $skip_empty = false, $all_statuses = false, $skip_cache = false )
  3646. {
  3647. $cache_key_args = array_filter( array( $trid, $el_type, $skip_empty, $all_statuses ) );
  3648. $cache_key = md5(json_encode( $cache_key_args ));
  3649. $cache_group = 'element_translations';
  3650. $temp_elements = $skip_cache ? false : wp_cache_get($cache_key, $cache_group);
  3651. if($temp_elements) return $temp_elements;
  3652. global $wpdb;
  3653. $translations = array();
  3654. $sel_add = '';
  3655. $where_add = '';
  3656. if ( $trid ) {
  3657. if ( 0 === strpos( $el_type, 'post_' ) ) {
  3658. $sel_add = ', p.post_title, p.post_status';
  3659. $join_add = " LEFT JOIN {$wpdb->posts} p ON t.element_id=p.ID";
  3660. $groupby_add = "";
  3661. if ( !is_admin() && empty( $all_statuses ) && $el_type != 'post_attachment' ) {
  3662. // the current user may not be the admin but may have read private post/page caps!
  3663. if ( current_user_can( 'read_private_pages' ) || current_user_can( 'read_private_posts' ) ) {
  3664. $where_add .= " AND (p.post_status = 'publish' OR p.post_status = 'private')";
  3665. } else {
  3666. $where_add .= " AND (";
  3667. $where_add .= "p.post_status = 'publish'";
  3668. if ( $uid = $this->get_current_user()->ID ) {
  3669. $where_add .= " OR (post_status in ('draft', 'private') AND post_author = {$uid})";
  3670. }
  3671. $where_add .= ") ";
  3672. }
  3673. }
  3674. } elseif ( preg_match( '#^tax_(.+)$#', $el_type ) ) {
  3675. $sel_add = ', tm.name, tm.term_id, COUNT(tr.object_id) AS instances';
  3676. $join_add = " LEFT JOIN {$wpdb->term_taxonomy} tt ON t.element_id=tt.term_taxonomy_id
  3677. LEFT JOIN {$wpdb->terms} tm ON tt.term_id = tm.term_id
  3678. LEFT JOIN {$wpdb->term_relationships} tr ON tr.term_taxonomy_id=tt.term_taxonomy_id
  3679. ";
  3680. $groupby_add = "GROUP BY tm.term_id";
  3681. }
  3682. $where_add .= " AND t.trid='{$trid}'";
  3683. if ( !isset( $join_add ) ) {
  3684. $join_add = "";
  3685. }
  3686. if ( !isset( $groupby_add ) ) {
  3687. $groupby_add = "";
  3688. }
  3689. $query = "
  3690. SELECT t.translation_id, t.language_code, t.element_id, t.source_language_code, NULLIF(t.source_language_code, '') IS NULL AS original {$sel_add}
  3691. FROM {$wpdb->prefix}icl_translations t
  3692. {$join_add}
  3693. WHERE 1 {$where_add}
  3694. {$groupby_add}
  3695. ";
  3696. $ret = $wpdb->get_results( $query );
  3697. foreach ( $ret as $t ) {
  3698. if ( ( preg_match( '#^tax_(.+)$#', $el_type ) ) && $t->instances == 0 && !_icl_tax_has_objects_recursive( $t->element_id ) && $skip_empty ) {
  3699. continue;
  3700. }
  3701. $cached_object_key = $t->element_id . '#' . $el_type . '#0#' . $t->language_code;
  3702. wp_cache_set( $cached_object_key, $cached_object_key, 'icl_object_id' );
  3703. $translations[ $t->language_code ] = $t;
  3704. }
  3705. }
  3706. if($translations) {
  3707. wp_cache_set($cache_key, $translations, $cache_group);
  3708. }
  3709. return $translations;
  3710. }
  3711. static function get_original_element_id($element_id, $element_type = 'post_post', $skip_empty = false, $all_statuses = false, $skip_cache = false) {
  3712. $cache_key_args = array_filter( array( $element_id, $element_type, $skip_empty, $all_statuses ) );
  3713. $cache_key = md5(json_encode( $cache_key_args ));
  3714. $cache_group = 'original_element';
  3715. $temp_elements = $skip_cache ? false : wp_cache_get($cache_key, $cache_group);
  3716. if($temp_elements) return $temp_elements;
  3717. global $sitepress;
  3718. $original_element_id = false;
  3719. $trid = $sitepress->get_element_trid($element_id, $element_type);
  3720. if($trid) {
  3721. $element_translations = $sitepress->get_element_translations($trid, $element_type,$skip_empty,$all_statuses,$skip_cache);
  3722. foreach($element_translations as $element_translation) {
  3723. if($element_translation->original) {
  3724. $original_element_id = $element_translation->element_id;
  3725. break;
  3726. }
  3727. }
  3728. }
  3729. if($original_element_id) {
  3730. wp_cache_set($cache_key, $original_element_id, $cache_group);
  3731. }
  3732. return $original_element_id;
  3733. }
  3734. /**
  3735. * @param int $element_id Use term_taxonomy_id for taxonomies, post_id for posts
  3736. * @param string $el_type Use comment, post, page, {custom post time name}, nav_menu, nav_menu_item, category, post_tag, etc. (prefixed with 'post_', 'tax_', or nothing for 'comment')
  3737. *
  3738. * @return bool|mixed|null|string
  3739. */
  3740. function get_element_trid( $element_id, $el_type = 'post_post' ) {
  3741. $cache_key = $element_id . ':' . $el_type;
  3742. $cache_group = 'element_trid';
  3743. $temp_trid = wp_cache_get($cache_key, $cache_group);
  3744. if($temp_trid) return $temp_trid;
  3745. global $wpdb;
  3746. $trid_prepared = $wpdb->prepare( "SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id=%d AND element_type=%s", array( $element_id, $el_type ) );
  3747. $trid = $wpdb->get_var( $trid_prepared );
  3748. if($trid) {
  3749. wp_cache_add($cache_key, $trid, $cache_group);
  3750. }
  3751. return $trid;
  3752. }
  3753. /**
  3754. * @param int $element_id Use term_taxonomy_id for taxonomies, post_id for posts
  3755. * @param string $el_type Use comment, post, page, {custom post time name}, nav_menu, nav_menu_item, category, post_tag, etc. (prefixed with 'post_', 'tax_', or nothing for 'comment')
  3756. *
  3757. * @return null|string
  3758. */
  3759. function get_language_for_element( $element_id, $el_type = 'post_post' ) {
  3760. global $wpdb;
  3761. $cache_key_array = array( $element_id, $el_type );
  3762. $cache_key = md5( serialize( $cache_key_array ) );
  3763. $cache_group = 'get_language_for_element';
  3764. $cache_found = false;
  3765. $result = wp_cache_get( $cache_key, $cache_group, false, $cache_found );
  3766. if ( $cache_found ) {
  3767. return $result;
  3768. }
  3769. $language_for_element_prepared = $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id=%d AND element_type=%s", array( $element_id, $el_type ) );
  3770. $result = $wpdb->get_var( $language_for_element_prepared );
  3771. wp_cache_set( $cache_key, $result, $cache_group );
  3772. return $result;
  3773. }
  3774. /**
  3775. * @param string $el_type Use comment, post, page, {custom post time name}, nav_menu, nav_menu_item, category, post_tag, etc. (prefixed with 'post_', 'tax_', or nothing for 'comment')
  3776. * @param string $target_lang Target language code
  3777. * @param string $source_lang Source language code
  3778. *
  3779. * @return array
  3780. */
  3781. function get_elements_without_translations( $el_type, $target_lang, $source_lang )
  3782. {
  3783. global $wpdb;
  3784. // first get all the trids for the target languages
  3785. // These will be the trids that we don't want.
  3786. $sql = "SELECT
  3787. trid
  3788. FROM
  3789. {$wpdb->prefix}icl_translations
  3790. WHERE
  3791. language_code = '{$target_lang}'
  3792. AND element_type = '{$el_type}'
  3793. ";
  3794. $trids_for_target = $wpdb->get_col( $sql );
  3795. if ( sizeof( $trids_for_target ) > 0 ) {
  3796. $trids_for_target = join( ',', $trids_for_target );
  3797. $not_trids = 'AND trid NOT IN (' . $trids_for_target . ')';
  3798. } else {
  3799. $not_trids = '';
  3800. }
  3801. $join = $where = '';
  3802. // exclude trashed posts
  3803. if ( 0 === strpos( $el_type, 'post_' ) ) {
  3804. $join .= " JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->prefix}icl_translations.element_id";
  3805. $where .= " AND {$wpdb->posts}.post_status <> 'trash' AND {$wpdb->posts}.post_status <> 'auto-draft'";
  3806. }
  3807. // Now get all the elements that are in the source language that
  3808. // are not already translated into the target language.
  3809. $sql = "SELECT
  3810. element_id
  3811. FROM
  3812. {$wpdb->prefix}icl_translations
  3813. {$join}
  3814. WHERE
  3815. language_code = '{$source_lang}'
  3816. {$not_trids}
  3817. AND element_type= '{$el_type}'
  3818. {$where}
  3819. ";
  3820. return $wpdb->get_col( $sql );
  3821. }
  3822. /**
  3823. * @param string $selected_language
  3824. * @param string $default_language
  3825. * @param string $post_type
  3826. *
  3827. * @used_by SitePress:meta_box
  3828. *
  3829. * @return array
  3830. */
  3831. function get_posts_without_translations( $selected_language, $default_language, $post_type = 'post_post' )
  3832. {
  3833. global $wpdb;
  3834. $untranslated_ids = $this->get_elements_without_translations( $post_type, $selected_language, $default_language );
  3835. $untranslated = array();
  3836. foreach ( $untranslated_ids as $id ) {
  3837. $untranslated[ $id ] = $wpdb->get_var( "SELECT post_title FROM {$wpdb->prefix}posts WHERE ID = {$id}" );
  3838. }
  3839. return $untranslated;
  3840. }
  3841. static function get_orphan_translations($trid, $post_type = 'post', $source_language) {
  3842. global $sitepress, $wpdb;
  3843. $translations = $sitepress->get_element_translations($trid, 'post_' . $post_type);
  3844. if(count($translations) == 1) {
  3845. $sql = " SELECT trid, ";
  3846. $active_languages = $sitepress->get_active_languages();
  3847. $language_codes = array_keys($active_languages);
  3848. $sql_languages = array();
  3849. $sql_languages_having = array();
  3850. foreach($language_codes as $language_code) {
  3851. $sql_languages[] = "SUM(CASE language_code WHEN '" . esc_sql($language_code) . "' THEN 1 ELSE 0 END) AS `" . esc_sql($language_code) . '`';
  3852. if($language_code==$source_language) {
  3853. $sql_languages_having[] = '`' . $language_code . '`= 0';
  3854. }
  3855. }
  3856. $sql .= implode(',', $sql_languages);
  3857. $sql .= " FROM {$wpdb->prefix}icl_translations
  3858. WHERE element_type = %s ";
  3859. $sql .= 'GROUP BY trid ';
  3860. $sql .= 'HAVING ' . implode(' AND ', $sql_languages_having);
  3861. $sql .= " ORDER BY trid;";
  3862. $sql_prepared = $wpdb->prepare($sql, array('post_' . $post_type));
  3863. $trid_results = $wpdb->get_results($sql_prepared, 'ARRAY_A');
  3864. $trid_list = array_column($trid_results, 'trid');
  3865. if($trid_list) {
  3866. $sql = "SELECT trid AS value, CONCAT('[', t.language_code, '] ', (CASE p.post_title WHEN '' THEN CONCAT(LEFT(p.post_content, 30), '...') ELSE p.post_title END)) AS label
  3867. FROM {$wpdb->posts} p
  3868. INNER JOIN {$wpdb->prefix}icl_translations t
  3869. ON p.ID = t.element_id
  3870. WHERE t.element_type = %s
  3871. AND t.language_code <> %s
  3872. AND t.trid IN (" . implode(',', $trid_list) . ')';
  3873. $sql_prepared = $wpdb->prepare($sql, array('post_' . $post_type, $source_language));
  3874. $results = $wpdb->get_results($sql_prepared);
  3875. } else {
  3876. $results = array();
  3877. }
  3878. return $results;
  3879. }
  3880. return false;
  3881. }
  3882. /**
  3883. * @param WP_Post $post
  3884. */
  3885. function meta_box( $post ) {
  3886. if ( in_array( $post->post_type, array_keys( $this->get_translatable_documents() ) ) ) {
  3887. include ICL_PLUGIN_PATH . '/menu/post-menu.php';
  3888. }
  3889. }
  3890. function icl_get_metabox_states()
  3891. {
  3892. global $icl_meta_box_globals, $wpdb;
  3893. $translation = false;
  3894. $source_id = null;
  3895. $translated_id = null;
  3896. if ( sizeof( $icl_meta_box_globals[ 'translations' ] ) > 0 ) {
  3897. if ( !isset( $icl_meta_box_globals[ 'translations' ][ $icl_meta_box_globals[ 'selected_language' ] ] ) ) {
  3898. // We are creating a new translation
  3899. $translation = true;
  3900. // find the original
  3901. foreach ( $icl_meta_box_globals[ 'translations' ] as $trans_data ) {
  3902. if ( $trans_data->original == '1' ) {
  3903. $source_id = $trans_data->element_id;
  3904. break;
  3905. }
  3906. }
  3907. } else {
  3908. $trans_data = $icl_meta_box_globals[ 'translations' ][ $icl_meta_box_globals[ 'selected_language' ] ];
  3909. // see if this is an original or a translation.
  3910. if ( $trans_data->original == '0' ) {
  3911. // double check that it's not the original
  3912. // This is because the source_language_code field in icl_translations is not always being set to null.
  3913. $source_language_code = $wpdb->get_var( "SELECT source_language_code FROM {$wpdb->prefix}icl_translations WHERE translation_id = $trans_data->translation_id" );
  3914. $translation = !( $source_language_code == "" || $source_language_code == null );
  3915. if ( $translation ) {
  3916. $source_id = $icl_meta_box_globals[ 'translations' ][ $source_language_code ]->element_id;
  3917. $translated_id = $trans_data->element_id;
  3918. } else {
  3919. $source_id = $trans_data->element_id;
  3920. }
  3921. } else {
  3922. $source_id = $trans_data->element_id;
  3923. }
  3924. }
  3925. }
  3926. return array( $translation, $source_id, $translated_id );
  3927. }
  3928. function meta_box_config( $post )
  3929. {
  3930. global $iclTranslationManagement;
  3931. global $wp_taxonomies, $wp_post_types, $sitepress_settings;
  3932. echo '<div class="icl_form_success" style="display:none">' . __( 'Settings saved', 'sitepress' ) . '</div>';
  3933. $cp_editable = false;
  3934. $checked = false;
  3935. if ( !in_array( $post->post_type, array( 'post', 'page' ) ) ) {
  3936. if ( !isset( $iclTranslationManagement->settings[ 'custom_types_readonly_config' ][ $post->post_type ] ) || $iclTranslationManagement->settings[ 'custom_types_readonly_config' ][ $post->post_type ] !== 0 ) {
  3937. if ( in_array( $post->post_type, array_keys( $this->get_translatable_documents() ) ) ) {
  3938. $checked = ' checked="checked"';
  3939. $radio_disabled = isset( $iclTranslationManagement->settings[ 'custom_types_readonly_config' ][ $post->post_type ] ) ? 'disabled="disabled"' : '';
  3940. } else {
  3941. $checked = $radio_disabled = '';
  3942. }
  3943. if ( !$radio_disabled ) {
  3944. $cp_editable = true;
  3945. }
  3946. echo '<br style="line-height:8px;" /><label><input id="icl_make_translatable" type="checkbox" value="' . $post->post_type . '"' . $checked . $radio_disabled . '/>&nbsp;' . sprintf( __( "Make '%s' translatable", 'sitepress' ), $wp_post_types[ $post->post_type ]->labels->name ) . '</label><br style="line-height:8px;" />';
  3947. }
  3948. } else {
  3949. echo '<input id="icl_make_translatable" type="checkbox" checked="checked" value="' . $post->post_type . '" style="display:none" />';
  3950. $checked = true;
  3951. }
  3952. echo '<br clear="all" /><span id="icl_mcs_details">';
  3953. if ( $checked ) {
  3954. //echo '<div style="width:49%;float:left;min-width:265px;margin-right:5px;margin-top:3px;">';
  3955. $custom_taxonomies = array_diff( get_object_taxonomies( $post->post_type ), array( 'post_tag', 'category', 'nav_menu', 'link_category', 'post_format' ) );
  3956. if ( !empty( $custom_taxonomies ) ) {
  3957. ?>
  3958. <table class="widefat">
  3959. <thead>
  3960. <tr>
  3961. <th colspan="2"><?php _e( 'Custom taxonomies', 'sitepress' ); ?></th>
  3962. </tr>
  3963. </thead>
  3964. <tbody>
  3965. <?php foreach ( $custom_taxonomies as $ctax ): ?>
  3966. <?php
  3967. $checked1 = !empty( $sitepress_settings[ 'taxonomies_sync_option' ][ $ctax ] ) ? ' checked="checked"' : '';
  3968. $checked0 = empty( $sitepress_settings[ 'taxonomies_sync_option' ][ $ctax ] ) ? ' checked="checked"' : '';
  3969. $radio_disabled = isset( $iclTranslationManagement->settings[ 'taxonomies_readonly_config' ][ $ctax ] ) ? ' disabled="disabled"' : '';
  3970. ?>
  3971. <tr>
  3972. <td><?php echo $wp_taxonomies[ $ctax ]->labels->name ?></td>
  3973. <td align="right">
  3974. <label><input name="icl_mcs_custom_taxs_<?php echo $ctax ?>" class="icl_mcs_custom_taxs" type="radio"
  3975. value="<?php echo $ctax ?>" <?php echo $checked1; ?><?php echo $radio_disabled ?> />&nbsp;<?php _e( 'Translate', 'sitepress' ) ?></label>
  3976. <label><input name="icl_mcs_custom_taxs_<?php echo $ctax ?>" type="radio" value="0" <?php echo $checked0; ?><?php echo $radio_disabled ?> />&nbsp;<?php _e( 'Do nothing', 'sitepress' ) ?></label>
  3977. </td>
  3978. </tr>
  3979. <?php endforeach; ?>
  3980. </tbody>
  3981. </table>
  3982. <br/>
  3983. <?php
  3984. }
  3985. //echo '</div><div style="width:50%;float:left;min-width:265px;margin-top:3px;">';
  3986. if ( defined( 'WPML_TM_VERSION' ) ) {
  3987. $custom_keys = (array)get_post_custom_keys( $post->ID );
  3988. $cf_keys_exceptions = array(
  3989. '_edit_last', '_edit_lock', '_wp_page_template', '_wp_attachment_metadata', '_icl_translator_note', '_alp_processed', '_pingme', '_encloseme', '_icl_lang_duplicate_of', '_wpml_media_duplicate', '_wpml_media_featured',
  3990. '_thumbnail_id'
  3991. );
  3992. $custom_keys = array_diff( $custom_keys, $cf_keys_exceptions );
  3993. $cf_settings_read_only = isset( $iclTranslationManagement->settings[ 'custom_fields_readonly_config' ] ) ? (array)$iclTranslationManagement->settings[ 'custom_fields_readonly_config' ] : array();
  3994. $cf_settings = isset( $iclTranslationManagement->settings[ 'custom_fields_translation' ] ) ? $iclTranslationManagement->settings[ 'custom_fields_translation' ] : array();
  3995. if ( !empty( $custom_keys ) ) {
  3996. ?>
  3997. <table class="widefat">
  3998. <thead>
  3999. <tr>
  4000. <th colspan="2"><?php _e( 'Custom fields', 'sitepress' ); ?></th>
  4001. </tr>
  4002. </thead>
  4003. <tbody>
  4004. <?php
  4005. foreach ( $custom_keys as $cfield ) {
  4006. if ( empty( $cf_settings[ $cfield ] ) || $cf_settings[ $cfield ] != 3 ) {
  4007. $radio_disabled = in_array( $cfield, $cf_settings_read_only ) ? 'disabled="disabled"' : '';
  4008. $checked0 = empty( $cf_settings[ $cfield ] ) ? ' checked="checked"' : '';
  4009. $checked1 = isset( $cf_settings[ $cfield ] ) && $cf_settings[ $cfield ] == 1 ? ' checked="checked"' : '';
  4010. $checked2 = isset( $cf_settings[ $cfield ] ) && $cf_settings[ $cfield ] == 2 ? ' checked="checked"' : '';
  4011. ?>
  4012. <tr>
  4013. <td><?php echo $cfield; ?></td>
  4014. <td align="right">
  4015. <label><input class="icl_mcs_cfs" name="icl_mcs_cf_<?php echo base64_encode( $cfield ); ?> " type="radio"
  4016. value="0" <?php echo $radio_disabled . $checked0 ?> />&nbsp;<?php _e( "Don't translate", 'sitepress' ) ?></label>
  4017. <label><input class="icl_mcs_cfs" name="icl_mcs_cf_<?php echo base64_encode( $cfield ); ?> " type="radio" value="1" <?php echo $radio_disabled . $checked1 ?> />&nbsp;<?php _e( "Copy", 'sitepress' ) ?>
  4018. </label>
  4019. <label><input class="icl_mcs_cfs" name="icl_mcs_cf_<?php echo base64_encode( $cfield ); ?> " type="radio" value="2" <?php echo $radio_disabled . $checked2 ?> />&nbsp;<?php _e( "Translate", 'sitepress' ) ?>
  4020. </label>
  4021. </td>
  4022. </tr>
  4023. <?php
  4024. }
  4025. }
  4026. ?>
  4027. </tbody>
  4028. </table>
  4029. <br/>
  4030. <?php
  4031. }
  4032. }
  4033. //echo '</div><br clear="all" />';
  4034. if ( !empty( $custom_taxonomies ) || !empty( $custom_keys ) ) {
  4035. echo '<small>' . __( 'Note: Custom taxonomies and custom fields are shared across different post types.', 'sitepress' ) . '</small>';
  4036. }
  4037. }
  4038. echo '</span>';
  4039. if ( $cp_editable || !empty( $custom_taxonomies ) || !empty( $custom_keys ) ) {
  4040. echo '<p class="submit" style="margin:0;padding:0"><input class="button-secondary" id="icl_make_translatable_submit" type="button" value="' . __( 'Apply', 'sitepress' ) . '" /></p><br clear="all" />';
  4041. wp_nonce_field( 'icl_mcs_inline_nonce', '_icl_nonce_imi' );
  4042. } else {
  4043. _e( 'Nothing to configure.', 'sitepress' );
  4044. }
  4045. }
  4046. function pre_get_posts( $wpq )
  4047. {
  4048. // case of internal links list
  4049. //
  4050. if ( isset( $_POST[ 'action' ] ) && 'wp-link-ajax' == $_POST[ 'action' ] ) {
  4051. $default_language = $this->get_default_language();
  4052. if ( !empty( $_SERVER[ 'HTTP_REFERER' ] ) ) {
  4053. $parts = parse_url( $_SERVER[ 'HTTP_REFERER' ] );
  4054. parse_str( strval( $parts[ 'query' ] ), $query );
  4055. $lang = isset( $query[ 'lang' ] ) ? $query[ 'lang' ] : $default_language;
  4056. } else $lang = $default_language;
  4057. $this->this_lang = $lang;
  4058. $wpq->query_vars[ 'suppress_filters' ] = false;
  4059. }
  4060. if ($this->is_gallery_on_root_page($wpq)) {
  4061. $wpq->query_vars['page_id'] = 0;
  4062. $wpq->query['page_id'] = 0;
  4063. }
  4064. return $wpq;
  4065. }
  4066. function is_gallery_on_root_page( $wpq ) {
  4067. // are we on root page and we are searching for attachments to gallery on it?
  4068. if ( isset( $this->settings[ 'urls' ][ 'show_on_root' ] ) &&
  4069. $this->settings[ 'urls' ][ 'show_on_root' ] == 'page' &&
  4070. isset( $this->settings[ 'urls' ][ 'root_page' ] ) &&
  4071. isset( $wpq->query_vars[ 'page_id' ] ) &&
  4072. $wpq->query_vars[ 'page_id' ] > 0 &&
  4073. $wpq->query_vars[ 'page_id' ] == $this->settings[ 'urls' ][ 'root_page' ] &&
  4074. isset( $wpq->query_vars[ 'post_type' ] ) &&
  4075. $wpq->query_vars[ 'post_type' ] == 'attachment'
  4076. ) {
  4077. return true;
  4078. }
  4079. return false;
  4080. }
  4081. /**
  4082. * @param $join string
  4083. * @param $query WP_Query
  4084. *
  4085. * @return string
  4086. */
  4087. function posts_join_filter( $join, $query )
  4088. {
  4089. global $wpdb, $pagenow, $wp_taxonomies, $sitepress_settings;
  4090. $attachment_is_translatable = $this->is_translated_post_type( 'attachment' );
  4091. if ( (($pagenow == 'upload.php' || $pagenow == 'media-upload.php' || $query->is_attachment()) && !$attachment_is_translatable) || ( isset( $query->queried_object ) && isset( $query->queried_object->ID ) && $query->queried_object->ID == $sitepress_settings[ 'urls' ][ 'root_page' ] ) ) {
  4092. return $join;
  4093. }
  4094. // determine post type
  4095. $debug_backtrace = $this->get_backtrace( 0, true, false ); //Limit to a maximum level?
  4096. $post_type = false;
  4097. foreach ( $debug_backtrace as $o ) {
  4098. if ( $o[ 'function' ] == 'apply_filters_ref_array' && $o[ 'args' ][ 0 ] == 'posts_join' ) {
  4099. $post_type = esc_sql( $o[ 'args' ][ 1 ][ 1 ]->query_vars[ 'post_type' ] );
  4100. break;
  4101. }
  4102. }
  4103. if ( $post_type == 'any' || 'all' == $this->this_lang ) {
  4104. $left_join = "LEFT";
  4105. } else {
  4106. $left_join = "";
  4107. }
  4108. if ( is_array( $post_type ) ) {
  4109. $post_types = array();
  4110. foreach ( $post_type as $ptype ) {
  4111. if ( $this->is_translated_post_type( $ptype ) ) {
  4112. $post_types[ ] = esc_sql( 'post_' . $ptype );
  4113. }
  4114. }
  4115. if ( !empty( $post_types ) ) {
  4116. $join .= " {$left_join} JOIN {$wpdb->prefix}icl_translations t ON {$wpdb->posts}.ID = t.element_id
  4117. AND t.element_type IN ('" . join( "','", $post_types ) . "') JOIN {$wpdb->prefix}icl_languages l ON t.language_code=l.code AND l.active=1";
  4118. }
  4119. } elseif ( $post_type ) {
  4120. if ( $this->is_translated_post_type( $post_type ) ) {
  4121. $join .= " {$left_join} JOIN {$wpdb->prefix}icl_translations t ON {$wpdb->posts}.ID = t.element_id
  4122. AND t.element_type = 'post_{$post_type}' JOIN {$wpdb->prefix}icl_languages l ON t.language_code=l.code AND l.active=1";
  4123. } elseif ( $post_type == 'any' ) {
  4124. $join .= " {$left_join} JOIN {$wpdb->prefix}icl_translations t ON {$wpdb->posts}.ID = t.element_id
  4125. AND t.element_type LIKE 'post\\_%' {$left_join} JOIN {$wpdb->prefix}icl_languages l ON t.language_code=l.code AND l.active=1";
  4126. }
  4127. } else {
  4128. if ( $query->is_tax() && $query->is_main_query() ) {
  4129. $tax = $query->get( 'taxonomy' );
  4130. $taxonomy_post_types = $wp_taxonomies[ $tax ]->object_type;
  4131. foreach ( $taxonomy_post_types as $k => $v ) {
  4132. if ( !$this->is_translated_post_type( $v ) ) {
  4133. unset( $taxonomy_post_types[ $k ] );
  4134. }
  4135. }
  4136. } else {
  4137. $taxonomy_post_types = array_keys( $this->get_translatable_documents( false ) );
  4138. }
  4139. if ( !empty( $taxonomy_post_types ) ) {
  4140. foreach ( $taxonomy_post_types as $k => $v ) {
  4141. $taxonomy_post_types[ $k ] = 'post_' . $v;
  4142. }
  4143. $post_types = "'" . join( "','", $taxonomy_post_types ) . "'";
  4144. $join .= " {$left_join} JOIN {$wpdb->prefix}icl_translations t ON {$wpdb->posts}.ID = t.element_id
  4145. AND t.element_type IN ({$post_types}) JOIN {$wpdb->prefix}icl_languages l ON t.language_code=l.code AND l.active=1";
  4146. }
  4147. }
  4148. return $join;
  4149. }
  4150. /**
  4151. * @param string $where
  4152. * @param WP_Query $query
  4153. *
  4154. * @return string
  4155. */
  4156. function posts_where_filter( $where, $query )
  4157. {
  4158. global $pagenow, $wp_taxonomies, $sitepress, $sitepress_settings;
  4159. //exceptions
  4160. $post_type = false;
  4161. if ( isset( $query->queried_object ) && isset( $query->queried_object->ID ) && $query->queried_object->ID == $sitepress_settings[ 'urls' ][ 'root_page' ] ) {
  4162. return $where;
  4163. }
  4164. // determine post type
  4165. $debug_backtrace = $this->get_backtrace( 0, true, false ); //Limit to a maximum level?
  4166. foreach ( $debug_backtrace as $o ) {
  4167. if ( $o[ 'function' ] == 'apply_filters_ref_array' && $o[ 'args' ][ 0 ] == 'posts_where' ) {
  4168. $post_type = $o[ 'args' ][ 1 ][ 1 ]->query_vars[ 'post_type' ];
  4169. break;
  4170. }
  4171. }
  4172. // case of taxonomy archive
  4173. if ( empty( $post_type ) && $query->is_tax() ) {
  4174. $tax = $query->get( 'taxonomy' );
  4175. $post_type = $wp_taxonomies[ $tax ]->object_type;
  4176. foreach ( $post_type as $k => $v ) {
  4177. if ( !$this->is_translated_post_type( $v ) ) {
  4178. unset( $post_type[ $k ] );
  4179. }
  4180. }
  4181. if ( empty( $post_type ) ) {
  4182. return $where;
  4183. } // don't filter
  4184. }
  4185. if ( !$post_type ) {
  4186. $post_type = 'post';
  4187. }
  4188. if ( is_array( $post_type ) && !empty( $post_type ) ) {
  4189. $none_translated = true;
  4190. foreach ( $post_type as $ptype ) {
  4191. if ( $this->is_translated_post_type( $ptype ) ) {
  4192. $none_translated = false;
  4193. }
  4194. }
  4195. if ( $none_translated ) {
  4196. return $where;
  4197. }
  4198. } else {
  4199. if ( !$this->is_translated_post_type( $post_type ) && 'any' != $post_type ) {
  4200. return $where;
  4201. }
  4202. }
  4203. $attachment_is_translatable = $sitepress->is_translated_post_type( 'attachment' );
  4204. if ( ($pagenow == 'upload.php' || $pagenow == 'media-upload.php' || $query->is_attachment()) && !$attachment_is_translatable) {
  4205. return $where;
  4206. }
  4207. $current_language = $sitepress->get_current_language();
  4208. $requested_id = false;
  4209. // Fix for when $sitepress->get_current_language() does not return the correct value (e.g. when request is made for an attachment, an iframe or an ajax call)
  4210. if ( isset( $_REQUEST[ 'attachment_id' ] ) && $_REQUEST[ 'attachment_id' ] ) {
  4211. $requested_id = $_REQUEST[ 'attachment_id' ];
  4212. }
  4213. if ( isset( $_REQUEST[ 'post_id' ] ) && $_REQUEST[ 'post_id' ] ) {
  4214. $requested_id = $_REQUEST[ 'post_id' ];
  4215. }
  4216. if($requested_id) {
  4217. $post_type = get_post_type( $requested_id );
  4218. $current_language = $sitepress->get_language_for_element( $requested_id, 'post_' . $post_type );
  4219. if(!$current_language) {
  4220. $current_language = $sitepress->get_current_language();
  4221. }
  4222. }
  4223. if ( 'all' != $this->this_lang ) {
  4224. if ( 'any' == $post_type ) {
  4225. $condition = " AND (t.language_code='" . esc_sql( $current_language ) . "' OR t.language_code IS NULL )";
  4226. } else {
  4227. $condition = " AND t.language_code='" . esc_sql( $current_language ) . "'";
  4228. }
  4229. } else {
  4230. $condition = '';
  4231. }
  4232. $where .= $condition;
  4233. return $where;
  4234. }
  4235. function comment_feed_join( $join )
  4236. {
  4237. global $wpdb, $wp_query;
  4238. $type = $wp_query->query_vars[ 'post_type' ] ? esc_sql( $wp_query->query_vars[ 'post_type' ] ) : 'post';
  4239. $wp_query->query_vars[ 'is_comment_feed' ] = true;
  4240. $join .= " JOIN {$wpdb->prefix}icl_translations t ON {$wpdb->comments}.comment_post_ID = t.element_id
  4241. AND t.element_type='post_{$type}' AND t.language_code='" . esc_sql( $this->this_lang ) . "'";
  4242. return $join;
  4243. }
  4244. function comments_clauses( $clauses, $obj )
  4245. {
  4246. global $wpdb;
  4247. if ( isset( $obj->query_vars[ 'post_id' ] ) ) {
  4248. $post_id = $obj->query_vars[ 'post_id' ];
  4249. } elseif ( isset( $obj->query_vars[ 'post_ID' ] ) ) {
  4250. $post_id = $obj->query_vars[ 'post_ID' ];
  4251. }
  4252. if ( !empty( $post_id ) ) {
  4253. $post = get_post( $post_id );
  4254. if ( !$this->is_translated_post_type( $post->post_type ) ) {
  4255. return $clauses;
  4256. }
  4257. }
  4258. $current_language = $this->get_current_language();
  4259. if ( $current_language != 'all' ) {
  4260. $clauses[ 'join' ] .= " JOIN {$wpdb->prefix}icl_translations icltr1 ON
  4261. icltr1.element_id = {$wpdb->comments}.comment_ID
  4262. JOIN {$wpdb->prefix}icl_translations icltr2 ON
  4263. icltr2.element_id = {$wpdb->comments}.comment_post_ID
  4264. ";
  4265. $clauses[ 'where' ] .= " AND icltr1.element_type = 'comment'
  4266. AND icltr1.language_code = '" . $current_language . "'
  4267. AND icltr2.language_code = '" . $current_language . "'
  4268. AND icltr2.element_type LIKE 'post\\_%' ";
  4269. }
  4270. return $clauses;
  4271. }
  4272. function language_filter()
  4273. {
  4274. require_once ICL_PLUGIN_PATH . '/inc/cache.php';
  4275. global $wpdb;
  4276. $type = isset( $_GET[ 'post_type' ] ) ? $_GET[ 'post_type' ] : 'post';
  4277. if ( !in_array( $type, array( 'post', 'page' ) ) && !in_array( $type, array_keys( $this->get_translatable_documents() ) ) ) {
  4278. return;
  4279. }
  4280. $active_languages = $this->get_active_languages();
  4281. $post_status = get_query_var( 'post_status' );
  4282. if ( is_string( $post_status ) ) {
  4283. $post_status = $post_status ? array( $post_status ) : array();
  4284. }
  4285. $key = join( ',', $post_status );
  4286. if ( $key ) {
  4287. $key = '#' . $key;
  4288. }
  4289. $languages = icl_cache_get( $type . 's_per_language' . $key );
  4290. if ( !$languages ) {
  4291. $extra_conditions = "";
  4292. if ( $post_status ) {
  4293. $extra_conditions .= apply_filters( '_icl_posts_language_count_status', " AND post_status IN('" . join( "','", $post_status ) . "') " );
  4294. }
  4295. if ( $post_status != array( 'trash' ) ) {
  4296. $extra_conditions .= " AND post_status <> 'trash'";
  4297. }
  4298. // dont count auto drafts
  4299. $extra_conditions .= " AND post_status <> 'auto-draft'";
  4300. // only active language
  4301. $extra_conditions .= " AND t.language_code IN ('" . join( "','", array_keys( $active_languages ) ) . "') ";
  4302. $res = $wpdb->get_results( "
  4303. SELECT language_code, COUNT(p.ID) AS c FROM {$wpdb->prefix}icl_translations t
  4304. JOIN {$wpdb->posts} p ON t.element_id=p.ID
  4305. JOIN {$wpdb->prefix}icl_languages l ON t.language_code=l.code AND l.active = 1
  4306. WHERE p.post_type='{$type}' AND t.element_type='post_{$type}' {$extra_conditions}
  4307. GROUP BY language_code
  4308. " );
  4309. $languages[ 'all' ] = 0;
  4310. foreach ( $res as $r ) {
  4311. $languages[ $r->language_code ] = $r->c;
  4312. $languages[ 'all' ] += $r->c;
  4313. }
  4314. icl_cache_set( $type . 's_per_language' . $key, $languages );
  4315. }
  4316. $active_languages[ ] = array( 'code' => 'all', 'display_name' => __( 'All languages', 'sitepress' ) );
  4317. $as = array();
  4318. foreach ( $active_languages as $lang ) {
  4319. if ( $lang[ 'code' ] == $this->this_lang ) {
  4320. $px = '<strong>';
  4321. $sx = ' <span class="count">(' . @intval( $languages[ $lang[ 'code' ] ] ) . ')<\/span><\/strong>';
  4322. } elseif ( !isset( $languages[ $lang[ 'code' ] ] ) ) {
  4323. $px = '<span>';
  4324. $sx = '<\/span>';
  4325. } else {
  4326. if ( $post_status ) {
  4327. $px = '<a href="?post_type=' . $type . '&post_status=' . join( ',', $post_status ) . '&lang=' . $lang[ 'code' ] . '">';
  4328. } else {
  4329. $px = '<a href="?post_type=' . $type . '&lang=' . $lang[ 'code' ] . '">';
  4330. }
  4331. $sx = '<\/a> <span class="count">(' . intval( $languages[ $lang[ 'code' ] ] ) . ')<\/span>';
  4332. }
  4333. $as[ ] = $px . $lang[ 'display_name' ] . $sx;
  4334. }
  4335. $allas = join( ' | ', $as );
  4336. if ( empty( $this->settings[ 'hide_how_to_translate' ] ) && $type == 'page' && !$this->get_icl_translation_enabled() ) {
  4337. $prot_link = '<span id="icl_how_to_translate_link" class="button" style="padding-right:3px;" ><img align="baseline" src="' . ICL_PLUGIN_URL . '/res/img/icon16.png" width="16" height="16" style="margin-bottom:-4px" /> <a href="https://wpml.org/?page_id=3416">' . __( 'How to translate', 'sitepress' ) . '</a><a href="#" title="' . esc_attr__( 'hide this', 'sitepress' ) . '" onclick=" if(confirm(\\\'' . __( 'Are you sure you want to remove this button?', 'sitepress' ) . '\\\')) jQuery.ajax({url:icl_ajx_url,type:\\\'POST\\\',data:{icl_ajx_action:\\\'update_option\\\', option:\\\'hide_how_to_translate\\\',value:1,_icl_nonce:\\\'' . wp_create_nonce( 'update_option_nonce' ) . '\\\'},success:function(){jQuery(\\\'#icl_how_to_translate_link\\\').fadeOut()}});return false;" style="outline:none;"><img src="' . ICL_PLUGIN_URL . '/res/img/close2.png" width="10" height="10" style="border:none" alt="' . esc_attr__( 'hide', 'sitepress' ) . '" /><\/a>' . '<\/span>';
  4338. } else {
  4339. $prot_link = '';
  4340. }
  4341. ?>
  4342. <script type="text/javascript">
  4343. jQuery(".subsubsub").append('<br /><span id="icl_subsubsub"><?php echo $allas ?></span><br /><?php echo $prot_link ?>');
  4344. </script>
  4345. <?php
  4346. }
  4347. function exclude_other_language_pages2( $arr )
  4348. {
  4349. $new_arr = $arr;
  4350. $current_language = $this->get_current_language();
  4351. if ( $current_language != 'all' ) {
  4352. $cache_key = md5(json_encode($new_arr));
  4353. $cache_group = 'exclude_other_language_pages2';
  4354. $found = false;
  4355. $result = wp_cache_get($cache_key, $cache_group, false, $found);
  4356. if(!$found) {
  4357. global $wpdb;
  4358. if ( is_array( $new_arr ) && !empty( $new_arr[ 0 ]->post_type ) ) {
  4359. $post_type = $new_arr[ 0 ]->post_type;
  4360. } else {
  4361. $post_type = 'page';
  4362. }
  4363. $filtered_pages = array();
  4364. // grab list of pages NOT in the current language
  4365. $excl_pages = $wpdb->get_col( $wpdb->prepare( "
  4366. SELECT p.ID FROM {$wpdb->posts} p
  4367. JOIN {$wpdb->prefix}icl_translations t ON p.ID = t.element_id
  4368. WHERE t.element_type=%s AND p.post_type=%s AND t.language_code <> %s
  4369. ", 'post_' . $post_type, $post_type, $current_language ) );
  4370. // exclude them from the result set
  4371. if ( !empty( $new_arr ) ) {
  4372. foreach ( $new_arr as $page ) {
  4373. if ( !in_array( $page->ID, $excl_pages ) ) {
  4374. $filtered_pages[ ] = $page;
  4375. }
  4376. }
  4377. $new_arr = $filtered_pages;
  4378. }
  4379. wp_cache_set($cache_key, $new_arr, $cache_group);
  4380. } else {
  4381. $new_arr = $result;
  4382. }
  4383. }
  4384. return $new_arr;
  4385. }
  4386. function wp_dropdown_pages( $output )
  4387. {
  4388. global $wpdb;
  4389. if ( isset( $_POST[ 'lang_switch' ] ) ) {
  4390. $post_id = esc_sql( $_POST[ 'lang_switch' ] );
  4391. $lang = esc_sql( strip_tags( $_GET[ 'lang' ] ) );
  4392. $parent = $wpdb->get_var( $wpdb->prepare( "SELECT post_parent FROM {$wpdb->posts} WHERE ID=%d", $post_id ) );
  4393. if ( $parent ) {
  4394. $trid = $wpdb->get_var( "SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id='{$parent}' AND element_type='post_page'" );
  4395. $translated_parent_id = $wpdb->get_var( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid='{$trid}' AND element_type='post_page' AND language_code='{$lang}'" );
  4396. if ( $translated_parent_id ) {
  4397. $output = str_replace( 'selected="selected"', '', $output );
  4398. $output = str_replace( 'value="' . $translated_parent_id . '"', 'value="' . $translated_parent_id . '" selected="selected"', $output );
  4399. }
  4400. }
  4401. } elseif ( isset( $_GET[ 'lang' ] ) && isset( $_GET[ 'trid' ] ) ) {
  4402. $lang = esc_sql( strip_tags( $_GET[ 'lang' ] ) );
  4403. $trid = esc_sql( $_GET[ 'trid' ] );
  4404. $post_type = isset( $_GET[ 'post_type' ] ) ? $_GET[ 'post_type' ] : 'page';
  4405. $elements_id = $wpdb->get_col( $wpdb->prepare( "SELECT element_id FROM {$wpdb->prefix}icl_translations
  4406. WHERE trid=%d AND element_type=%s AND element_id IS NOT NULL", $trid, 'post_' . $post_type ) );
  4407. $translated_parent_id = 0;
  4408. foreach ( $elements_id as $element_id ) {
  4409. $parent = $wpdb->get_var( $wpdb->prepare( "SELECT post_parent FROM {$wpdb->posts} WHERE ID=%d", $element_id ) );
  4410. $trid = $wpdb->get_var( $wpdb->prepare( "
  4411. SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id=%d AND element_type=%s", $parent, 'post_' . $post_type ) );
  4412. $translated_parent_id = $wpdb->get_var( $wpdb->prepare( "
  4413. SELECT element_id FROM {$wpdb->prefix}icl_translations
  4414. WHERE trid=%d AND element_type=%s AND language_code=%s", $trid, 'post_' . $post_type, $lang ) );
  4415. if ( $translated_parent_id ) {
  4416. break;
  4417. }
  4418. }
  4419. if ( $translated_parent_id ) {
  4420. $output = str_replace( 'selected="selected"', '', $output );
  4421. $output = str_replace( 'value="' . $translated_parent_id . '"', 'value="' . $translated_parent_id . '" selected="selected"', $output );
  4422. }
  4423. }
  4424. if ( !$output ) {
  4425. $output = '<select id="parent_id"><option value="">' . __( 'Main Page (no parent)', 'sitepress' ) . '</option></select>';
  4426. }
  4427. return $output;
  4428. }
  4429. function edit_term_form( $term )
  4430. {
  4431. include ICL_PLUGIN_PATH . '/menu/taxonomy-menu.php';
  4432. }
  4433. function wp_dropdown_cats_select_parent( $html )
  4434. {
  4435. global $wpdb;
  4436. if ( isset( $_GET[ 'trid' ] ) ) {
  4437. $element_type = $taxonomy = isset( $_GET[ 'taxonomy' ] ) ? $_GET[ 'taxonomy' ] : 'post_tag';
  4438. $icl_element_type = 'tax_' . $element_type;
  4439. $trid = intval( $_GET[ 'trid' ] );
  4440. $source_lang = isset( $_GET[ 'source_lang' ] ) ? $_GET[ 'source_lang' ] : $this->get_default_language();
  4441. $parent = $wpdb->get_var( "
  4442. SELECT parent
  4443. FROM {$wpdb->term_taxonomy} tt
  4444. JOIN {$wpdb->prefix}icl_translations tr ON tr.element_id=tt.term_taxonomy_id
  4445. AND tr.element_type='{$icl_element_type}' AND tt.taxonomy='{$taxonomy}'
  4446. WHERE trid='{$trid}' AND tr.language_code='{$source_lang}'
  4447. " );
  4448. if ( $parent ) {
  4449. $parent = icl_object_id( $parent, $element_type );
  4450. $html = str_replace( 'value="' . $parent . '"', 'value="' . $parent . '" selected="selected"', $html );
  4451. }
  4452. }
  4453. return $html;
  4454. }
  4455. function add_language_selector_to_page( $active_languages, $selected_language, $translations, $element_id, $type )
  4456. {
  4457. ?>
  4458. <div id="icl_tax_menu" style="display:none">
  4459. <div id="dashboard-widgets" class="metabox-holder">
  4460. <div class="postbox-container" style="width: 99%;line-height:normal;">
  4461. <div id="icl_<?php echo $type ?>_lang" class="postbox" style="line-height:normal;">
  4462. <h3 class="hndle">
  4463. <span><?php echo __( 'Language', 'sitepress' ) ?></span>
  4464. </h3>
  4465. <div class="inside" style="padding: 10px;">
  4466. <select name="icl_<?php echo $type ?>_language">
  4467. <?php
  4468. foreach ( $active_languages as $lang ) {
  4469. if ( $lang[ 'code' ] == $selected_language ) {
  4470. ?>
  4471. <option value="<?php echo $selected_language ?>" selected="selected"><?php echo $lang[ 'display_name' ] ?></option>
  4472. <?php
  4473. }
  4474. }
  4475. ?>
  4476. <?php foreach ( $active_languages as $lang ): ?>
  4477. <?php if ( $lang[ 'code' ] == $selected_language || ( isset( $translations[ $lang[ 'code' ] ]->element_id ) && $translations[ $lang[ 'code' ] ]->element_id != $element_id ) ) {
  4478. continue;
  4479. } ?>
  4480. <option value="<?php echo $lang[ 'code' ] ?>"<?php if ( $selected_language == $lang[ 'code' ] ): ?> selected="selected"<?php endif; ?>><?php echo $lang[ 'display_name' ] ?></option>
  4481. <?php endforeach; ?>
  4482. </select>
  4483. <?php
  4484. }
  4485. function get_category_name( $id )
  4486. {
  4487. _deprecated_function( __FUNCTION__, '2.3.1', 'get_cat_name()' );
  4488. global $wpdb;
  4489. $term_id = $wpdb->get_var( "SELECT term_id FROM {$wpdb->prefix}term_taxonomy WHERE term_taxonomy_id = {$id}" );
  4490. if ( $term_id ) {
  4491. return $wpdb->get_var( "SELECT name FROM {$wpdb->prefix}terms WHERE term_id = {$term_id}" );
  4492. } else {
  4493. return null;
  4494. }
  4495. }
  4496. function add_translation_of_selector_to_page( $trid, $selected_language, $default_language, $source_language, $untranslated_ids, $element_id, $type )
  4497. {
  4498. global $wpdb;
  4499. ?>
  4500. <input type="hidden" name="icl_trid" value="<?php echo $trid ?>"/>
  4501. <?php
  4502. if ( $selected_language != $default_language && 'all' != $this->get_current_language() ) {
  4503. ?>
  4504. <br/><br/>
  4505. <?php echo __( 'This is a translation of', 'sitepress' ); ?><br/>
  4506. <select name="icl_translation_of" id="icl_translation_of"<?php if ( ( !isset( $_GET[ 'action' ] ) || $_GET[ 'action' ] != 'edit' ) && $trid ) {
  4507. echo " disabled";
  4508. } ?>>
  4509. <?php
  4510. if ( !$source_language || $source_language == $default_language ) {
  4511. if ( $trid ) {
  4512. ?>
  4513. <option value="none"><?php echo __( '--None--', 'sitepress' ) ?></option>
  4514. <?php
  4515. //get source
  4516. $src_language_id = $wpdb->get_var( $wpdb->prepare( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND language_code=%s", $trid, $default_language ) );
  4517. if ( !$src_language_id ) {
  4518. // select the first id found for this trid
  4519. $src_language_id = $wpdb->get_var( $wpdb->prepare( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid=%d", $trid ) );
  4520. }
  4521. if ( $src_language_id && $src_language_id != $element_id ) {
  4522. $term_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id=%d", $src_language_id ) );
  4523. $src_language_title = $wpdb->get_var( $wpdb->prepare( "SELECT name FROM {$wpdb->terms} WHERE term_id=%d", $term_id ) );
  4524. }
  4525. if ( !empty( $src_language_title ) ) {
  4526. ?>
  4527. <option value="<?php echo $src_language_id; ?>" selected="selected"><?php echo $src_language_title; ?></option>
  4528. <?php
  4529. }
  4530. } else {
  4531. ?>
  4532. <option value="none" selected="selected"><?php echo __( '--None--', 'sitepress' ); ?></option>
  4533. <?php
  4534. }
  4535. foreach ( $untranslated_ids as $translation_of_id ) {
  4536. $title = $wpdb->get_var( $wpdb->prepare( "SELECT name FROM {$wpdb->terms} WHERE term_id=%d", $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id=%d", $translation_of_id ) ) ) );
  4537. if ( !empty( $title ) ) {
  4538. ?>
  4539. <option value="<?php echo $translation_of_id; ?>"><?php echo $title; ?></option>
  4540. <?php
  4541. }
  4542. }
  4543. } else {
  4544. if ( $trid ) {
  4545. $src_language_title = false;
  4546. // add the source language
  4547. $src_language_id = $wpdb->get_var( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE trid={$trid} AND language_code='{$source_language}'" );
  4548. if ( $src_language_id ) {
  4549. $term_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id=%d", $src_language_id ) );
  4550. $src_language_title = $wpdb->get_var( $wpdb->prepare( "SELECT name FROM {$wpdb->terms} WHERE term_id=%d", $term_id ) );
  4551. }
  4552. if ( $src_language_title ) {
  4553. ?>
  4554. <option value="<?php echo $src_language_id; ?>" selected="selected"><?php echo $src_language_title; ?></option>
  4555. <?php
  4556. }
  4557. } else {
  4558. ?>
  4559. <option value="none" selected="selected"><?php echo __( '--None--', 'sitepress' ); ?></option>
  4560. <?php
  4561. }
  4562. }
  4563. ?>
  4564. </select>
  4565. <?php
  4566. }
  4567. }
  4568. function add_translate_options( $trid, $active_languages, $selected_language, $translations, $type )
  4569. {
  4570. if ( $trid && isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'edit' ):
  4571. ?>
  4572. <div id="icl_translate_options">
  4573. <?php
  4574. // count number of translated and un-translated pages.
  4575. $translations_found = 0;
  4576. $untranslated_found = 0;
  4577. foreach ( $active_languages as $lang ) {
  4578. if ( $selected_language == $lang[ 'code' ] ) {
  4579. continue;
  4580. }
  4581. if ( isset( $translations[ $lang[ 'code' ] ]->element_id ) ) {
  4582. $translations_found += 1;
  4583. } else {
  4584. $untranslated_found += 1;
  4585. }
  4586. }
  4587. ?>
  4588. <?php if ( $untranslated_found > 0 ): ?>
  4589. <table cellspacing="1" class="icl_translations_table" style="min-width:200px;margin-top:10px;">
  4590. <thead>
  4591. <tr>
  4592. <th colspan="2" style="padding:4px;background-color:#DFDFDF"><b><?php _e( 'Translate', 'sitepress' ); ?></b></th>
  4593. </tr>
  4594. </thead>
  4595. <tbody>
  4596. <?php foreach ( $active_languages as $lang ): if ( $selected_language == $lang[ 'code' ] ) {
  4597. continue;
  4598. } ?>
  4599. <tr>
  4600. <?php if ( !isset( $translations[ $lang[ 'code' ] ]->element_id ) ): ?>
  4601. <td style="padding:4px;line-height:normal;"><?php echo $lang[ 'display_name' ] ?></td>
  4602. <?php
  4603. $taxonomy = $_GET[ 'taxonomy' ];
  4604. $post_type_q = isset( $_GET[ 'post_type' ] ) ? '&amp;post_type=' . esc_html( $_GET[ 'post_type' ] ) : '';
  4605. $add_link = admin_url( "edit-tags.php?taxonomy=" . esc_html( $taxonomy ) . "&amp;trid=" . $trid . "&amp;lang=" . $lang[ 'code' ] . "&amp;source_lang=" . $selected_language . $post_type_q );
  4606. ?>
  4607. <td style="padding:4px;line-height:normal;"><a href="<?php echo $add_link ?>"><?php echo __( 'add', 'sitepress' ) ?></a></td>
  4608. <?php endif; ?>
  4609. </tr>
  4610. <?php endforeach; ?>
  4611. </tbody>
  4612. </table>
  4613. <?php endif; ?>
  4614. <?php if ( $translations_found > 0 ): ?>
  4615. <p style="clear:both;margin:5px 0 5px 0">
  4616. <b><?php _e( 'Translations', 'sitepress' ) ?></b>
  4617. (<a class="icl_toggle_show_translations" href="#" <?php if (empty( $this->settings[ 'show_translations_flag' ] )): ?>style="display:none;"<?php endif; ?>><?php _e( 'hide', 'sitepress' ) ?></a><a
  4618. class="icl_toggle_show_translations" href="#" <?php if (!empty( $this->settings[ 'show_translations_flag' ] )): ?>style="display:none;"<?php endif; ?>><?php _e( 'show', 'sitepress' ) ?></a>)
  4619. <?php wp_nonce_field( 'toggle_show_translations_nonce', '_icl_nonce_tst' ) ?>
  4620. <table cellspacing="1" width="100%" id="icl_translations_table" style="<?php if ( empty( $this->settings[ 'show_translations_flag' ] ) ): ?>display:none;<?php endif; ?>margin-left:0;">
  4621. <?php foreach ( $active_languages as $lang ): if ( $selected_language == $lang[ 'code' ] )
  4622. continue; ?>
  4623. <tr>
  4624. <?php if ( isset( $translations[ $lang[ 'code' ] ]->element_id ) ): ?>
  4625. <td style="line-height:normal;"><?php echo $lang[ 'display_name' ] ?></td>
  4626. <?php
  4627. $taxonomy = $_GET[ 'taxonomy' ];
  4628. $post_type_q = isset( $_GET[ 'post_type' ] ) ? '&amp;post_type=' . esc_html( $_GET[ 'post_type' ] ) : '';
  4629. $edit_link = admin_url( "edit-tags.php?taxonomy=" . esc_html( $taxonomy ) . "&amp;action=edit&amp;tag_ID=" . $translations[ $lang[ 'code' ] ]->term_id . "&amp;lang=" . $lang[ 'code' ] . $post_type_q );
  4630. ?>
  4631. <td align="right" width="30%"
  4632. style="line-height:normal;"><?php echo isset( $translations[ $lang[ 'code' ] ]->name ) ? '<a href="' . $edit_link . '" title="' . __( 'Edit', 'sitepress' ) . '">' . $translations[ $lang[ 'code' ] ]->name . '</a>' : __( 'n/a', 'sitepress' ) ?></td>
  4633. <?php endif; ?>
  4634. </tr>
  4635. <?php endforeach; ?>
  4636. </table>
  4637. <?php endif; ?>
  4638. <br clear="all" style="line-height:1px;"/>
  4639. </div>
  4640. <?php
  4641. endif;
  4642. }
  4643. function create_term( $cat_id, $tt_id )
  4644. {
  4645. global $wpdb, $wp_taxonomies;
  4646. $default_language = $this->get_default_language();
  4647. // case of ajax inline category creation
  4648. // ajax actions
  4649. $ajx_actions = array();
  4650. foreach ( $wp_taxonomies as $ktx => $tx ) {
  4651. $ajx_actions[ ] = 'add-' . $ktx;
  4652. }
  4653. if ( isset( $_POST[ '_ajax_nonce' ] ) && in_array( $_POST[ 'action' ], $ajx_actions ) ) {
  4654. $referer = $_SERVER[ 'HTTP_REFERER' ];
  4655. $url_pieces = parse_url( $referer );
  4656. @parse_str( $url_pieces[ 'query' ], $qvars );
  4657. if ( !empty( $qvars[ 'post' ] ) ) {
  4658. $post_type = $wpdb->get_var( $wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE ID = %d", $qvars[ 'post' ] ) );
  4659. $term_lang = $qvars[ 'lang' ];
  4660. if($this->is_translated_post_type($post_type)) {
  4661. $lang_details = $this->get_element_language_details( $qvars[ 'post' ], 'post_' . $post_type );
  4662. if(isset($lang_details->language_code)) {
  4663. $term_lang = $lang_details->language_code;
  4664. }
  4665. }
  4666. } else {
  4667. $term_lang = isset( $qvars[ 'lang' ] ) ? $qvars[ 'lang' ] : $this->get_language_cookie();
  4668. }
  4669. }
  4670. $el_type = $wpdb->get_var( "SELECT taxonomy FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id={$tt_id}" );
  4671. if ( !$this->is_translated_taxonomy( $el_type ) ) {
  4672. return;
  4673. };
  4674. $icl_el_type = 'tax_' . $el_type;
  4675. // case of adding a tag via post save
  4676. $post_action = isset( $_POST[ 'action' ] ) ? $_POST[ 'action' ] : false;
  4677. if ( $post_action == 'editpost' && !empty( $_POST[ 'icl_post_language' ] ) ) {
  4678. $term_lang = $_POST[ 'icl_post_language' ];
  4679. } elseif ( $post_action == 'post-quickpress-publish' ) {
  4680. $term_lang = $default_language;
  4681. } elseif ( $post_action == 'inline-save-tax' ) {
  4682. $lang_details = $this->get_element_language_details( $tt_id, $icl_el_type );
  4683. $term_lang = $lang_details->language_code;
  4684. } elseif ( $post_action == 'inline-save' ) {
  4685. $post_type = $wpdb->get_var( "SELECT post_type FROM {$wpdb->posts} WHERE ID=" . $_POST[ 'post_ID' ] );
  4686. $lang_details = $this->get_element_language_details( $_POST[ 'post_ID' ], 'post_' . $post_type );
  4687. $term_lang = $lang_details->language_code;
  4688. }
  4689. // has trid only when it's a translation of another tag
  4690. $trid = isset( $_POST[ 'icl_trid' ] ) && ( isset( $_POST[ 'icl_' . $icl_el_type . '_language' ] ) ) ? $_POST[ 'icl_trid' ] : null;
  4691. // see if we have a "translation of" setting.
  4692. $src_language = false;
  4693. if ( isset( $_POST[ 'icl_translation_of' ] ) && $_POST[ 'icl_translation_of' ] ) {
  4694. $src_term_id = $_POST[ 'icl_translation_of' ];
  4695. $trid = $this->get_element_trid( $src_term_id, $icl_el_type );
  4696. if ( $src_term_id != 'none' && $trid ) {
  4697. $language_details = $this->get_element_language_details( $trid, $icl_el_type );
  4698. if ( empty( $language_details ) || !is_object( $language_details ) || !isset( $language_details->source_language_code ) ) {
  4699. $src_language = null;
  4700. } else {
  4701. $src_language = $language_details->source_language_code;
  4702. }
  4703. } else {
  4704. $trid = null;
  4705. }
  4706. }
  4707. if ( !isset( $term_lang ) ) {
  4708. $term_lang = isset( $_POST[ 'icl_' . $icl_el_type . '_language' ] ) ? $_POST[ 'icl_' . $icl_el_type . '_language' ] : $this->this_lang;
  4709. }
  4710. if ( $post_action == 'inline-save-tax' || $post_action == 'add-' . $el_type ) {
  4711. $trid = $this->get_element_trid( $tt_id, $icl_el_type );
  4712. }
  4713. // set term language if front-end translation creating
  4714. $term_lang = apply_filters( 'wpml_create_term_lang', $term_lang );
  4715. $this->set_element_language_details( $tt_id, $icl_el_type, $trid, $term_lang, $src_language );
  4716. // sync translations parent
  4717. if ( $this->settings[ 'sync_taxonomy_parents' ] && isset( $_POST[ 'parent' ] ) && $term_lang == $default_language ) {
  4718. $parent = intval( $_POST[ 'parent' ] );
  4719. $translations = $this->get_element_translations( $trid, $icl_el_type );
  4720. $taxonomy = isset($_POST[ 'taxonomy' ]) ? $_POST[ 'taxonomy' ] : false;
  4721. foreach ( $translations as $lang => $translation ) {
  4722. if ( $lang != $default_language ) {
  4723. $translated_parent = false;
  4724. //check for translation only if we know the id
  4725. if ( $parent > 0 ) {
  4726. $translated_parent = icl_object_id( $parent, $el_type, false, $lang );
  4727. }
  4728. //update information about parent only if translation exists or we are setting parent to None
  4729. if ( $parent == 0 || $parent == -1 || $translated_parent != $parent ) {
  4730. $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => $translated_parent ), array( 'term_taxonomy_id' => $translation->element_id ) );
  4731. }
  4732. }
  4733. }
  4734. $this->update_terms_relationship_cache( $parent, $taxonomy );
  4735. }
  4736. }
  4737. function get_language_for_term( $term_id, $el_type )
  4738. {
  4739. global $wpdb;
  4740. $term_id = $wpdb->get_var( "SELECT term_taxonomy_id FROM {$wpdb->prefix}term_taxonomy WHERE term_id = {$term_id}" );
  4741. if ( $term_id ) {
  4742. return $wpdb->get_var( "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id = {$term_id} AND element_type = '{$el_type}'" );
  4743. } else {
  4744. return $this->get_default_language();
  4745. }
  4746. }
  4747. function pre_term_name( $value, $taxonomy )
  4748. {
  4749. //allow adding terms with the same name in different languages
  4750. global $wpdb;
  4751. //check if term exists
  4752. $term_id = $wpdb->get_var( "SELECT term_id FROM {$wpdb->terms} WHERE name='" . esc_sql( $value ) . "'" );
  4753. // translate to WPML notation
  4754. $taxonomy = 'tax_' . $taxonomy;
  4755. if ( !empty( $term_id ) ) {
  4756. if ( isset( $_POST[ 'icl_' . $taxonomy . '_language' ] ) ) {
  4757. // see if the term_id is for a different language
  4758. $this_lang = $_POST[ 'icl_' . $taxonomy . '_language' ];
  4759. if ( $this_lang != $this->get_language_for_term( $term_id, $taxonomy ) ) {
  4760. if ( $this_lang != $this->get_default_language() ) {
  4761. $value .= ' @' . $_POST[ 'icl_' . $taxonomy . '_language' ];
  4762. }
  4763. }
  4764. }
  4765. }
  4766. return $value;
  4767. }
  4768. function pre_save_category()
  4769. {
  4770. // allow adding categories with the same name in different languages
  4771. if ( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'add-cat' ) {
  4772. if ( category_exists( $_POST[ 'cat_name' ] ) && isset( $_POST[ 'icl_category_language' ] ) && $_POST[ 'icl_category_language' ] != $this->get_default_language() ) {
  4773. $_POST[ 'cat_name' ] .= ' @' . $_POST[ 'icl_category_language' ];
  4774. }
  4775. }
  4776. }
  4777. function delete_term( $cat, $tt_id, $taxonomy )
  4778. {
  4779. global $wpdb;
  4780. $icl_el_type = 'tax_' . $taxonomy;
  4781. static $recursion;
  4782. if ( $this->settings[ 'sync_delete_tax' ] && empty( $recursion ) ) {
  4783. // only for translated
  4784. $lang_details = $this->get_element_language_details( $tt_id, $icl_el_type );
  4785. if ( empty( $lang_details->source_language_code ) ) {
  4786. // get translations
  4787. $trid = $this->get_element_trid( $tt_id, $icl_el_type );
  4788. $translations = $this->get_element_translations( $trid, $icl_el_type );
  4789. $recursion = true;
  4790. // delete translations
  4791. foreach ( $translations as $translation ) {
  4792. if ( $translation->element_id != $tt_id ) {
  4793. wp_delete_term( $translation->term_id, $taxonomy );
  4794. }
  4795. }
  4796. $recursion = false;
  4797. }
  4798. }
  4799. $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->prefix}icl_translations WHERE element_type =%s AND element_id=%d LIMIT 1", array($icl_el_type, $tt_id) ) );
  4800. }
  4801. function get_term_filter( $_term, $taxonomy )
  4802. {
  4803. // case of calling from get_category_parents
  4804. $debug_backtrace = $this->get_backtrace( 6 ); //Limit to first 6 stack frames, since 5 is the highest index we use
  4805. if ( isset( $debug_backtrace[ 5 ][ 'function' ] ) && $debug_backtrace[ 5 ][ 'function' ] == 'get_category_parents' ) {
  4806. $_term->name = $this->the_category_name_filter( $_term->name );
  4807. }
  4808. return $_term;
  4809. }
  4810. function terms_language_filter()
  4811. {
  4812. global $wpdb;
  4813. $taxonomy = isset( $_GET[ 'taxonomy' ] ) ? $_GET[ 'taxonomy' ] : 'post_tag';
  4814. $icl_element_type = 'tax_' . $taxonomy;
  4815. $active_languages = $this->get_active_languages();
  4816. $current_language=$this->get_current_language();
  4817. $default_language=$this->get_default_language();
  4818. $res_query = "
  4819. SELECT language_code, COUNT(tm.term_id) AS c FROM {$wpdb->prefix}icl_translations t
  4820. JOIN {$wpdb->term_taxonomy} tt ON t.element_id = tt.term_taxonomy_id
  4821. JOIN {$wpdb->terms} tm ON tt.term_id = tm.term_id
  4822. JOIN {$wpdb->prefix}icl_languages l ON t.language_code = l.code
  4823. WHERE t.element_type=%s AND tt.taxonomy=%s AND l.active=1
  4824. GROUP BY language_code
  4825. ";
  4826. $res_query_prepared = $wpdb->prepare($res_query, array($icl_element_type, $taxonomy) );
  4827. $res = $wpdb->get_results( $res_query_prepared );
  4828. $languages = array( 'all' => 0 );
  4829. foreach ( $res as $r ) {
  4830. $languages[ $r->language_code ] = $r->c;
  4831. $languages[ 'all' ] += $r->c;
  4832. }
  4833. $active_languages[ ] = array( 'code' => 'all', 'display_name' => __( 'All languages', 'sitepress' ) );
  4834. $languages_links = array();
  4835. foreach ( $active_languages as $lang ) {
  4836. if ( $lang[ 'code' ] == $this->this_lang ) {
  4837. $px = '<strong>';
  4838. $sx = ' (' . @intval( $languages[ $lang[ 'code' ] ] ) . ')<\/strong>';
  4839. /*
  4840. }elseif(!isset($langs[$lang['code']])){
  4841. $px = '<span>';
  4842. $sx = '<\/span>';
  4843. */
  4844. } else {
  4845. $px = '<a href="?taxonomy=' . $taxonomy . '&amp;lang=' . $lang[ 'code' ];
  4846. $px .= isset( $_GET[ 'post_type' ] ) ? '&amp;post_type=' . $_GET[ 'post_type' ] : '';
  4847. $px .= '">';
  4848. $sx = '<\/a> (' . @intval( $languages[ $lang[ 'code' ] ] ) . ')';
  4849. }
  4850. $languages_links[ ] = $px . $lang[ 'display_name' ] . $sx;
  4851. }
  4852. $all_languages_links = join( ' | ', $languages_links );
  4853. ?>
  4854. <script type="text/javascript">
  4855. jQuery('table.widefat').before('<span id="icl_subsubsub"><?php echo $all_languages_links ?><\/span>');
  4856. <?php // the search form, add language ?>
  4857. <?php if($current_language != $default_language): ?>
  4858. jQuery('.search-form').append('<input type="hidden" name="lang" value="<?php echo $current_language ?>" />');
  4859. <?php endif; ?>
  4860. </script>
  4861. <?php
  4862. }
  4863. function get_terms_args_filter( $args )
  4864. {
  4865. // Unique cache domain for each language.
  4866. if ( isset( $args[ 'cache_domain' ] ) ) {
  4867. $args[ 'cache_domain' ] .= '_' . $this->get_current_language();
  4868. }
  4869. // special case for when term hierarchy is cached in wp_options
  4870. $debug_backtrace = $this->get_backtrace( 5 ); //Limit to first 5 stack frames, since 4 is the highest index we use
  4871. if ( isset( $debug_backtrace[ 4 ] ) && $debug_backtrace[ 4 ][ 'function' ] == '_get_term_hierarchy' ) {
  4872. $args[ '_icl_show_all_langs' ] = true;
  4873. }
  4874. return $args;
  4875. }
  4876. function exclude_other_terms( $exclusions, $args )
  4877. {
  4878. if ( !version_compare( $GLOBALS[ 'wp_version' ], '3.1', '>=' ) ) {
  4879. $default_language = $this->get_default_language();
  4880. // special case for when term hierarchy is cached in wp_options
  4881. if ( isset( $args[ '_icl_show_all_langs' ] ) && $args[ '_icl_show_all_langs' ] )
  4882. return $exclusions;
  4883. // get_terms doesn't seem to have a filter that can be used efficiently in order to filter the terms by language
  4884. // in addition the taxonomy name is not being passed to this filter we're using 'list_terms_exclusions'
  4885. // getting the taxonomy name from debug_backtrace
  4886. global $wpdb, $pagenow;
  4887. $taxonomy = false;
  4888. if ( isset( $_GET[ 'taxonomy' ] ) ) {
  4889. $taxonomy = $_GET[ 'taxonomy' ];
  4890. } elseif ( isset( $args[ 'taxonomy' ] ) ) {
  4891. $taxonomy = $args[ 'taxonomy' ];
  4892. } elseif ( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'get-tagcloud' ) {
  4893. $taxonomy = $_POST[ 'tax' ];
  4894. } else {
  4895. if ( in_array( $pagenow, array( 'post-new.php', 'post.php', 'edit.php' ) ) ) {
  4896. $debug_backtrace = $this->get_backtrace( 4, false, true ); //Limit to first 4 stack frames, since 3 is the highest index we use
  4897. if ( isset( $debug_backtrace[ 3 ][ 'args' ][ 0 ] ) ) {
  4898. $taxonomy = $debug_backtrace[ 3 ][ 'args' ][ 0 ];
  4899. } else {
  4900. $taxonomy = 'post_tag';
  4901. }
  4902. }
  4903. }
  4904. if ( ! $taxonomy || ! $this->is_translated_taxonomy( $taxonomy ) ) {
  4905. return $exclusions;
  4906. }
  4907. $icl_element_type = 'tax_' . $taxonomy;
  4908. if ( isset( $_GET[ 'lang' ] ) && $_GET[ 'lang' ] == 'all' ) {
  4909. return $exclusions;
  4910. }
  4911. if ( isset( $_GET[ 'tag_ID' ] ) && $_GET[ 'tag_ID' ] ) {
  4912. $element_lang_details = $this->get_element_language_details( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy=%s", $_GET[ 'tag_ID' ], $taxonomy ) ), $icl_element_type );
  4913. $this_lang = $element_lang_details->language_code;
  4914. } elseif ( $this->this_lang != $default_language ) {
  4915. $this_lang = $this->get_current_language();
  4916. } elseif ( isset( $_GET[ 'post' ] ) ) {
  4917. $icl_post_type = isset( $_GET[ 'post_type' ] ) ? 'post_' . $_GET[ 'post_type' ] : 'post_' . $wpdb->get_var( $wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE ID = %d", $_GET[ 'post' ] ) );
  4918. $element_lang_details = $this->get_element_language_details( $_GET[ 'post' ], $icl_post_type );
  4919. $this_lang = $element_lang_details ? $element_lang_details->language_code : $default_language;
  4920. } elseif ( isset( $_POST[ 'action' ] ) && ( $_POST[ 'action' ] == 'get-tagcloud' || $_POST[ 'action' ] == 'menu-quick-search' ) ) {
  4921. if ( ! isset( $_SERVER[ 'HTTP_REFERER' ] ) ) {
  4922. $this_lang = $default_language;
  4923. } else {
  4924. $urlparts = parse_url( $_SERVER[ 'HTTP_REFERER' ] );
  4925. @parse_str( $urlparts[ 'query' ], $qvars );
  4926. $this_lang = isset( $qvars[ 'lang' ] ) ? $qvars[ 'lang' ] : $default_language;
  4927. }
  4928. } else {
  4929. $this_lang = $default_language;
  4930. }
  4931. $exclude = $wpdb->get_col( "
  4932. SELECT tt.term_taxonomy_id FROM {$wpdb->term_taxonomy} tt
  4933. LEFT JOIN {$wpdb->terms} tm ON tt.term_id = tm.term_id
  4934. LEFT JOIN {$wpdb->prefix}icl_translations t ON (tt.term_taxonomy_id = t.element_id OR t.element_id IS NULL)
  4935. WHERE tt.taxonomy='{$taxonomy}' AND t.element_type='{$icl_element_type}' AND t.language_code <> '{$this_lang}'
  4936. " );
  4937. $exclude[ ] = 0;
  4938. $exclusions .= ' AND tt.term_taxonomy_id NOT IN (' . join( ',', $exclude ) . ')';
  4939. }
  4940. return $exclusions;
  4941. }
  4942. function terms_clauses( $clauses, $taxonomies, $args )
  4943. {
  4944. if ( version_compare( $GLOBALS[ 'wp_version' ], '3.1', '>=' ) ) {
  4945. global $wpdb;
  4946. // special case for when term hierarchy is cached in wp_options
  4947. $debug_backtrace = $this->get_backtrace( 6 ); //Limit to first 5 stack frames, since 4 is the highest index we use
  4948. if ( isset( $debug_backtrace[ 4 ] ) && $debug_backtrace[ 4 ][ 'function' ] == '_get_term_hierarchy' ) {
  4949. return $clauses;
  4950. }
  4951. //Todo: to remove: this is for debug purposes and only temporary
  4952. if(defined('WP_DEBUG') && WP_DEBUG===true) {
  4953. $wp_upload_dir = wp_upload_dir();
  4954. $icl_log_file = $wp_upload_dir['basedir'] . "/wpml.debug.txt";
  4955. foreach($debug_backtrace as $index => $data) {
  4956. if($index!=4 && $debug_backtrace[ $index ][ 'function' ] == '_get_term_hierarchy') {
  4957. file_put_contents($icl_log_file, '_get_term_hierarchy found on position ' . $index . PHP_EOL, FILE_APPEND);
  4958. file_put_contents($icl_log_file, 'Stack: ' . print_r($debug_backtrace, true) . PHP_EOL, FILE_APPEND);
  4959. }
  4960. }
  4961. }
  4962. $int = preg_match( '#tt\.taxonomy IN \(([^\)]+)\)#', $clauses[ 'where' ], $matches );
  4963. $left_join = '';
  4964. $icl_taxonomies = array();
  4965. if ( $int ) {
  4966. $exp = explode( ',', $matches[ 1 ] );
  4967. foreach ( $exp as $v ) {
  4968. $tax = trim( $v, ' \'' );
  4969. if ( $this->is_translated_taxonomy( $tax ) ) {
  4970. $icl_taxonomies[ ] = 'tax_' . $tax;
  4971. } else {
  4972. $left_join = ' LEFT';
  4973. }
  4974. }
  4975. } else {
  4976. // taxonomy type not found
  4977. return $clauses;
  4978. }
  4979. if ( empty( $icl_taxonomies ) )
  4980. return $clauses;
  4981. $icl_taxonomies = "'" . join( "','", $icl_taxonomies ) . "'";
  4982. $lang = $this->get_current_language();
  4983. if ( $lang == 'all' ) {
  4984. $left_join = ' LEFT';
  4985. $where_lang = '';
  4986. } else {
  4987. $where_lang = " AND icl_t.language_code = '{$lang}'";
  4988. }
  4989. $clauses[ 'join' ] .= "{$left_join} JOIN {$wpdb->prefix}icl_translations icl_t ON icl_t.element_id = tt.term_taxonomy_id";
  4990. $clauses[ 'where' ] .= "{$where_lang} AND icl_t.element_type IN({$icl_taxonomies})";
  4991. //echo '<pre>' . print_r($clauses) . '</pre>';
  4992. }
  4993. return $clauses;
  4994. }
  4995. function set_wp_query()
  4996. {
  4997. global $wp_query;
  4998. $this->wp_query = $wp_query;
  4999. }
  5000. private function home_url_in_language_switcher($debug_backtrace) {
  5001. $level_7 = isset($debug_backtrace[7]);
  5002. $in_footer = ($debug_backtrace[7]['function']=='language_selector_footer');
  5003. $in_dropdown_widget = ( $debug_backtrace[7]['function']=='language_selector' );
  5004. $in_list_widget = ( $debug_backtrace[7]['function']=='icl_get_languages' );
  5005. $in_nav_menu = ( $debug_backtrace[7]['function']=='wp_nav_menu_items_filter' );
  5006. return ( $level_7 && ( $in_footer || $in_dropdown_widget || $in_list_widget || $in_nav_menu ) );
  5007. }
  5008. // filter for WP home_url function
  5009. function home_url( $url, $path, $orig_scheme, $blog_id )
  5010. {
  5011. $debug_backtrace = $this->get_backtrace( 8 ); //Limit to first 8 stack frames, since 7 is the highest index we use
  5012. // exception for get_page_num_link and language_negotiation_type = 3
  5013. if ( $this->settings[ 'language_negotiation_type' ] == 3 ) {
  5014. if ( !empty( $debug_backtrace[ 6 ] ) && $debug_backtrace[ 6 ][ 'function' ] == 'get_pagenum_link' )
  5015. return $url;
  5016. }
  5017. $convert_url = false;
  5018. // only apply this in some specific cases (1)
  5019. if(isset($debug_backtrace[5]) && $debug_backtrace[5]['function']=='get_post_type_archive_link') {
  5020. $convert_url = true;
  5021. if($this->home_url_in_language_switcher($debug_backtrace)) {
  5022. $convert_url = false;
  5023. }
  5024. }
  5025. remove_filter( 'home_url', array( $this, 'home_url' ), 1 );
  5026. // only apply this in some specific cases (2)
  5027. if(!$convert_url && ( did_action( 'template_redirect' ) && rtrim( $url, '/' ) == rtrim( get_home_url(), '/' ) ) || $path == '/') {
  5028. $convert_url = true;
  5029. }
  5030. if ( $convert_url ) {
  5031. $url = $this->convert_url( $url );
  5032. }
  5033. add_filter( 'home_url', array( $this, 'home_url' ), 1, 4 );
  5034. return $url;
  5035. }
  5036. /**
  5037. * Converts WP generated url to language specific based on plugin settings
  5038. *
  5039. * @param string $url
  5040. * @param null|string $code (if null, fallback to detaulf language for root page, or current language in all other cases)
  5041. *
  5042. * @return bool|string
  5043. */
  5044. function convert_url( $url, $code = null ) {
  5045. if(!$url) return false;
  5046. $default_language = $this->get_default_language();
  5047. $current_language = $this->get_current_language();
  5048. $language_negotiation_type = $this->settings[ 'language_negotiation_type' ];
  5049. if ( is_null( $code ) && $language_negotiation_type == '2' && isset( $this->settings[ 'language_domains' ] ) ) {
  5050. foreach ( $this->settings[ 'language_domains' ] as $lang => $domain ) {
  5051. $domain = preg_replace( '/^https?\:\/\//', '', $domain );
  5052. $domain_data = explode('/', $domain);
  5053. $domain = $domain_data[0];
  5054. if ( $domain == $this->get_server_host_name() ) {
  5055. $code = $lang;
  5056. }
  5057. }
  5058. if ( is_null( $code ) ) {
  5059. $code = $default_language;
  5060. }
  5061. }
  5062. if ( is_null( $code ) ) {
  5063. $code = $this->this_lang;
  5064. }
  5065. $cache_key_args = array( $url, $code, $current_language );
  5066. $cache_key = md5(json_encode( $cache_key_args ));
  5067. $cache_group = 'convert_url';
  5068. $cache_found = false;
  5069. $new_url = wp_cache_get($cache_key, $cache_group, false, $cache_found);
  5070. if(!$cache_found) {
  5071. $new_url = $url;
  5072. if ( $code && ( $code != $default_language || ( $language_negotiation_type == 1 && $this->settings[ 'urls' ][ 'directory_for_default_language' ] ) ) ) {
  5073. remove_filter( 'home_url', array( $this, 'home_url' ), 1 );
  5074. $absolute_home_url = preg_replace( '@\?lang=' . $code . '@i', '', get_home_url() );
  5075. add_filter( 'home_url', array( $this, 'home_url' ), 1, 4 );
  5076. switch ( $language_negotiation_type ) {
  5077. case '1':
  5078. if ( 0 === strpos( $new_url, 'https://' ) ) {
  5079. $absolute_home_url = preg_replace( '#^http://#', 'https://', $absolute_home_url );
  5080. }
  5081. if ( $absolute_home_url == $new_url ) {
  5082. $new_url .= '/';
  5083. }
  5084. if ( false === strpos( $new_url, $absolute_home_url . '/' . $code . '/' ) ) {
  5085. $new_url = str_replace( $absolute_home_url, $absolute_home_url . '/' . $code, $new_url );
  5086. }
  5087. break;
  5088. case '2':
  5089. $is_https = strpos( $new_url, 'https://' ) === 0;
  5090. if ( $is_https ) {
  5091. preg_replace( '#^http://#', 'https://', $new_url );
  5092. } // normalize protocol
  5093. $new_url = str_replace( $absolute_home_url, $this->settings[ 'language_domains' ][ $code ], $new_url );
  5094. if ( $is_https ) {
  5095. preg_replace( '#^http://#', 'https://', $new_url );
  5096. } // normalize protocol (rev)
  5097. break;
  5098. case '3':
  5099. default:
  5100. // remove any previous value.
  5101. if ( strpos( $new_url, '?lang=' . $code . '&' ) !== false ) {
  5102. $new_url = str_replace( '?lang=' . $code . '&', '', $new_url );
  5103. } elseif ( strpos( $new_url, '?lang=' . $code . '/' ) !== false ) {
  5104. $new_url = str_replace( '?lang=' . $code . '/', '', $new_url );
  5105. } elseif ( strpos( $new_url, '?lang=' . $code ) !== false ) {
  5106. $new_url = str_replace( '?lang=' . $code, '', $new_url );
  5107. } elseif ( strpos( $new_url, '&lang=' . $code . '/' ) !== false ) {
  5108. $new_url = str_replace( '&lang=' . $code . '/', '', $new_url );
  5109. } elseif ( strpos( $new_url, '&lang=' . $code ) !== false ) {
  5110. $new_url = str_replace( '&lang=' . $code, '', $new_url );
  5111. }
  5112. if ( false === strpos( $new_url, '?' ) ) {
  5113. $new_url_glue = '?';
  5114. } else {
  5115. $new_url_glue = '&';
  5116. }
  5117. $new_url .= $new_url_glue . 'lang=' . $code;
  5118. }
  5119. }
  5120. wp_cache_set($cache_key, $new_url, $cache_group);
  5121. }
  5122. return $new_url;
  5123. }
  5124. function language_url( $code = null )
  5125. {
  5126. if ( is_null( $code ) )
  5127. $code = $this->this_lang;
  5128. remove_filter( 'home_url', array( $this, 'home_url' ), 1 );
  5129. $abshome = get_home_url();
  5130. add_filter( 'home_url', array( $this, 'home_url' ), 1, 4 );
  5131. if ( $this->settings[ 'language_negotiation_type' ] == 1 || $this->settings[ 'language_negotiation_type' ] == 2 ) {
  5132. $url = trailingslashit( $this->convert_url( $abshome, $code ) );
  5133. } else {
  5134. $url = $this->convert_url( $abshome, $code );
  5135. }
  5136. return $url;
  5137. }
  5138. function permalink_filter( $p, $pid )
  5139. {
  5140. if ( is_object( $pid ) ) {
  5141. $post_type = $pid->post_type;
  5142. $pid = $pid->ID;
  5143. } else {
  5144. $_post = get_post( $pid );
  5145. $post_type = $_post->post_type;
  5146. }
  5147. if ( !$this->is_translated_post_type( $post_type ) )
  5148. return $p;
  5149. if ( $pid == (int)get_option( 'page_on_front' ) ) {
  5150. return $p;
  5151. }
  5152. $default_language = $this->get_default_language();
  5153. $element_lang_details = $this->get_element_language_details( $pid, 'post_' . $post_type );
  5154. $use_directory = $this->settings[ 'language_negotiation_type' ] == 1 && $this->settings[ 'urls' ][ 'directory_for_default_language' ];
  5155. if ( !empty( $element_lang_details ) && $element_lang_details->language_code && ( $default_language != $element_lang_details->language_code || $use_directory ) ) {
  5156. $p = $this->convert_url( $p, $element_lang_details->language_code );
  5157. } elseif ( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'sample-permalink' ) { // check whether this is an autosaved draft
  5158. if ( !isset( $_SERVER[ 'HTTP_REFERER' ] ) ) {
  5159. $p = $this->convert_url( $p, $default_language );
  5160. } else {
  5161. $exp = explode( '?', $_SERVER[ "HTTP_REFERER" ] );
  5162. if ( isset( $exp[ 1 ] ) )
  5163. parse_str( $exp[ 1 ], $args );
  5164. if ( isset( $args[ 'lang' ] ) && $default_language != $args[ 'lang' ] ) {
  5165. $p = $this->convert_url( $p, $args[ 'lang' ] );
  5166. }
  5167. }
  5168. }
  5169. if ( is_feed() ) {
  5170. $p = str_replace( "&lang=", "&#038;lang=", $p );
  5171. }
  5172. return $p;
  5173. }
  5174. function category_permalink_filter( $p, $cat_id )
  5175. {
  5176. global $wpdb;
  5177. if ( isset( $this->icl_term_taxonomy_cache ) ) {
  5178. $term_cat_id = $this->icl_term_taxonomy_cache->get( 'category_' . $cat_id );
  5179. } else {
  5180. $term_cat_id = null;
  5181. }
  5182. if ( !$term_cat_id ) {
  5183. $term_cat_id = $wpdb->get_var( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id={$cat_id} AND taxonomy='category'" );
  5184. if ( isset( $this->icl_term_taxonomy_cache ) ) {
  5185. $this->icl_term_taxonomy_cache->set( 'category_' . $cat_id, $term_cat_id );
  5186. }
  5187. }
  5188. $cat_id = $term_cat_id;
  5189. $element_lang_details = $this->get_element_language_details( $cat_id, 'tax_category' );
  5190. $use_directory = $this->settings[ 'language_negotiation_type' ] == 1 && $this->settings[ 'urls' ][ 'directory_for_default_language' ];
  5191. if ( $this->get_default_language() != $element_lang_details->language_code || $use_directory ) {
  5192. $p = $this->convert_url( $p, $element_lang_details->language_code );
  5193. }
  5194. return $p;
  5195. }
  5196. function post_type_archive_link_filter( $link, $post_type )
  5197. {
  5198. if ( isset( $this->settings[ 'custom_posts_sync_option' ][ $post_type ] ) && $this->settings[ 'custom_posts_sync_option' ][ $post_type ] ) {
  5199. $link = $this->convert_url( $link );
  5200. $link = $this->adjust_cpt_in_url( $link, $post_type );
  5201. }
  5202. return $link;
  5203. }
  5204. function adjust_cpt_in_url($link, $post_type) {
  5205. $default_language = $this->get_default_language();
  5206. $current_language = $this->get_current_language();
  5207. if ($default_language != $current_language && function_exists('icl_t')) {
  5208. if ( isset($this->settings['posts_slug_translation']['types'][$post_type]) && $this->settings['posts_slug_translation']['types'][$post_type] ) {
  5209. $post_type_object = get_post_type_object($post_type);
  5210. if (isset($post_type_object->rewrite)) {
  5211. $slug = $post_type_object->rewrite['slug'];
  5212. } else {
  5213. $slug = $post_type_object->name;
  5214. }
  5215. $translated_slug = icl_t('WordPress', 'URL slug: ' . $slug, $slug);
  5216. if (is_string($translated_slug)) {
  5217. $link = preg_replace("/". preg_quote($slug, "/") ."/", $translated_slug, $link, 1);
  5218. }
  5219. }
  5220. }
  5221. return $link;
  5222. }
  5223. function tax_permalink_filter( $p, $tag )
  5224. {
  5225. global $wpdb;
  5226. if ( is_object( $tag ) ) {
  5227. $tag_id = $tag->term_taxonomy_id;
  5228. $taxonomy = $tag->taxonomy;
  5229. } else {
  5230. $taxonomy = 'post_tag';
  5231. if ( empty( $tag_id ) ) {
  5232. $tag_id = $wpdb->get_var( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id={$tag} AND taxonomy='{$taxonomy}'" );
  5233. if ( isset( $this->icl_term_taxonomy_cache ) ) {
  5234. $this->icl_term_taxonomy_cache->set( $taxonomy . '_' . $tag, $tag_id );
  5235. }
  5236. }
  5237. }
  5238. $cached_permalink_key = $tag_id . '.' . $taxonomy;
  5239. $cached_permalink = wp_cache_get($cached_permalink_key, 'icl_tax_permalink_filter');
  5240. if($cached_permalink) {
  5241. return $cached_permalink;
  5242. }
  5243. $element_lang_details = $this->get_element_language_details( $tag_id, 'tax_' . $taxonomy );
  5244. $use_directory = $this->settings[ 'language_negotiation_type' ] == 1 && $this->settings[ 'urls' ][ 'directory_for_default_language' ];
  5245. if ( !empty( $element_lang_details ) && ( $this->get_default_language() != $element_lang_details->language_code || $use_directory ) ) {
  5246. $p = $this->convert_url( $p, $element_lang_details->language_code );
  5247. }
  5248. wp_cache_set($cached_permalink_key, $p, 'icl_tax_permalink_filter');
  5249. return $p;
  5250. }
  5251. function get_comment_link_filter( $link )
  5252. {
  5253. // decode html characters since they are already encoded in the template for some reason
  5254. $link = html_entity_decode( $link );
  5255. return $link;
  5256. }
  5257. function attachment_link_filter( $link, $id )
  5258. {
  5259. //FIXME: check if we really need to call SitePress::convert_url in all other cases
  5260. if($this->get_setting( 'language_negotiation_type' ) == 2) {
  5261. $convert_url = $this->permalink_filter( $link, $id );
  5262. } else {
  5263. $convert_url = $this->convert_url( $link );
  5264. }
  5265. return $convert_url;
  5266. }
  5267. function get_ls_languages( $template_args = array() )
  5268. {
  5269. //Returns false if is admin and settings are corrupted
  5270. if(is_admin() && !SitePress::check_settings_integrity()) return false;
  5271. /** @var $wp_query WP_Query */
  5272. global $sitepress, $wpdb, $wp_query, $w_this_lang;
  5273. $current_language = $this->get_current_language();
  5274. $default_language = $this->get_default_language();
  5275. $cache_key_args = $template_args ? array_filter($template_args) : array('default');
  5276. $cache_key_args[] = $current_language;
  5277. $cache_key_args[] = $default_language;
  5278. $cache_key_args = array_filter($cache_key_args);
  5279. $cache_key = md5(json_encode($cache_key_args));
  5280. $cache_group = 'ls_languages';
  5281. $found = false;
  5282. $ls_languages = wp_cache_get($cache_key, $cache_group, $found);
  5283. if($found) return $ls_languages;
  5284. if ( is_null( $this->wp_query ) )
  5285. $this->set_wp_query();
  5286. // use original wp_query for this
  5287. // backup current $wp_query
  5288. if ( !isset( $wp_query ) )
  5289. return $this->get_active_languages();
  5290. $_wp_query_back = clone $wp_query;
  5291. unset( $wp_query );
  5292. global $wp_query; // make it global again after unset
  5293. $wp_query = clone $this->wp_query;
  5294. $w_active_languages = $this->get_active_languages();
  5295. $this_lang = $this->this_lang;
  5296. if ( $this_lang == 'all' ) {
  5297. $w_this_lang = array(
  5298. 'code' => 'all', 'english_name' => 'All languages', 'display_name' => __( 'All languages', 'sitepress' )
  5299. );
  5300. } else {
  5301. $w_this_lang = $this->get_language_details( $this_lang );
  5302. }
  5303. if ( isset( $template_args[ 'skip_missing' ] ) ) {
  5304. //override default setting
  5305. $icl_lso_link_empty = !$template_args[ 'skip_missing' ];
  5306. } else {
  5307. $icl_lso_link_empty = $this->settings[ 'icl_lso_link_empty' ];
  5308. }
  5309. // 1. Determine translations
  5310. if ( is_category() ) {
  5311. $skip_empty = false;
  5312. $term_taxonomy_id_prepared = $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy=%s", array( get_query_var( 'cat' ), 'category' ) );
  5313. $term_taxonomy_id = $wpdb->get_var( $term_taxonomy_id_prepared );
  5314. $trid = $sitepress->get_element_trid( $term_taxonomy_id, 'tax_category' );
  5315. $translations = $this->get_element_translations( $trid, 'tax_category', $skip_empty );
  5316. } elseif ( is_tag() ) {
  5317. $skip_empty = false;
  5318. $term_taxonomy_id_prepared = $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy=%s", array( get_query_var( 'tag_id' ), 'post_tag' ) );
  5319. $term_taxonomy_id = $wpdb->get_var( $term_taxonomy_id_prepared );
  5320. $trid = $sitepress->get_element_trid( $term_taxonomy_id, 'tax_post_tag' );
  5321. $translations = $this->get_element_translations( $trid, 'tax_post_tag', $skip_empty );
  5322. } elseif ( is_tax() ) {
  5323. $skip_empty = false;
  5324. $term_taxonomy_id_prepared = $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy=%s", array( $wp_query->get_queried_object_id(), get_query_var( 'taxonomy' ) ) );
  5325. $term_taxonomy_id = $wpdb->get_var( $term_taxonomy_id_prepared );
  5326. if ( $this->is_translated_taxonomy( get_query_var( 'taxonomy' ) ) ) {
  5327. $trid = $this->get_element_trid( $term_taxonomy_id, 'tax_' . get_query_var( 'taxonomy' ) );
  5328. $translations = $this->get_element_translations( $trid, 'tax_' . get_query_var( 'taxonomy' ), $skip_empty );
  5329. } else {
  5330. $translations[ $this->get_current_language() ] = (object)array(
  5331. 'translation_id' => 0,
  5332. 'language_code' => $this->get_default_language(),
  5333. 'original' => 1,
  5334. 'name' => get_query_var( 'taxonomy' ),
  5335. 'term_id' => $wp_query->get_queried_object_id()
  5336. );
  5337. }
  5338. } elseif ( is_archive() && !empty( $wp_query->posts ) ) {
  5339. $translations = array();
  5340. } elseif ( is_attachment() ) { // Exception for attachments. Not translated.
  5341. $trid = $sitepress->get_element_trid($wp_query->get_queried_object_id(), 'post_attachment' );
  5342. $translations = $this->get_element_translations( $trid, 'post_attachment' );
  5343. } elseif (is_page() || ('page' == get_option( 'show_on_front' ) && ( isset( $this->wp_query->queried_object_id ) && $this->wp_query->queried_object_id == get_option( 'page_on_front' ) || ( isset( $this->wp_query->queried_object_id ) && $this->wp_query->queried_object_id == get_option( 'page_for_posts' )) ) ) ) {
  5344. $trid = $sitepress->get_element_trid($wp_query->get_queried_object_id(), 'post_page' );
  5345. $translations = $this->get_element_translations( $trid, 'post_page' );
  5346. } elseif ( is_singular() && !empty( $wp_query->posts ) ) {
  5347. $trid = $sitepress->get_element_trid($this->wp_query->post->ID, 'post_' . $wp_query->posts[ 0 ]->post_type);
  5348. $translations = $this->get_element_translations( $trid, 'post_' . $wp_query->posts[ 0 ]->post_type );
  5349. } else {
  5350. $wp_query->is_singular = false;
  5351. $wp_query->is_archive = false;
  5352. $wp_query->is_category = false;
  5353. $wp_query->is_404 = true;
  5354. }
  5355. // 2. determine url
  5356. foreach ( $w_active_languages as $k => $lang ) {
  5357. $skip_lang = false;
  5358. if ( is_singular() || ( !empty( $this->wp_query->queried_object_id ) && $this->wp_query->queried_object_id == get_option( 'page_for_posts' ) ) ) {
  5359. $this_lang_tmp = $this->this_lang;
  5360. $this->this_lang = $lang[ 'code' ];
  5361. $lang_page_on_front = get_option( 'page_on_front' );
  5362. $lang_page_for_posts = get_option( 'page_for_posts' );
  5363. if($lang_page_on_front && $lang[ 'code' ] != $default_language) {
  5364. $lang_page_on_front = icl_object_id($lang_page_on_front, 'page', false, $lang[ 'code' ]);
  5365. }
  5366. if($lang_page_for_posts && $lang[ 'code' ] != $default_language) {
  5367. $lang_page_for_posts = icl_object_id($lang_page_for_posts, 'page', false, $lang[ 'code' ]);
  5368. }
  5369. if ( 'page' == get_option( 'show_on_front' ) && !empty( $translations[ $lang[ 'code' ] ] ) && $translations[ $lang[ 'code' ] ]->element_id == $lang_page_on_front ) {
  5370. $lang[ 'translated_url' ] = $this->language_url( $lang[ 'code' ] );
  5371. } elseif ( 'page' == get_option( 'show_on_front' ) && !empty( $translations[ $lang[ 'code' ] ] ) && $translations[ $lang[ 'code' ] ]->element_id && $translations[ $lang[ 'code' ] ]->element_id == $lang_page_for_posts ) {
  5372. if ( $lang_page_for_posts ) {
  5373. $lang[ 'translated_url' ] = get_permalink( $lang_page_for_posts );
  5374. } else {
  5375. $lang[ 'translated_url' ] = $this->language_url( $lang[ 'code' ] );
  5376. }
  5377. } else {
  5378. if ( !empty( $translations[ $lang[ 'code' ] ] ) && isset( $translations[ $lang[ 'code' ] ]->post_title ) ) {
  5379. $lang[ 'translated_url' ] = get_permalink( $translations[ $lang[ 'code' ] ]->element_id );
  5380. $lang[ 'missing' ] = 0;
  5381. } else {
  5382. if ( $icl_lso_link_empty ) {
  5383. if ( !empty( $template_args[ 'link_empty_to' ] ) ) {
  5384. $lang[ 'translated_url' ] = str_replace( '{%lang}', $lang[ 'code' ], $template_args[ 'link_empty_to' ] );
  5385. } else {
  5386. $lang[ 'translated_url' ] = $this->language_url( $lang[ 'code' ] );
  5387. }
  5388. } else {
  5389. $skip_lang = true;
  5390. }
  5391. $lang[ 'missing' ] = 1;
  5392. }
  5393. }
  5394. $this->this_lang = $this_lang_tmp;
  5395. } elseif ( is_category() ) {
  5396. if ( isset( $translations[ $lang[ 'code' ] ] ) ) {
  5397. global $icl_adjust_id_url_filter_off; // force the category_link_adjust_id to not modify this
  5398. $icl_adjust_id_url_filter_off = true;
  5399. $lang[ 'translated_url' ] = get_category_link( $translations[ $lang[ 'code' ] ]->term_id );
  5400. $icl_adjust_id_url_filter_off = false; // restore default bahavior
  5401. $lang[ 'missing' ] = 0;
  5402. } else {
  5403. if ( $icl_lso_link_empty ) {
  5404. if ( !empty( $template_args[ 'link_empty_to' ] ) ) {
  5405. $lang[ 'translated_url' ] = str_replace( '{%lang}', $lang[ 'code' ], $template_args[ 'link_empty_to' ] );
  5406. } else {
  5407. $lang[ 'translated_url' ] = $this->language_url( $lang[ 'code' ] );
  5408. }
  5409. } else {
  5410. // dont skip the currrent language
  5411. if ( $current_language != $lang[ 'code' ] ) {
  5412. $skip_lang = true;
  5413. }
  5414. }
  5415. $lang[ 'missing' ] = 1;
  5416. }
  5417. } elseif ( is_tax() ) {
  5418. if ( isset( $translations[ $lang[ 'code' ] ] ) ) {
  5419. global $icl_adjust_id_url_filter_off; // force the category_link_adjust_id to not modify this
  5420. $icl_adjust_id_url_filter_off = true;
  5421. $lang[ 'translated_url' ] = get_term_link( (int)$translations[ $lang[ 'code' ] ]->term_id, get_query_var( 'taxonomy' ) );
  5422. $icl_adjust_id_url_filter_off = false; // restore default bahavior
  5423. $lang[ 'missing' ] = 0;
  5424. } else {
  5425. if ( $icl_lso_link_empty ) {
  5426. if ( !empty( $template_args[ 'link_empty_to' ] ) ) {
  5427. $lang[ 'translated_url' ] = str_replace( '{%lang}', $lang[ 'code' ], $template_args[ 'link_empty_to' ] );
  5428. } else {
  5429. $lang[ 'translated_url' ] = $this->language_url( $lang[ 'code' ] );
  5430. }
  5431. } else {
  5432. // dont skip the currrent language
  5433. if ( $current_language != $lang[ 'code' ] ) {
  5434. $skip_lang = true;
  5435. }
  5436. }
  5437. $lang[ 'missing' ] = 1;
  5438. }
  5439. } elseif ( is_tag() ) {
  5440. if ( isset( $translations[ $lang[ 'code' ] ] ) ) {
  5441. global $icl_adjust_id_url_filter_off; // force the category_link_adjust_id to not modify this
  5442. $icl_adjust_id_url_filter_off = true;
  5443. $lang[ 'translated_url' ] = get_tag_link( $translations[ $lang[ 'code' ] ]->term_id );
  5444. $icl_adjust_id_url_filter_off = false; // restore default bahavior
  5445. $lang[ 'missing' ] = 0;
  5446. } else {
  5447. if ( $icl_lso_link_empty ) {
  5448. if ( !empty( $template_args[ 'link_empty_to' ] ) ) {
  5449. $lang[ 'translated_url' ] = str_replace( '{%lang}', $lang[ 'code' ], $template_args[ 'link_empty_to' ] );
  5450. } else {
  5451. $lang[ 'translated_url' ] = $this->language_url( $lang[ 'code' ] );
  5452. }
  5453. } else {
  5454. // dont skip the currrent language
  5455. if ( $current_language != $lang[ 'code' ] ) {
  5456. $skip_lang = true;
  5457. }
  5458. }
  5459. $lang[ 'missing' ] = 1;
  5460. }
  5461. } elseif ( is_author() ) {
  5462. global $authordata, $wp_query;
  5463. if ( empty( $authordata ) ) {
  5464. $authordata = get_userdata( get_query_var( 'author' ) );
  5465. }
  5466. $post_type = get_query_var( 'post_type' ) ? get_query_var( 'post_type' ) : 'post';
  5467. if ( $wpdb->get_var( "SELECT COUNT(p.ID) FROM {$wpdb->posts} p
  5468. JOIN {$wpdb->prefix}icl_translations t ON p.ID=t.element_id AND t.element_type = 'post_{$post_type}'
  5469. WHERE p.post_author='{$authordata->ID}' AND post_type='{$post_type}' AND post_status='publish' AND language_code='{$lang['code']}'" )
  5470. ) {
  5471. remove_filter( 'home_url', array( $this, 'home_url' ), 1, 4 );
  5472. remove_filter( 'author_link', array( $this, 'author_link' ) );
  5473. $author_url = get_author_posts_url( $authordata->ID );
  5474. add_filter( 'home_url', array( $this, 'home_url' ), 1, 4 );
  5475. add_filter( 'author_link', array( $this, 'author_link' ) );
  5476. $lang[ 'translated_url' ] = $this->convert_url( $author_url, $lang[ 'code' ] );
  5477. $lang[ 'missing' ] = 0;
  5478. } else {
  5479. if ( $icl_lso_link_empty ) {
  5480. if ( !empty( $template_args[ 'link_empty_to' ] ) ) {
  5481. $lang[ 'translated_url' ] = str_replace( '{%lang}', $lang[ 'code' ], $template_args[ 'link_empty_to' ] );
  5482. } else {
  5483. $lang[ 'translated_url' ] = $this->language_url( $lang[ 'code' ] );
  5484. }
  5485. } else {
  5486. // dont skip the currrent language
  5487. if ( $current_language != $lang[ 'code' ] ) {
  5488. $skip_lang = true;
  5489. }
  5490. }
  5491. $lang[ 'missing' ] = 1;
  5492. }
  5493. } elseif ( is_archive() && !is_tag() ) {
  5494. global $icl_archive_url_filter_off;
  5495. $icl_archive_url_filter_off = true;
  5496. if ( $this->wp_query->is_year ) {
  5497. if ( isset( $this->wp_query->query_vars[ 'm' ] ) && !$this->wp_query->query_vars[ 'year' ] ) {
  5498. $this->wp_query->query_vars[ 'year' ] = substr( $this->wp_query->query_vars[ 'm' ], 0, 4 );
  5499. }
  5500. $lang[ 'translated_url' ] = $this->archive_url( get_year_link( $this->wp_query->query_vars[ 'year' ] ), $lang[ 'code' ] );
  5501. } elseif ( $this->wp_query->is_month ) {
  5502. if ( isset( $this->wp_query->query_vars[ 'm' ] ) && !$this->wp_query->query_vars[ 'year' ] ) {
  5503. $this->wp_query->query_vars[ 'year' ] = substr( $this->wp_query->query_vars[ 'm' ], 0, 4 );
  5504. $this->wp_query->query_vars[ 'monthnum' ] = substr( $this->wp_query->query_vars[ 'm' ], 4, 2 );
  5505. } else {
  5506. if ( $icl_lso_link_empty ) {
  5507. if ( !empty( $template_args[ 'link_empty_to' ] ) ) {
  5508. $lang[ 'translated_url' ] = str_replace( '{%lang}', $lang[ 'code' ], $template_args[ 'link_empty_to' ] );
  5509. } else {
  5510. $lang[ 'translated_url' ] = $this->language_url( $lang[ 'code' ] );
  5511. }
  5512. }
  5513. $lang[ 'missing' ] = 1;
  5514. }
  5515. $lang[ 'translated_url' ] = $this->archive_url( get_month_link( $this->wp_query->query_vars[ 'year' ], $this->wp_query->query_vars[ 'monthnum' ] ), $lang[ 'code' ] );
  5516. } elseif ( $this->wp_query->is_day ) {
  5517. if ( isset( $this->wp_query->query_vars[ 'm' ] ) && !$this->wp_query->query_vars[ 'year' ] ) {
  5518. $this->wp_query->query_vars[ 'year' ] = substr( $this->wp_query->query_vars[ 'm' ], 0, 4 );
  5519. $this->wp_query->query_vars[ 'monthnum' ] = substr( $this->wp_query->query_vars[ 'm' ], 4, 2 );
  5520. $this->wp_query->query_vars[ 'day' ] = substr( $this->wp_query->query_vars[ 'm' ], 6, 2 );
  5521. gmdate( 'Y', current_time( 'timestamp' ) ); //force wp_timezone_override_offset to be called
  5522. }
  5523. $lang[ 'translated_url' ] = $this->archive_url( get_day_link( $this->wp_query->query_vars[ 'year' ], $this->wp_query->query_vars[ 'monthnum' ], $this->wp_query->query_vars[ 'day' ] ), $lang[ 'code' ] );
  5524. } else if ( isset( $this->wp_query->query_vars[ 'post_type' ] ) ) {
  5525. do_action( '_icl_before_archive_url', $this->wp_query->query_vars[ 'post_type' ], $lang[ 'code' ] );
  5526. if ( $this->is_translated_post_type( $this->wp_query->query_vars[ 'post_type' ] ) && function_exists( 'get_post_type_archive_link' ) ) {
  5527. remove_filter( 'post_type_archive_link', array( $this, 'post_type_archive_link_filter' ), 10 );
  5528. $lang[ 'translated_url' ] = $this->convert_url( get_post_type_archive_link( $this->wp_query->query_vars[ 'post_type' ] ), $lang[ 'code' ] );
  5529. } else {
  5530. $lang[ 'translated_url' ] = $this->language_url( $lang[ 'code' ] );
  5531. }
  5532. do_action( '_icl_after_archive_url', $this->wp_query->query_vars[ 'post_type' ], $lang[ 'code' ] );
  5533. }
  5534. add_filter( 'post_type_archive_link', array( $this, 'post_type_archive_link_filter' ), 10, 2 );
  5535. $icl_archive_url_filter_off = false;
  5536. } elseif ( is_search() ) {
  5537. $url_glue = strpos( $this->language_url( $lang[ 'code' ] ), '?' ) === false ? '?' : '&';
  5538. $lang[ 'translated_url' ] = $this->language_url( $lang[ 'code' ] ) . $url_glue . 's=' . urlencode( $wp_query->query[ 's' ] );
  5539. } else {
  5540. global $icl_language_switcher_preview;
  5541. if ( $icl_lso_link_empty || is_home() || is_404() || ( 'page' == get_option( 'show_on_front' ) && ( $this->wp_query->queried_object_id == get_option( 'page_on_front' ) || $this->wp_query->queried_object_id == get_option( 'page_for_posts' ) ) ) || $icl_language_switcher_preview ) {
  5542. $lang[ 'translated_url' ] = $this->language_url( $lang[ 'code' ] );
  5543. $skip_lang = false;
  5544. } else {
  5545. $skip_lang = true;
  5546. unset( $w_active_languages[ $k ] );
  5547. }
  5548. }
  5549. if ( !$skip_lang ) {
  5550. $w_active_languages[ $k ] = $lang;
  5551. } else {
  5552. unset( $w_active_languages[ $k ] );
  5553. }
  5554. }
  5555. // 3.
  5556. foreach ( $w_active_languages as $k => $v ) {
  5557. $lang_code = $w_active_languages[ $k ][ 'language_code' ] = $w_active_languages[ $k ][ 'code' ];
  5558. unset( $w_active_languages[ $k ][ 'code' ] );
  5559. $native_name = $this->get_display_language_name( $lang_code, $lang_code );
  5560. if ( !$native_name )
  5561. $native_name = $w_active_languages[ $k ][ 'english_name' ];
  5562. $w_active_languages[ $k ][ 'native_name' ] = $native_name;
  5563. $translated_name = $this->get_display_language_name( $lang_code, $current_language );
  5564. if ( !$translated_name )
  5565. $translated_name = $w_active_languages[ $k ][ 'english_name' ];
  5566. $w_active_languages[ $k ][ 'translated_name' ] = $translated_name;
  5567. unset( $w_active_languages[ $k ][ 'display_name' ] );
  5568. unset( $w_active_languages[ $k ][ 'english_name' ] );
  5569. if ( isset( $w_active_languages[ $k ][ 'translated_url' ] ) ) {
  5570. $w_active_languages[ $k ][ 'url' ] = $w_active_languages[ $k ][ 'translated_url' ];
  5571. unset( $w_active_languages[ $k ][ 'translated_url' ] );
  5572. } else {
  5573. $w_active_languages[ $k ][ 'url' ] = $this->language_url( $k );
  5574. }
  5575. $flag = $this->get_flag( $lang_code );
  5576. if ( $flag->from_template ) {
  5577. $wp_upload_dir = wp_upload_dir();
  5578. $flag_url = $wp_upload_dir[ 'baseurl' ] . '/flags/' . $flag->flag;
  5579. } else {
  5580. $flag_url = ICL_PLUGIN_URL . '/res/flags/' . $flag->flag;
  5581. }
  5582. $w_active_languages[ $k ][ 'country_flag_url' ] = $flag_url;
  5583. $w_active_languages[ $k ][ 'active' ] = $current_language == $lang_code ? '1' : 0;;
  5584. }
  5585. // 4. pass GET parameters
  5586. $parameters_copied = apply_filters( 'icl_lang_sel_copy_parameters', array_map( 'trim', explode( ',', $this->settings[ 'icl_lang_sel_copy_parameters' ] ) ) );
  5587. if ( $parameters_copied ) {
  5588. foreach ( $_GET as $k => $v ) {
  5589. if ( in_array( $k, $parameters_copied ) ) {
  5590. $gets_passed[ $k ] = $v;
  5591. }
  5592. }
  5593. }
  5594. if ( !empty( $gets_passed ) ) {
  5595. $gets_passed = http_build_query( $gets_passed );
  5596. foreach ( $w_active_languages as $code => $al ) {
  5597. if ( empty( $al[ 'missing' ] ) ) {
  5598. $glue = false !== strpos( $w_active_languages[ $code ][ 'url' ], '?' ) ? '&' : '?';
  5599. $w_active_languages[ $code ][ 'url' ] .= $glue . $gets_passed;
  5600. }
  5601. }
  5602. }
  5603. // restore current $wp_query
  5604. unset( $wp_query );
  5605. global $wp_query; // make it global again after unset
  5606. $wp_query = clone $_wp_query_back;
  5607. unset( $_wp_query_back );
  5608. $w_active_languages = apply_filters( 'icl_ls_languages', $w_active_languages );
  5609. $w_active_languages = $this->sort_ls_languages( $w_active_languages, $template_args );
  5610. wp_reset_query();
  5611. wp_cache_set($cache_key, $w_active_languages, $cache_group);
  5612. return $w_active_languages;
  5613. }
  5614. function sort_ls_languages( $w_active_languages, $template_args )
  5615. {
  5616. // sort languages according to parameters
  5617. $orderby = isset( $template_args[ 'orderby' ] ) ? $template_args[ 'orderby' ] : 'custom';
  5618. $order = isset( $template_args[ 'order' ] ) ? $template_args[ 'order' ] : 'asc';
  5619. $comp = $order == 'asc' ? '>' : '<';
  5620. switch ( $orderby ) {
  5621. case 'id':
  5622. uasort( $w_active_languages, create_function( '$a,$b', 'return $a[\'id\'] ' . $comp . ' $b[\'id\'];' ) );
  5623. break;
  5624. case 'code':
  5625. ksort( $w_active_languages );
  5626. if ( $order == 'desc' ) {
  5627. $w_active_languages = array_reverse( $w_active_languages );
  5628. }
  5629. break;
  5630. case 'name':
  5631. uasort( $w_active_languages, create_function( '$a,$b', 'return $a[\'translated_name\'] ' . $comp . ' $b[\'translated_name\'];' ) );
  5632. break;
  5633. case 'custom':
  5634. default:
  5635. $w_active_languages = $this->order_languages( $w_active_languages );
  5636. }
  5637. return $w_active_languages;
  5638. }
  5639. function get_display_language_name( $lang_code, $display_code )
  5640. {
  5641. global $wpdb;
  5642. if ( isset( $this->icl_language_name_cache ) ) {
  5643. $translated_name = $this->icl_language_name_cache->get( $lang_code . $display_code );
  5644. } else {
  5645. $translated_name = null;
  5646. }
  5647. if ( !$translated_name ) {
  5648. $display_code = $display_code == 'all' ? $this->get_admin_language() : $display_code;
  5649. $translated_name = $wpdb->get_var( "SELECT name FROM {$wpdb->prefix}icl_languages_translations WHERE language_code='{$lang_code}' AND display_language_code='{$display_code}'" );
  5650. if ( isset( $this->icl_language_name_cache ) ) {
  5651. $this->icl_language_name_cache->set( $lang_code . $display_code, $translated_name );
  5652. }
  5653. }
  5654. return $translated_name;
  5655. }
  5656. function get_flag( $lang_code )
  5657. {
  5658. global $wpdb;
  5659. if ( isset( $this->icl_flag_cache ) ) {
  5660. $flag = $this->icl_flag_cache->get( $lang_code );
  5661. } else {
  5662. $flag = null;
  5663. }
  5664. if ( !$flag ) {
  5665. $flag = $wpdb->get_row( "SELECT flag, from_template FROM {$wpdb->prefix}icl_flags WHERE lang_code='{$lang_code}'" );
  5666. if ( isset( $this->icl_flag_cache ) ) {
  5667. $this->icl_flag_cache->set( $lang_code, $flag );
  5668. }
  5669. }
  5670. return $flag;
  5671. }
  5672. function get_flag_url( $code )
  5673. {
  5674. $flag = $this->get_flag( $code );
  5675. if ( $flag->from_template ) {
  5676. $wp_upload_dir = wp_upload_dir();
  5677. $flag_url = $wp_upload_dir[ 'baseurl' ] . '/flags/' . $flag->flag;
  5678. } else {
  5679. $flag_url = ICL_PLUGIN_URL . '/res/flags/' . $flag->flag;
  5680. }
  5681. return $flag_url;
  5682. }
  5683. function set_up_language_selector()
  5684. {
  5685. // language selector
  5686. // load js and style for js language selector
  5687. if (isset($this->settings[ 'icl_lang_sel_type' ]) && $this->settings[ 'icl_lang_sel_type' ] == 'dropdown' && ( !is_admin() || ( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == ICL_PLUGIN_FOLDER . '/menu/languages.php' ) ) ) {
  5688. if ( $this->settings[ 'icl_lang_sel_stype' ] == 'mobile-auto' ) {
  5689. include ICL_PLUGIN_PATH . '/lib/mobile-detect.php';
  5690. $WPML_Mobile_Detect = new WPML_Mobile_Detect;
  5691. $this->is_mobile = $WPML_Mobile_Detect->isMobile();
  5692. $this->is_tablet = $WPML_Mobile_Detect->isTablet();
  5693. }
  5694. if ( ( $this->settings[ 'icl_lang_sel_stype' ] == 'mobile-auto' && ( !empty( $this->is_mobile ) || !empty( $this->is_tablet ) ) ) || $this->settings[ 'icl_lang_sel_stype' ] == 'mobile'
  5695. ) {
  5696. if(!defined('ICL_DONT_LOAD_LANGUAGES_JS') || !ICL_DONT_LOAD_LANGUAGES_JS) {
  5697. wp_enqueue_script( 'language-selector', ICL_PLUGIN_URL . '/res/js/language-selector.js', ICL_SITEPRESS_VERSION );
  5698. }
  5699. if(!defined('ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS') || !ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS) {
  5700. wp_enqueue_style( 'language-selector', ICL_PLUGIN_URL . '/res/css/language-selector-click.css', ICL_SITEPRESS_VERSION );
  5701. }
  5702. }
  5703. }
  5704. }
  5705. function language_selector()
  5706. {
  5707. // Mobile or auto
  5708. $is_mobile = $this->settings[ 'icl_lang_sel_stype' ] == 'mobile' || ( $this->settings[ 'icl_lang_sel_stype' ] == 'mobile-auto' && !empty( $this->is_tablet ) && !empty( $this->is_mobile ) );
  5709. if ( $this->settings[ 'icl_lang_sel_type' ] == 'dropdown' && ( $is_mobile )
  5710. ) {
  5711. include ICL_PLUGIN_PATH . '/menu/language-selector-mobile.php';
  5712. } else {
  5713. global $icl_language_switcher_preview;
  5714. if ( $this->settings[ 'icl_lang_sel_type' ] == 'list' || $icl_language_switcher_preview ) {
  5715. global $icl_language_switcher;
  5716. $icl_language_switcher->widget_list();
  5717. if ( !$icl_language_switcher_preview ) {
  5718. return '';
  5719. }
  5720. }
  5721. $active_languages = $this->get_ls_languages();
  5722. if($active_languages) {
  5723. /**
  5724. * @var $main_language bool|string
  5725. * @used_by menu/language-selector.php
  5726. */
  5727. foreach ( $active_languages as $k => $al ) {
  5728. if ( $al[ 'active' ] == 1 ) {
  5729. $main_language = $al;
  5730. unset( $active_languages[ $k ] );
  5731. break;
  5732. }
  5733. }
  5734. include ICL_PLUGIN_PATH . '/menu/language-selector.php';
  5735. }
  5736. }
  5737. return '';
  5738. }
  5739. function have_icl_translator( $source, $target )
  5740. {
  5741. // returns true if we have ICL translators for the language pair
  5742. if ( isset( $this->settings[ 'icl_lang_status' ] ) ) {
  5743. foreach ( $this->settings[ 'icl_lang_status' ] as $lang ) {
  5744. if ( $lang[ 'from' ] == $source && $lang[ 'to' ] == $target ) {
  5745. return $lang[ 'have_translators' ];
  5746. }
  5747. }
  5748. }
  5749. return false;
  5750. }
  5751. function get_default_categories()
  5752. {
  5753. $default_categories_all = $this->settings[ 'default_categories' ];
  5754. $active_languages_codes = false;
  5755. foreach ( $this->active_languages as $l ) {
  5756. $active_languages_codes[ ] = $l[ 'code' ];
  5757. }
  5758. $default_categories = array();
  5759. if ( is_array( $default_categories_all ) && is_array( $active_languages_codes ) ) {
  5760. foreach ( $default_categories_all as $c => $v ) {
  5761. if ( in_array( $c, $active_languages_codes ) ) {
  5762. $default_categories[ $c ] = $v;
  5763. }
  5764. }
  5765. }
  5766. return $default_categories;
  5767. }
  5768. function set_default_categories( $def_cat )
  5769. {
  5770. $this->settings[ 'default_categories' ] = $def_cat;
  5771. $this->save_settings();
  5772. }
  5773. function pre_option_default_category( $setting )
  5774. {
  5775. global $wpdb;
  5776. if ( isset( $_POST[ 'icl_post_language' ] ) && $_POST[ 'icl_post_language' ] || ( isset( $_GET[ 'lang' ] ) && $_GET[ 'lang' ] != 'all' ) ) {
  5777. $lang = isset( $_POST[ 'icl_post_language' ] ) && $_POST[ 'icl_post_language' ] ? $_POST[ 'icl_post_language' ] : $_GET[ 'lang' ];
  5778. $ttid = @intval( $this->settings[ 'default_categories' ][ $lang ] );
  5779. return $tid = $wpdb->get_var( "SELECT term_id FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id={$ttid} AND taxonomy='category'" );
  5780. }
  5781. return false;
  5782. }
  5783. function update_option_default_category( $oldvalue, $new_value )
  5784. {
  5785. global $wpdb;
  5786. $new_value = $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE taxonomy='category' AND term_id=%d", $new_value ) );
  5787. $translations = $this->get_element_translations( $this->get_element_trid( $new_value, 'tax_category' ) );
  5788. if ( !empty( $translations ) ) {
  5789. foreach ( $translations as $t ) {
  5790. $icl_settings[ 'default_categories' ][ $t->language_code ] = $t->element_id;
  5791. }
  5792. if ( isset( $icl_settings ) ) {
  5793. $this->save_settings( $icl_settings );
  5794. }
  5795. }
  5796. }
  5797. function the_category_name_filter( $name )
  5798. {
  5799. if ( is_array( $name ) ) {
  5800. foreach ( $name as $k => $v ) {
  5801. $name[ $k ] = $this->the_category_name_filter( $v );
  5802. }
  5803. return $name;
  5804. }
  5805. if ( false === strpos( $name, '@' ) )
  5806. return $name;
  5807. if ( false !== strpos( $name, '<a' ) ) {
  5808. $int = preg_match_all( '|<a([^>]+)>([^<]+)</a>|i', $name, $matches );
  5809. if ( $int && count( $matches[ 0 ] ) > 1 ) {
  5810. $originals = $filtered = array();
  5811. foreach ( $matches[ 0 ] as $m ) {
  5812. $originals[ ] = $m;
  5813. $filtered[ ] = $this->the_category_name_filter( $m );
  5814. }
  5815. $name = str_replace( $originals, $filtered, $name );
  5816. } else {
  5817. $name_sh = strip_tags( $name );
  5818. $exp = explode( '@', $name_sh );
  5819. $name = str_replace( $name_sh, trim( $exp[ 0 ] ), $name );
  5820. }
  5821. } else {
  5822. $name = preg_replace( '#(.*) @(.*)#i', '$1', $name );
  5823. }
  5824. return $name;
  5825. }
  5826. function get_terms_filter( $terms )
  5827. {
  5828. if (is_wp_error($terms)) {
  5829. return $terms;
  5830. }
  5831. foreach ( $terms as $k => $v ) {
  5832. if ( isset( $terms[ $k ]->name ) )
  5833. $terms[ $k ]->name = $this->the_category_name_filter( $terms[ $k ]->name );
  5834. }
  5835. return $terms;
  5836. }
  5837. function get_the_terms_filter( $terms, $id, $taxonomy )
  5838. {
  5839. if ( !empty( $this->settings[ 'taxonomies_sync_option' ][ $taxonomy ] ) ) {
  5840. $terms = $this->get_terms_filter( $terms );
  5841. }
  5842. return $terms;
  5843. }
  5844. function get_term_adjust_id( $term ) {
  5845. //TODO: To remove? I couldn't find a single place where this is used, since $term->term_id == $translated_id. Testing always returning the passed value.
  5846. // comment from Konrad: don't remove this as it is still used by nav menus
  5847. global $icl_adjust_id_url_filter_off;
  5848. if ( $icl_adjust_id_url_filter_off ) {
  5849. return $term;
  5850. } // special cases when we need the category in a different language
  5851. // exception: don't filter when called from get_permalink. When category parents are determined
  5852. $debug_backtrace = $this->get_backtrace( 7 ); //Limit to first 7 stack frames, since 6 is the highest index we use
  5853. if ( isset( $debug_backtrace[ 5 ][ 'function' ] ) &&
  5854. $debug_backtrace[ 5 ][ 'function' ] == 'get_category_parents' ||
  5855. isset( $debug_backtrace[ 6 ][ 'function' ] ) &&
  5856. $debug_backtrace[ 6 ][ 'function' ] == 'get_permalink' ||
  5857. isset( $debug_backtrace[ 4 ][ 'function' ] ) &&
  5858. $debug_backtrace[ 4 ][ 'function' ] == 'get_permalink' // WP 3.5
  5859. ) {
  5860. return $term;
  5861. }
  5862. $translated_id = icl_object_id( $term->term_id, $term->taxonomy, true );
  5863. $cache_key = $term->taxonomy . ':' . $translated_id;
  5864. $cache_group = 'icl_get_term_adjust_id';
  5865. $cache_found = false;
  5866. $cached_term = wp_cache_get( $cache_key, $cache_group, false, $cache_found );
  5867. if ( $cache_found ) {
  5868. return $cached_term;
  5869. }
  5870. if ( $translated_id != $term->term_id ) {
  5871. //$translated_id = $wpdb->get_var("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id='{$translated_id}'");
  5872. remove_filter( 'get_term', array( $this, 'get_term_adjust_id' ), 1 );
  5873. $t_term = get_term( $translated_id, $term->taxonomy );
  5874. if ( !is_wp_error( $t_term ) ) {
  5875. $term = $t_term;
  5876. }
  5877. add_filter( 'get_term', array( $this, 'get_term_adjust_id' ), 1, 1 );
  5878. }
  5879. wp_cache_set( $cache_key, $term, $cache_group );
  5880. return $term;
  5881. }
  5882. function get_term_by_name_and_lang( &$name, $type, $lang )
  5883. {
  5884. global $wpdb;
  5885. //decode name
  5886. // $name = htmlspecialchars_decode($name);
  5887. $the_term = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON x.term_id = t.term_id WHERE name=%s AND taxonomy=%s", array( $name , $type) ) );
  5888. if ( $the_term ) {
  5889. $term_lang = $wpdb->get_var( $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations
  5890. WHERE element_id=%d AND element_type=%s", $the_term->term_taxonomy_id, 'tax_' . $type ) );
  5891. if ( $term_lang != $lang ) {
  5892. // term is in the wrong language
  5893. // Add lang code to term name.
  5894. $name .= ' @' . $lang;
  5895. $the_term = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON x.term_id = t.term_id WHERE name=%s AND taxonomy=%s", array( $name , $type) ) );
  5896. }
  5897. }
  5898. return $the_term;
  5899. }
  5900. function set_term_translation( $original_term, $translation_id, $type, $lang, $original_lang )
  5901. {
  5902. global $wpdb;
  5903. if ( $original_term ) {
  5904. $trid = $this->get_element_trid( $original_term->term_taxonomy_id, 'tax_' . $type );
  5905. $this->set_element_language_details( $translation_id, 'tax_' . $type, $trid, $lang, $original_lang );
  5906. } else {
  5907. // Original has been deleled.
  5908. // we need to set the language of the new term.
  5909. $wpdb->update( $wpdb->prefix . 'icl_translations', array( 'language_code' => $lang ), array( 'element_type' => 'tax_' . $type, 'element_id' => $translation_id ) );
  5910. }
  5911. }
  5912. function wp_list_pages_adjust_ids( $out, $args )
  5913. {
  5914. static $__run_once = false; // only run for calls that have 'include' as an argument. ant only run once.
  5915. if ( $args[ 'include' ] && !$__run_once && $this->get_current_language() != $this->get_default_language() ) {
  5916. $__run_once = true;
  5917. $include = array_map( 'trim', explode( ',', $args[ 'include' ] ) );
  5918. $tr_include = array();
  5919. foreach ( $include as $i ) {
  5920. $t = icl_object_id( $i, 'page', true );
  5921. if ( $t ) {
  5922. $tr_include[ ] = $t;
  5923. }
  5924. }
  5925. $args[ 'include' ] = join( ',', $tr_include );
  5926. $out = wp_list_pages( $args );
  5927. }
  5928. return $out;
  5929. }
  5930. function get_terms_adjust_ids( $terms, $taxonomies, $args )
  5931. {
  5932. static $__run_once = false; // only run for calls that have 'include' as an argument. ant only run once.
  5933. if ( $args[ 'include' ] && !$__run_once && $this->get_current_language() != $this->get_default_language() ) {
  5934. $__run_once = true;
  5935. if ( is_array( $args[ 'include' ] ) ) {
  5936. $include = $args[ 'include' ];
  5937. } else {
  5938. $include = array_map( 'trim', explode( ',', $args[ 'include' ] ) );
  5939. }
  5940. $tr_include = array();
  5941. foreach ( $include as $i ) {
  5942. $t = icl_object_id( $i, $taxonomies[ 0 ], true );
  5943. if ( $t ) {
  5944. $tr_include[ ] = $t;
  5945. }
  5946. }
  5947. $args[ 'include' ] = join( ',', $tr_include );
  5948. $terms = get_terms( $taxonomies, $args );
  5949. }
  5950. return $terms;
  5951. }
  5952. function get_pages_adjust_ids( $pages, $args )
  5953. {
  5954. if ($pages && $this->get_current_language() != $this->get_default_language() ) {
  5955. $cache_key_args = md5(json_encode(wp_list_pluck($pages, 'ID')));
  5956. $cache_key_args .= ":";
  5957. $cache_key_args .= md5(json_encode($args));
  5958. $cache_key = $cache_key_args;
  5959. $cache_group = 'get_pages_adjust_ids';
  5960. $found = false;
  5961. $cached_result = wp_cache_get($cache_key, $cache_group, false, $found);
  5962. if(!$found) {
  5963. $args_updated = false;
  5964. if ( $args[ 'include' ] ) {
  5965. $include = array_map( 'trim', explode( ',', $args[ 'include' ] ) );
  5966. $tr_include = array();
  5967. foreach ( $include as $i ) {
  5968. $t = icl_object_id( $i, 'page', true );
  5969. if ( $t ) {
  5970. $tr_include[ ] = $t;
  5971. }
  5972. }
  5973. $args[ 'include' ] = join( ',', $tr_include );
  5974. $args_updated = true;
  5975. }
  5976. if ( $args[ 'exclude' ] ) {
  5977. $exclude = array_map( 'trim', explode( ',', $args[ 'exclude' ] ) );
  5978. $tr_exclude = array();
  5979. foreach ( $exclude as $i ) {
  5980. $t = icl_object_id( $i, 'page', true );
  5981. if ( $t ) {
  5982. $tr_exclude[ ] = $t;
  5983. }
  5984. }
  5985. $args[ 'exclude' ] = join( ',', $tr_exclude );
  5986. $args_updated = true;
  5987. }
  5988. if ( $args[ 'child_of' ] ) {
  5989. $args[ 'child_of' ] = icl_object_id( $args[ 'child_of' ], 'page', true );
  5990. $args_updated = true;
  5991. }
  5992. if ( $args_updated ) {
  5993. remove_filter( 'get_pages', array( $this, 'get_pages_adjust_ids' ), 1 );
  5994. $pages = get_pages( $args );
  5995. add_filter( 'get_pages', array( $this, 'get_pages_adjust_ids' ), 1, 2 );
  5996. }
  5997. wp_cache_set($cache_key, $pages, $cache_group);
  5998. } else {
  5999. $pages = $cached_result;
  6000. }
  6001. }
  6002. return $pages;
  6003. }
  6004. function category_link_adjust_id( $catlink, $cat_id )
  6005. {
  6006. global $icl_adjust_id_url_filter_off, $wpdb;
  6007. if ( $icl_adjust_id_url_filter_off )
  6008. return $catlink; // special cases when we need the categiry in a different language
  6009. $translated_id = icl_object_id( $cat_id, 'category', true );
  6010. if ( $translated_id && $translated_id != $cat_id ) {
  6011. $translated_id = $wpdb->get_var( "SELECT term_id FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id='{$translated_id}'" );
  6012. remove_filter( 'category_link', array( $this, 'category_link_adjust_id' ), 1 );
  6013. $catlink = get_category_link( $translated_id, 'category' );
  6014. add_filter( 'category_link', array( $this, 'category_link_adjust_id' ), 1, 2 );
  6015. }
  6016. return $catlink;
  6017. }
  6018. // adjacent posts links
  6019. function get_adjacent_post_join( $join )
  6020. {
  6021. global $wpdb;
  6022. $post_type = get_query_var( 'post_type' );
  6023. $cache_key = md5( json_encode( array( $post_type, $join ) ) );
  6024. $cache_group = 'adjacent_post_join';
  6025. $temp_join = wp_cache_get( $cache_key, $cache_group );
  6026. if ( $temp_join ) {
  6027. return $temp_join;
  6028. }
  6029. if ( !$post_type ) {
  6030. $post_type = 'post';
  6031. }
  6032. if ( $this->is_translated_post_type( $post_type ) ) {
  6033. $join .= " JOIN {$wpdb->prefix}icl_translations t ON t.element_id = p.ID AND t.element_type = 'post_{$post_type}'";
  6034. }
  6035. wp_cache_set( $cache_key, $join, $cache_group );
  6036. return $join;
  6037. }
  6038. function get_adjacent_post_where( $where )
  6039. {
  6040. $post_type = get_query_var( 'post_type' );
  6041. $cache_key = md5(json_encode( array( $post_type, $where ) ) );
  6042. $cache_group = 'adjacent_post_where';
  6043. $temp_where = wp_cache_get( $cache_key, $cache_group );
  6044. if ( $temp_where ) {
  6045. return $temp_where;
  6046. }
  6047. if ( !$post_type ) {
  6048. $post_type = 'post';
  6049. }
  6050. if ( $this->is_translated_post_type( $post_type ) ) {
  6051. $where .= " AND language_code = '" . esc_sql( $this->this_lang ) . "'";
  6052. }
  6053. wp_cache_set( $cache_key, $where, $cache_group );
  6054. return $where;
  6055. }
  6056. // feeds links
  6057. function feed_link( $out )
  6058. {
  6059. return $this->convert_url( $out );
  6060. }
  6061. // commenting links
  6062. function post_comments_feed_link( $out )
  6063. {
  6064. if ( $this->settings[ 'language_negotiation_type' ] == 3 ) {
  6065. $out = preg_replace( '@(\?|&)lang=([^/]+)/feed/@i', 'feed/$1lang=$2', $out );
  6066. }
  6067. return $out;
  6068. //return $this->convert_url($out);
  6069. }
  6070. function trackback_url( $out )
  6071. {
  6072. return $this->convert_url( $out );
  6073. }
  6074. function user_trailingslashit( $string, $type_of_url )
  6075. {
  6076. // fixes comment link for when the comments list pagination is enabled
  6077. if ( $type_of_url == 'comment' ) {
  6078. $string = preg_replace( '@(.*)/\?lang=([a-z-]+)/(.*)@is', '$1/$3?lang=$2', $string );
  6079. }
  6080. return $string;
  6081. }
  6082. // archives links
  6083. function getarchives_join( $join )
  6084. {
  6085. global $wpdb;
  6086. $join .= " JOIN {$wpdb->prefix}icl_translations t ON t.element_id = {$wpdb->posts}.ID AND t.element_type='post_post'";
  6087. return $join;
  6088. }
  6089. function getarchives_where( $where )
  6090. {
  6091. $where .= " AND language_code = '" . esc_sql( $this->this_lang ) . "'";
  6092. return $where;
  6093. }
  6094. function archives_link( $out )
  6095. {
  6096. global $icl_archive_url_filter_off;
  6097. if ( !$icl_archive_url_filter_off ) {
  6098. $out = $this->archive_url( $out, $this->this_lang );
  6099. }
  6100. $icl_archive_url_filter_off = false;
  6101. return $out;
  6102. }
  6103. function archive_url( $url, $lang )
  6104. {
  6105. $url = $this->convert_url( $url, $lang );
  6106. return $url;
  6107. }
  6108. function author_link( $url )
  6109. {
  6110. $url = $this->convert_url( $url );
  6111. return preg_replace( '#^http://(.+)//(.+)$#', 'http://$1/$2', $url );
  6112. }
  6113. function pre_option_home( $setting = false )
  6114. {
  6115. if ( !defined( 'TEMPLATEPATH' ) )
  6116. return $setting;
  6117. $template_real_path = realpath( TEMPLATEPATH );
  6118. $debug_backtrace = $this->get_backtrace( 7 ); //Ignore objects and limit to first 7 stack frames, since 6 is the highest index we use
  6119. $inc_methods = array( 'include', 'include_once', 'require', 'require_once' );
  6120. if ( isset( $debug_backtrace[ 4 ] ) && $debug_backtrace[ 4 ][ 'function' ] == 'get_bloginfo' && isset( $debug_backtrace[ 5 ] ) && $debug_backtrace[ 5 ][ 'function' ] == 'bloginfo' ) {
  6121. // case of bloginfo
  6122. $is_template_file = false !== strpos( $debug_backtrace[ 5 ][ 'file' ], $template_real_path );
  6123. $is_direct_call = in_array( $debug_backtrace[ 6 ][ 'function' ], $inc_methods ) || ( false !== strpos( $debug_backtrace[ 6 ][ 'file' ], $template_real_path ) );
  6124. } elseif ( isset( $debug_backtrace[ 4 ] ) && $debug_backtrace[ '4' ][ 'function' ] == 'get_bloginfo' ) {
  6125. // case of get_bloginfo
  6126. $is_template_file = false !== strpos( $debug_backtrace[ 4 ][ 'file' ], $template_real_path );
  6127. $is_direct_call = in_array( $debug_backtrace[ 5 ][ 'function' ], $inc_methods ) || ( false !== strpos( $debug_backtrace[ 5 ][ 'file' ], $template_real_path ) );
  6128. } elseif ( isset( $debug_backtrace[ 4 ] ) && $debug_backtrace[ '4' ][ 'function' ] == 'get_settings' ) {
  6129. // case of get_settings
  6130. $is_template_file = false !== strpos( $debug_backtrace[ 4 ][ 'file' ], $template_real_path );
  6131. $is_direct_call = in_array( $debug_backtrace[ 5 ][ 'function' ], $inc_methods ) || ( false !== strpos( $debug_backtrace[ 5 ][ 'file' ], $template_real_path ) );
  6132. } else {
  6133. // case of get_option
  6134. $is_template_file = isset( $debug_backtrace[ 3 ][ 'file' ] ) && ( false !== strpos( $debug_backtrace[ 3 ][ 'file' ], $template_real_path ) );
  6135. $is_direct_call = isset( $debug_backtrace[ 4 ] ) && in_array( $debug_backtrace[ 4 ][ 'function' ], $inc_methods ) || ( isset( $debug_backtrace[ 4 ][ 'file' ] ) && false !== strpos( $debug_backtrace[ 4 ][ 'file' ], $template_real_path ) );
  6136. }
  6137. //if($dbbt[3]['file'] == @realpath(TEMPLATEPATH . '/header.php')){
  6138. if ( $is_template_file && $is_direct_call ) {
  6139. $ret = $this->language_url( $this->this_lang );
  6140. } else {
  6141. $ret = $setting;
  6142. }
  6143. return $ret;
  6144. }
  6145. function query_vars( $public_query_vars )
  6146. {
  6147. $public_query_vars[ ] = 'lang';
  6148. global $wp_query;
  6149. $wp_query->query_vars[ 'lang' ] = $this->this_lang;
  6150. return $public_query_vars;
  6151. }
  6152. function parse_query( $q )
  6153. {
  6154. global $wp_query, $wpdb;
  6155. //if($q == $wp_query) return; // not touching the WP query
  6156. if ( is_admin() ) {
  6157. return $q;
  6158. }
  6159. $current_language = $this->get_current_language();
  6160. $default_language = $this->get_default_language();
  6161. if ( $current_language != $default_language ) {
  6162. $cat_array = array();
  6163. // cat
  6164. if ( isset( $q->query_vars[ 'cat' ] ) && !empty( $q->query_vars[ 'cat' ] ) ) {
  6165. $cat_array = array_map( 'intval', array_map( 'trim', explode( ',', $q->query_vars[ 'cat' ] ) ) );
  6166. }
  6167. // category_name
  6168. if ( isset( $q->query_vars[ 'category_name' ] ) && !empty( $q->query_vars[ 'category_name' ] ) ) {
  6169. $categories = explode(",", $q->query_vars[ 'category_name' ] );
  6170. $cat_array = array();
  6171. foreach ($categories as $category) {
  6172. $category = trim($category);
  6173. if ($category == "") { // it happens for category_name = "some-cat,some-cat-2,", with comma at end
  6174. continue;
  6175. }
  6176. $cat = get_term_by( 'slug', preg_replace( '#((.*)/)#', '', $category), 'category' );
  6177. if ( !$cat ) {
  6178. $cat = get_term_by( 'name', $category, 'category' );
  6179. }
  6180. if ( isset($cat) && is_object($cat) && $cat->term_id ) {
  6181. $cat_array[] = $cat->term_id;
  6182. }
  6183. }
  6184. if (empty($cat_array)) {
  6185. $q->query_vars[ 'p' ] = -1;
  6186. }
  6187. }
  6188. // category_and
  6189. if ( isset( $q->query_vars[ 'category__and' ] ) && !empty( $q->query_vars[ 'category__and' ] ) ) {
  6190. $cat_array = $q->query_vars[ 'category__and' ];
  6191. }
  6192. // category_in
  6193. if ( isset( $q->query_vars[ 'category__in' ] ) && !empty( $q->query_vars[ 'category__in' ] ) ) {
  6194. $cat_array = array_unique( array_merge( $cat_array, array_map( 'intval', $q->query_vars[ 'category__in' ] ) ) );
  6195. }
  6196. // category__not_in
  6197. if ( isset( $q->query_vars[ 'category__not_in' ] ) && !empty( $q->query_vars[ 'category__not_in' ] ) ) {
  6198. $__cats = array_map( create_function( '$a', 'return -1*intval($a);' ), $q->query_vars[ 'category__not_in' ] );
  6199. $cat_array = array_unique( array_merge( $cat_array, $__cats ) );
  6200. }
  6201. if ( !empty( $cat_array ) ) {
  6202. $translated_ids = array();
  6203. foreach ( $cat_array as $c ) {
  6204. if ( intval( $c ) < 0 ) {
  6205. $sign = -1;
  6206. } else {
  6207. $sign = 1;
  6208. }
  6209. $translated_ids[ ] = $sign * intval( icl_object_id( abs( $c ), 'category', true ) );
  6210. }
  6211. //cat
  6212. if ( isset( $q->query_vars[ 'cat' ] ) && !empty( $q->query_vars[ 'cat' ] ) ) {
  6213. $q->query_vars[ 'cat' ] = join( ',', $translated_ids );
  6214. }
  6215. // category_name
  6216. if ( isset( $q->query_vars[ 'category_name' ] ) && !empty( $q->query_vars[ 'category_name' ] ) ) {
  6217. $_ctmp = get_term_by( 'id', $translated_ids[ 0 ], 'category' );
  6218. $q->query_vars[ 'category_name' ] = $_ctmp->slug;
  6219. }
  6220. // category__and
  6221. if ( isset( $q->query_vars[ 'category__and' ] ) && !empty( $q->query_vars[ 'category__and' ] ) ) {
  6222. $q->query_vars[ 'category__and' ] = $translated_ids;
  6223. }
  6224. // category__in
  6225. if ( isset( $q->query_vars[ 'category__in' ] ) && !empty( $q->query_vars[ 'category__in' ] ) ) {
  6226. $q->query_vars[ 'category__in' ] = array_filter( $translated_ids, create_function( '$a', 'return $a>0;' ) );
  6227. }
  6228. // category__not_in
  6229. if ( isset( $q->query_vars[ 'category__not_in' ] ) && !empty( $q->query_vars[ 'category__not_in' ] ) ) {
  6230. $q->query_vars[ 'category__not_in' ] = array_filter( $translated_ids, create_function( '$a', 'return $a<0;' ) );
  6231. }
  6232. }
  6233. // TAGS
  6234. $tag_array = array();
  6235. // tag
  6236. $tag_glue = '';
  6237. if ( isset( $q->query_vars[ 'tag' ] ) && !empty( $q->query_vars[ 'tag' ] ) ) {
  6238. if ( false !== strpos( $q->query_vars[ 'tag' ], ' ' ) ) {
  6239. $tag_glue = '+';
  6240. $exp = explode( ' ', $q->query_vars[ 'tag' ] );
  6241. } else {
  6242. $tag_glue = ',';
  6243. $exp = explode( ',', $q->query_vars[ 'tag' ] );
  6244. }
  6245. foreach ( $exp as $e ) {
  6246. $tag_array[ ] = $wpdb->get_var( $wpdb->prepare( "SELECT x.term_id FROM $wpdb->terms t
  6247. JOIN $wpdb->term_taxonomy x ON t.term_id=x.term_id WHERE x.taxonomy='post_tag' AND t.slug=%s", $e ) );
  6248. }
  6249. $_tmp = array_unique( $tag_array );
  6250. if ( count( $_tmp ) == 1 && empty( $_tmp[ 0 ] ) ) {
  6251. $tag_array = array();
  6252. }
  6253. }
  6254. // tag_id
  6255. if ( isset( $q->query_vars[ 'tag_id' ] ) && !empty( $q->query_vars[ 'tag_id' ] ) ) {
  6256. $tag_array = array_map( 'trim', explode( ',', $q->query_vars[ 'tag_id' ] ) );
  6257. }
  6258. // tag__and
  6259. if ( isset( $q->query_vars[ 'tag__and' ] ) && !empty( $q->query_vars[ 'tag__and' ] ) ) {
  6260. $tag_array = $q->query_vars[ 'tag__and' ];
  6261. }
  6262. // tag__in
  6263. if ( isset( $q->query_vars[ 'tag__in' ] ) && !empty( $q->query_vars[ 'tag__in' ] ) ) {
  6264. $tag_array = $q->query_vars[ 'tag__in' ];
  6265. }
  6266. // tag__not_in
  6267. if ( isset( $q->query_vars[ 'tag__not_in' ] ) && !empty( $q->query_vars[ 'tag__not_in' ] ) ) {
  6268. $tag_array = $q->query_vars[ 'tag__not_in' ];
  6269. }
  6270. // tag_slug__in
  6271. if ( isset( $q->query_vars[ 'tag_slug__in' ] ) && !empty( $q->query_vars[ 'tag_slug__in' ] ) ) {
  6272. foreach ( $q->query_vars[ 'tag_slug__in' ] as $t ) {
  6273. if ( $tg = $wpdb->get_var( $wpdb->prepare( "
  6274. SELECT x.term_id FROM $wpdb->terms t
  6275. JOIN $wpdb->term_taxonomy x ON t.term_id=x.term_id
  6276. WHERE x.taxonomy='post_tag' AND t.slug=%s", $t ) )
  6277. ) {
  6278. $tag_array[ ] = $tg;
  6279. }
  6280. }
  6281. }
  6282. // tag_slug__and
  6283. if ( isset( $q->query_vars[ 'tag_slug__and' ] ) && !empty( $q->query_vars[ 'tag_slug__and' ] ) ) {
  6284. foreach ( $q->query_vars[ 'tag_slug__and' ] as $t ) {
  6285. $tag_array[ ] = $wpdb->get_var( $wpdb->prepare( "SELECT x.term_id FROM $wpdb->terms t
  6286. JOIN $wpdb->term_taxonomy x ON t.term_id=x.term_id WHERE x.taxonomy='post_tag' AND t.slug=%s", $t ) );
  6287. }
  6288. }
  6289. if ( !empty( $tag_array ) ) {
  6290. $translated_ids = array();
  6291. foreach ( $tag_array as $c ) {
  6292. if ( intval( $c ) < 0 ) {
  6293. $sign = -1;
  6294. } else {
  6295. $sign = 1;
  6296. }
  6297. $_tid = intval( icl_object_id( abs( $c ), 'post_tag', true ) );
  6298. $translated_ids[ ] = $sign * $_tid;
  6299. }
  6300. }
  6301. if ( !empty( $translated_ids ) ) {
  6302. //tag
  6303. if ( isset( $q->query_vars[ 'tag' ] ) && !empty( $q->query_vars[ 'tag' ] ) ) {
  6304. $slugs = $wpdb->get_col( "SELECT slug FROM $wpdb->terms WHERE term_id IN (" . join( ',', $translated_ids ) . ")" );
  6305. $q->query_vars[ 'tag' ] = join( $tag_glue, $slugs );
  6306. }
  6307. //tag_id
  6308. if ( isset( $q->query_vars[ 'tag_id' ] ) && !empty( $q->query_vars[ 'tag_id' ] ) ) {
  6309. $q->query_vars[ 'tag_id' ] = join( ',', $translated_ids );
  6310. }
  6311. // tag__and
  6312. if ( isset( $q->query_vars[ 'tag__and' ] ) && !empty( $q->query_vars[ 'tag__and' ] ) ) {
  6313. $q->query_vars[ 'tag__and' ] = $translated_ids;
  6314. }
  6315. // tag__in
  6316. if ( isset( $q->query_vars[ 'tag__in' ] ) && !empty( $q->query_vars[ 'tag__in' ] ) ) {
  6317. $q->query_vars[ 'tag__in' ] = $translated_ids;
  6318. }
  6319. // tag__not_in
  6320. if ( isset( $q->query_vars[ 'tag__not_in' ] ) && !empty( $q->query_vars[ 'tag__not_in' ] ) ) {
  6321. $q->query_vars[ 'tag__not_in' ] = array_map( 'abs', $translated_ids );
  6322. }
  6323. // tag_slug__in
  6324. if ( isset( $q->query_vars[ 'tag_slug__in' ] ) && !empty( $q->query_vars[ 'tag_slug__in' ] ) ) {
  6325. $q->query_vars[ 'tag_slug__in' ] = $wpdb->get_col( "SELECT slug FROM $wpdb->terms WHERE term_id IN (" . join( ',', $translated_ids ) . ")" );
  6326. }
  6327. // tag_slug__and
  6328. if ( isset( $q->query_vars[ 'tag_slug__and' ] ) && !empty( $q->query_vars[ 'tag_slug__and' ] ) ) {
  6329. $q->query_vars[ 'tag_slug__and' ] = $wpdb->get_col( "SELECT slug FROM $wpdb->terms WHERE term_id IN (" . join( ',', $translated_ids ) . ")" );
  6330. }
  6331. }
  6332. // POST & PAGES
  6333. $post_type = !empty( $q->query_vars[ 'post_type' ] ) ? $q->query_vars[ 'post_type' ] : 'post';
  6334. if(!is_array($post_type)) {
  6335. $post_type = (array)$post_type;
  6336. }
  6337. // page_id
  6338. if ( isset( $q->query_vars[ 'page_id' ] ) && !empty( $q->query_vars[ 'page_id' ] ) ) {
  6339. $q->query_vars[ 'page_id' ] = icl_object_id( $q->query_vars[ 'page_id' ], 'page', true );
  6340. $q->query = preg_replace( '/page_id=[0-9]+/', 'page_id=' . $q->query_vars[ 'page_id' ], $q->query );
  6341. }
  6342. // Adjust included IDs adjusting them with translated element, if present
  6343. if ( isset( $q->query_vars[ 'include' ] ) && !empty( $q->query_vars[ 'include' ] ) ) {
  6344. $include_arr = is_array( $q->query_vars[ 'include' ] ) ? $q->query_vars[ 'include' ] : explode( ',', $q->query_vars[ 'include' ] );
  6345. $include_arr_adjusted = array();
  6346. foreach ( $include_arr as $include_arr_id ) {
  6347. $include_arr_adjusted[ ] = icl_object_id( $include_arr_id, get_post_type($include_arr_id), true );
  6348. }
  6349. $q->query_vars[ 'include' ] = is_array( $q->query_vars[ 'include' ] ) ? $include_arr_adjusted : implode( ',', $include_arr_adjusted );
  6350. }
  6351. // Adjust excluded IDs adjusting them with translated element, if present
  6352. if ( isset( $q->query_vars[ 'exclude' ] ) && !empty( $q->query_vars[ 'exclude' ] ) ) {
  6353. $exclude_arr = is_array( $q->query_vars[ 'exclude' ] ) ? $q->query_vars[ 'exclude' ] : explode( ',', $q->query_vars[ 'exclude' ] );
  6354. $exclude_arr_adjusted = array();
  6355. foreach ( $exclude_arr as $exclude_arr_id ) {
  6356. $exclude_arr_adjusted[ ] = icl_object_id( $exclude_arr_id, get_post_type($exclude_arr_id), true );
  6357. }
  6358. $q->query_vars[ 'exclude' ] = is_array( $q->query_vars[ 'exclude' ] ) ? $exclude_arr_adjusted : implode( ',', $exclude_arr_adjusted );
  6359. }
  6360. // Adjust post id
  6361. if ( isset( $q->query_vars[ 'p' ] ) && !empty( $q->query_vars[ 'p' ] ) ) {
  6362. $q->query_vars[ 'p' ] = icl_object_id( $q->query_vars[ 'p' ], $post_type[0], true );
  6363. }
  6364. // Adjust name
  6365. if ( $this->is_translated_post_type($post_type[0]) && isset( $q->query_vars[ 'name' ] ) && !empty( $q->query_vars[ 'name' ] ) ) {
  6366. $pid_prepared = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name=%s AND post_type=%s", array($q->query_vars[ 'name' ], $post_type[0]));
  6367. $pid = $wpdb->get_var( $pid_prepared );
  6368. if ( !empty( $pid ) ) {
  6369. $q->query_vars[ 'p' ] = icl_object_id( $pid, $post_type[0], true );
  6370. unset( $q->query_vars[ 'name' ] );
  6371. }
  6372. }
  6373. // Adjust page name
  6374. if ( isset( $q->query_vars[ 'pagename' ] ) && !empty( $q->query_vars[ 'pagename' ] ) ) {
  6375. // find the page with the page name in the current language.
  6376. $pid = $wpdb->get_var( $wpdb->prepare( "
  6377. SELECT ID
  6378. FROM $wpdb->posts p
  6379. JOIN {$wpdb->prefix}icl_translations t
  6380. ON p.ID = t.element_id AND element_type='post_page'
  6381. WHERE p.post_name=%s AND t.language_code = %s
  6382. ", $q->query_vars[ 'pagename' ], $current_language ) );
  6383. if ( $pid ) {
  6384. $q->query_vars[ 'page_id' ] = $pid;
  6385. // We have found the page id
  6386. unset( $q->query_vars[ 'pagename' ] );
  6387. if ( $q->query_vars[ 'page_id' ] == get_option( 'page_for_posts' ) ) {
  6388. // it's the blog page.
  6389. $q->is_page = false; // $wp_query->is_page = false;
  6390. $q->is_home = true; // $wp_query->is_home = true;
  6391. $q->is_posts_page = true; // $wp_query->is_posts_page = true;
  6392. }
  6393. }
  6394. }
  6395. // post__in
  6396. if ( isset( $q->query_vars[ 'post__in' ] ) && !empty( $q->query_vars[ 'post__in' ] ) ) {
  6397. $pid = array();
  6398. foreach ( $q->query_vars[ 'post__in' ] as $p ) {
  6399. if ( $post_type ) {
  6400. foreach ( $post_type as $pt ) {
  6401. $pid[ ] = icl_object_id( $p, $pt, true );
  6402. }
  6403. }
  6404. }
  6405. $q->query_vars[ 'post__in' ] = $pid;
  6406. }
  6407. // post__not_in
  6408. if ( isset( $q->query_vars[ 'post__not_in' ] ) && !empty( $q->query_vars[ 'post__not_in' ] ) ) {
  6409. $pid = array();
  6410. foreach ( $q->query_vars[ 'post__not_in' ] as $p ) {
  6411. if ( $post_type ) {
  6412. foreach ( $post_type as $pt ) {
  6413. $pid[ ] = icl_object_id( $p, $pt, true );
  6414. }
  6415. }
  6416. }
  6417. $q->query_vars[ 'post__not_in' ] = $pid;
  6418. }
  6419. // post_parent
  6420. if ( isset( $q->query_vars[ 'post_parent' ] ) && !empty( $q->query_vars[ 'post_parent' ] ) && $q->query_vars[ 'post_type' ] != 'attachment' ) {
  6421. if ( $post_type ) {
  6422. $_parent_type = $wpdb->get_var( $wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE ID=%d", $q->query_vars[ 'post_parent' ] ) );
  6423. $q->query_vars[ 'post_parent' ] = icl_object_id( $q->query_vars[ 'post_parent' ], $_parent_type, true );
  6424. }
  6425. }
  6426. // custom taxonomies
  6427. if ( isset( $q->query_vars[ 'taxonomy' ] ) && $q->query_vars[ 'taxonomy' ] ) {
  6428. $tax_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM {$wpdb->terms} WHERE slug=%s", $q->query_vars[ 'term' ] ) );
  6429. if ( $tax_id ) {
  6430. $translated_tax_id = icl_object_id( $tax_id, $q->query_vars[ 'taxonomy' ], true );
  6431. }
  6432. if ( isset( $translated_tax_id ) ) {
  6433. $q->query_vars[ 'term' ] = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM {$wpdb->terms} WHERE term_id = %d", $translated_tax_id ) );
  6434. $q->query[ $q->query_vars[ 'taxonomy' ] ] = $q->query_vars[ 'term' ];
  6435. }
  6436. }
  6437. // TODO Discuss this. Why WP assumes it's there if query vars are altered?
  6438. // Look at wp-includes/query.php line #2468 search: if ( $this->query_vars_changed ) {
  6439. if ( !isset( $q->query_vars[ 'meta_query' ] ) ) {
  6440. $q->query_vars[ 'meta_query' ] = array();
  6441. }
  6442. if ( isset( $q->query_vars[ 'tax_query' ] ) && is_array( $q->query_vars[ 'tax_query' ] ) ) {
  6443. foreach ( $q->query[ 'tax_query' ] as $num => $fields ) {
  6444. if ( ! isset( $fields[ 'terms' ] ) ) {
  6445. continue;
  6446. }
  6447. if ( is_array( $fields[ 'terms' ] ) ) {
  6448. foreach ( $fields[ 'terms' ] as $term ) {
  6449. $taxonomy = get_term_by( $fields[ 'field' ], $term, $fields[ 'taxonomy' ] );
  6450. if ( is_object( $taxonomy ) ) {
  6451. if ( $fields[ 'field' ] == 'id' ) {
  6452. $field = isset($taxonomy->term_id) ? $taxonomy->term_id : null;
  6453. } else {
  6454. $field = isset($taxonomy->{$fields[ 'field' ]}) ? $taxonomy->{$fields[ 'field' ]} : null;
  6455. }
  6456. //$q->query[ 'tax_query' ][ $num ][ 'terms' ] = array_diff( $q->query[ 'tax_query' ][ $num ][ 'terms' ], array( $term ) ); // removes from array element with original value
  6457. //$q->query[ 'tax_query' ][ $num ][ 'terms' ][ ] = $field;
  6458. //
  6459. //$q->tax_query->queries[ $num ][ 'terms' ] = array_diff( $q->tax_query->queries[ $num ][ 'terms' ], array( $term ) ); // see above
  6460. //$q->tax_query->queries[ $num ][ 'terms' ][ ] = $field;
  6461. //
  6462. //$q->query_vars[ 'tax_query' ][ $num ][ 'terms' ] = array_diff( $q->query_vars[ 'tax_query' ][ $num ][ 'terms' ], array( $term ) ); // see above
  6463. //$q->query_vars[ 'tax_query' ][ $num ][ 'terms' ][ ] = $field;
  6464. $tmp = $q->query[ 'tax_query' ][ $num ][ 'terms' ];
  6465. $tmp = array_diff( (array)$tmp, array( $term ) ); // removes from array element with original value
  6466. $tmp[ ] = $field;
  6467. //Reindex array
  6468. $q->query[ 'tax_query' ][ $num ][ 'terms' ] = array_values( $tmp );
  6469. if (isset($q->tax_query->queries[ $num ][ 'terms' ])) {
  6470. $tmp = $q->tax_query->queries[ $num ][ 'terms' ];
  6471. } else {
  6472. $tmp = array(); // clean $tmp variable
  6473. }
  6474. $tmp = array_diff( (array)$tmp, array( $term ) ); // see above
  6475. $tmp[ ] = $field;
  6476. //Reindex array
  6477. $q->tax_query->queries[ $num ][ 'terms' ] = array_values( $tmp );
  6478. $tmp = $q->query_vars[ 'tax_query' ][ $num ][ 'terms' ];
  6479. $tmp = array_diff( (array)$tmp, array( $term ) ); // see above
  6480. $tmp[ ] = $field;
  6481. //Reindex array
  6482. $q->query_vars[ 'tax_query' ][ $num ][ 'terms' ] = array_values( $tmp );
  6483. if ($fields['field'] == 'id') {
  6484. $field = $taxonomy->term_id;
  6485. } else {
  6486. $field = $taxonomy->{$fields[ 'field' ]};
  6487. }
  6488. unset( $tmp );
  6489. }
  6490. }
  6491. } else if ( is_string( $fields[ 'terms' ] ) ) {
  6492. $taxonomy = get_term_by( $fields[ 'field' ], $fields[ 'terms' ], $fields[ 'taxonomy' ] );
  6493. if ( is_object( $taxonomy ) ) {
  6494. $field = isset($taxonomy->{$fields[ 'field' ]}) ? $taxonomy->{$fields[ 'field' ]} : null;
  6495. $q->query[ 'tax_query' ][ $num ][ 'terms' ] = $field;
  6496. $q->tax_query->queries[ $num ][ 'terms' ][ 0 ] = $field;
  6497. $q->query_vars[ 'tax_query' ][ $num ][ 'terms' ] = $field;
  6498. }
  6499. }
  6500. }
  6501. }
  6502. }
  6503. return $q;
  6504. }
  6505. function adjust_wp_list_pages_excludes( $pages )
  6506. {
  6507. foreach ( $pages as $k => $v ) {
  6508. $pages[ $k ] = icl_object_id( $v, 'page', true );
  6509. }
  6510. return $pages;
  6511. }
  6512. function language_attributes( $output )
  6513. {
  6514. if ( preg_match( '#lang="[a-z-]+"#i', $output ) ) {
  6515. $output = preg_replace( '#lang="([a-z-]+)"#i', 'lang="' . $this->this_lang . '"', $output );
  6516. } else {
  6517. $output .= ' lang="' . $this->this_lang . '"';
  6518. }
  6519. return $output;
  6520. }
  6521. // Localization
  6522. function plugin_localization()
  6523. {
  6524. load_plugin_textdomain( 'sitepress', false, ICL_PLUGIN_FOLDER . '/locale' );
  6525. }
  6526. function locale()
  6527. {
  6528. global $locale;
  6529. add_filter( 'language_attributes', array( $this, '_language_attributes' ) );
  6530. $l = false;
  6531. if(defined('DOING_AJAX') && DOING_AJAX && isset($_REQUEST['action']) && isset($_REQUEST['lang'])) {
  6532. $l = $this->get_locale($_REQUEST['lang']);
  6533. }
  6534. if(!$l) {
  6535. if (defined( 'WP_ADMIN' ) ) {
  6536. if ( get_user_meta( $this->get_current_user()->ID, 'icl_admin_language_for_edit', true ) && icl_is_post_edit() ) {
  6537. $l = $this->get_locale( $this->get_current_language() );
  6538. } else {
  6539. $l = $this->get_locale( $this->admin_language );
  6540. }
  6541. } else {
  6542. $l = $this->get_locale( $this->this_lang );
  6543. }
  6544. }
  6545. if ( $l ) {
  6546. $locale = $l;
  6547. }
  6548. // theme localization
  6549. remove_filter( 'locale', array( $this, 'locale' ) ); //avoid infinite loop
  6550. static $theme_locales_loaded = false;
  6551. if ( !$theme_locales_loaded && !empty( $this->settings[ 'theme_localization_load_textdomain' ] ) && !empty( $this->settings[ 'gettext_theme_domain_name' ] ) && !empty( $this->settings[ 'theme_language_folders' ] )
  6552. ) {
  6553. foreach ( $this->settings[ 'theme_language_folders' ] as $folder ) {
  6554. load_textdomain( $this->settings[ 'gettext_theme_domain_name' ], $folder . '/' . $locale . '.mo' );
  6555. }
  6556. $theme_locales_loaded = true;
  6557. }
  6558. add_filter( 'locale', array( $this, 'locale' ) );
  6559. return $locale;
  6560. }
  6561. function _language_attributes( $latr )
  6562. {
  6563. global $locale;
  6564. $latr = preg_replace( '#lang="(.[a-z])"#i', 'lang="' . str_replace( '_', '-', $locale ) . '"', $latr );
  6565. return $latr;
  6566. }
  6567. function get_language_tag( $code )
  6568. {
  6569. global $wpdb;
  6570. static $all_tags = null;
  6571. if ( is_null( $code ) )
  6572. return false;
  6573. if ( $tag = wp_cache_get( 'icl_language_tags_' . $code ) ) {
  6574. return $tag;
  6575. }
  6576. if($all_tags==null) {
  6577. $all_tags_data = $wpdb->get_results( "SELECT code, tag FROM {$wpdb->prefix}icl_languages" );
  6578. foreach($all_tags_data as $tag_data) {
  6579. $all_tags[$tag_data->code] = $tag_data->tag;
  6580. }
  6581. }
  6582. $tag = $this->get_locale( $code );
  6583. if($all_tags) {
  6584. $tag = isset($all_tags[$code]) ? $all_tags[$code] : false;
  6585. if ( $tag ) {
  6586. wp_cache_set( 'icl_language_tags_' . $code, $tag );
  6587. }
  6588. }
  6589. return $tag;
  6590. }
  6591. function get_locale( $code )
  6592. {
  6593. global $wpdb;
  6594. $all_locales = null;
  6595. if ( is_null( $code ) )
  6596. return false;
  6597. $found = false;
  6598. $locale = wp_cache_get( 'get_locale' . $code, '', false, $found );
  6599. if ( $found ) {
  6600. return $locale;
  6601. }
  6602. $all_locales_data = $wpdb->get_results("SELECT code, locale FROM {$wpdb->prefix}icl_locale_map" );
  6603. foreach($all_locales_data as $locales_data) {
  6604. $all_locales[$locales_data->code] = $locales_data->locale;
  6605. }
  6606. $locale = isset($all_locales[$code]) ? $all_locales[$code] : false;
  6607. if ($locale == false) {
  6608. $this_locale_data = $wpdb->get_row( $wpdb->prepare("SELECT code, default_locale FROM {$wpdb->prefix}icl_languages WHERE code = '%s'", $code) );
  6609. if ($this_locale_data) {
  6610. $locale = $this_locale_data->default_locale;
  6611. }
  6612. }
  6613. wp_cache_set( 'get_locale' . $code, $locale );
  6614. return $locale;
  6615. }
  6616. function switch_locale( $lang_code = false )
  6617. {
  6618. global $l10n;
  6619. static $original_l10n;
  6620. if ( !empty( $lang_code ) ) {
  6621. $original_l10n = $l10n[ 'sitepress' ];
  6622. unset( $l10n[ 'sitepress' ] );
  6623. load_textdomain( 'sitepress', ICL_PLUGIN_PATH . '/locale/sitepress-' . $this->get_locale( $lang_code ) . '.mo' );
  6624. } else { // switch back
  6625. $l10n[ 'sitepress' ] = $original_l10n;
  6626. }
  6627. }
  6628. function get_locale_file_names()
  6629. {
  6630. global $wpdb;
  6631. $locales = array();
  6632. $res = $wpdb->get_results( "
  6633. SELECT lm.code, locale
  6634. FROM {$wpdb->prefix}icl_locale_map lm JOIN {$wpdb->prefix}icl_languages l ON lm.code = l.code AND l.active=1" );
  6635. foreach ( $res as $row ) {
  6636. $locales[ $row->code ] = $row->locale;
  6637. }
  6638. return $locales;
  6639. }
  6640. function set_locale_file_names( $locale_file_names_pairs )
  6641. {
  6642. global $wpdb;
  6643. $lfn = $this->get_locale_file_names();
  6644. $new = array_diff( array_keys( $locale_file_names_pairs ), array_keys( $lfn ) );
  6645. if ( !empty( $new ) ) {
  6646. foreach ( $new as $code ) {
  6647. $wpdb->insert( $wpdb->prefix . 'icl_locale_map', array( 'code' => $code, 'locale' => $locale_file_names_pairs[ $code ] ) );
  6648. }
  6649. }
  6650. $remove = array_diff( array_keys( $lfn ), array_keys( $locale_file_names_pairs ) );
  6651. if ( !empty( $remove ) ) {
  6652. $wpdb->query( "DELETE FROM {$wpdb->prefix}icl_locale_map WHERE code IN (" . join( ',', array_map( create_function( '$a', 'return "\'".$a."\'";' ), $remove ) ) . ")" );
  6653. }
  6654. $update = array_diff( $locale_file_names_pairs, $lfn );
  6655. foreach ( $update as $code => $locale ) {
  6656. $wpdb->update( $wpdb->prefix . 'icl_locale_map', array( 'locale' => $locale ), array( 'code' => $code ) );
  6657. }
  6658. $this->icl_locale_cache->clear();
  6659. return true;
  6660. }
  6661. function pre_option_page_on_front() {
  6662. global $wpdb;
  6663. $result = null;
  6664. static $page_on_front = null;
  6665. if ($page_on_front===false || ($GLOBALS[ 'pagenow' ] == 'site-new.php' && isset( $_REQUEST[ 'action' ] ) && 'add-site' == $_REQUEST[ 'action' ] )) {
  6666. return false;
  6667. }
  6668. $cache_key = $this->this_lang;
  6669. $cache_group = 'pre_option_page_on_front';
  6670. $found = false;
  6671. $result = wp_cache_get( $cache_key, $cache_group, false, $found );
  6672. if ( !$found || ICL_DISABLE_CACHE ) {
  6673. $result = false;
  6674. $page_on_front = $wpdb->get_var( "SELECT option_value FROM {$wpdb->options} WHERE option_name='page_on_front'" );
  6675. if ( $page_on_front ) {
  6676. $_el_lang_det = $this->get_element_language_details( $page_on_front, 'post_page' );
  6677. if ($_el_lang_det && !empty( $_el_lang_det->trid ) ) {
  6678. $trid = $_el_lang_det->trid;
  6679. $translations = $wpdb->get_results( $wpdb->prepare("SELECT element_id, language_code FROM {$wpdb->prefix}icl_translations WHERE trid=%d", $trid ));
  6680. foreach ( $translations as $t ) {
  6681. if ( $t->language_code == $this->this_lang ) {
  6682. $result = $t->element_id;
  6683. $page = get_post( $result );
  6684. if ( !$page || $page->post_type != 'page' ) {
  6685. $result = false;
  6686. }
  6687. }
  6688. $cache_key = $t->language_code;
  6689. $found = false;
  6690. wp_cache_get( $cache_key, $cache_group, false, $found );
  6691. if ( !$found ) {
  6692. wp_cache_set( $cache_key, $result, $cache_group );
  6693. }
  6694. }
  6695. }
  6696. return $result;
  6697. }
  6698. }
  6699. return $result;
  6700. }
  6701. function pre_option_page_for_posts() {
  6702. global $wpdb;
  6703. static $page_for_posts = null;
  6704. static $result = null;
  6705. if ($page_for_posts===false || $result != null ) {
  6706. return $result;
  6707. }
  6708. $cache_key = $this->this_lang;
  6709. $cache_group = 'pre_option_page_for_posts';
  6710. $found = false;
  6711. $result = wp_cache_get($cache_key, $cache_group, false, $found);
  6712. if ( !$found || ICL_DISABLE_CACHE ) {
  6713. $page_for_posts = $wpdb->get_var( "SELECT option_value FROM {$wpdb->options} WHERE option_name='page_for_posts'" );
  6714. $result = false;
  6715. if ( $page_for_posts ) {
  6716. $_el_lang_det = $this->get_element_language_details( $page_for_posts, 'post_page' );
  6717. if ($_el_lang_det && !empty( $_el_lang_det->trid ) ) {
  6718. $trid = $_el_lang_det->trid;
  6719. $translations = $wpdb->get_results( $wpdb->prepare("SELECT element_id, language_code FROM {$wpdb->prefix}icl_translations WHERE trid=%d", $trid ));
  6720. foreach ( $translations as $t ) {
  6721. if ( $t->language_code == $this->this_lang ) {
  6722. $result = $t->element_id;
  6723. }
  6724. //Cache all translations
  6725. $cache_key = $t->language_code;
  6726. wp_cache_set( $cache_key, $result, $cache_group );
  6727. }
  6728. return $result;
  6729. }
  6730. }
  6731. }
  6732. return $result;
  6733. }
  6734. function verify_home_and_blog_pages_translations()
  6735. {
  6736. global $wpdb;
  6737. $warn_home = $warn_posts = '';
  6738. $page_on_front = get_option( 'page_on_front' );
  6739. if ( 'page' == get_option( 'show_on_front' ) && $page_on_front ) {
  6740. $page_home_trid = $wpdb->get_var( "SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id={$page_on_front} AND element_type='post_page'" );
  6741. $page_home_translations = $this->get_element_translations( $page_home_trid, 'post_page' );
  6742. $missing_home = array();
  6743. foreach ( $this->active_languages as $lang ) {
  6744. if ( !isset( $page_home_translations[ $lang[ 'code' ] ] ) ) {
  6745. $missing_home[ ] = $lang[ 'display_name' ];
  6746. } elseif ( $page_home_translations[ $lang[ 'code' ] ]->post_status != 'publish' ) {
  6747. $missing_home[ ] = $lang[ 'display_name' ];
  6748. }
  6749. }
  6750. if ( !empty( $missing_home ) ) {
  6751. $warn_home = '<div class="icl_form_errors" style="font-weight:bold">';
  6752. $warn_home .= sprintf( __( 'Your home page does not exist or its translation is not published in %s.', 'sitepress' ), join( ', ', $missing_home ) );
  6753. $warn_home .= '<br />';
  6754. $warn_home .= '<a href="' . get_edit_post_link( $page_on_front ) . '">' . __( 'Edit this page to add translations', 'sitepress' ) . '</a>';
  6755. $warn_home .= '</div>';
  6756. }
  6757. }
  6758. if ( get_option( 'page_for_posts' ) ) {
  6759. $page_for_posts = get_option( 'page_for_posts' );
  6760. $page_posts_trid = $wpdb->get_var( "SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id={$page_for_posts} AND element_type='post_page'" );
  6761. $page_posts_translations = $this->get_element_translations( $page_posts_trid, 'post_page' );
  6762. $missing_posts = array();
  6763. foreach ( $this->active_languages as $lang ) {
  6764. if ( !isset( $page_posts_translations[ $lang[ 'code' ] ] ) ) {
  6765. $missing_posts[ ] = $lang[ 'display_name' ];
  6766. } elseif ( $page_posts_translations[ $lang[ 'code' ] ]->post_status != 'publish' ) {
  6767. $missing_posts[ ] = $lang[ 'display_name' ];
  6768. }
  6769. }
  6770. if ( !empty( $missing_posts ) ) {
  6771. $warn_posts = '<div class="icl_form_errors" style="font-weight:bold;margin-top:4px;">';
  6772. $warn_posts .= sprintf( __( 'Your blog page does not exist or its translation is not published in %s.', 'sitepress' ), join( ', ', $missing_posts ) );
  6773. $warn_posts .= '<br />';
  6774. $warn_posts .= '<a href="' . get_edit_post_link( $page_for_posts ) . '">' . __( 'Edit this page to add translations', 'sitepress' ) . '</a>';
  6775. $warn_posts .= '</div>';
  6776. }
  6777. }
  6778. return array( $warn_home, $warn_posts );
  6779. }
  6780. // adds the language parameter to the admin post filtering/search
  6781. function restrict_manage_posts()
  6782. {
  6783. echo '<input type="hidden" name="lang" value="' . $this->this_lang . '" />';
  6784. }
  6785. // adds the language parameter to the admin pages search
  6786. function restrict_manage_pages()
  6787. {
  6788. ?>
  6789. <script type="text/javascript">
  6790. addLoadEvent(function () {
  6791. jQuery('p.search-box').append('<input type="hidden" name="lang" value="<?php echo $this->this_lang ?>">');
  6792. });
  6793. </script>
  6794. <?php
  6795. }
  6796. function get_edit_post_link( $link, $id, $context = 'display' )
  6797. {
  6798. global $wpdb;
  6799. $_cache_key = $link . '|' . $id . '|' . $context;
  6800. $cached_edit_post_link = wp_cache_get($_cache_key, 'icl_get_edit_post_link');
  6801. if ( $cached_edit_post_link ) {
  6802. $link = $cached_edit_post_link;
  6803. } else {
  6804. if ( current_user_can( 'edit_post', $id ) ) {
  6805. if ( 'display' == $context )
  6806. $and = '&amp;'; else
  6807. $and = '&';
  6808. if ( $id ) {
  6809. $post_type = $wpdb->get_var( "SELECT post_type FROM {$wpdb->posts} WHERE ID='{$id}'" );
  6810. $details = $this->get_element_language_details( $id, 'post_' . $post_type );
  6811. if ( isset( $details->language_code ) ) {
  6812. $lang = $details->language_code;
  6813. } else {
  6814. $lang = $this->get_current_language();
  6815. }
  6816. $link .= $and . 'lang=' . $lang;
  6817. }
  6818. }
  6819. wp_cache_set($_cache_key , $link,'icl_get_edit_post_link');
  6820. }
  6821. return $link;
  6822. }
  6823. function get_edit_term_link( $link, $term_id, $taxonomy, $object_type )
  6824. {
  6825. global $wpdb;
  6826. $default_language = $this->get_default_language();
  6827. $term_tax_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id=%d AND taxonomy=%s", $term_id, $taxonomy ) );
  6828. $details = $this->get_element_language_details( $term_tax_id, 'tax_' . $taxonomy );
  6829. $and = '&';
  6830. $current_language = $this->get_current_language();
  6831. if ( isset( $details->language_code ) ) {
  6832. $lang = $details->language_code;
  6833. } else {
  6834. $lang = $current_language;
  6835. }
  6836. if ( $lang != $default_language || $current_language != $default_language ) {
  6837. $link .= $and . 'lang=' . $lang;
  6838. }
  6839. return $link;
  6840. }
  6841. function option_sticky_posts( $posts ) {
  6842. global $wpdb;
  6843. if ( is_array( $posts ) && !empty( $posts ) ) {
  6844. $md5_posts = md5( json_encode( $posts ) );
  6845. $cache_key = $this->this_lang . ':' . $md5_posts;
  6846. $cache_group = 'icl_option_sticky_posts';
  6847. $cached_sticky_posts = wp_cache_get( $cache_key, $cache_group );
  6848. if ( $cached_sticky_posts ) {
  6849. return $cached_sticky_posts;
  6850. }
  6851. $new_posts = array();
  6852. $posts = array_filter( $posts, create_function( '$a', 'return $a > 0;' ) );
  6853. if(count($posts)==1) {
  6854. $option_sticky_posts_trids_prepared = $wpdb->prepare( "SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id = %d AND element_type='post_post'", array($posts[0]) );
  6855. } else {
  6856. $option_sticky_posts_trids_prepared = "SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_id IN (" . join( ',', $posts ) . ") AND element_type='post_post'";
  6857. }
  6858. $trids = $wpdb->get_col( $option_sticky_posts_trids_prepared );
  6859. if ( $trids ) {
  6860. if ( count( $trids ) == 1 ) {
  6861. $option_sticky_posts_prepared = $wpdb->prepare( "SELECT trid, element_id, language_code FROM {$wpdb->prefix}icl_translations WHERE trid = %d AND element_type='post_post'", array( $trids[ 0 ] ) );
  6862. } else {
  6863. $option_sticky_posts_prepared = "SELECT trid, element_id, language_code FROM {$wpdb->prefix}icl_translations WHERE trid IN (" . join( ',', $trids ) . ") AND element_type='post_post'";
  6864. }
  6865. $option_sticky_posts = $wpdb->get_results( $option_sticky_posts_prepared );
  6866. foreach ( $option_sticky_posts as $option_sticky_post ) {
  6867. if ( $option_sticky_post->language_code == $this->this_lang ) {
  6868. if ( !in_array( $option_sticky_post->element_id, $new_posts ) ) {
  6869. $new_posts[ ] = $option_sticky_post->element_id;
  6870. }
  6871. }
  6872. }
  6873. wp_cache_set( $cache_key, $new_posts, $cache_group );
  6874. }
  6875. return $new_posts;
  6876. }
  6877. return $posts;
  6878. }
  6879. function request_filter( $request )
  6880. {
  6881. if ( !defined( 'WP_ADMIN' ) && $this->settings[ 'language_negotiation_type' ] == 3 && isset( $request[ 'lang' ] ) ) {
  6882. // Count the parameters that have settings and remove our 'lang ' setting it's the only one.
  6883. // This is required so that home page detection works for other languages.
  6884. $count = 0;
  6885. foreach ( $request as $data ) {
  6886. if ( $data !== '' ) {
  6887. $count += 1;
  6888. }
  6889. }
  6890. if ( $count == 1 ) {
  6891. unset( $request[ 'lang' ] );
  6892. }
  6893. }
  6894. return $request;
  6895. }
  6896. function noscript_notice()
  6897. {
  6898. ?>
  6899. <noscript>
  6900. <div class="error"><?php echo __( 'WPML admin screens require JavaScript in order to display. JavaScript is currently off in your browser.', 'sitepress' ) ?></div></noscript><?php
  6901. }
  6902. function filter_queries( $sql )
  6903. {
  6904. global $wpdb, $pagenow;
  6905. // keep a record of the queries
  6906. $this->queries[ ] = $sql;
  6907. $current_language = $this->get_current_language();
  6908. if ( $pagenow == 'categories.php' || $pagenow == 'edit-tags.php' ) {
  6909. if ( preg_match( '#^SELECT COUNT\(\*\) FROM ' . $wpdb->term_taxonomy . ' WHERE taxonomy = \'(category|post_tag)\' $#', $sql, $matches ) ) {
  6910. $element_type = 'tax_' . $matches[ 1 ];
  6911. $sql = "
  6912. SELECT COUNT(*) FROM {$wpdb->term_taxonomy} tx
  6913. JOIN {$wpdb->prefix}icl_translations tr ON tx.term_taxonomy_id=tr.element_id
  6914. WHERE tx.taxonomy='{$matches[1]}' AND tr.element_type='{$element_type}' AND tr.language_code='" . $current_language . "'";
  6915. }
  6916. }
  6917. if ( $pagenow == 'edit.php' || $pagenow == 'edit-pages.php' ) {
  6918. $post_type = isset( $_GET[ 'post_type' ] ) ? $_GET[ 'post_type' ] : 'post';
  6919. $element_type = 'post_' . $post_type;
  6920. if ( $this->is_translated_post_type( $post_type ) ) {
  6921. if ( preg_match( '#SELECT post_status, COUNT\( \* \) AS num_posts FROM ' . $wpdb->posts . ' WHERE post_type = \'(.+)\' GROUP BY post_status#i', $sql, $matches ) ) {
  6922. if ( 'all' != $current_language ) {
  6923. $sql = '
  6924. SELECT post_status, COUNT( * ) AS num_posts
  6925. FROM ' . $wpdb->posts . ' p
  6926. JOIN ' . $wpdb->prefix . 'icl_translations t ON p.ID = t.element_id
  6927. WHERE p.post_type = \'' . $matches[ 1 ] . '\'
  6928. AND t.element_type=\'' . $element_type . '\'
  6929. AND t.language_code=\'' . $current_language . '\'
  6930. GROUP BY post_status';
  6931. } else {
  6932. $sql = '
  6933. SELECT post_status, COUNT( * ) AS num_posts
  6934. FROM ' . $wpdb->posts . ' p
  6935. JOIN ' . $wpdb->prefix . 'icl_translations t ON p.ID = t.element_id
  6936. JOIN ' . $wpdb->prefix . 'icl_languages l ON t.language_code = l.code AND l.active = 1
  6937. WHERE p.post_type = \'' . $matches[ 1 ] . '\'
  6938. AND t.element_type=\'' . $element_type . '\'
  6939. GROUP BY post_status';
  6940. }
  6941. }
  6942. }
  6943. }
  6944. if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'ajax-tag-search' ) {
  6945. $search = 'SELECT t.name FROM ' . $wpdb->term_taxonomy . ' AS tt INNER JOIN ' . $wpdb->terms . ' AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = \'' . esc_sql( $_GET[ 'tax' ] ) . '\' AND t.name LIKE (\'%' . esc_sql( $_GET[ 'q' ] ) . '%\')';
  6946. if ( $sql == $search ) {
  6947. $parts = parse_url( $_SERVER[ 'HTTP_REFERER' ] );
  6948. @parse_str( $parts[ 'query' ], $query );
  6949. $lang = isset( $query[ 'lang' ] ) ? $query[ 'lang' ] : $this->get_language_cookie();
  6950. $element_type = 'tax_' . $_GET[ 'tax' ];
  6951. $sql = 'SELECT t.name FROM ' . $wpdb->term_taxonomy . ' AS tt
  6952. INNER JOIN ' . $wpdb->terms . ' AS t ON tt.term_id = t.term_id
  6953. JOIN ' . $wpdb->prefix . 'icl_translations tr ON tt.term_taxonomy_id = tr.element_id
  6954. WHERE tt.taxonomy = \'' . esc_sql( $_GET[ 'tax' ] ) . '\' AND tr.language_code=\'' . $lang . '\' AND element_type=\'' . $element_type . '\'
  6955. AND t.name LIKE (\'%' . esc_sql( $_GET[ 'q' ] ) . '%\')
  6956. ';
  6957. }
  6958. }
  6959. // filter get page by path WP 3.9+
  6960. if ( version_compare( $GLOBALS[ 'wp_version' ], '3.9', '>=' ) ) {
  6961. if ( preg_match( "#\n\t\tSELECT ID, post_name, post_parent, post_type\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_name IN \(([^)]+)\)\n\t\tAND post_type IN \(([^)]+)\)#", $sql, $matches ) ) {
  6962. //add 'post_' at the beginning of each post type
  6963. $post_types = explode( ',', str_replace('\'', '', $matches[2]) );
  6964. $element_types = array();
  6965. foreach ($post_types as $post_type){
  6966. $element_types[] = "'post_".$post_type."'";
  6967. }
  6968. $element_types = implode(',', $element_types);
  6969. $sql = "SELECT p.ID, p.post_name, p.post_parent, post_type
  6970. FROM {$wpdb->posts} p
  6971. LEFT JOIN {$wpdb->prefix}icl_translations t on t.element_id = p.ID AND t.element_type IN ({$element_types}) AND t.language_code='" . $current_language . "'
  6972. WHERE p.post_name IN ({$matches[1]}) AND p.post_type IN ({$matches[2]})
  6973. ORDER BY t.language_code='" . $current_language . "' DESC
  6974. ";
  6975. // added order by to ensure that we get the result in teh current language first
  6976. }
  6977. }elseif( version_compare( $GLOBALS[ 'wp_version' ], '3.5', '>=' ) ){
  6978. if ( preg_match( "#SELECT ID, post_name, post_parent, post_type FROM {$wpdb->posts} WHERE post_name IN \(([^)]+)\) AND \(post_type = '([^']+)' OR post_type = 'attachment'\)#", $sql, $matches ) ) {
  6979. $sql = "SELECT p.ID, p.post_name, p.post_parent, post_type
  6980. FROM {$wpdb->posts} p
  6981. LEFT JOIN {$wpdb->prefix}icl_translations t on t.element_id = p.ID AND t.element_type = 'post_{$matches[2]}' AND t.language_code='" . $current_language . "'
  6982. WHERE p.post_name IN ({$matches[1]}) AND (p.post_type = '{$matches[2]}' OR p.post_type = 'attachment')
  6983. ORDER BY t.language_code='" . $current_language . "' DESC
  6984. ";
  6985. // added order by to ensure that we get the result in teh current language first
  6986. }
  6987. } else {
  6988. // filter get page by path WP 3.3+
  6989. if ( preg_match( "#SELECT ID, post_name, post_parent FROM {$wpdb->posts} WHERE post_name IN \(([^)]+)\) AND \(post_type = '([^']+)' OR post_type = 'attachment'\)#", $sql, $matches ) ) {
  6990. $sql = "SELECT p.ID, p.post_name, p.post_parent
  6991. FROM {$wpdb->posts} p
  6992. LEFT JOIN {$wpdb->prefix}icl_translations t on t.element_id = p.ID AND t.element_type = 'post_{$matches[2]}' AND t.language_code='" . $current_language . "'
  6993. WHERE p.post_name IN ({$matches[1]}) AND (p.post_type = '{$matches[2]}' OR p.post_type = 'attachment')
  6994. ORDER BY t.language_code='" . $current_language . "' DESC
  6995. ";
  6996. // added order by to ensure that we get the result in teh current language first
  6997. } // filter get page by path < WP 3.3
  6998. elseif ( preg_match( "#SELECT ID, post_name, post_parent FROM {$wpdb->posts} WHERE post_name = '([^']+)' AND \(post_type = '([^']+)' OR post_type = 'attachment'\)#", $sql, $matches ) ) {
  6999. $sql = "SELECT p.ID, p.post_name, p.post_parent
  7000. FROM {$wpdb->posts} p
  7001. JOIN {$wpdb->prefix}icl_translations t on t.element_id = p.ID AND t.element_type = 'post_{$matches[2]}'
  7002. WHERE p.post_name = '{$matches[1]}' AND (p.post_type = '{$matches[2]}' OR p.post_type = 'attachment')
  7003. AND t.language_code='" . $current_language . "'";
  7004. }
  7005. }
  7006. // filter calendar widget queries
  7007. //elseif( preg_match("##", $sql, $matches) ){
  7008. //
  7009. //}
  7010. return $sql;
  7011. }
  7012. function get_inactive_content()
  7013. {
  7014. global $wpdb;
  7015. $inactive = array();
  7016. $current_language = $this->get_current_language();
  7017. $res_p_prepared = $wpdb->prepare( "
  7018. SELECT COUNT(p.ID) AS c, p.post_type, lt.name AS language FROM {$wpdb->prefix}icl_translations t
  7019. JOIN {$wpdb->posts} p ON t.element_id=p.ID AND t.element_type LIKE %s
  7020. JOIN {$wpdb->prefix}icl_languages l ON t.language_code = l.code AND l.active = 0
  7021. JOIN {$wpdb->prefix}icl_languages_translations lt ON lt.language_code = l.code AND lt.display_language_code=%s
  7022. GROUP BY p.post_type, t.language_code
  7023. ", array( like_escape('post_') . '%', $current_language) );
  7024. $res_p = $wpdb->get_results( $res_p_prepared );
  7025. if ($res_p) {
  7026. foreach ( $res_p as $r ) {
  7027. $inactive[ $r->language ][ $r->post_type ] = $r->c;
  7028. }
  7029. }
  7030. $res_t_query = "
  7031. SELECT COUNT(p.term_taxonomy_id) AS c, p.taxonomy, lt.name AS language FROM {$wpdb->prefix}icl_translations t
  7032. JOIN {$wpdb->term_taxonomy} p ON t.element_id=p.term_taxonomy_id
  7033. JOIN {$wpdb->prefix}icl_languages l ON t.language_code = l.code AND l.active = 0
  7034. JOIN {$wpdb->prefix}icl_languages_translations lt ON lt.language_code = l.code AND lt.display_language_code=%s
  7035. WHERE t.element_type LIKE %s
  7036. GROUP BY p.taxonomy, t.language_code
  7037. ";
  7038. $res_t_query_prepared = $wpdb->prepare($res_t_query, $current_language, like_escape('tax_') . '%');
  7039. $res_t = $wpdb->get_results( $res_t_query_prepared );
  7040. if ($res_t) {
  7041. foreach ( $res_t as $r ) {
  7042. if ( $r->taxonomy == 'category' && $r->c == 1 ) {
  7043. continue; //ignore the case of just the default category that gets automatically created for a new language
  7044. }
  7045. $inactive[ $r->language ][ $r->taxonomy ] = $r->c;
  7046. }
  7047. }
  7048. return $inactive;
  7049. }
  7050. function menu_footer()
  7051. {
  7052. include ICL_PLUGIN_PATH . '/menu/menu-footer.php';
  7053. }
  7054. function _allow_calling_template_file_directly()
  7055. {
  7056. if ( is_404() ) {
  7057. global $wp_query;
  7058. $parts = parse_url( get_bloginfo( 'url' ) );
  7059. if ( !isset( $parts[ 'path' ] ) )
  7060. $parts[ 'path' ] = '';
  7061. $req = str_replace( $parts[ 'path' ], '', $_SERVER[ 'REQUEST_URI' ] );
  7062. if ( file_exists( ABSPATH . $req ) && !is_dir( ABSPATH . $req ) ) {
  7063. $wp_query->is_404 = false;
  7064. header( 'HTTP/1.1 200 OK' );
  7065. include ABSPATH . $req;
  7066. exit;
  7067. }
  7068. }
  7069. }
  7070. function show_user_options()
  7071. {
  7072. global $current_user;
  7073. $active_languages = $this->get_active_languages();
  7074. $default_language = $this->get_default_language();
  7075. $user_language = get_user_meta( $current_user->data->ID, 'icl_admin_language', true );
  7076. if ( $this->settings[ 'admin_default_language' ] == '_default_' ) {
  7077. $this->settings[ 'admin_default_language' ] = $default_language;
  7078. }
  7079. $lang_details = $this->get_language_details( $this->settings[ 'admin_default_language' ] );
  7080. $admin_default_language = $lang_details[ 'display_name' ];
  7081. ?>
  7082. <a name="wpml"></a>
  7083. <h3><?php _e( 'WPML language settings', 'sitepress' ); ?></h3>
  7084. <table class="form-table">
  7085. <tbody>
  7086. <tr>
  7087. <th><?php _e( 'Select your language:', 'sitepress' ) ?></th>
  7088. <td>
  7089. <select name="icl_user_admin_language">
  7090. <option value=""<?php if ( $user_language == $this->settings[ 'admin_default_language' ] )
  7091. echo ' selected="selected"' ?>><?php printf( __( 'Default admin language (currently %s)', 'sitepress' ), $admin_default_language ); ?>&nbsp;</option>
  7092. <?php foreach ( $active_languages as $al ): ?>
  7093. <option value="<?php echo $al[ 'code' ] ?>"<?php if ( $user_language == $al[ 'code' ] )
  7094. echo ' selected="selected"' ?>><?php echo $al[ 'display_name' ];
  7095. if ( $this->admin_language != $al[ 'code' ] )
  7096. echo ' (' . $al[ 'native_name' ] . ')'; ?>&nbsp;</option>
  7097. <?php endforeach; ?>
  7098. </select>
  7099. <span class="description"><?php _e( 'this will be your admin language and will also be used for translating comments.', 'sitepress' ); ?></span>
  7100. <br/>
  7101. <label><input type="checkbox" name="icl_admin_language_for_edit" value="1"
  7102. <?php if (get_user_meta( $this->get_current_user()->ID, 'icl_admin_language_for_edit', true )): ?>checked="checked"<?php endif; ?> />&nbsp;<?php _e( 'Set admin language as editing language.', 'sitepress' ); ?>
  7103. </label>
  7104. </td>
  7105. </tr>
  7106. <?php //display "hidden languages block" only if user can "manage_options"
  7107. if ( current_user_can( 'manage_options' ) || current_user_can('translate') ): ?>
  7108. <tr>
  7109. <th><?php _e( 'Hidden languages:', 'sitepress' ) ?></th>
  7110. <td>
  7111. <p>
  7112. <?php if ( !empty( $this->settings[ 'hidden_languages' ] ) ): ?>
  7113. <?php
  7114. if ( 1 == count( $this->settings[ 'hidden_languages' ] ) ) {
  7115. printf( __( '%s is currently hidden to visitors.', 'sitepress' ), $active_languages[ $this->settings[ 'hidden_languages' ][ 0 ] ][ 'display_name' ] );
  7116. } else {
  7117. $hidden_languages_array = array();
  7118. foreach ( $this->settings[ 'hidden_languages' ] as $l ) {
  7119. $hidden_languages_array[ ] = $active_languages[ $l ][ 'display_name' ];
  7120. }
  7121. $hidden_languages = join( ', ', $hidden_languages_array );
  7122. printf( __( '%s are currently hidden to visitors.', 'sitepress' ), $hidden_languages );
  7123. }
  7124. ?>
  7125. <?php else: ?>
  7126. <?php _e( 'All languages are currently displayed. Choose what to do when site languages are hidden.', 'sitepress' ); ?>
  7127. <?php endif; ?>
  7128. </p>
  7129. <p>
  7130. <label><input name="icl_show_hidden_languages" type="checkbox" value="1" <?php
  7131. if (get_user_meta( $current_user->data->ID, 'icl_show_hidden_languages', true )):?>checked="checked"<?php endif ?> />&nbsp;<?php
  7132. _e( 'Display hidden languages', 'sitepress' ) ?></label>
  7133. </p>
  7134. </td>
  7135. </tr>
  7136. <?php endif; ?>
  7137. </tbody>
  7138. </table>
  7139. <?php
  7140. }
  7141. function save_user_options()
  7142. {
  7143. $user_id = $_POST[ 'user_id' ];
  7144. if ( $user_id ) {
  7145. update_user_meta( $user_id, 'icl_admin_language', $_POST[ 'icl_user_admin_language' ] );
  7146. update_user_meta( $user_id, 'icl_show_hidden_languages', isset( $_POST[ 'icl_show_hidden_languages' ] ) ? intval( $_POST[ 'icl_show_hidden_languages' ] ) : 0 );
  7147. update_user_meta( $user_id, 'icl_admin_language_for_edit', isset( $_POST[ 'icl_admin_language_for_edit' ] ) ? intval( $_POST[ 'icl_admin_language_for_edit' ] ) : 0 );
  7148. $this->icl_locale_cache->clear();
  7149. }
  7150. }
  7151. function help_admin_notice()
  7152. {
  7153. $args = array(
  7154. 'name' => 'wpml-intro',
  7155. 'iso' => defined( 'WPLANG' ) ? WPLANG : '',
  7156. 'src' => get_home_url()
  7157. );
  7158. $q = http_build_query( $args );
  7159. ?>
  7160. <br clear="all"/>
  7161. <div id="message" class="updated message fade" style="clear:both;margin-top:5px;"><p>
  7162. <?php _e( 'WPML is a powerful plugin with many features. Would you like to see a quick overview?', 'sitepress' ); ?>
  7163. </p>
  7164. <p>
  7165. <a href="<?php echo ICL_API_ENDPOINT ?>/destinations/go?<?php echo $q ?>" target="_blank" class="button-primary"><?php _e( 'Yes', 'sitepress' ) ?></a>&nbsp;
  7166. <input type="hidden" id="icl_dismiss_help_nonce" value="<?php echo $icl_dhn = wp_create_nonce( 'dismiss_help_nonce' ) ?>"/>
  7167. <a href="admin.php?page=<?php echo basename( ICL_PLUGIN_PATH ) . '/menu/languages.php&icl_action=dismiss_help&_icl_nonce=' . $icl_dhn; ?>" class="button"><?php _e( 'No thanks, I will configure myself', 'sitepress' ) ?></a>&nbsp;
  7168. <a title="<?php _e( 'Stop showing this message', 'sitepress' ) ?>" id="icl_dismiss_help" href=""><?php _e( 'Dismiss', 'sitepress' ) ?></a>
  7169. </p>
  7170. </div>
  7171. <?php
  7172. }
  7173. function upgrade_notice()
  7174. {
  7175. include ICL_PLUGIN_PATH . '/menu/upgrade_notice.php';
  7176. }
  7177. function icl_reminders()
  7178. {
  7179. include ICL_PLUGIN_PATH . '/menu/icl_reminders.php';
  7180. }
  7181. function add_posts_management_column( $columns )
  7182. {
  7183. global $posts, $wpdb, $__management_columns_posts_translations;
  7184. $element_type = isset( $_REQUEST[ 'post_type' ] ) ? 'post_' . $_REQUEST[ 'post_type' ] : 'post_post';
  7185. if ( count( $this->get_active_languages() ) <= 1 || get_query_var( 'post_status' ) == 'trash' ) {
  7186. return $columns;
  7187. }
  7188. if ( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'inline-save' && $_POST[ 'post_ID' ] ) {
  7189. $p = new stdClass();
  7190. $p->ID = $_POST[ 'post_ID' ];
  7191. $posts = array( $p );
  7192. } elseif ( empty( $posts ) ) {
  7193. return $columns;
  7194. }
  7195. if ( is_null( $__management_columns_posts_translations ) ) {
  7196. $post_ids = array();
  7197. foreach ( $posts as $p ) {
  7198. $post_ids[ ] = $p->ID;
  7199. }
  7200. // get posts translations
  7201. // get trids
  7202. $trid_array = $wpdb->get_col( "
  7203. SELECT trid FROM {$wpdb->prefix}icl_translations WHERE element_type='{$element_type}' AND element_id IN (" . join( ',', $post_ids ) . ")
  7204. " );
  7205. $elements_translations = $wpdb->get_results( "
  7206. SELECT trid, element_id, language_code, source_language_code FROM {$wpdb->prefix}icl_translations WHERE trid IN (" . join( ',', $trid_array ) . ")
  7207. " );
  7208. foreach ( $elements_translations as $v ) {
  7209. $by_trid[ $v->trid ][ ] = $v;
  7210. }
  7211. foreach ( $elements_translations as $v ) {
  7212. if ( in_array( $v->element_id, $post_ids ) ) {
  7213. $el_trid = $v->trid;
  7214. foreach ( $elements_translations as $val ) {
  7215. if ( $val->trid == $el_trid ) {
  7216. $__management_columns_posts_translations[ $v->element_id ][ $val->language_code ] = $val;
  7217. }
  7218. }
  7219. }
  7220. }
  7221. }
  7222. $active_languages = $this->get_active_languages();
  7223. $languages = array();
  7224. foreach ( $active_languages as $v ) {
  7225. if ( $v[ 'code' ] == $this->get_current_language() )
  7226. continue;
  7227. $languages[ ] = $v[ 'code' ];
  7228. }
  7229. $res = $wpdb->get_results( "
  7230. SELECT f.lang_code, f.flag, f.from_template, l.name
  7231. FROM {$wpdb->prefix}icl_flags f
  7232. JOIN {$wpdb->prefix}icl_languages_translations l ON f.lang_code = l.language_code
  7233. WHERE l.display_language_code = '" . $this->admin_language . "' AND f.lang_code IN('" . join( "','", $languages ) . "')
  7234. " );
  7235. foreach ( $res as $r ) {
  7236. if ( $r->from_template ) {
  7237. $wp_upload_dir = wp_upload_dir();
  7238. $flag_path = $wp_upload_dir[ 'baseurl' ] . '/flags/';
  7239. } else {
  7240. $flag_path = ICL_PLUGIN_URL . '/res/flags/';
  7241. }
  7242. $flags[ $r->lang_code ] = '<img src="' . $flag_path . $r->flag . '" width="18" height="12" alt="' . $r->name . '" title="' . $r->name . '" />';
  7243. }
  7244. $flags_column = '';
  7245. foreach ( $active_languages as $v ) {
  7246. if ( isset( $flags[ $v[ 'code' ] ] ) )
  7247. $flags_column .= $flags[ $v[ 'code' ] ];
  7248. }
  7249. $new_columns = array();
  7250. foreach ( $columns as $k => $v ) {
  7251. $new_columns[ $k ] = $v;
  7252. if ( $k == 'title' ) {
  7253. $new_columns[ 'icl_translations' ] = $flags_column;
  7254. }
  7255. }
  7256. return $new_columns;
  7257. }
  7258. function add_content_for_posts_management_column( $column_name )
  7259. {
  7260. if ( $column_name != 'icl_translations' )
  7261. return;
  7262. global $wpdb, $id, $__management_columns_posts_translations, $sitepress, $iclTranslationManagement;
  7263. $active_languages = $this->get_active_languages();
  7264. $current_language = $this->get_current_language();
  7265. foreach ( $active_languages as $v ) {
  7266. if ( $v[ 'code' ] == $current_language )
  7267. continue;
  7268. $post_type = isset( $_REQUEST[ 'post_type' ] ) ? $_REQUEST[ 'post_type' ] : 'post';
  7269. if ( isset( $__management_columns_posts_translations[ $id ][ $v[ 'code' ] ] ) && $__management_columns_posts_translations[ $id ][ $v[ 'code' ] ]->element_id ) {
  7270. // Translation exists
  7271. $exist_translation = true;
  7272. $trid = $this->get_element_trid( $__management_columns_posts_translations[ $id ][ $v[ 'code' ] ]->element_id, 'post_' . $post_type );
  7273. $source_language_code = $wpdb->get_var( $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE trid=%d AND source_language_code IS NULL", $trid ) );
  7274. $needs_update = $wpdb->get_var( $wpdb->prepare( "
  7275. SELECT needs_update
  7276. FROM {$wpdb->prefix}icl_translation_status s JOIN {$wpdb->prefix}icl_translations t ON t.translation_id = s.translation_id
  7277. WHERE t.trid = %d AND t.language_code = '%s'
  7278. ", $trid, $v[ 'code' ] ) );
  7279. if ( $needs_update ) {
  7280. $img = 'needs-update.png';
  7281. $alt = sprintf( __( 'Update %s translation', 'sitepress' ), $v[ 'display_name' ] );
  7282. } else {
  7283. $img = 'edit_translation.png';
  7284. $alt = sprintf( __( 'Edit the %s translation', 'sitepress' ), $v[ 'display_name' ] );
  7285. }
  7286. switch ( $iclTranslationManagement->settings[ 'doc_translation_method' ] ) {
  7287. case ICL_TM_TMETHOD_EDITOR:
  7288. $job_id = $iclTranslationManagement->get_translation_job_id( $__management_columns_posts_translations[ $id ][ $v[ 'code' ] ]->trid, $v[ 'code' ] );
  7289. $args = array( 'lang_from' => $current_language, 'lang_to' => $v[ 'code' ], 'job_id' => @intval( $job_id ) );
  7290. // is a translator of this document?
  7291. $current_user_is_translator = $iclTranslationManagement->is_translator( $this->get_current_user()->ID, $args );
  7292. if ( !$current_user_is_translator ) {
  7293. $img = 'edit_translation_disabled.png';
  7294. $link = '#';
  7295. // is a translator of this language?
  7296. unset( $args[ 'job_id' ] );
  7297. $current_user_is_translator = $iclTranslationManagement->is_translator( $this->get_current_user()->ID, $args );
  7298. if ( $current_user_is_translator ) {
  7299. $alt = sprintf( __( "You can't edit this translation because you're not the translator. <a%s>Learn more.</a>", 'sitepress' ), ' href="https://wpml.org/?page_id=52218"' );
  7300. } else {
  7301. $alt = sprintf( __( "You can't edit this translation because you're not a %s translator. <a%s>Learn more.</a>", 'sitepress' ), $v[ 'display_name' ], ' href="https://wpml.org/?page_id=52218"' );
  7302. }
  7303. } elseif ( $v[ 'code' ] == $source_language_code ) {
  7304. $img = 'edit_translation_disabled.png';
  7305. $link = '#';
  7306. $alt = __( "You can't edit the original document using the translation editor", 'sitepress' );
  7307. } else {
  7308. if ( $job_id ) {
  7309. $link = admin_url( 'admin.php?page=' . WPML_TM_FOLDER . '/menu/translations-queue.php&job_id=' . $job_id . '&lang=' . $v[ 'code' ] );
  7310. } else {
  7311. $link = admin_url( 'admin.php?page=' . WPML_TM_FOLDER . '/menu/translations-queue.php&icl_tm_action=create_job&iclpost[]=' . $id . '&translate_to[' . $v[ 'code' ] . ']=1&iclnonce=' . wp_create_nonce( 'pro-translation-icl' ) . '&lang=' . $v[ 'code' ] );
  7312. }
  7313. }
  7314. break;
  7315. case ICL_TM_TMETHOD_PRO:
  7316. if ( !$__management_columns_posts_translations[ $id ][ $v[ 'code' ] ]->source_language_code ) {
  7317. $link = get_edit_post_link( $__management_columns_posts_translations[ $id ][ $v[ 'code' ] ]->element_id );
  7318. $alt = __( 'Edit the original document', 'sitepress' );
  7319. } else {
  7320. $job_id = $iclTranslationManagement->get_translation_job_id( $__management_columns_posts_translations[ $id ][ $v[ 'code' ] ]->trid, $v[ 'code' ] );
  7321. if ( $job_id ) {
  7322. $job_details = $iclTranslationManagement->get_translation_job( $job_id );
  7323. if ( $job_details->status == ICL_TM_IN_PROGRESS || $job_details->status == ICL_TM_WAITING_FOR_TRANSLATOR ) {
  7324. $img = 'in-progress.png';
  7325. $alt = sprintf( __( 'Translation to %s is in progress', 'sitepress' ), $v[ 'display_name' ] );
  7326. $link = false;
  7327. echo '<img style="padding:1px;margin:2px;" border="0" src="' . ICL_PLUGIN_URL . '/res/img/' . $img . '" title="' . $alt . '" alt="' . $alt . '" width="16" height="16" />';
  7328. } else {
  7329. $link = admin_url( 'admin.php?page=' . WPML_TM_FOLDER . '/menu/translations-queue.php&job_id=' . $job_id );
  7330. }
  7331. }
  7332. }
  7333. break;
  7334. default:
  7335. $link = 'post.php?post_type=' . $post_type . '&action=edit&amp;post=' . $__management_columns_posts_translations[ $id ][ $v[ 'code' ] ]->element_id . '&amp;lang=' . $v[ 'code' ];
  7336. }
  7337. } else {
  7338. // Translation does not exist
  7339. $exist_translation = false;
  7340. $img = 'add_translation.png';
  7341. $alt = sprintf( __( 'Add translation to %s', 'sitepress' ), $v[ 'display_name' ] );
  7342. $default_language = $this->get_default_language();
  7343. $src_lang = $current_language;
  7344. if($src_lang == 'all') {
  7345. $trid = $sitepress->get_element_trid($id, 'post_' . $post_type);
  7346. $element_translations = $sitepress->get_element_translations($trid, 'post_' . $post_type);
  7347. foreach($element_translations as $element_translation) {
  7348. if($element_translation->original) {
  7349. $src_lang = $element_translation->language_code;
  7350. break;
  7351. }
  7352. }
  7353. }
  7354. switch ( $iclTranslationManagement->settings[ 'doc_translation_method' ] ) {
  7355. case ICL_TM_TMETHOD_EDITOR:
  7356. if ( isset( $__management_columns_posts_translations[ $id ][ $v[ 'code' ] ] ) ) {
  7357. $job_id = $iclTranslationManagement->get_translation_job_id( $__management_columns_posts_translations[ $id ][ $v[ 'code' ] ]->trid, $v[ 'code' ] );
  7358. } else {
  7359. $job_id = 0;
  7360. }
  7361. $args = array( 'lang_from' => $src_lang, 'lang_to' => $v[ 'code' ], 'job_id' => @intval( $job_id ) );
  7362. $current_user_is_translator = $iclTranslationManagement->is_translator( $this->get_current_user()->ID, $args );
  7363. if ( $job_id ) {
  7364. if ( $current_user_is_translator ) {
  7365. $job_details = $iclTranslationManagement->get_translation_job( $job_id );
  7366. if ( $job_details && $job_details->status == ICL_TM_IN_PROGRESS ) {
  7367. $img = 'in-progress.png';
  7368. $alt = sprintf( __( 'Translation to %s is in progress', 'sitepress' ), $v[ 'display_name' ] );
  7369. }
  7370. $link = admin_url( 'admin.php?page=' . WPML_TM_FOLDER . '/menu/translations-queue.php&job_id=' . $job_id . '&lang=' . $v[ 'code' ] );
  7371. } else {
  7372. $link = '#';
  7373. $tres = $wpdb->get_row( $wpdb->prepare( "
  7374. SELECT s.* FROM {$wpdb->prefix}icl_translation_status s
  7375. JOIN {$wpdb->prefix}icl_translate_job j ON j.rid = s.rid
  7376. WHERE job_id=%d
  7377. ", $job_id ) );
  7378. if ( $tres->status == ICL_TM_IN_PROGRESS ) {
  7379. $img = 'in-progress.png';
  7380. $alt = sprintf( __( 'Translation to %s is in progress (by a different translator)', 'sitepress' ), $v[ 'display_name' ] );
  7381. } elseif ( $tres->status == ICL_TM_NOT_TRANSLATED || $tres->status == ICL_TM_WAITING_FOR_TRANSLATOR ) {
  7382. $img = 'add_translation_disabled.png';
  7383. $alt = sprintf( __( 'Translation to %s is in progress (by a different translator)', 'sitepress' ), $v[ 'display_name' ] );
  7384. } elseif ( $tres->status == ICL_TM_NEEDS_UPDATE || $tres->status == ICL_TM_COMPLETE ) {
  7385. $img = 'edit_translation_disabled.png';
  7386. $alt = sprintf( __( 'Translation to %s is maintained by a different translator', 'sitepress' ), $v[ 'display_name' ] );
  7387. }
  7388. }
  7389. } else {
  7390. if ( $current_user_is_translator ) {
  7391. $link = admin_url( 'admin.php?page=' . WPML_TM_FOLDER . '/menu/translations-queue.php&icl_tm_action=create_job&iclpost[]=' . $id . '&translate_to[' . $v[ 'code' ] . ']=1&iclnonce=' . wp_create_nonce( 'pro-translation-icl' ) );
  7392. if ( $current_language != $default_language ) {
  7393. $link .= '&translate_from=' . $current_language;
  7394. }
  7395. } else {
  7396. $link = '#';
  7397. $img = 'add_translation_disabled.png';
  7398. $alt = sprintf( __( "You can't add this translation because you're not a %s translator. <a%s>Learn more.</a>", 'sitepress' ), $v[ 'display_name' ], ' href="https://wpml.org/?page_id=52218"' );
  7399. }
  7400. }
  7401. break;
  7402. case ICL_TM_TMETHOD_PRO:
  7403. if ( $this->have_icl_translator( $src_lang, $v[ 'code' ] ) ) {
  7404. if ( !isset( $__management_columns_posts_translations[ $id ][ $v[ 'code' ] ] ) )
  7405. $job_id = false; else
  7406. $job_id = @$iclTranslationManagement->get_translation_job_id( $__management_columns_posts_translations[ $id ][ $v[ 'code' ] ]->trid, $v[ 'code' ] );
  7407. if ( $job_id ) {
  7408. $job_details = $iclTranslationManagement->get_translation_job( $job_id );
  7409. if ( $job_details->status == ICL_TM_IN_PROGRESS || $job_details->status == ICL_TM_WAITING_FOR_TRANSLATOR ) {
  7410. $img = 'in-progress.png';
  7411. $alt = sprintf( __( 'Translation to %s is in progress', 'sitepress' ), $v[ 'display_name' ] );
  7412. $link = false;
  7413. echo '<img style="padding:1px;margin:2px;" border="0" src="' . ICL_PLUGIN_URL . '/res/img/' . $img . '" title="' . $alt . '" alt="' . $alt . '" width="16" height="16" />';
  7414. } else {
  7415. $link = admin_url( 'admin.php?page=' . WPML_TM_FOLDER . '/menu/translations-queue.php&job_id=' . $job_id );
  7416. }
  7417. } else {
  7418. $qs = array();
  7419. if ( !empty( $_SERVER[ 'QUERY_STRING' ] ) )
  7420. foreach ( $_exp = explode( '&', $_SERVER[ 'QUERY_STRING' ] ) as $q => $qv ) {
  7421. $__exp = explode( '=', $qv );
  7422. $__exp[ 0 ] = preg_replace( '#\[(.*)\]#', '', $__exp[ 0 ] );
  7423. if ( !in_array( $__exp[ 0 ], array( 'icl_tm_action', 'translate_from', 'translate_to', 'iclpost', 'service', 'iclnonce' ) ) ) {
  7424. $qs[ $q ] = $qv;
  7425. }
  7426. }
  7427. $link = admin_url( 'edit.php?' . join( '&', $qs ) . '&icl_tm_action=send_jobs&translate_from=' . $src_lang . '&translate_to[' . $v[ 'code' ] . ']=1&iclpost[]=' . $id . '&service=icanlocalize&iclnonce=' . wp_create_nonce( 'pro-translation-icl' ) );
  7428. }
  7429. } else {
  7430. $link = false;
  7431. $alt = sprintf( __( 'Get %s translators', 'sitepress' ), $v[ 'display_name' ] );
  7432. $img = 'add_translators.png';
  7433. echo $this->create_icl_popup_link( "@select-translators;{$src_lang};{$v['code']}@", array(
  7434. 'ar' => 1, 'title' => $alt, 'unload_cb' => 'icl_pt_reload_translation_box'
  7435. ) ) . '<img style="padding:1px;margin:2px;" border="0" src="' . ICL_PLUGIN_URL . '/res/img/' . $img . '" alt="' . $alt . '" width="16" height="16" />' . '</a>';
  7436. }
  7437. break;
  7438. default:
  7439. global $sitepress;
  7440. $trid = $sitepress->get_element_trid( $id, 'post_' . $post_type );
  7441. $link = 'post-new.php?post_type=' . $post_type . '&trid=' . $trid . '&amp;lang=' . $v[ 'code' ] . '&amp;source_lang=' . $src_lang;
  7442. }
  7443. }
  7444. if ( isset($link) && $link ) {
  7445. if ( $link == '#' ) {
  7446. icl_pop_info( $alt, ICL_PLUGIN_URL . '/res/img/' . $img, array( 'icon_size' => 16, 'but_style' => array( 'icl_pop_info_but_noabs' ) ) );
  7447. } else {
  7448. $link = apply_filters( 'wpml_link_to_translation', $link, $exist_translation, $v[ 'code' ] );
  7449. echo '<a href="' . $link . '" title="' . $alt . '">';
  7450. echo '<img style="padding:1px;margin:2px;" border="0" src="' . ICL_PLUGIN_URL . '/res/img/' . $img . '" alt="' . $alt . '" width="16" height="16" />';
  7451. echo '</a>';
  7452. }
  7453. }
  7454. }
  7455. }
  7456. function __set_posts_management_column_width()
  7457. {
  7458. $w = 22 * count( $this->get_active_languages() );
  7459. echo '<style type="text/css">.column-icl_translations{width:' . $w . 'px;}.column-icl_translations img{margin:2px;}</style>';
  7460. }
  7461. function display_wpml_footer()
  7462. {
  7463. if ( $this->settings[ 'promote_wpml' ] ) {
  7464. $wpml_in_other_langs = array( 'es', 'de', 'ja', 'zh-hans' );
  7465. $cl = in_array( ICL_LANGUAGE_CODE, $wpml_in_other_langs ) ? ICL_LANGUAGE_CODE . '/' : '';
  7466. $wpml_in_other_langs_icl = array( 'es', 'fr', 'de' );
  7467. $cl_icl = in_array( ICL_LANGUAGE_CODE, $wpml_in_other_langs_icl ) ? ICL_LANGUAGE_CODE . '/' : '';
  7468. $nofollow_wpml = is_home() ? '' : ' rel="nofollow"';
  7469. if ( in_array( ICL_LANGUAGE_CODE, array( 'ja', 'zh-hans', 'zh-hant', 'ko' ) ) ) {
  7470. // parameters order is set according to teh translation
  7471. echo '<p id="wpml_credit_footer">' . sprintf( __( '<a href="%s"%s>Multilingual WordPress</a> by <a href="%s" rel="nofollow">ICanLocalize</a>', 'sitepress' ), 'http://www.icanlocalize.com/site/' . $cl_icl, 'https://wpml.org/' . $cl, $nofollow_wpml ) . '</p>';
  7472. } else {
  7473. echo '<p id="wpml_credit_footer">' . sprintf( __( '<a href="%s"%s>Multilingual WordPress</a> by <a href="%s" rel="nofollow">ICanLocalize</a>', 'sitepress' ), 'https://wpml.org/' . $cl, $nofollow_wpml, 'http://www.icanlocalize.com/site/' . $cl_icl ) . '</p>';
  7474. }
  7475. }
  7476. }
  7477. function xmlrpc_methods( $methods )
  7478. {
  7479. $methods[ 'icanlocalize.get_languages_list' ] = array( $this, 'xmlrpc_get_languages_list' );
  7480. return $methods;
  7481. }
  7482. function xmlrpc_call_actions( $action )
  7483. {
  7484. global $HTTP_RAW_POST_DATA, $wpdb;
  7485. $params = icl_xml2array( $HTTP_RAW_POST_DATA );
  7486. add_filter( 'is_protected_meta', array( $this, 'xml_unprotect_wpml_meta' ), 10, 3 );
  7487. switch ( $action ) {
  7488. case 'wp.getPage':
  7489. case 'blogger.getPost': // yet this doesn't return custom fields
  7490. if ( isset( $params[ 'methodCall' ][ 'params' ][ 'param' ][ 1 ][ 'value' ][ 'int' ][ 'value' ] ) ) {
  7491. $page_id = $params[ 'methodCall' ][ 'params' ][ 'param' ][ 1 ][ 'value' ][ 'int' ][ 'value' ];
  7492. $lang_details = $this->get_element_language_details( $page_id, 'post_' . get_post_type( $page_id ) );
  7493. $this->this_lang = $lang_details->language_code; // set the current language to the posts language
  7494. update_post_meta( $page_id, '_wpml_language', $lang_details->language_code );
  7495. update_post_meta( $page_id, '_wpml_trid', $lang_details->trid );
  7496. $active_languages = $this->get_active_languages();
  7497. $res = $this->get_element_translations( $lang_details->trid );
  7498. $translations = array();
  7499. foreach ( $active_languages as $k => $v ) {
  7500. if ( $page_id != $res[ $k ]->element_id ) {
  7501. $translations[ $k ] = isset( $res[ $k ]->element_id ) ? $res[ $k ]->element_id : 0;
  7502. }
  7503. }
  7504. update_post_meta( $page_id, '_wpml_translations', json_encode( $translations ) );
  7505. }
  7506. break;
  7507. case 'metaWeblog.getPost':
  7508. if ( isset( $params[ 'methodCall' ][ 'params' ][ 'param' ][ 0 ][ 'value' ][ 'int' ][ 'value' ] ) ) {
  7509. $page_id = $params[ 'methodCall' ][ 'params' ][ 'param' ][ 0 ][ 'value' ][ 'int' ][ 'value' ];
  7510. $lang_details = $this->get_element_language_details( $page_id, 'post_' . get_post_type( $page_id ) );
  7511. $this->this_lang = $lang_details->language_code; // set the current language to the posts language
  7512. update_post_meta( $page_id, '_wpml_language', $lang_details->language_code );
  7513. update_post_meta( $page_id, '_wpml_trid', $lang_details->trid );
  7514. $active_languages = $this->get_active_languages();
  7515. $res = $this->get_element_translations( $lang_details->trid );
  7516. $translations = array();
  7517. foreach ( $active_languages as $k => $v ) {
  7518. if ( isset( $res[ $k ] ) && $page_id != $res[ $k ]->element_id ) {
  7519. $translations[ $k ] = isset( $res[ $k ]->element_id ) ? $res[ $k ]->element_id : 0;
  7520. }
  7521. }
  7522. update_post_meta( $page_id, '_wpml_translations', json_encode( $translations ) );
  7523. }
  7524. break;
  7525. case 'metaWeblog.getRecentPosts':
  7526. if ( isset( $params[ 'methodCall' ][ 'params' ][ 'param' ][ 3 ][ 'value' ][ 'int' ][ 'value' ] ) ) {
  7527. $num_posts = intval( $params[ 'methodCall' ][ 'params' ][ 'param' ][ 3 ][ 'value' ][ 'int' ][ 'value' ] );
  7528. if ( $num_posts ) {
  7529. $posts = get_posts( 'suppress_filters=false&numberposts=' . $num_posts );
  7530. foreach ( $posts as $p ) {
  7531. $lang_details = $this->get_element_language_details( $p->ID, 'post_post' );
  7532. update_post_meta( $p->ID, '_wpml_language', $lang_details->language_code );
  7533. update_post_meta( $p->ID, '_wpml_trid', $lang_details->trid );
  7534. $active_languages = $this->get_active_languages();
  7535. $res = $this->get_element_translations( $lang_details->trid );
  7536. $translations = array();
  7537. foreach ( $active_languages as $k => $v ) {
  7538. if ( $p->ID != $res[ $k ]->element_id ) {
  7539. $translations[ $k ] = isset( $res[ $k ]->element_id ) ? $res[ $k ]->element_id : 0;
  7540. }
  7541. }
  7542. update_post_meta( $p->ID, '_wpml_translations', json_encode( $translations ) );
  7543. }
  7544. }
  7545. }
  7546. break;
  7547. case 'metaWeblog.newPost':
  7548. $custom_fields = false;
  7549. if ( is_array( $params[ 'methodCall' ][ 'params' ][ 'param' ][ 3 ][ 'value' ][ 'struct' ][ 'member' ] ) ) {
  7550. foreach ( $params[ 'methodCall' ][ 'params' ][ 'param' ][ 3 ][ 'value' ][ 'struct' ][ 'member' ] as $m ) {
  7551. if ( $m[ 'name' ][ 'value' ] == 'custom_fields' ) {
  7552. $custom_fields_raw = $m[ 'value' ][ 'array' ][ 'data' ][ 'value' ];
  7553. break;
  7554. }
  7555. }
  7556. }
  7557. if ( !empty( $custom_fields_raw ) ) {
  7558. foreach ( $custom_fields_raw as $cf ) {
  7559. $key = $value = null;
  7560. foreach ( $cf[ 'struct' ][ 'member' ] as $m ) {
  7561. if ( $m[ 'name' ][ 'value' ] == 'key' )
  7562. $key = $m[ 'value' ][ 'string' ][ 'value' ]; elseif ( $m[ 'name' ][ 'value' ] == 'value' )
  7563. $value = $m[ 'value' ][ 'string' ][ 'value' ];
  7564. }
  7565. if ( $key !== null && $value !== null )
  7566. $custom_fields[ $key ] = $value;
  7567. }
  7568. }
  7569. if ( is_array( $custom_fields ) && isset( $custom_fields[ '_wpml_language' ] ) && isset( $custom_fields[ '_wpml_trid' ] ) ) {
  7570. $icl_post_language = $custom_fields[ '_wpml_language' ];
  7571. $icl_trid = $custom_fields[ '_wpml_trid' ];
  7572. $post_type = $params[ 'methodCall' ][ 'params' ][ 'param' ][ 3 ][ 'value' ][ 'struct' ][ 'member' ][ 2 ][ 'value' ][ 'string' ][ 'value' ];
  7573. if ( !$wpdb->get_var( "SELECT translation_id FROM {$wpdb->prefix}icl_translations WHERE element_type='post_{$post_type}' AND trid={$icl_trid} AND language_code='{$icl_post_language}'" ) ) {
  7574. $_POST[ 'icl_post_language' ] = $icl_post_language;
  7575. $_POST[ 'icl_trid' ] = $icl_trid;
  7576. } else {
  7577. $IXR_Error = new IXR_Error( 401, __( 'A translation for this post already exists', 'sitepress' ) );
  7578. echo $IXR_Error->getXml();
  7579. exit( 1 );
  7580. }
  7581. }
  7582. break;
  7583. case 'metaWeblog.editPost':
  7584. $post_id = $params[ 'methodCall' ][ 'params' ][ 'param' ][ 0 ][ 'value' ][ 'int' ][ 'value' ];
  7585. if ( !$post_id ) {
  7586. break;
  7587. }
  7588. $custom_fields = $params[ 'methodCall' ][ 'params' ][ 'param' ][ 3 ][ 'value' ][ 'struct' ][ 'member' ][ 3 ][ 'value' ][ 'array' ][ 'data' ][ 'value' ];
  7589. if ( is_array( $custom_fields ) ) {
  7590. $icl_trid = false;
  7591. $icl_post_language = false;
  7592. foreach ( $custom_fields as $cf ) {
  7593. if ( $cf[ 'struct' ][ 'member' ][ 0 ][ 'value' ][ 'string' ][ 'value' ] == '_wpml_language' ) {
  7594. $icl_post_language = $cf[ 'struct' ][ 'member' ][ 1 ][ 'value' ][ 'string' ][ 'value' ];
  7595. } elseif ( $cf[ 'struct' ][ 'member' ][ 0 ][ 'value' ][ 'string' ][ 'value' ] == '_wpml_trid' ) {
  7596. $icl_trid = $cf[ 'struct' ][ 'member' ][ 1 ][ 'value' ][ 'string' ][ 'value' ];
  7597. }
  7598. }
  7599. $epost_id = $wpdb->get_var( "SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type='post_post'
  7600. AND trid={$icl_trid} AND language_code='{$icl_post_language}'" );
  7601. if ( $icl_trid && $icl_post_language && ( !$epost_id || $epost_id == $post_id ) ) {
  7602. $_POST[ 'icl_post_language' ] = $icl_post_language;
  7603. $_POST[ 'icl_trid' ] = $icl_trid;
  7604. } else {
  7605. $IXR_Error = new IXR_Error( 401, __( 'A translation in this language already exists', 'sitepress' ) );
  7606. echo $IXR_Error->getXml();
  7607. exit( 1 );
  7608. }
  7609. }
  7610. break;
  7611. }
  7612. }
  7613. function xmlrpc_get_languages_list( $lang )
  7614. {
  7615. global $wpdb;
  7616. if ( !is_null( $lang ) ) {
  7617. if ( !$wpdb->get_var( "SELECT code FROM {$wpdb->prefix}icl_languages WHERE code='" . esc_sql( $lang ) . "'" ) ) {
  7618. $IXR_Error = new IXR_Error( 401, __( 'Invalid language code', 'sitepress' ) );
  7619. echo $IXR_Error->getXml();
  7620. exit( 1 );
  7621. }
  7622. $this->admin_language = $lang;
  7623. }
  7624. define( 'WP_ADMIN', true ); // hack - allow to force display language
  7625. $active_languages = $this->get_active_languages( true );
  7626. return $active_languages;
  7627. }
  7628. function xml_unprotect_wpml_meta( $protected, $meta_key, $meta_type )
  7629. {
  7630. $metas_list = array( '_wpml_trid', '_wpml_translations', '_wpml_language' );
  7631. if ( in_array( $meta_key, $metas_list, true ) ) {
  7632. $protected = false;
  7633. }
  7634. return $protected;
  7635. }
  7636. function get_current_action_step()
  7637. {
  7638. global $wpdb;
  7639. $icl_lang_status = $this->settings[ 'icl_lang_status' ];
  7640. $has_translators = false;
  7641. foreach ( (array)$icl_lang_status as $k => $lang ) {
  7642. if ( !is_numeric( $k ) )
  7643. continue;
  7644. if ( !empty( $lang[ 'translators' ] ) ) {
  7645. $has_translators = true;
  7646. break;
  7647. }
  7648. }
  7649. if ( !$has_translators ) {
  7650. return 0;
  7651. }
  7652. $cms_count = $wpdb->get_var( "SELECT COUNT(rid) FROM {$wpdb->prefix}icl_core_status WHERE status=3" );
  7653. if ( $cms_count > 0 ) {
  7654. return 4;
  7655. }
  7656. $cms_count = $wpdb->get_var( "SELECT COUNT(rid) FROM {$wpdb->prefix}icl_core_status WHERE 1" );
  7657. if ( $cms_count == 0 ) {
  7658. // No documents sent yet
  7659. return 1;
  7660. }
  7661. if ( $this->settings[ 'icl_balance' ] <= 0 ) {
  7662. return 2;
  7663. }
  7664. return 3;
  7665. }
  7666. function show_action_list()
  7667. {
  7668. $steps = array(
  7669. __( 'Select translators', 'sitepress' ), __( 'Send documents to translation', 'sitepress' ), __( 'Deposit payment', 'sitepress' ), __( 'Translations will be returned to your site', 'sitepress' )
  7670. );
  7671. $current_step = $this->get_current_action_step();
  7672. if ( $current_step >= sizeof( $steps ) ) {
  7673. // everything is already setup.
  7674. if ( $this->settings[ 'last_action_step_shown' ] ) {
  7675. return '';
  7676. } else {
  7677. $this->save_settings( array( 'last_action_step_shown' => 1 ) );
  7678. }
  7679. }
  7680. $output = '
  7681. <h3>' . __( 'Setup check list', 'sitepress' ) . '</h3>
  7682. <ul id="icl_check_list">';
  7683. foreach ( $steps as $index => $step ) {
  7684. $step_data = $step;
  7685. if ( $index < $current_step || ( $index == 4 && $this->settings[ 'icl_balance' ] > 0 ) ) {
  7686. $attr = ' class="icl_tick"';
  7687. } else {
  7688. $attr = ' class="icl_next_step"';
  7689. }
  7690. if ( $index == $current_step ) {
  7691. $output .= '<li class="icl_info"><b>' . $step_data . '</b></li>';
  7692. } else {
  7693. $output .= '<li' . $attr . '>' . $step_data . '</li>';
  7694. }
  7695. $output .= "\n";
  7696. }
  7697. $output .= '
  7698. </ul>';
  7699. return $output;
  7700. }
  7701. function show_pro_sidebar()
  7702. {
  7703. $output = '<div id="icl_sidebar" class="icl_sidebar" style="display:none">';
  7704. $action_list = $this->show_action_list();
  7705. $show_minimized = $this->settings[ 'icl_sidebar_minimized' ];
  7706. if ( $action_list != '' ) {
  7707. $show_minimized = false;
  7708. }
  7709. if ( $show_minimized ) {
  7710. $output .= '<div id="icl_sidebar_full" style="display:none">';
  7711. } else {
  7712. $output .= '<div id="icl_sidebar_full">';
  7713. }
  7714. if ( $action_list == '' ) {
  7715. $output .= '<a id="icl_sidebar_hide" href="#">hide</a>';
  7716. } else {
  7717. $output .= $action_list;
  7718. }
  7719. $output .= '<h3>' . __( 'Help', 'sitepress' ) . '</h3>';
  7720. $output .= '<div id="icl_help_links"></div>';
  7721. $output .= wp_nonce_field( 'icl_help_links_nonce', '_icl_nonce_hl', false, false );
  7722. $output .= '</div>';
  7723. if ( $show_minimized ) {
  7724. $output .= '<div id="icl_sidebar_hide_div">';
  7725. } else {
  7726. $output .= '<div id="icl_sidebar_hide_div" style="display:none">';
  7727. }
  7728. $output .= '<a id="icl_sidebar_show" href="#"><img width="16" height="16" src="' . ICL_PLUGIN_URL . '/res/img/question1.png' . '" alt="' . __( 'Get help', 'sitepress' ) . '" title="' . __( 'Get help', 'sitepress' ) . '" /></a>';
  7729. $output .= wp_nonce_field( 'icl_show_sidebar_nonce', '_icl_nonce_ss', false, false );
  7730. $output .= '</div>';
  7731. $output .= '</div>';
  7732. return $output;
  7733. }
  7734. function meta_generator_tag()
  7735. {
  7736. $lids = array();
  7737. $active_languages = $this->get_active_languages();
  7738. if($active_languages) {
  7739. foreach ( $active_languages as $l ) {
  7740. $lids[ ] = $l[ 'id' ];
  7741. }
  7742. $stt = join( ",", $lids );
  7743. $stt .= ";" . intval( $this->get_icl_translation_enabled() );
  7744. printf( '<meta name="generator" content="WPML ver:%s stt:%s" />' . PHP_EOL, ICL_SITEPRESS_VERSION, $stt );
  7745. }
  7746. }
  7747. function set_language_cookie()
  7748. {
  7749. if ( !headers_sent() ) {
  7750. if ( preg_match( '@\.(css|js|png|jpg|gif|jpeg|bmp)@i', basename( preg_replace( '@\?.*$@', '', $_SERVER[ 'REQUEST_URI' ] ) ) ) || isset( $_POST[ 'icl_ajx_action' ] ) || isset( $_POST[ '_ajax_nonce' ] ) || defined( 'DOING_AJAX' ) ) {
  7751. return;
  7752. }
  7753. $server_host_name = $this->get_server_host_name();
  7754. $cookie_domain = defined( 'COOKIE_DOMAIN' ) ? COOKIE_DOMAIN : $server_host_name;
  7755. $cookie_path = defined( 'COOKIEPATH' ) ? COOKIEPATH : '/';
  7756. setcookie( '_icl_current_language', $this->get_current_language(), time() + 86400, $cookie_path, $cookie_domain );
  7757. }
  7758. }
  7759. function update_language_cookie($language_code) {
  7760. $_COOKIE[ '_icl_current_language' ] = $language_code;
  7761. }
  7762. function get_language_cookie()
  7763. {
  7764. static $active_languages = false;
  7765. if ( isset( $_COOKIE[ '_icl_current_language' ] ) ) {
  7766. $lang = substr( $_COOKIE[ '_icl_current_language' ], 0, 10 );
  7767. if(!$active_languages) {
  7768. $active_languages = $this->get_active_languages();
  7769. }
  7770. if ( !isset( $active_languages[ $lang ] ) ) {
  7771. $lang = $this->get_default_language();
  7772. }
  7773. } else {
  7774. $lang = '';
  7775. }
  7776. return $lang;
  7777. }
  7778. // _icl_current_language will have to be replaced with _icl_current_language
  7779. function set_admin_language_cookie( $lang = false )
  7780. {
  7781. if ( !headers_sent() ) {
  7782. if ( preg_match( '@\.(css|js|png|jpg|gif|jpeg|bmp)@i', basename( preg_replace( '@\?.*$@', '', $_SERVER[ 'REQUEST_URI' ] ) ) ) || isset( $_POST[ 'icl_ajx_action' ] ) || isset( $_POST[ '_ajax_nonce' ] ) ) {
  7783. return;
  7784. }
  7785. $parts = parse_url( admin_url() );
  7786. $cookie_path = $parts[ 'path' ];
  7787. if ( $lang === false )
  7788. $lang = $this->get_current_language();
  7789. setcookie( '_icl_current_admin_language', $lang, time() + 7200, $cookie_path );
  7790. }
  7791. }
  7792. function get_admin_language_cookie()
  7793. {
  7794. static $active_languages = false;
  7795. if ( isset( $_COOKIE[ '_icl_current_admin_language' ] ) ) {
  7796. $lang = $_COOKIE[ '_icl_current_admin_language' ];
  7797. if(!$active_languages) {
  7798. $active_languages = $this->get_active_languages();
  7799. }
  7800. if ( !isset( $active_languages[ $lang ] ) && $lang != 'all' ) {
  7801. $lang = $this->get_default_language();
  7802. }
  7803. } else {
  7804. $lang = '';
  7805. }
  7806. return $lang;
  7807. }
  7808. function reset_admin_language_cookie()
  7809. {
  7810. $this->set_admin_language_cookie( $this->get_default_language() );
  7811. }
  7812. function rewrite_rules_filter( $value ) {
  7813. $current_language = $this->get_current_language();
  7814. $default_language = $this->get_default_language();
  7815. $directory_for_default_language = false;
  7816. $setting_url = $this->get_setting( 'urls' );
  7817. if ( $setting_url ) {
  7818. $directory_for_default_language = $setting_url[ 'directory_for_default_language' ];
  7819. }
  7820. if ( $this->get_setting( 'language_negotiation_type' ) == 1 && ( $current_language != $default_language || $directory_for_default_language ) ) {
  7821. foreach ( (array)$value as $k => $v ) {
  7822. $value[ $current_language . '/' . $k ] = $v;
  7823. unset( $value[ $k ] );
  7824. }
  7825. $value[ $current_language . '/?$' ] = 'index.php';
  7826. }
  7827. return $value;
  7828. }
  7829. function is_rtl( $lang = false )
  7830. {
  7831. if ( is_admin() ) {
  7832. if ( empty( $lang ) )
  7833. $lang = $this->get_admin_language();
  7834. } else {
  7835. if ( empty( $lang ) )
  7836. $lang = $this->get_current_language();
  7837. }
  7838. return in_array( $lang, array( 'ar', 'he', 'fa', 'ku' ) );
  7839. }
  7840. function get_translatable_documents( $include_not_synced = false )
  7841. {
  7842. global $wp_post_types;
  7843. $icl_post_types = array();
  7844. $attachment_is_translatable = $this->is_translated_post_type( 'attachment' );
  7845. $exceptions = array( 'revision', 'nav_menu_item' );
  7846. if(!$attachment_is_translatable) {
  7847. $exceptions[] = 'attachment';
  7848. }
  7849. foreach ( $wp_post_types as $k => $v ) {
  7850. if ( !in_array( $k, $exceptions ) ) {
  7851. if ( !$include_not_synced && ( empty( $this->settings[ 'custom_posts_sync_option' ][ $k ] ) || $this->settings[ 'custom_posts_sync_option' ][ $k ] != 1 ) && !in_array( $k, array( 'post', 'page' ) ) )
  7852. continue;
  7853. $icl_post_types[ $k ] = $v;
  7854. }
  7855. }
  7856. $icl_post_types = apply_filters( 'get_translatable_documents', $icl_post_types );
  7857. return $icl_post_types;
  7858. }
  7859. function get_translatable_taxonomies( $include_not_synced = false, $object_type = 'post' )
  7860. {
  7861. global $wp_taxonomies;
  7862. $t_taxonomies = array();
  7863. if ( $include_not_synced ) {
  7864. if ( in_array( $object_type, $wp_taxonomies[ 'post_tag' ]->object_type ) )
  7865. $t_taxonomies[ ] = 'post_tag';
  7866. if ( in_array( $object_type, $wp_taxonomies[ 'category' ]->object_type ) )
  7867. $t_taxonomies[ ] = 'category';
  7868. }
  7869. foreach ( $wp_taxonomies as $taxonomy_name => $taxonomy ) {
  7870. // exceptions
  7871. if ( 'post_format' == $taxonomy_name )
  7872. continue;
  7873. if ( in_array( $object_type, $taxonomy->object_type ) && !empty( $this->settings[ 'taxonomies_sync_option' ][ $taxonomy_name ] ) ) {
  7874. $t_taxonomies[ ] = $taxonomy_name;
  7875. }
  7876. }
  7877. if ( has_filter( 'get_translatable_taxonomies' ) ) {
  7878. $filtered = apply_filters( 'get_translatable_taxonomies', array( 'taxs' => $t_taxonomies, 'object_type' => $object_type ) );
  7879. $t_taxonomies = $filtered[ 'taxs' ];
  7880. if ( empty( $t_taxonomies ) )
  7881. $t_taxonomies = array();
  7882. }
  7883. return $t_taxonomies;
  7884. }
  7885. function is_translated_taxonomy( $tax )
  7886. {
  7887. global $sitepress_settings;
  7888. $settings = empty( $sitepress_settings ) ? $this->settings : $sitepress_settings;
  7889. $ret = false;
  7890. if ( is_scalar( $tax ) ) {
  7891. switch ( $tax ) {
  7892. case 'category':
  7893. case 'post_tag':
  7894. $ret = true;
  7895. break;
  7896. default:
  7897. if ( isset( $settings[ 'taxonomies_sync_option' ][ $tax ] ) ) {
  7898. $ret = $settings[ 'taxonomies_sync_option' ][ $tax ];
  7899. } elseif ( isset( $settings[ 'translation-management' ][ 'taxonomies_readonly_config' ][ $tax ] ) && $settings[ 'translation-management' ][ 'taxonomies_readonly_config' ][ $tax ] == 1 ) {
  7900. $ret = true;
  7901. } else {
  7902. $ret = false;
  7903. }
  7904. }
  7905. }
  7906. return $ret;
  7907. }
  7908. function is_translated_post_type( $type )
  7909. {
  7910. global $sitepress_settings;
  7911. $settings = empty( $sitepress_settings ) ? $this->settings : $sitepress_settings;
  7912. $ret = false;
  7913. if ( is_scalar( $type ) ) {
  7914. switch ( $type ) {
  7915. case 'post':
  7916. case 'page':
  7917. $ret = true;
  7918. break;
  7919. default:
  7920. if ( isset( $settings[ 'custom_posts_sync_option' ][ $type ] ) ) {
  7921. $ret = $settings[ 'custom_posts_sync_option' ][ $type ];
  7922. } elseif ( isset( $settings[ 'translation-management' ][ 'custom_types_readonly_config' ][ $type ] ) ) {
  7923. $ret = $settings[ 'translation-management' ][ 'custom_types_readonly_config' ][ $type ];
  7924. } else {
  7925. $ret = false;
  7926. }
  7927. }
  7928. }
  7929. return $ret;
  7930. }
  7931. function print_translatable_custom_content_status()
  7932. {
  7933. global $wp_taxonomies;
  7934. $icl_post_types = $this->get_translatable_documents( true );
  7935. $cposts = array();
  7936. $notice = '';
  7937. foreach ( $icl_post_types as $k => $v ) {
  7938. if ( !in_array( $k, array( 'post', 'page' ) ) ) {
  7939. $cposts[ $k ] = $v;
  7940. }
  7941. }
  7942. foreach ( $cposts as $k => $cpost ) {
  7943. if ( !isset( $this->settings[ 'custom_posts_sync_option' ][ $k ] ) ) {
  7944. $cposts_sync_not_set[ ] = $cpost->labels->name;
  7945. }
  7946. }
  7947. if ( defined( 'WPML_TM_VERSION' ) && !empty( $cposts_sync_not_set ) ) {
  7948. $notice = '<p class="updated fade">';
  7949. $notice .= sprintf( __( "You haven't set your <a %s>synchronization preferences</a> for these custom posts: %s. Default value was selected.", 'sitepress' ), 'href="admin.php?page=' . WPML_TM_FOLDER . '/menu/main.php&sm=mcsetup"', '<i>' . join( '</i>, <i>', $cposts_sync_not_set ) . '</i>' );
  7950. $notice .= '</p>';
  7951. }
  7952. $icl_post_types = $this->get_translatable_documents( true );
  7953. if ( defined( 'WPML_TM_VERSION' ) && $icl_post_types ) {
  7954. global $wpdb, $sitepress_settings;
  7955. $default_language = $this->get_default_language();
  7956. $custom_posts = array();
  7957. $icl_post_types = $this->get_translatable_documents( true );
  7958. foreach ( $icl_post_types as $k => $v ) {
  7959. if ( !in_array( $k, array( 'post', 'page' ) ) ) {
  7960. $custom_posts[ $k ] = $v;
  7961. }
  7962. }
  7963. foreach ( $custom_posts as $k => $custom_post ) {
  7964. $_has_slug = isset( $custom_post->rewrite[ 'slug' ] ) && $custom_post->rewrite[ 'slug' ];
  7965. $_translate = !empty($sitepress_settings['posts_slug_translation']['types'][$k]);
  7966. if ( $_has_slug ) {
  7967. if (isset($sitepress_settings[ 'st' ]) && $default_language != $sitepress_settings[ 'st' ][ 'strings_language' ] ) {
  7968. $string_id_prepared = $wpdb->prepare( "
  7969. SELECT s.id FROM {$wpdb->prefix}icl_strings s
  7970. JOIN {$wpdb->prefix}icl_string_translations st
  7971. ON st.string_id = s.id
  7972. WHERE st.language=%s AND s.value=%s AND s.name LIKE %s
  7973. ", array( $default_language, $custom_post->rewrite[ 'slug' ], 'URL slug: %' ) );
  7974. } else {
  7975. $string_id_prepared = $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}icl_strings WHERE name = %s AND value = %s ", array(
  7976. 'Url slug: ' . $custom_post->rewrite[ 'slug' ],
  7977. $custom_post->rewrite[ 'slug' ]
  7978. ) );
  7979. }
  7980. $string_id = $wpdb->get_var( $string_id_prepared );
  7981. if ( $_translate && !$string_id ) {
  7982. $message = sprintf( __( "%s slugs are set to be translated, but they are missing their translation", 'sitepress'), $custom_post->labels->name);
  7983. $notice .= ICL_AdminNotifier::displayInstantMessage( $message, 'error', 'below-h2', true );
  7984. }
  7985. }
  7986. }
  7987. }
  7988. $ctaxonomies = array_diff( array_keys( (array)$wp_taxonomies ), array( 'post_tag', 'category', 'nav_menu', 'link_category', 'post_format' ) );
  7989. foreach ( $ctaxonomies as $ctax ) {
  7990. if ( !isset( $this->settings[ 'taxonomies_sync_option' ][ $ctax ] ) ) {
  7991. $tax_sync_not_set[ ] = $wp_taxonomies[ $ctax ]->label;
  7992. }
  7993. }
  7994. if ( defined( 'WPML_TM_VERSION' ) && !empty( $tax_sync_not_set ) ) {
  7995. $notice .= '<p class="updated">';
  7996. $notice .= sprintf( __( "You haven't set your <a %s>synchronization preferences</a> for these taxonomies: %s. Default value was selected.", 'sitepress' ), 'href="admin.php?page=' . WPML_TM_FOLDER . '/menu/main.php&sm=mcsetup"', '<i>' . join( '</i>, <i>', $tax_sync_not_set ) . '</i>' );
  7997. $notice .= '</p>';
  7998. }
  7999. echo $notice;
  8000. }
  8001. function dashboard_widget_setup()
  8002. {
  8003. if ( current_user_can( 'manage_options' ) ) {
  8004. $dashboard_widgets_order = (array)get_user_option( "meta-box-order_dashboard" );
  8005. $icl_dashboard_widget_id = 'icl_dashboard_widget';
  8006. $all_widgets = array();
  8007. foreach ( $dashboard_widgets_order as $v ) {
  8008. $all_widgets = array_merge( $all_widgets, explode( ',', $v ) );
  8009. }
  8010. if ( !in_array( $icl_dashboard_widget_id, $all_widgets ) ) {
  8011. $install = true;
  8012. } else {
  8013. $install = false;
  8014. }
  8015. wp_add_dashboard_widget( $icl_dashboard_widget_id, sprintf( __( 'Multi-language | WPML %s', 'sitepress' ), ICL_SITEPRESS_VERSION ), array( $this, 'dashboard_widget' ), null );
  8016. if ( $install ) {
  8017. //FIXME: reported one case of NOTICE: wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php:7815 - Undefined index: side
  8018. $dashboard_widgets_order[ 'side' ] = $icl_dashboard_widget_id . ',' . @strval( $dashboard_widgets_order[ 'side' ] );
  8019. $user = wp_get_current_user();
  8020. update_user_option( $user->ID, 'meta-box-order_dashboard', $dashboard_widgets_order, true );
  8021. }
  8022. }
  8023. }
  8024. function dashboard_widget()
  8025. {
  8026. do_action( 'icl_dashboard_widget_notices' );
  8027. include_once ICL_PLUGIN_PATH . '/menu/dashboard-widget.php';
  8028. }
  8029. function verify_post_translations( $post_type ) {
  8030. global $wpdb;
  8031. $active_languages = count( $this->get_active_languages() );
  8032. $sql = "
  8033. SELECT p1.ID, t.translation_id
  8034. FROM {$wpdb->prefix}icl_translations t
  8035. INNER JOIN {$wpdb->posts} p1
  8036. ON t.element_id = p1.ID
  8037. LEFT JOIN {$wpdb->prefix}icl_translations tt
  8038. ON t.trid = tt.trid
  8039. WHERE t.element_type = %s
  8040. AND t.source_language_code IS null
  8041. GROUP BY p1.ID, p1.post_parent
  8042. HAVING count(tt.language_code) < %d
  8043. ";
  8044. $sql_prepared = $wpdb->prepare( $sql, array( 'post_' . $post_type, $active_languages ) );
  8045. $results = $wpdb->get_results( $sql_prepared );
  8046. if ( $results ) {
  8047. foreach ( $results as $result ) {
  8048. $id = $result->ID;
  8049. $translation_id = $result->translation_id;
  8050. if ( !$translation_id ) {
  8051. $this->set_element_language_details( $id, 'post_' . $post_type, false, $this->get_default_language() );
  8052. }
  8053. }
  8054. } else {
  8055. $post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type='{$post_type}' AND post_status <> 'auto-draft'" );
  8056. if ( !empty( $post_ids ) ) {
  8057. foreach ( $post_ids as $id ) {
  8058. $translation_id_prepared = $wpdb->prepare( "SELECT translation_id FROM {$wpdb->prefix}icl_translations WHERE element_id=%d AND element_type=%s", array( $id, 'post_' . $post_type ) );
  8059. $translation_id = $wpdb->get_var( $translation_id_prepared );
  8060. if ( !$translation_id ) {
  8061. $this->set_element_language_details( $id, 'post_' . $post_type, false, $this->get_default_language() );
  8062. }
  8063. }
  8064. }
  8065. }
  8066. }
  8067. function verify_taxonomy_translations( $taxonomy )
  8068. {
  8069. global $wpdb;
  8070. $element_ids_prepared = $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE taxonomy=%s", $taxonomy);
  8071. $element_ids = $wpdb->get_col( $element_ids_prepared );
  8072. if ( !empty( $element_ids ) ) {
  8073. foreach ( $element_ids as $id ) {
  8074. $translation_id_prepared = $wpdb->prepare( "SELECT translation_id FROM {$wpdb->prefix}icl_translations WHERE element_id=%d AND element_type=%s", $id, 'tax_' . $taxonomy);
  8075. $translation_id = $wpdb->get_var( $translation_id_prepared );
  8076. if ( !$translation_id ) {
  8077. $this->set_element_language_details( $id, 'tax_' . $taxonomy, false, $this->get_default_language() );
  8078. }
  8079. }
  8080. }
  8081. }
  8082. function copy_from_original()
  8083. {
  8084. global $wpdb;
  8085. $show = false;
  8086. $trid = false;
  8087. $source_lang = false;
  8088. $source_lang_name = false;
  8089. $disabled = '';
  8090. if ( isset( $_GET[ 'source_lang' ] ) && isset( $_GET[ 'trid' ] ) ) {
  8091. $source_lang = $_GET[ 'source_lang' ];
  8092. $trid = intval( $_GET[ 'trid' ] );
  8093. $_lang_details = $this->get_language_details( $source_lang );
  8094. $source_lang_name = $_lang_details[ 'display_name' ];
  8095. $show = true;
  8096. } elseif ( isset( $_GET[ 'post' ] ) && isset( $_GET[ 'lang' ] ) && $_GET[ 'lang' ] != $this->get_default_language() ) {
  8097. global $post;
  8098. if ( trim( $post->post_content ) ) {
  8099. $disabled = ' disabled="disabled"';
  8100. }
  8101. $trid = $this->get_element_trid( $post->ID, 'post_' . $post->post_type );
  8102. $source_lang = $wpdb->get_var( $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE source_language_code IS NULL AND trid=%d", $trid ) );
  8103. $_lang_details = $this->get_language_details( $source_lang );
  8104. $source_lang_name = $_lang_details[ 'display_name' ];
  8105. $show = true && $source_lang;
  8106. }
  8107. if ( $show ) {
  8108. wp_nonce_field( 'copy_from_original_nonce', '_icl_nonce_cfo_' . $trid );
  8109. echo '<input id="icl_cfo" class="button-secondary" type="button" value="' . sprintf( __( 'Copy content from %s', 'sitepress' ), $source_lang_name ) . '"
  8110. onclick="icl_copy_from_original(\'' . esc_js( $source_lang ) . '\', \'' . esc_js( $trid ) . '\')"' . $disabled . '/>';
  8111. icl_pop_info( __( "This operation copies the content from the original language onto this translation. It's meant for when you want to start with the original content, but keep translating in this language. This button is only enabled when there's no content in the editor.", 'sitepress' ), 'question' );
  8112. echo '<br clear="all" />';
  8113. }
  8114. }
  8115. function wp_upgrade_locale( $locale )
  8116. {
  8117. if ( defined( 'WPLANG' ) && WPLANG ) {
  8118. $locale = WPLANG;
  8119. } else {
  8120. $locale = ICL_WP_UPDATE_LOCALE;
  8121. }
  8122. return $locale;
  8123. }
  8124. function admin_language_switcher_legacy()
  8125. {
  8126. global $pagenow, $wpdb;
  8127. $all_languages_enabled = true;
  8128. $current_page = basename( $_SERVER[ 'SCRIPT_NAME' ] );
  8129. $current_language = $this->get_current_language();
  8130. // individual translations
  8131. $is_post = false;
  8132. $is_tax = false;
  8133. $is_menu = false;
  8134. $post_type = false;
  8135. $trid = false;
  8136. $translations = false;
  8137. switch ( $pagenow ) {
  8138. case 'post.php':
  8139. $is_post = true;
  8140. $all_languages_enabled = false;
  8141. $post_id = @intval( $_GET[ 'post' ] );
  8142. $post = get_post( $post_id );
  8143. $trid = $this->get_element_trid( $post_id, 'post_' . $post->post_type );
  8144. $translations = $this->get_element_translations( $trid, 'post_' . $post->post_type, true );
  8145. break;
  8146. case 'post-new.php':
  8147. $all_languages_enabled = false;
  8148. if ( isset( $_GET[ 'trid' ] ) ) {
  8149. $trid = intval( $_GET[ 'trid' ] );
  8150. $post_type = isset( $_GET[ 'post_type' ] ) ? $_GET[ 'post_type' ] : 'post';
  8151. $translations = $this->get_element_translations( $trid, 'post_' . $post_type, true );
  8152. $is_post = true;
  8153. }
  8154. break;
  8155. case 'edit-tags.php':
  8156. $is_tax = true;
  8157. if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'edit' ) {
  8158. $all_languages_enabled = false;
  8159. }
  8160. $term_id = @intval( $_GET[ 'tag_ID' ] );
  8161. $taxonomy = $_GET[ 'taxonomy' ];
  8162. $term_tax_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE taxonomy=%s AND term_id=%d", $taxonomy, $term_id ) );
  8163. $trid = $this->get_element_trid( $term_tax_id, 'tax_' . $taxonomy );
  8164. $translations = $this->get_element_translations( $trid, 'tax_' . $taxonomy, true );
  8165. break;
  8166. case 'nav-menus.php':
  8167. $is_menu = true;
  8168. if ( isset( $_GET[ 'menu' ] ) && $_GET[ 'menu' ] ) {
  8169. $menu_id = $_GET[ 'menu' ];
  8170. $trid = $trid = $this->get_element_trid( $menu_id, 'tax_nav_menu' );
  8171. $translations = $this->get_element_translations( $trid, 'tax_nav_menu', true );
  8172. }
  8173. $all_languages_enabled = false;
  8174. break;
  8175. }
  8176. foreach ( $this->get_active_languages() as $lang ) {
  8177. $current_page_lang = $current_page;
  8178. parse_str( $_SERVER[ 'QUERY_STRING' ], $query_vars );
  8179. unset( $query_vars[ 'lang' ], $query_vars[ 'admin_bar' ] );
  8180. // individual translations
  8181. if ( $is_post ) {
  8182. if ( isset( $translations[ $lang[ 'code' ] ] ) && isset( $translations[ $lang[ 'code' ] ]->element_id ) ) {
  8183. $query_vars[ 'post' ] = $translations[ $lang[ 'code' ] ]->element_id;
  8184. unset( $query_vars[ 'source_lang' ] );
  8185. $current_page_lang = 'post.php';
  8186. $query_vars[ 'action' ] = 'edit';
  8187. } else {
  8188. $current_page_lang = 'post-new.php';
  8189. if ( isset( $post ) ) {
  8190. $query_vars[ 'post_type' ] = $post->post_type;
  8191. $query_vars[ 'source_lang' ] = $current_language;
  8192. } else {
  8193. $query_vars[ 'post_type' ] = $post_type;
  8194. }
  8195. $query_vars[ 'trid' ] = $trid;
  8196. unset( $query_vars[ 'post' ], $query_vars[ 'action' ] );
  8197. }
  8198. } elseif ( $is_tax ) {
  8199. if ( isset( $translations[ $lang[ 'code' ] ] ) && isset( $translations[ $lang[ 'code' ] ]->element_id ) ) {
  8200. $query_vars[ 'tag_ID' ] = $translations[ $lang[ 'code' ] ]->element_id;
  8201. } else {
  8202. $query_vars[ 'trid' ] = $trid;
  8203. $query_vars[ 'source_lang' ] = $current_language;
  8204. unset( $query_vars[ 'tag_ID' ], $query_vars[ 'action' ] );
  8205. }
  8206. } elseif ( $is_menu ) {
  8207. if ( !empty( $menu_id ) ) {
  8208. if ( isset( $translations[ $lang[ 'code' ] ]->element_id ) ) {
  8209. $query_vars[ 'menu' ] = $translations[ $lang[ 'code' ] ]->element_id;
  8210. } else {
  8211. $query_vars[ 'menu' ] = 0;
  8212. $query_vars[ 'trid' ] = $trid;
  8213. $query_vars[ 'action' ] = 'edit';
  8214. }
  8215. }
  8216. }
  8217. $query_string = http_build_query( $query_vars );
  8218. $query = '?';
  8219. if ( !empty( $query_string ) ) {
  8220. $query .= $query_string . '&';
  8221. }
  8222. $query .= 'lang=' . $lang[ 'code' ]; // the default language need to specified explictly yoo in order to set the lang cookie
  8223. $link_url = admin_url( $current_page_lang . $query );
  8224. $flag = $this->get_flag( $lang[ 'code' ] );
  8225. if ( $flag->from_template ) {
  8226. $wp_upload_dir = wp_upload_dir();
  8227. $flag_url = $wp_upload_dir[ 'baseurl' ] . '/flags/' . $flag->flag;
  8228. } else {
  8229. $flag_url = ICL_PLUGIN_URL . '/res/flags/' . $flag->flag;
  8230. }
  8231. $languages_links[ $lang[ 'code' ] ] = array(
  8232. 'url' => $link_url . '&admin_bar=1', 'current' => $lang[ 'code' ] == $current_language, 'anchor' => $lang[ 'display_name' ],
  8233. 'flag' => '<img class="admin_iclflag" src="' . $flag_url . '" alt="' . $lang[ 'code' ] . '" width="18" height="12" />'
  8234. );
  8235. }
  8236. if ( $all_languages_enabled ) {
  8237. $query = '?';
  8238. if ( !empty( $query_string ) ) {
  8239. $query .= $query_string . '&';
  8240. }
  8241. $query .= 'lang=all';
  8242. $link_url = admin_url( basename( $_SERVER[ 'SCRIPT_NAME' ] ) . $query );
  8243. $languages_links[ 'all' ] = array(
  8244. 'url' => $link_url, 'current' => 'all' == $current_language, 'anchor' => __( 'All languages', 'sitepress' ),
  8245. 'flag' => '<img class="admin_iclflag" src="' . ICL_PLUGIN_URL . '/res/img/icon16.png" alt="all" width="16" height="16" />'
  8246. );
  8247. } else {
  8248. // set the default language as current
  8249. if ( 'all' == $current_language ) {
  8250. $languages_links[ $this->get_default_language() ][ 'current' ] = true;
  8251. }
  8252. }
  8253. include ICL_PLUGIN_PATH . '/menu/admin-language-switcher.php';
  8254. }
  8255. function admin_language_switcher()
  8256. {
  8257. if(!SitePress::check_settings_integrity()) return;
  8258. /** @var $wp_admin_bar WP_Admin_Bar */
  8259. global $wpdb, $wp_admin_bar, $pagenow;
  8260. $all_languages_enabled = true;
  8261. $current_page = basename( $_SERVER[ 'SCRIPT_NAME' ] );
  8262. $post_type = false;
  8263. $trid = false;
  8264. $translations = false;
  8265. $languages_links = array();
  8266. // individual translations
  8267. $is_post = false;
  8268. $is_tax = false;
  8269. $is_menu = false;
  8270. $current_language = $this->get_current_language();
  8271. switch ( $pagenow ) {
  8272. case 'post.php':
  8273. $is_post = true;
  8274. $post_id = @intval( $_GET[ 'post' ] );
  8275. $post = get_post( $post_id );
  8276. $post_language = $this->get_language_for_element( $post_id, 'post_' . get_post_type( $post_id ) );
  8277. if ( $post_language && $post_language != $current_language ) {
  8278. $this->switch_lang( $post_language );
  8279. $current_language = $this->get_current_language();
  8280. }
  8281. $trid = $this->get_element_trid( $post_id, 'post_' . $post->post_type );
  8282. $translations = $this->get_element_translations( $trid, 'post_' . $post->post_type, true );
  8283. break;
  8284. case 'post-new.php':
  8285. $all_languages_enabled = false;
  8286. if ( isset( $_GET[ 'trid' ] ) ) {
  8287. $trid = intval( $_GET[ 'trid' ] );
  8288. $post_type = isset( $_GET[ 'post_type' ] ) ? $_GET[ 'post_type' ] : 'post';
  8289. $translations = $this->get_element_translations( $trid, 'post_' . $post_type, true );
  8290. $is_post = true;
  8291. }
  8292. break;
  8293. case 'edit-tags.php':
  8294. $is_tax = true;
  8295. if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'edit' ) {
  8296. $all_languages_enabled = false;
  8297. }
  8298. $term_id = @intval( $_GET[ 'tag_ID' ] );
  8299. $taxonomy = $_GET[ 'taxonomy' ];
  8300. $term_tax_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE taxonomy=%s AND term_id=%d", $taxonomy, $term_id ) );
  8301. $trid = $this->get_element_trid( $term_tax_id, 'tax_' . $taxonomy );
  8302. $translations = $this->get_element_translations( $trid, 'tax_' . $taxonomy, true );
  8303. break;
  8304. case 'nav-menus.php':
  8305. $is_menu = true;
  8306. if ( isset( $_GET[ 'menu' ] ) && $_GET[ 'menu' ] ) {
  8307. $menu_id = $_GET[ 'menu' ];
  8308. $trid = $trid = $this->get_element_trid( $menu_id, 'tax_nav_menu' );
  8309. $translations = $this->get_element_translations( $trid, 'tax_nav_menu', true );
  8310. }
  8311. $all_languages_enabled = false;
  8312. break;
  8313. }
  8314. foreach ( $this->get_active_languages() as $lang ) {
  8315. $current_page_lang = $current_page;
  8316. parse_str( $_SERVER[ 'QUERY_STRING' ], $query_vars );
  8317. unset( $query_vars[ 'lang' ], $query_vars[ 'admin_bar' ] );
  8318. // individual translations
  8319. if ( $is_post ) {
  8320. if ( isset( $translations[ $lang[ 'code' ] ] ) && isset( $translations[ $lang[ 'code' ] ]->element_id ) ) {
  8321. $query_vars[ 'post' ] = $translations[ $lang[ 'code' ] ]->element_id;
  8322. unset( $query_vars[ 'source_lang' ] );
  8323. $current_page_lang = 'post.php';
  8324. $query_vars[ 'action' ] = 'edit';
  8325. } else {
  8326. $current_page_lang = 'post-new.php';
  8327. if ( isset( $post ) ) {
  8328. $query_vars[ 'post_type' ] = $post->post_type;
  8329. $query_vars[ 'source_lang' ] = $current_language;
  8330. } else {
  8331. $query_vars[ 'post_type' ] = $post_type;
  8332. }
  8333. $query_vars[ 'trid' ] = $trid;
  8334. unset( $query_vars[ 'post' ], $query_vars[ 'action' ] );
  8335. }
  8336. } elseif ( $is_tax ) {
  8337. if ( isset( $translations[ $lang[ 'code' ] ] ) && isset( $translations[ $lang[ 'code' ] ]->element_id ) ) {
  8338. $query_vars[ 'tag_ID' ] = $translations[ $lang[ 'code' ] ]->element_id;
  8339. } else {
  8340. $query_vars[ 'trid' ] = $trid;
  8341. $query_vars[ 'source_lang' ] = $current_language;
  8342. unset( $query_vars[ 'tag_ID' ], $query_vars[ 'action' ] );
  8343. }
  8344. } elseif ( $is_menu ) {
  8345. if ( !empty( $menu_id ) ) {
  8346. if ( isset( $translations[ $lang[ 'code' ] ]->element_id ) ) {
  8347. $query_vars[ 'menu' ] = $translations[ $lang[ 'code' ] ]->element_id;
  8348. } else {
  8349. $query_vars[ 'menu' ] = 0;
  8350. $query_vars[ 'trid' ] = $trid;
  8351. $query_vars[ 'action' ] = 'edit';
  8352. }
  8353. }
  8354. }
  8355. $query_string = http_build_query( $query_vars );
  8356. $query = '?';
  8357. if ( !empty( $query_string ) ) {
  8358. $query .= $query_string . '&';
  8359. }
  8360. $query .= 'lang=' . $lang[ 'code' ]; // the default language need to specified explicitly yoo in order to set the lang cookie
  8361. $link_url = admin_url( $current_page_lang . $query );
  8362. $flag = $this->get_flag( $lang[ 'code' ] );
  8363. if ( $flag->from_template ) {
  8364. $wp_upload_dir = wp_upload_dir();
  8365. $flag_url = $wp_upload_dir[ 'baseurl' ] . '/flags/' . $flag->flag;
  8366. } else {
  8367. $flag_url = ICL_PLUGIN_URL . '/res/flags/' . $flag->flag;
  8368. }
  8369. $languages_links[ $lang[ 'code' ] ] = array(
  8370. 'url' => $link_url . '&admin_bar=1',
  8371. 'current' => $lang[ 'code' ] == $current_language,
  8372. 'anchor' => $lang[ 'display_name' ],
  8373. 'flag' => '<img class="icl_als_iclflag" src="' . $flag_url . '" alt="' . $lang[ 'code' ] . '" width="18" height="12" />'
  8374. );
  8375. }
  8376. if ( $all_languages_enabled ) {
  8377. $query = '?';
  8378. if ( !empty( $query_string ) ) {
  8379. $query .= $query_string . '&';
  8380. }
  8381. $query .= 'lang=all';
  8382. $link_url = admin_url( basename( $_SERVER[ 'SCRIPT_NAME' ] ) . $query );
  8383. $languages_links[ 'all' ] = array(
  8384. 'url' => $link_url, 'current' => 'all' == $current_language, 'anchor' => __( 'All languages', 'sitepress' ),
  8385. 'flag' => '<img class="icl_als_iclflag" src="' . ICL_PLUGIN_URL . '/res/img/icon16.png" alt="all" width="16" height="16" />'
  8386. );
  8387. } else {
  8388. // set the default language as current
  8389. if ( 'all' == $current_language ) {
  8390. $languages_links[ $this->get_default_language() ][ 'current' ] = true;
  8391. }
  8392. }
  8393. $parent = 'WPML_ALS';
  8394. $lang = $languages_links[ $current_language ];
  8395. // Current language
  8396. $wp_admin_bar->add_menu( array(
  8397. 'parent' => false, 'id' => $parent,
  8398. 'title' => $lang[ 'flag' ] . '&nbsp;' . $lang[ 'anchor' ] . '&nbsp;&nbsp;<img title="' . __( 'help', 'sitepress' ) . '" id="wpml_als_help_link" src="' . ICL_PLUGIN_URL . '/res/img/question1.png" alt="' . __( 'help', 'sitepress' ) . '" width="16" height="16"/>',
  8399. 'href' => false, 'meta' => array(
  8400. 'title' => __( 'Showing content in:', 'sitepress' ) . ' ' . $lang[ 'anchor' ],
  8401. )
  8402. ) );
  8403. if ( $languages_links ) {
  8404. foreach ( $languages_links as $code => $lang ) {
  8405. if ( $code == $current_language )
  8406. continue;
  8407. $wp_admin_bar->add_menu( array(
  8408. 'parent' => $parent, 'id' => $parent . '_' . $code, 'title' => $lang[ 'flag' ] . '&nbsp;' . $lang[ 'anchor' ], 'href' => $lang[ 'url' ], 'meta' => array(
  8409. 'title' => __( 'Show content in:', 'sitepress' ) . ' ' . $lang[ 'anchor' ],
  8410. )
  8411. ) );
  8412. }
  8413. }
  8414. add_action( 'all_admin_notices', array( $this, '_admin_language_switcher_help_popup' ) );
  8415. }
  8416. function _admin_language_switcher_help_popup()
  8417. {
  8418. echo '<div id="icl_als_help_popup" class="icl_cyan_box icl_pop_info">';
  8419. echo '<img class="icl_pop_info_but_close" align="right" src="' . ICL_PLUGIN_URL . '/res/img/ico-close.png" width="12" height="12" alt="x" />';
  8420. printf( __( 'This language selector determines which content to display. You can choose items in a specific language or in all languages. To change the language of the WordPress Admin interface, go to <a%s>your profile</a>.', 'sitepress' ), ' href="' . admin_url( 'profile.php' ) . '"' );
  8421. echo '</div>';
  8422. }
  8423. function admin_notices( $message, $class = "updated" )
  8424. {
  8425. static $hook_added = 0;
  8426. $this->_admin_notices[ ] = array( 'class' => $class, 'message' => $message );
  8427. if ( !$hook_added )
  8428. add_action( 'admin_notices', array( $this, '_admin_notices_hook' ) );
  8429. $hook_added = 1;
  8430. }
  8431. function _admin_notices_hook()
  8432. {
  8433. if ( !empty( $this->_admin_notices ) )
  8434. foreach ( $this->_admin_notices as $n ) {
  8435. echo '<div class="' . $n[ 'class' ] . '">';
  8436. echo '<p>' . $n[ 'message' ] . '</p>';
  8437. echo '</div>';
  8438. }
  8439. }
  8440. /**
  8441. * Adjust template (taxonomy-)$taxonomy-$term.php for translated term slugs and IDs
  8442. *
  8443. * @since 3.1
  8444. *
  8445. * @param string $template
  8446. *
  8447. * @return string The template filename if found.
  8448. */
  8449. function slug_template($template){
  8450. global $wp_query;
  8451. $term = $wp_query->get_queried_object();
  8452. //Taxonomies
  8453. if(!isset($term) || !$term) return $template;
  8454. $taxonomy = $term->taxonomy;
  8455. if(!isset($taxonomy) || !$taxonomy) return $template;
  8456. $templates = array();
  8457. $template_prefix = 'taxonomy-';
  8458. $is_taxonomy = true;
  8459. if(in_array($taxonomy, array('category','tag'))) {
  8460. $template_prefix = '';
  8461. $is_taxonomy = false;
  8462. }
  8463. remove_filter( 'get_term', array( $this, 'get_term_adjust_id' ), 1 );
  8464. $current_language = $this->get_current_language();
  8465. $default_language = $this->get_default_language();
  8466. if (!$is_taxonomy || $this->is_translated_taxonomy( $taxonomy ) && $current_language != $default_language ) {
  8467. $current_term = get_term_by( "id", $term->term_id, $taxonomy );
  8468. if ( $current_term ) {
  8469. $templates[ ] = "$template_prefix$taxonomy-{$current_language}-{$current_term->slug}.php";
  8470. $templates[ ] = "$template_prefix$taxonomy-{$current_language}-{$term->term_id}.php";
  8471. $templates[ ] = "$template_prefix$taxonomy-{$current_language}.php";
  8472. $templates[ ] = "$template_prefix$taxonomy-{$current_term->slug}.php";
  8473. $templates[ ] = "$template_prefix$taxonomy-{$term->term_id}.php";
  8474. }
  8475. }
  8476. $original_term_id = icl_object_id( $term->term_id, $taxonomy, true, $default_language );
  8477. $original_term = get_term_by( "id", $original_term_id, $taxonomy );
  8478. if ( $original_term ) {
  8479. $templates[ ] = "$template_prefix$taxonomy-{$current_language}-{$original_term->slug}.php";
  8480. $templates[ ] = "$template_prefix$taxonomy-{$current_language}-{$original_term_id}.php";
  8481. $templates[ ] = "$template_prefix$taxonomy-{$original_term->slug}.php";
  8482. $templates[ ] = "$template_prefix$taxonomy-{$original_term_id}.php";
  8483. $templates[ ] = "$template_prefix$taxonomy-{$current_language}.php";
  8484. $templates[ ] = "$template_prefix$taxonomy.php";
  8485. }
  8486. if ( $is_taxonomy ) {
  8487. $templates[ ] = 'taxonomy-{$current_language}.php';
  8488. $templates[ ] = 'taxonomy.php';
  8489. }
  8490. $templates = array_unique($templates);
  8491. add_filter( 'get_term', array( $this, 'get_term_adjust_id' ), 1, 1 );
  8492. $new_template = locate_template( $templates );
  8493. if($new_template) {
  8494. $template = $new_template;
  8495. }
  8496. return $template;
  8497. }
  8498. function setup_canonical_urls()
  8499. {
  8500. global $wp_the_query;
  8501. // Yoast Exception
  8502. global $wpseo_front;
  8503. if ( isset( $wpseo_front ) && has_filter( 'wp_head', array( $wpseo_front, 'head' ) ) )
  8504. return;
  8505. if ( is_singular() ) {
  8506. $id = $wp_the_query->get_queried_object_id();
  8507. $master_post_id = get_post_meta( $id, '_icl_lang_duplicate_of', true );
  8508. if ( $id && $master_post_id != $id ) {
  8509. remove_action( 'wp_head', 'rel_canonical' );
  8510. add_action( 'wp_head', array( $this, 'rel_canonical' ) );
  8511. }
  8512. }
  8513. }
  8514. function rel_canonical()
  8515. {
  8516. global $wp_the_query;
  8517. $id = $wp_the_query->get_queried_object_id();
  8518. if ( $master_post_id = get_post_meta( $id, '_icl_lang_duplicate_of', true ) ) {
  8519. $link = get_permalink( $master_post_id );
  8520. echo "<link rel='canonical' href='$link' />\n";
  8521. }
  8522. }
  8523. function head_langs()
  8524. {
  8525. $languages = $this->get_ls_languages( array( 'skip_missing' => true ) );
  8526. // If there are translations and is not paged content...
  8527. //Renders head alternate links only on certain conditions
  8528. $the_post = get_post();
  8529. $the_id = $the_post ? $the_post->ID : false;
  8530. $is_valid = $the_id && count( $languages ) > 1 && !is_paged() && ( ( ( is_single() || is_page() ) && get_post_status( $the_id ) == 'publish' ) || ( is_home() || is_front_page() || is_archive() ) );
  8531. if ( $is_valid ) {
  8532. foreach ( $languages as $code => $lang ) {
  8533. printf( '<link rel="alternate" hreflang="%s" href="%s" />' . PHP_EOL, $this->get_language_tag( $code ), str_replace( '&amp;', '&', $lang[ 'url' ] ) );
  8534. }
  8535. }
  8536. }
  8537. function allowed_redirect_hosts( $hosts )
  8538. {
  8539. if ( $this->settings[ 'language_negotiation_type' ] == 2 ) {
  8540. foreach ( $this->settings[ 'language_domains' ] as $code => $url ) {
  8541. if ( !empty( $this->active_languages[ $code ] ) ) {
  8542. $parts = parse_url( $url );
  8543. if ( !in_array( $parts[ 'host' ], $hosts ) ) {
  8544. $hosts[ ] = $parts[ 'host' ];
  8545. }
  8546. }
  8547. }
  8548. }
  8549. return $hosts;
  8550. }
  8551. function icl_nonces()
  8552. {
  8553. //@since 3.1 Calls made only when in Translation Management pages
  8554. $allowed_pages = array();
  8555. if(defined('WPML_TM_FOLDER')) {
  8556. $allowed_pages[] = WPML_TM_FOLDER . '/menu/main.php';
  8557. }
  8558. if(!isset($_REQUEST['page']) || !in_array($_REQUEST['page'], $allowed_pages)) {
  8559. return;
  8560. }
  8561. //messages
  8562. wp_nonce_field( 'icl_messages_nonce', '_icl_nonce_m' );
  8563. wp_nonce_field( 'icl_show_reminders_nonce', '_icl_nonce_sr' );
  8564. }
  8565. //For when it will be possible to add custom bulk actions
  8566. function bulk_actions($actions) {
  8567. $active_languages = $this->get_active_languages();
  8568. $actions['duplicate_all'] = 'duplicate_all';
  8569. foreach($active_languages as $language_code => $language_name) {
  8570. $actions['duplicate_' . $language_code] = 'duplicate_' . $language_code;
  8571. }
  8572. return $actions;
  8573. }
  8574. /**
  8575. * Returns SERVER_NAME, or HTTP_HOST if the first is not available
  8576. * @return mixed
  8577. */
  8578. private function get_server_host_name() {
  8579. if(!isset($_SERVER[ 'HTTP_HOST' ])) {
  8580. $host = $_SERVER[ 'SERVER_NAME' ];
  8581. if(isset( $_SERVER[ 'SERVER_PORT' ] ) && $_SERVER[ 'SERVER_PORT' ]!=80) {
  8582. $host .= ':' . $_SERVER[ 'SERVER_PORT' ];
  8583. }
  8584. } else {
  8585. $host = $_SERVER[ 'HTTP_HOST' ];
  8586. }
  8587. //Removes standard ports 443 (80 should be already omitted in all cases)
  8588. $result = preg_replace( "@:[443]+([/]?)@", '$1', $host );
  8589. return $result;
  8590. }
  8591. public static function get_installed_plugins() {
  8592. if(!function_exists('get_plugins')) {
  8593. require_once(ABSPATH . 'wp-admin/includes/plugin.php');
  8594. }
  8595. $wp_plugins = get_plugins();
  8596. $wpml_plugins_list = array(
  8597. 'WPML Multilingual CMS' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'sitepress-multilingual-cms' ),
  8598. 'WPML CMS Nav' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'wpml-cms-nav' ),
  8599. 'WPML String Translation' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'wpml-string-translation' ),
  8600. 'WPML Sticky Links' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'wpml-sticky-links' ),
  8601. 'WPML Translation Management' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'wpml-translation-management' ),
  8602. 'WPML Translation Analytics' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'wpml-translation-analytics' ),
  8603. 'WPML XLIFF' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'wpml-xliff' ),
  8604. 'WPML Media' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'wpml-media' ),
  8605. 'WooCommerce Multilingual' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'woocommerce-multilingual' ),
  8606. 'JigoShop Multilingual' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'jigoshop-multilingual' ),
  8607. 'Gravity Forms Multilingual' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'gravityforms-multilingual' ),
  8608. 'CRED Frontend Translation' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'cred-frontend-translation' ),
  8609. 'Installer' => array( 'installed' => false, 'active' => false, 'file' => false, 'plugin' => false, 'slug' => 'installer' ),
  8610. );
  8611. foreach ( $wpml_plugins_list as $wpml_plugin_name => $v ) {
  8612. foreach ( $wp_plugins as $file => $plugin ) {
  8613. $plugin_name = $plugin[ 'Name' ];
  8614. if ( $plugin_name == $wpml_plugin_name ) {
  8615. $wpml_plugins_list[ $plugin_name ][ 'installed' ] = true;
  8616. $wpml_plugins_list[ $plugin_name ][ 'plugin' ] = $plugin;
  8617. $wpml_plugins_list[ $plugin_name ][ 'file' ] = $file;
  8618. }
  8619. }
  8620. }
  8621. return $wpml_plugins_list;
  8622. }
  8623. public static function check_settings_integrity() {
  8624. if(wpml_is_ajax()) return true;
  8625. if ( isset( $_GET[ 'debug_action' ]) && $_GET[ 'nonce' ] == wp_create_nonce( $_GET[ 'debug_action' ] ) ) {
  8626. if($_GET[ 'debug_action' ] == 'reset_wpml_settings') {
  8627. $referrer = isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : get_admin_url();
  8628. $current_settings = get_option( 'icl_sitepress_settings' );
  8629. unset($current_settings['setup_complete']);
  8630. unset($current_settings['setup_wizard_step']);
  8631. unset($current_settings['existing_content_language_verified']);
  8632. unset($current_settings['dont_show_help_admin_notice']);
  8633. global $wpdb;
  8634. $wpdb->query('TRUNCATE TABLE ' . $wpdb->prefix . 'icl_translations');
  8635. update_option('icl_sitepress_settings', $current_settings);
  8636. wp_redirect($referrer);
  8637. exit();
  8638. }
  8639. }
  8640. global $wpdb;
  8641. static $result;
  8642. if(isset($result)) {
  8643. return $result;
  8644. }
  8645. $current_settings = get_option( 'icl_sitepress_settings' );
  8646. if(!$current_settings) return true;
  8647. $setup_wizard_step = false;
  8648. if ( isset( $current_settings[ 'setup_wizard_step' ] ) ) {
  8649. $setup_wizard_step = $current_settings[ 'setup_wizard_step' ];
  8650. }
  8651. $setup_complete = false;
  8652. $setup_complete_missing = true;
  8653. if ( isset( $current_settings[ 'setup_complete' ] ) ) {
  8654. $setup_complete = $current_settings[ 'setup_complete' ];
  8655. $setup_complete_missing = false;
  8656. }
  8657. //Skip checks during first setup wizard
  8658. if(($setup_wizard_step!==false && $setup_wizard_step < 3) || (!$setup_complete_missing && $setup_complete===false && $setup_wizard_step==3 )) return true;
  8659. $default_language = false;
  8660. $default_language_missing = true;
  8661. if ( isset( $current_settings[ 'default_language' ] ) ) {
  8662. $default_language = $current_settings[ 'default_language' ];
  8663. $default_language_missing = false;
  8664. }
  8665. $active_languages_sql = "SELECT * FROM " . $wpdb->prefix . 'icl_languages WHERE active=%d';
  8666. $active_languages_prepared = $wpdb->prepare( $active_languages_sql, array(1) );
  8667. $active_languages = $wpdb->get_results( $active_languages_prepared );
  8668. $existing_translations_sql = "SELECT count(*) FROM " . $wpdb->prefix . 'icl_translations';
  8669. $existing_translations = $wpdb->get_var( $existing_translations_sql );
  8670. $show_notice = false;
  8671. $message = '';
  8672. if ( (!$setup_complete || !$default_language) && $existing_translations ) {
  8673. $message .= '<p>';
  8674. $message .= __( 'Your WPML settings seem to be corrupted. To avoid corrupting your existing data, we have hidden WPML from this site.', 'sitepress' );
  8675. $message .= '</p>';
  8676. $message .= '<p>';
  8677. $message .= __( 'If this is the first time you install WPML on this site, you may have faced a database or script connection drop, that caused settings to be not completely store.', 'sitepress' );
  8678. $message .= __( 'In this case, you can click on the <strong>Reset Settings</strong> button: this will reset WPML settings and any language translation information, allowing you to restart the wizard.', 'sitepress' );
  8679. $message .= '</p>';
  8680. $message .= '<p>';
  8681. $message .= sprintf( __( 'If you have just upgraded WPML or after starting over you keep getting this message, please contact the <a href="%s">support forum</a> as soon as possible, in order to provide you with a fix to this issue.', 'sitepress' ), 'https://wpml.org/forums/' );
  8682. $message .= '</p>';
  8683. $message .= '<p>';
  8684. $confirm_message = _x('Are you sure you want to reset WPML Settings?', 'Reset WPML settings', 'sitepress');
  8685. $confirm_message .= ' ';
  8686. $confirm_message .= _x('This will also empty translation information (if any).', 'Reset WPML settings', 'sitepress');
  8687. $message .= '<a href="?icl_reset_settings=1&debug_action=reset_wpml_settings&nonce=' . wp_create_nonce( 'reset_wpml_settings' ) . '" class="button" onclick="return window.confirm(\'' . $confirm_message . '\');" >' . __('Reset Settings','sitepress') . '</a>';
  8688. $message .= '&nbsp;';
  8689. $message .= '&nbsp;';
  8690. $message .= '&nbsp;';
  8691. $message .= '<a href="https://wpml.org/forums/" class="button">' . __('Contact Support','sitepress') . '</a>';
  8692. $message .= '</p>';
  8693. $message .= '<p>';
  8694. $message .= __( 'Additional details for the support team (there is no need to copy it, as the support team will be able to see it once logged in):', 'sitepress' );
  8695. $message .= '</p>';
  8696. $message .= '<p><textarea rows="10" style="width:100%;display:block;" onclick="this.focus();this.select();" readonly="readonly">';
  8697. $message .= str_repeat( '=', 50 );
  8698. $wpml_plugins_list = SitePress::get_installed_plugins();
  8699. foreach ( $wpml_plugins_list as $name => $plugin_data ) {
  8700. $plugin_name = $name;
  8701. $file = $plugin_data['file'];
  8702. $message .= PHP_EOL . $plugin_name;
  8703. $message .= ' ' . (isset( $plugin_data['plugin']['Version'] ) ? $plugin_data['plugin']['Version'] : __( 'Version n/a', 'sitepress' ));
  8704. $message .= ' => ';
  8705. if ( empty( $plugin_data['plugin'] ) ) {
  8706. $message .= 'Not installed';
  8707. } else {
  8708. $message .= 'Installed';
  8709. }
  8710. $message .= '/';
  8711. $message .= isset( $file ) && is_plugin_active( $file ) ? 'Active' : 'Not Active';
  8712. }
  8713. $message .= PHP_EOL . str_repeat( '-', 50 );
  8714. $message .= PHP_EOL . 'icl_translations count: ' . ( $existing_translations ? $existing_translations : '0' );
  8715. $message .= PHP_EOL . 'setup_complete: ' . ( $setup_complete ? 'true' : 'false' );
  8716. $message .= PHP_EOL . 'setup_complete missing: ' . ( $setup_complete_missing ? 'true' : 'false' );
  8717. $message .= PHP_EOL . 'default_language: ' . ( $default_language ? $default_language : '""' );
  8718. $message .= PHP_EOL . 'default_language_missing: ' . ( $default_language_missing ? 'true' : 'false' );
  8719. $message .= PHP_EOL . PHP_EOL . 'active_languages: ' . PHP_EOL . print_r( $active_languages, true );
  8720. $message .= PHP_EOL . PHP_EOL . 'icl_sitepress_settings (serialized): ' . PHP_EOL . serialize( $current_settings );
  8721. $message .= PHP_EOL . PHP_EOL . 'icl_sitepress_settings (unserialized): ' . PHP_EOL . print_r( $current_settings, true );
  8722. $message .= PHP_EOL . str_repeat( '=', 50 );
  8723. $message .= '</textarea></p>';
  8724. $show_notice = true;
  8725. }
  8726. // ICL_AdminNotifier::removeMessage( 'check_settings_integrity' );
  8727. ICL_AdminNotifier::removeMessage( 'check_settings_integrity_corrupted' );
  8728. if ( $show_notice ) {
  8729. ICL_AdminNotifier::addMessage( 'check_settings_integrity_corrupted', $message, 'error', false, false, false, 'check_settings_integrity', true );
  8730. ICL_AdminNotifier::displayMessages( 'check_settings_integrity' );
  8731. }
  8732. $result = !$show_notice;
  8733. return $result;
  8734. }
  8735. /**
  8736. * @param int $limit
  8737. * @param bool $provide_object
  8738. * @param bool $ignore_args
  8739. *
  8740. * @return array
  8741. */
  8742. public function get_backtrace($limit = 0, $provide_object = false, $ignore_args = true) {
  8743. $options = false;
  8744. if ( version_compare( phpversion(), '5.3.6' ) < 0 ) {
  8745. // Before 5.3.6, the only values recognized are TRUE or FALSE,
  8746. // which are the same as setting or not setting the DEBUG_BACKTRACE_PROVIDE_OBJECT option respectively.
  8747. $options = $provide_object;
  8748. } else {
  8749. // As of 5.3.6, 'options' parameter is a bitmask for the following options:
  8750. if ( $provide_object )
  8751. $options |= DEBUG_BACKTRACE_PROVIDE_OBJECT;
  8752. if ( $ignore_args )
  8753. $options |= DEBUG_BACKTRACE_IGNORE_ARGS;
  8754. }
  8755. if ( version_compare( phpversion(), '5.4.0' ) >= 0 ) {
  8756. $actual_limit = $limit == 0 ? 0 : $limit + 1;
  8757. $debug_backtrace = debug_backtrace( $options, $actual_limit ); //add one item to include the current frame
  8758. } elseif ( version_compare( phpversion(), '5.2.4' ) >= 0 ) {
  8759. //@link https://core.trac.wordpress.org/ticket/20953
  8760. $debug_backtrace = debug_backtrace();
  8761. } else {
  8762. $debug_backtrace = debug_backtrace( $options );
  8763. }
  8764. //Remove the current frame
  8765. if($debug_backtrace) {
  8766. array_shift($debug_backtrace);
  8767. }
  8768. return $debug_backtrace;
  8769. }
  8770. /**
  8771. * Translate the value returned by 'option_{taxonomy}_children' and store it in cache
  8772. *
  8773. * @param array $original_value
  8774. * @param bool|string $current_language
  8775. * @param bool|string $taxonomy
  8776. *
  8777. * @return array
  8778. */
  8779. function option_taxonomy_children( $original_value, $current_language = false, $taxonomy = false ) {
  8780. if(!is_array($original_value) || count($original_value)==0) return $original_value;
  8781. $current_language = !$current_language ? $this->get_current_language() : $current_language;
  8782. $default_language = $this->get_default_language();
  8783. if ( $current_language == $default_language ) {
  8784. return $original_value;
  8785. }
  8786. $cache_key_array[ ] = $current_language;
  8787. $cache_key_array[ ] = $default_language;
  8788. $cache_key_array[ ] = $original_value;
  8789. $cache_key = md5( serialize( $cache_key_array ) );
  8790. $cache_group = 'translate_taxonomy_children';
  8791. $cache_found = false;
  8792. $result = wp_cache_get( $cache_key, $cache_group, false, $cache_found );
  8793. if ( $cache_found ) {
  8794. return $result;
  8795. }
  8796. $debug_backtrace = $this->get_backtrace( 4, false, false );
  8797. //Find the taxonomy name
  8798. if ( !$taxonomy && isset( $debug_backtrace[ 3 ] ) && isset( $debug_backtrace[ 3 ][ 'args' ] ) ) {
  8799. $option_name = $debug_backtrace[ 3 ][ 'args' ][ 0 ];
  8800. $taxonomies = explode( '_', $option_name );
  8801. $taxonomy = $taxonomies[ 0 ];
  8802. }
  8803. $translated_children = array();
  8804. if ( $taxonomy && is_array( $original_value ) ) {
  8805. foreach ( $original_value as $children_term_ids ) {
  8806. foreach ( $children_term_ids as $child_term_id ) {
  8807. $translated_child_term_id = icl_object_id( $child_term_id, $taxonomy, false, $current_language );
  8808. if ( $translated_child_term_id ) {
  8809. $translated_parent_term_id = wp_get_term_taxonomy_parent_id( $translated_child_term_id, $taxonomy );
  8810. if ( $translated_parent_term_id ) {
  8811. if ( ! isset( $translated_children[ $translated_parent_term_id ] ) ) {
  8812. $translated_children[ $translated_parent_term_id ] = array();
  8813. }
  8814. $translated_children[ $translated_parent_term_id ][ ] = $translated_child_term_id;
  8815. }
  8816. }
  8817. }
  8818. }
  8819. }
  8820. wp_cache_set( $cache_key, $translated_children, $cache_group );
  8821. return $translated_children;
  8822. }
  8823. function pre_update_option_taxonomy_children($value, $old_value) {
  8824. $current_language = $this->get_current_language();
  8825. $default_language = $this->get_default_language();
  8826. if ( $current_language == $default_language ) {
  8827. return $value;
  8828. }
  8829. $cache_key_array[ ] = $current_language;
  8830. $cache_key_array[ ] = $default_language;
  8831. $cache_key_array[ ] = $value;
  8832. $cache_key_array[ ] = $old_value;
  8833. $cache_key = md5( serialize( $cache_key_array ) );
  8834. $cache_group = 'pre_update_option_taxonomy_children';
  8835. $cache_found = false;
  8836. $result = wp_cache_get( $cache_key, $cache_group, false, $cache_found );
  8837. if ( $cache_found ) {
  8838. return $result;
  8839. }
  8840. $debug_backtrace = $this->get_backtrace( 4, false, false );
  8841. $taxonomy = false;
  8842. //Find the taxonomy name
  8843. if ( isset( $debug_backtrace[ 3 ] ) && isset( $debug_backtrace[ 3 ][ 'args' ] ) ) {
  8844. $option_name = $debug_backtrace[ 3 ][ 'args' ][ 0 ];
  8845. $taxonomies = explode( '_', $option_name );
  8846. $taxonomy = $taxonomies[ 0 ];
  8847. }
  8848. if($taxonomy) {
  8849. remove_filter("option_{$taxonomy}_children", array($this, 'option_taxonomy_children'), 10 );
  8850. remove_filter("pre_update_option_{$taxonomy}_children", array($this, 'pre_update_option_taxonomy_children'), 10 );
  8851. $new_value = get_option("{$taxonomy}_children");
  8852. add_filter("option_{$taxonomy}_children", array($this, 'option_taxonomy_children'), 10 );
  8853. add_filter("pre_update_option_{$taxonomy}_children", array($this, 'pre_update_option_taxonomy_children'), 10, 2 );
  8854. return $new_value;
  8855. }
  8856. return $value;
  8857. }
  8858. /**
  8859. * @param int|array $terms_ids
  8860. * @param $taxonomy
  8861. */
  8862. public function update_terms_relationship_cache( $terms_ids, $taxonomy ) {
  8863. remove_action( 'get_term', array( $this, 'get_term_filter' ), 1 );
  8864. remove_filter( 'get_terms_args', array( $this, 'get_terms_args_filter' ) );
  8865. remove_filter( 'terms_clauses', array( $this, 'terms_clauses' ), 10 );
  8866. remove_filter( 'list_terms_exclusions', array( $this, 'exclude_other_terms' ), 1 );
  8867. clean_term_cache( $terms_ids, $taxonomy );
  8868. add_action( 'get_term', array( $this, 'get_term_filter' ), 1, 2 );
  8869. add_filter( 'get_terms_args', array( $this, 'get_terms_args_filter' ) );
  8870. // filters terms by language
  8871. add_filter( 'terms_clauses', array( $this, 'terms_clauses' ), 10, 4 );
  8872. add_filter( 'list_terms_exclusions', array( $this, 'exclude_other_terms' ), 1, 2 );
  8873. }
  8874. /**
  8875. * Used as filter for wordpress core function url_to_postid()
  8876. *
  8877. * @global AbsoluteLinks $absolute_links_object
  8878. * @param string $url URL to filter
  8879. * @return string URL changed into format ...?p={ID} or original
  8880. */
  8881. function url_to_postid($url) {
  8882. if ((strpos($url, 'wp-login.php') !== false)) {
  8883. return $url;
  8884. } else if ( strpos($url, '/wp-admin/') !== false ) {
  8885. return $url;
  8886. } else if ( strpos($url, '/wp-content/') !== false ) {
  8887. return $url;
  8888. }
  8889. $is_language_in_domain = false; // if language negotiation type as lang. in domain
  8890. $is_translated_domain = false; // if this url is in secondary language domain
  8891. // for 'diffrent domain per language' we need to switch_lang according to domain of parsed $url
  8892. if (2 == $this->settings['language_negotiation_type'] && isset($this->settings['language_domains'])) {
  8893. $is_language_in_domain = true;
  8894. // if url domain fits to one of secondary language domains
  8895. // switch sitepress language to this
  8896. // but save current language context in $current_language, we will have to switch to this back
  8897. foreach ($this->settings['language_domains'] as $code => $domain) {
  8898. if ( strpos($url, $domain) === 0 ) {
  8899. $is_translated_domain = true;
  8900. $current_language = $this->get_current_language();
  8901. $this->switch_lang($code);
  8902. $url = str_replace($domain, site_url(), $url);
  8903. break;
  8904. }
  8905. }
  8906. // if it is url in original domain
  8907. // switch sitepress language to default language
  8908. // but save current language context in $current_language, we will have to switch to this back
  8909. if (!$is_translated_domain) {
  8910. $current_language = $this->get_current_language();
  8911. $default_language = $this->get_default_language();
  8912. $this->switch_lang($default_language);
  8913. }
  8914. }
  8915. // we will use AbsoluteLinks::_process_generic_text, so make sure that
  8916. // we have this object here
  8917. global $absolute_links_object;
  8918. if (!isset($absolute_links_object) || !is_a($absolute_links_object, 'AbsoluteLinks') || $is_language_in_domain ) {
  8919. require_once ICL_PLUGIN_PATH . '/inc/absolute-links/absolute-links.class.php';
  8920. $absolute_links_object = new AbsoluteLinks();
  8921. }
  8922. // in next steps we will have to compare processed url with original,
  8923. // so we need to save original
  8924. $original_url = $url;
  8925. // we also need site_url for comparisions
  8926. $site_url = site_url();
  8927. // _process_generic_text will change slug urls into ?p=1 or ?cpt-slug=cpt-title
  8928. // but this function operates not on clean url but on html <a> element
  8929. // we need to change temporary url into html, pass to this function and
  8930. // extract url from returned html
  8931. $html = '<a href="'.$url.'">removeit</a>';
  8932. $alp_broken_links = array();
  8933. remove_filter('url_to_postid', array($this, 'url_to_postid'));
  8934. $html = $absolute_links_object->_process_generic_text($html, $alp_broken_links);
  8935. add_filter('url_to_postid', array($this, 'url_to_postid'));
  8936. $url = str_replace(array('<a href="', '">removeit</a>'), array('', ''), $html);
  8937. // for 'diffrent domain per language', switch language back. now we can do this
  8938. if ($is_language_in_domain) {
  8939. $this->switch_lang($current_language);
  8940. }
  8941. // if this is not url to external site
  8942. if ( 0 === strpos($original_url, $site_url)) {
  8943. // if this is url like ...?cpt-rewrite-slug=cpt-title
  8944. // change it into ...?p=11
  8945. $url2 = $this->cpt_url_to_id_url($url, $original_url);
  8946. if ($url2 == $url && $original_url != $url) { // if it was not a case with ?cpt-slug=cpt-title
  8947. // if this is translated post and it has the same slug as original,
  8948. // _process_generic_text returns the same ID for both
  8949. // lets check if it is this case and replace ID in returned url
  8950. $url = $this->maybe_adjust_url($url, $original_url);
  8951. } else { // yes! it was not a case with ?cpt-slug=cpt-title
  8952. $url = $url2;
  8953. }
  8954. }
  8955. return $url;
  8956. }
  8957. /**
  8958. * Check if $url is in format ...?cpt-slug=cpt-title and change into ...?p={ID}
  8959. *
  8960. *
  8961. * @param string $url URL, probably in format ?cpt-slug=cpt-title
  8962. * @param string $original_url URL in original format (probably with permalink)
  8963. * @return string URL, if $url was in expected format ?cpt-slug format, url is now changed into ?p={ID}, otherwise, returns $url as it was passed in parameter
  8964. */
  8965. function cpt_url_to_id_url($url, $original_url) {
  8966. $parsed_url = parse_url($url);
  8967. if (!isset($parsed_url['query'])) {
  8968. return $url;
  8969. }
  8970. $query = $parsed_url['query'];
  8971. parse_str($query, $vars);
  8972. $args = array(
  8973. 'public' => true,
  8974. '_builtin' => false
  8975. );
  8976. $post_types = get_post_types($args, 'objects');
  8977. foreach ($post_types as $name => $attrs) {
  8978. if ( isset( $vars[ $attrs->rewrite['slug'] ] ) ) {
  8979. $post_type = $name;
  8980. $post_slug = $vars[ $attrs->rewrite['slug'] ];
  8981. break;
  8982. }
  8983. }
  8984. if (!isset($post_type, $post_slug)) {
  8985. return $url;
  8986. }
  8987. $args = array(
  8988. 'name' => $post_slug,
  8989. 'post_type' => $post_type
  8990. );
  8991. $post = new WP_Query($args);
  8992. if (!isset($post->post)) {
  8993. return $url;
  8994. }
  8995. $id = $post->post->ID;
  8996. $post_language = $this->get_language_for_element($id, 'post_' . $post_type);
  8997. $url_language = $this->get_language_from_url($original_url);
  8998. if ($post_language != $url_language) {
  8999. $trid = $this->get_element_trid( $id, 'post_' . $post_type );
  9000. $translations = $this->get_element_translations( $trid, 'post_' . $post_type );
  9001. if (isset($translations[$url_language])) {
  9002. $translation = $translations[$url_language];
  9003. if (isset($translation->element_id)) {
  9004. $nvars['p'] = $translation->element_id;
  9005. }
  9006. }
  9007. } else {
  9008. $nvars['p'] = $id;
  9009. }
  9010. $new_query = http_build_query($nvars);
  9011. $url = str_replace($query, $new_query, $url);
  9012. return $url;
  9013. }
  9014. /**
  9015. * Fix sticky link url to have ID of translated post (used in case both translations have same slug)
  9016. *
  9017. * @param string $url - url in sticky link form
  9018. * @param string $original_url - url in permalink form
  9019. * @return string - url in sticky link form to correct translation
  9020. */
  9021. private function maybe_adjust_url($url, $original_url) {
  9022. $url_language = $this->get_language_from_url($original_url);
  9023. $parsed_url = parse_url($url);
  9024. $query = $parsed_url['query'];
  9025. parse_str($query, $vars);
  9026. if (isset($vars['page_id'])) {
  9027. $inurl = 'page_id';
  9028. $post_id = $vars['page_id'];
  9029. } elseif (isset($vars['p'])) {
  9030. $inurl = 'p';
  9031. $post_id = $vars['p'];
  9032. }
  9033. if (isset($post_id)) {
  9034. $post_type = get_post_type($post_id);
  9035. $post_language = $this->get_language_for_element($post_id, 'post_' . $post_type);
  9036. if ($post_language != $url_language) {
  9037. $trid = $this->get_element_trid( $post_id, 'post_' . $post_type );
  9038. $translations = $this->get_element_translations( $trid, 'post_' . $post_type );
  9039. if (isset($translations[$url_language])) {
  9040. $translation = $translations[$url_language];
  9041. if (isset($translation->element_id)) {
  9042. $vars[$inurl] = $translation->element_id;
  9043. $new_query = http_build_query($vars);
  9044. $url = str_replace($query, $new_query, $url);
  9045. }
  9046. }
  9047. }
  9048. }
  9049. return $url;
  9050. }
  9051. /**
  9052. * Find language of document based on given permalink
  9053. *
  9054. * @param string $url Local url in permalink form
  9055. * @return string two letters language code
  9056. */
  9057. function get_language_from_url($url) {
  9058. static $languages;
  9059. if (isset($languages[$url])) {
  9060. return $languages[$url];
  9061. }
  9062. $site_url = site_url();
  9063. if (1 == $this->settings['language_negotiation_type']) {
  9064. $url_path = ltrim(
  9065. str_replace($site_url, "", $url),
  9066. "/");
  9067. $fragments = explode("/", $url_path);
  9068. $fragment = $fragments[0];
  9069. if (isset($this->active_languages[$fragment])) {
  9070. $languages[$url] = $fragment;
  9071. return $fragment;
  9072. } else {
  9073. $languages[$url] = $this->get_default_language();
  9074. return $this->get_default_language();
  9075. }
  9076. } else if ( 2 == $this->settings['language_negotiation_type'] ) {
  9077. if ( isset($this->settings['language_domains'])) {
  9078. $is_translated_domain = false;
  9079. foreach ($this->settings['language_domains'] as $code => $domain) {
  9080. if ( strpos($url, $domain) === 0 ) {
  9081. $is_translated_domain = true;
  9082. $languages[$url] = $code;
  9083. return $code;
  9084. }
  9085. }
  9086. }
  9087. $languages[$url] = $this->get_current_language();
  9088. return $this->get_current_language();
  9089. } else if (3 == $this->settings['language_negotiation_type']) {
  9090. $url_query = parse_url($url, PHP_URL_QUERY);
  9091. if (isset($url_query)) {
  9092. parse_str($url_query, $vars);
  9093. if (isset($vars['lang']) && isset($this->active_languages[ $vars['lang'] ])) {
  9094. $languages[$url] = $vars['lang'];
  9095. return $vars['lang'];
  9096. }
  9097. }
  9098. $languages[$url] = $this->get_default_language();
  9099. return $this->get_default_language();
  9100. }
  9101. }
  9102. }