PageRenderTime 68ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 4ms

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

https://github.com/ajency/Impruw
PHP | 11232 lines | 8826 code | 1653 blank | 753 comment | 2565 complexity | 9da0637d29f96643dfb3e6fb5771ee05 MD5 | raw file
Possible License(s): AGPL-1.0, MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0

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

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

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