PageRenderTime 9ms CodeModel.GetById 88ms app.highlight 55ms RepoModel.GetById 1ms app.codeStats 3ms

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

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

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

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

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