/Dossier wordpress/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php
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
- <?php
- /**
- * Main SitePress Class
- *
- * @package wpml-core
- */
- class SitePress
- {
- private $settings;
- private $active_languages = array();
- private $this_lang;
- private $wp_query;
- private $admin_language = null;
- private $user_preferences = array();
- private $current_user;
- public $queries = array();
- /**
- * @var icl_cache
- */
- public $icl_translations_cache;
- /**
- * @var icl_cache
- */
- public $icl_locale_cache;
- /**
- * @var icl_cache
- */
- public $icl_flag_cache;
- /**
- * @var icl_cache
- */
- public $icl_language_name_cache;
- /**
- * @var icl_cache
- */
- public $icl_term_taxonomy_cache;
- function __construct()
- {
- global $wpdb, $pagenow;
- $this->settings = get_option( 'icl_sitepress_settings' );
- //TODO: To remove in WPML 3.5
- //@since 3.1
- if(is_admin() && !$this->get_setting('icl_capabilities_verified')) {
- icl_enable_capabilities();
- $this->settings = get_option( 'icl_sitepress_settings' );
- }
- // set up current user early
- // no authentication
- if ( defined( 'LOGGED_IN_COOKIE' ) && isset( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
- list( $username ) = explode( '|', $_COOKIE[ LOGGED_IN_COOKIE ] );
- $user_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->users} WHERE user_login = %s", array($username) ) );
- } else {
- $user_id = 0;
- }
- $this->current_user = new WP_User( $user_id );
- if ( is_null( $pagenow ) && is_multisite() ) {
- include ICL_PLUGIN_PATH . '/inc/hacks/vars-php-multisite.php';
- }
- if ( false != $this->settings ) {
- $this->verify_settings();
- }
- if ( isset( $_GET[ 'icl_action' ] ) ) {
- require_once ABSPATH . WPINC . '/pluggable.php';
- if ( $_GET[ 'icl_action' ] == 'reminder_popup' ) {
- add_action( 'init', array( $this, 'reminders_popup' ) );
- } elseif ( $_GET[ 'icl_action' ] == 'dismiss_help' ) {
- $this->settings[ 'dont_show_help_admin_notice' ] = true;
- $this->save_settings();
- }
- }
- if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == ICL_PLUGIN_FOLDER . '/menu/troubleshooting.php' && isset( $_GET[ 'debug_action' ] ) ) {
- ob_start();
- }
- if ( isset( $_REQUEST[ 'icl_ajx_action' ] ) ) {
- add_action( 'init', array( $this, 'ajax_setup' ), 15 );
- }
- add_action( 'admin_footer', array( $this, 'icl_nonces' ) );
- // Process post requests
- if ( !empty( $_POST ) ) {
- add_action( 'init', array( $this, 'process_forms' ) );
- }
- add_action( 'plugins_loaded', array( $this, 'initialize_cache' ), 0 );
- add_action( 'plugins_loaded', array( $this, 'init' ), 1 );
- add_action( 'init', array( $this, 'on_wp_init' ), 1 );
- add_action( 'admin_print_scripts', array( $this, 'js_scripts_setup' ) );
- add_action( 'admin_print_styles', array( $this, 'css_setup' ) );
- // Administration menus
- add_action( 'admin_menu', array( $this, 'administration_menu' ) );
- add_action( 'admin_menu', array( $this, 'administration_menu2' ), 30 );
- add_action( 'init', array( $this, 'plugin_localization' ) );
- //add_filter('tag_template', array($this, 'load_taxonomy_template'));
- if ( $this->settings[ 'existing_content_language_verified' ] ) {
- // Post/page language box
- if ( $pagenow == 'post.php' || $pagenow == 'post-new.php' || $pagenow == 'edit.php' ) {
- add_action( 'admin_head', array( $this, 'post_edit_language_options' ) );
- }
- //For when it will be possible to add custom bulk actions
- //add_action( 'bulk_actions-edit-post', array( $this, 'bulk_actions' ) );
- // Post/page save actions
- add_action( 'save_post', array( $this, 'save_post_actions' ), 10, 2 );
- //post/page delete taxonomy
- add_action( 'deleted_term_relationships', array( $this, 'deleted_term_relationships' ), 10, 2 );
- add_action( 'updated_post_meta', array( $this, 'update_post_meta' ), 100, 4 );
- add_action( 'added_post_meta', array( $this, 'update_post_meta' ), 100, 4 );
- add_action( 'updated_postmeta', array( $this, 'update_post_meta' ), 100, 4 ); // ajax
- add_action( 'added_postmeta', array( $this, 'update_post_meta' ), 100, 4 ); // ajax
- add_action( 'delete_postmeta', array( $this, 'delete_post_meta' ) ); // ajax
- // filter user taxonomy input
- add_filter( 'pre_post_tax_input', array( $this, 'validate_taxonomy_input' ) );
- // Post/page delete actions
- // add_action('delete_post', array($this,'delete_post_actions'));
- add_action( 'before_delete_post', array( $this, 'before_delete_post_actions' ) );
- add_action( 'deleted_post', array( $this, 'deleted_post_actions' ) );
- add_action( 'wp_trash_post', array( $this, 'trash_post_actions' ) );
- add_action( 'untrashed_post', array( $this, 'untrashed_post_actions' ) );
- add_filter( 'posts_join', array( $this, 'posts_join_filter' ), 10, 2 );
- add_filter( 'posts_where', array( $this, 'posts_where_filter' ), 10, 2 );
- add_filter( 'comment_feed_join', array( $this, 'comment_feed_join' ) );
- add_filter( 'comments_clauses', array( $this, 'comments_clauses' ), 10, 2 );
- // Allow us to filter the Query vars before the posts query is being built and executed
- add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
- add_action( 'loop_start', array( $this, 'loop_start' ), 10 );
- add_filter( 'the_posts', array( $this, 'the_posts' ), 10 );
- add_filter( 'get_pages', array( $this, 'get_pages' ), 100, 2 );
- if ( $pagenow == 'edit.php' ) {
- add_action( 'admin_footer', array( $this, 'language_filter' ) );
- }
- add_filter( 'get_pages', array( $this, 'exclude_other_language_pages2' ) );
- add_filter( 'wp_dropdown_pages', array( $this, 'wp_dropdown_pages' ) );
- // posts and pages links filters
- add_filter( 'post_link', array( $this, 'permalink_filter' ), 1, 2 );
- add_filter( 'post_type_link', array( $this, 'permalink_filter' ), 1, 2 );
- add_filter( 'page_link', array( $this, 'permalink_filter' ), 1, 2 );
- add_filter( 'get_comment_link', array( $this, 'get_comment_link_filter' ) );
- //Taxonomies
- if ( version_compare( preg_replace( '#-RC[0-9]+(-[0-9]+)?$#', '', $GLOBALS[ 'wp_version' ] ), '3.1', '<' ) ) {
- add_filter( 'category_link', array( $this, 'category_permalink_filter' ), 1, 2 );
- add_filter( 'tag_link', array( $this, 'tax_permalink_filter' ), 1, 2 );
- }
- add_filter( 'term_link', array( $this, 'tax_permalink_filter' ), 1, 2 );
- add_action( 'create_term', array( $this, 'create_term' ), 1, 2 );
- add_action( 'edit_term', array( $this, 'create_term' ), 1, 2 );
- add_action( 'delete_term', array( $this, 'delete_term' ), 1, 3 );
- add_action( 'get_term', array( $this, 'get_term_filter' ), 1, 2 );
- add_filter( 'get_terms_args', array( $this, 'get_terms_args_filter' ) );
- // filters terms by language
- add_filter( 'terms_clauses', array( $this, 'terms_clauses' ), 10, 4 );
- add_filter( 'list_terms_exclusions', array( $this, 'exclude_other_terms' ), 1, 2 );
- // allow adding terms with the same name in different languages
- add_filter( "pre_term_name", array( $this, 'pre_term_name' ), 1, 2 );
- // allow adding categories with the same name in different languages
- add_action( 'admin_init', array( $this, 'pre_save_category' ) );
- //Hooking to 'option_{taxonomy}_children' to translate taxonomy children
- // global $wp_taxonomies;
- // foreach ( $wp_taxonomies as $tax_key => $tax ) {
- // if ( $this->is_translated_taxonomy( $tax_key ) && is_taxonomy_hierarchical($tax_key) ) {
- // add_filter("option_{$tax_key}_children", array($this, 'option_taxonomy_children'), 10 );
- // add_filter("pre_update_option_{$tax_key}_children", array($this, 'pre_update_option_taxonomy_children'), 10, 2 );
- // }
- // }
- add_filter( 'get_edit_term_link', array( $this, 'get_edit_term_link' ), 1, 4 );
- // short circuit get default category
- add_filter( 'pre_option_default_category', array( $this, 'pre_option_default_category' ) );
- add_filter( 'update_option_default_category', array( $this, 'update_option_default_category' ), 1, 2 );
- add_filter( 'the_category', array( $this, 'the_category_name_filter' ) );
- add_filter( 'get_terms', array( $this, 'get_terms_filter' ) );
- add_filter( 'get_the_terms', array( $this, 'get_the_terms_filter' ), 10, 3 );
- add_filter( 'single_cat_title', array( $this, 'the_category_name_filter' ) );
- add_filter( 'term_links-category', array( $this, 'the_category_name_filter' ) );
- add_filter( 'term_links-post_tag', array( $this, 'the_category_name_filter' ) );
- add_filter( 'tags_to_edit', array( $this, 'the_category_name_filter' ) );
- add_filter( 'single_tag_title', array( $this, 'the_category_name_filter' ) );
- // custom hook for adding the language selector to the template
- add_action( 'icl_language_selector', array( $this, 'language_selector' ) );
- // front end js
- add_action( 'wp_head', array( $this, 'front_end_js' ) );
- add_action( 'wp_head', array( $this, 'rtl_fix' ) );
- add_action( 'admin_print_styles', array( $this, 'rtl_fix' ) );
- add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) );
- add_filter( 'get_edit_post_link', array( $this, 'get_edit_post_link' ), 1, 3 );
- // adjacent posts links
- add_filter( 'get_previous_post_join', array( $this, 'get_adjacent_post_join' ) );
- add_filter( 'get_next_post_join', array( $this, 'get_adjacent_post_join' ) );
- add_filter( 'get_previous_post_where', array( $this, 'get_adjacent_post_where' ) );
- add_filter( 'get_next_post_where', array( $this, 'get_adjacent_post_where' ) );
- // feeds links
- add_filter( 'feed_link', array( $this, 'feed_link' ) );
- // commenting links
- add_filter( 'post_comments_feed_link', array( $this, 'post_comments_feed_link' ) );
- add_filter( 'trackback_url', array( $this, 'trackback_url' ) );
- add_filter( 'user_trailingslashit', array( $this, 'user_trailingslashit' ), 1, 2 );
- // date based archives
- add_filter( 'year_link', array( $this, 'archives_link' ) );
- add_filter( 'month_link', array( $this, 'archives_link' ) );
- add_filter( 'day_link', array( $this, 'archives_link' ) );
- add_filter( 'getarchives_join', array( $this, 'getarchives_join' ) );
- add_filter( 'getarchives_where', array( $this, 'getarchives_where' ) );
- add_filter( 'pre_option_home', array( $this, 'pre_option_home' ) );
- if ( !is_admin() ) {
- add_filter( 'attachment_link', array( $this, 'attachment_link_filter' ), 10, 2 );
- }
- // Filter custom type archive link (since WP 3.1)
- add_filter( 'post_type_archive_link', array( $this, 'post_type_archive_link_filter' ), 10, 2 );
- add_filter( 'author_link', array( $this, 'author_link' ) );
- add_filter( 'wp_unique_post_slug', array( $this, 'wp_unique_post_slug' ), 100, 5 );
- add_filter( 'home_url', array( $this, 'home_url' ), 1, 4 );
- // language negotiation
- add_action( 'query_vars', array( $this, 'query_vars' ) );
- add_filter( 'language_attributes', array( $this, 'language_attributes' ) );
- add_action( 'locale', array( $this, 'locale' ) );
- if ( isset( $_GET[ '____icl_validate_domain' ] ) ) {
- echo '<!--' . get_home_url() . '-->';
- exit;
- }
- add_filter( 'pre_option_page_on_front', array( $this, 'pre_option_page_on_front' ) );
- add_filter( 'pre_option_page_for_posts', array( $this, 'pre_option_page_for_posts' ) );
- add_filter( 'option_sticky_posts', array( $this, 'option_sticky_posts' ) );
- add_filter( 'request', array( $this, 'request_filter' ) );
- add_action( 'wp_head', array( $this, 'set_wp_query' ) );
- add_action( 'show_user_profile', array( $this, 'show_user_options' ) );
- add_action( 'personal_options_update', array( $this, 'save_user_options' ) );
- // column with links to translations (or add translation) - low priority
- add_action( 'init', array( $this, 'configure_custom_column' ), 1010 ); // accommodate Types init@999
- // adjust queried categories and tags ids according to the language
- if ( $this->settings[ 'auto_adjust_ids' ] ) {
- add_action( 'parse_query', array( $this, 'parse_query' ) );
- add_action( 'wp_list_pages_excludes', array( $this, 'adjust_wp_list_pages_excludes' ) );
- if ( !is_admin() ) {
- add_filter( 'get_term', array( $this, 'get_term_adjust_id' ), 1, 1 );
- add_filter( 'category_link', array( $this, 'category_link_adjust_id' ), 1, 2 );
- add_filter( 'get_terms', array( $this, 'get_terms_adjust_ids' ), 1, 3 );
- add_filter( 'get_pages', array( $this, 'get_pages_adjust_ids' ), 1, 2 );
- }
- }
- if ( !is_admin() ) {
- add_action( 'wp_head', array( $this, 'meta_generator_tag' ) );
- }
- require_once ICL_PLUGIN_PATH . '/inc/wp-nav-menus/iclNavMenu.class.php';
- new iclNavMenu;
- if ( is_admin() || defined( 'XMLRPC_REQUEST' ) || preg_match( '#wp-comments-post\.php$#', $_SERVER[ 'REQUEST_URI' ] ) ) {
- global $iclTranslationManagement, $ICL_Pro_Translation;
- $iclTranslationManagement = new TranslationManagement;
- $ICL_Pro_Translation = new ICL_Pro_Translation();
- }
- add_action( 'wp_login', array( $this, 'reset_admin_language_cookie' ) );
- if ( $this->settings[ 'seo' ][ 'canonicalization_duplicates' ] ) {
- add_action( 'template_redirect', array( $this, 'setup_canonical_urls' ), 100 );
- }
- add_filter( 'taxonomy_template', array($this, 'slug_template') );
- add_filter( 'category_template', array($this, 'slug_template') );
- add_action( 'init', array( $this, '_taxonomy_languages_menu' ), 99 ); //allow hooking in
- if ( $this->settings[ 'seo' ][ 'head_langs' ] ) {
- add_action( 'wp_head', array( $this, 'head_langs' ) );
- }
- } //end if the initial language is set - existing_content_language_verified
- add_action( 'wp_dashboard_setup', array( $this, 'dashboard_widget_setup' ) );
- if ( is_admin() && $pagenow == 'index.php' ) {
- add_action( 'icl_dashboard_widget_notices', array( $this, 'print_translatable_custom_content_status' ) );
- }
- add_filter( 'core_version_check_locale', array( $this, 'wp_upgrade_locale' ) );
- if ( $pagenow == 'post.php' && isset( $_REQUEST[ 'action' ] ) && $_REQUEST[ 'action' ] == 'edit' && isset( $_GET[ 'post' ] ) ) {
- add_action( 'init', '_icl_trash_restore_prompt' );
- }
- add_action( 'init', array( $this, 'js_load' ), 2 ); // enqueue scripts - higher priority
- add_filter( 'get_pagenum_link', array( $this, 'get_pagenum_link' ) );
-
- add_action('switch_blog', array($this, 'init_settings'));
- add_filter('url_to_postid', array($this, 'url_to_postid'));
- }
- function get_pagenum_link( $url ) {
- // fix cases like that in url:
- // lang=pl%2F%3Flang%3Dpl
- // lang=pl/?lang=pl
- $current_language = $this->get_current_language();
- $find[ ] = 'lang=' . $current_language . '%2F%3Flang%3D' . $current_language;
- $find[ ] = 'lang=' . $current_language . '/?lang=' . $current_language;
- $replace = 'lang=' . $current_language;
- $url = str_replace( $find, $replace, $url );
- // fix cases like that:
- // ?lang=pl/page/3/?lang=pl
- $pattern = '/(\?lang=' . $current_language . ')(\/page\/\d+)/';
- $url = preg_replace( $pattern, '$2', $url );
- return $url;
- }
-
- function init()
- {
- global $wpdb;
- $this->get_user_preferences();
- $this->set_admin_language();
- $default_language = $this->get_default_language();
- // default value for theme_localization_type OR
- // reset theme_localization_type if string translation was on (theme_localization_type was set to 2) and then it was deactivated
- if ( !isset( $this->settings[ 'theme_localization_type' ] ) || ( $this->settings[ 'theme_localization_type' ] == 1 && !defined( 'WPML_ST_VERSION' ) && !defined( 'WPML_DOING_UPGRADE' ) ) ) {
- global $sitepress_settings;
- $this->settings[ 'theme_localization_type' ] = $sitepress_settings[ 'theme_localization_type' ] = 2;
- }
- //configure callbacks for plugin menu pages
- if ( defined( 'WP_ADMIN' ) && isset( $_GET[ 'page' ] ) && 0 === strpos( $_GET[ 'page' ], basename( ICL_PLUGIN_PATH ) . '/' ) ) {
- add_action( 'icl_menu_footer', array( $this, 'menu_footer' ) );
- }
- //Run only if existing content language has been verified, and is front-end or settings are not corrupted
- if (!empty( $this->settings[ 'existing_content_language_verified' ] ) && (!is_admin() || SitePress::check_settings_integrity()) ) {
- if ( $this->settings[ 'language_negotiation_type' ] == 1 && $this->settings[ 'urls' ][ 'directory_for_default_language' ] && $this->settings[ 'urls' ][ 'show_on_root' ] == 'page' ) {
- include ICL_PLUGIN_PATH . '/inc/home-url-functions.php';
- }
-
- if ($this->settings[ 'language_negotiation_type' ] == 2) {
- add_filter( 'allowed_redirect_hosts', array( $this, 'allowed_redirect_hosts' ) );
- }
- if ( defined( 'WP_ADMIN' ) ) {
- if ( $this->settings[ 'language_negotiation_type' ] == 2 ) {
- //Login and Logout
- add_filter( 'login_url', array( $this, 'convert_url' ) );
- add_filter( 'logout_url', array( $this, 'convert_url' ) );
- add_filter( 'site_url', array( $this, 'convert_url' ) );
- }
- if (isset($_GET['post']) && is_numeric($_GET['post'])) {
- $post_type = get_post_type($_GET['post']);
- }
-
- if ( isset( $_GET[ 'lang' ] ) ) {
- $this->this_lang = rtrim( strip_tags( $_GET[ 'lang' ] ), '/' );
- $al = $this->get_active_languages();
- $al[ 'all' ] = true;
- if ( empty( $al[ $this->this_lang ] ) ) {
- $this->this_lang = $default_language;
- }
- // force default language for string translation
- // we also make sure it's not saved in the cookie
- } 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' ) )
- ) {
- $this->this_lang = $default_language;
- } elseif ( wpml_is_ajax() ) {
- $al = $this->get_active_languages();
- if ( isset( $_POST[ 'lang' ] ) && isset( $al[ $_POST[ 'lang' ] ] ) ) {
- $this->this_lang = $_POST[ 'lang' ];
- } else {
- $this->this_lang = $this->get_language_cookie();
- }
- } elseif ( $lang = $this->get_admin_language_cookie() ) {
- $this->this_lang = $lang;
- } elseif ( isset($_POST['action']) && $_POST['action'] == 'editpost' && isset($_POST['icl_post_language'])) {
- $this->this_lang = $_POST['icl_post_language'];
- } elseif ( isset($_GET['post']) && is_numeric($_GET['post']) && isset($post_type) && $this->is_translated_post_type($post_type)) {
- $this->this_lang = $this->get_language_for_element($_GET['post'], 'post_' . $post_type);
- } else {
- $this->this_lang = $default_language;
- }
- 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' ) ) {
- $this->set_admin_language_cookie();
- }
- } else {
- $al = $this->get_active_languages();
- foreach ( $al as $l ) {
- $active_languages[ ] = $l[ 'code' ];
- }
- $active_languages[ ] = 'all';
- $s = isset( $_SERVER[ 'HTTPS' ] ) && $_SERVER[ 'HTTPS' ] == 'on' ? 's' : '';
- $home = get_home_url();
- if ( $s ) {
- $home = preg_replace( '#^http://#', 'https://', $home );
- }
- $url_parts = parse_url( $home );
- $non_default_port = ( isset( $url_parts[ 'port' ] ) && $url_parts[ 'port' ] != 80 ) ? ':' . $url_parts[ 'port' ] : '';
- $request = 'http' . $s . '://' . $this->get_server_host_name() . $_SERVER[ 'REQUEST_URI' ];
- $blog_path = ! empty( $url_parts[ 'path' ] ) ? $url_parts[ 'path' ] : '';
-
- switch ( $this->settings[ 'language_negotiation_type' ] ) {
- case 1:
- $path = str_replace( $home, '', $request );
- $parts = explode( '?', $path );
- $path = $parts[ 0 ];
- $exp = explode( '/', trim( $path, '/' ) );
- $language_part = $exp[ 0 ];
- if ( in_array( $language_part, $active_languages ) ) {
- $this->this_lang = $exp[ 0 ];
- // before hijacking the SERVER[REQUEST_URI]
- // override the canonical_redirect action
- // keep a copy of the original request uri
- remove_action( 'template_redirect', 'redirect_canonical' );
- global $_icl_server_request_uri;
- $_icl_server_request_uri = $_SERVER[ 'REQUEST_URI' ];
- add_action( 'template_redirect', array($this,'icl_redirect_canonical_wrapper'), 0 );
- //deal with situations when template files need to be called directly
- add_action( 'template_redirect', array( $this, '_allow_calling_template_file_directly' ) );
- //$_SERVER['REQUEST_URI'] = preg_replace('@^'. $blog_path . '/' . $this->this_lang.'@i', $blog_path ,$_SERVER['REQUEST_URI']);
- // Check for special case of www.example.com/fr where the / is missing on the end
- $parts = parse_url( $_SERVER[ 'REQUEST_URI' ] );
- if ( strlen( $parts[ 'path' ] ) == 0 ) {
- $_SERVER[ 'REQUEST_URI' ] = '/' . $_SERVER[ 'REQUEST_URI' ];
- }
- } else {
- $this->this_lang = $default_language;
- }
- if ( !trim( $path, '/' ) && $this->settings[ 'urls' ][ 'directory_for_default_language' ] ) {
- if ( $this->settings[ 'urls' ][ 'show_on_root' ] == 'html_file' ) {
- // html file
- if ( false === strpos( $this->settings[ 'urls' ][ 'root_html_file_path' ], '/' ) ) {
- $html_file = ABSPATH . $this->settings[ 'urls' ][ 'root_html_file_path' ];
- } else {
- $html_file = $this->settings[ 'urls' ][ 'root_html_file_path' ];
- }
- include $html_file;
- exit;
- } else {
- //page
- if ( !trim( $path, '/' ) ) {
- wpml_home_url_setup_root_page();
- }
- }
- }
- break;
- case 2:
- $this->this_lang = $default_language;
- foreach( $this->settings[ 'language_domains' ] as $language_code => $domain ){
- if( rtrim( $this->get_server_host_name() . $blog_path, '/' ) == rtrim( preg_replace( '@^https?://@', '', $domain ), '/' ) ){
- $this->this_lang = $language_code;
- break;
- }
- }
-
- if ( defined( 'ICL_USE_MULTIPLE_DOMAIN_LOGIN' ) && ICL_USE_MULTIPLE_DOMAIN_LOGIN ) {
- include ICL_PLUGIN_PATH . '/modules/multiple-domains-login.php';
- }
- add_filter( 'site_url', array( $this, 'convert_url' ) );
- break;
- case 3:
- default:
- if ( isset( $_GET[ 'lang' ] ) ) {
- $this->this_lang = preg_replace( "/[^0-9a-zA-Z-]/i", '', strip_tags( $_GET[ 'lang' ] ) );
- // set the language based on the content id - for short links
- } elseif ( isset( $_GET[ 'page_id' ] ) ) {
- $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' ] );
- $this->this_lang = $wpdb->get_var( $language_code_prepared );
- } elseif ( isset( $_GET[ 'p' ] ) ) {
- $post_type_prepared = $wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE ID=%d", $_GET[ 'p' ] );
- $post_type = $wpdb->get_var( $post_type_prepared );
- $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' ] );
- $this->this_lang = $wpdb->get_var( $language_code_prepared );
- } elseif ( isset( $_GET[ 'cat_ID' ] ) ) {
- $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' );
- $cat_tax_id = $wpdb->get_var( $cat_tax_id_prepared );
- $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 );
- $this->this_lang = $wpdb->get_var( $language_code_prepared );
- } elseif ( isset( $_GET[ 'tag' ] ) ) {
- $tag_tax_id_prepared = $wpdb->prepare( "
- SELECT x.term_taxonomy_id FROM {$wpdb->term_taxonomy} x JOIN {$wpdb->terms} t ON t.term_id = x.term_id
- WHERE t.slug=%s AND x.taxonomy='post_tag'", $_GET[ 'tag' ] );
- $tag_tax_id = $wpdb->get_var( $tag_tax_id_prepared );
- $language_code_prepared = $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations
- WHERE element_type='tax_post_tag' AND element_id=%d", $tag_tax_id );
- $this->this_lang = $wpdb->get_var( $language_code_prepared );
- }
- //
- if ( !isset( $_GET[ 'lang' ] ) && ( $this->this_lang && $this->this_lang != $default_language ) ) {
- if ( !isset( $GLOBALS[ 'wp_rewrite' ] ) ) {
- require_once ABSPATH . WPINC . '/rewrite.php';
- $GLOBALS[ 'wp_rewrite' ] = new WP_Rewrite();
- }
- define( 'ICL_DOING_REDIRECT', true );
- if ( isset( $_GET[ 'page_id' ] ) ) {
- wp_redirect( get_page_link( $_GET[ 'page_id' ] ), '301' );
- exit;
- } elseif ( isset( $_GET[ 'p' ] ) ) {
- wp_redirect( get_permalink( $_GET[ 'p' ] ), '301' );
- exit;
- } elseif ( isset( $_GET[ 'cat_ID' ] ) ) {
- wp_redirect( get_term_link( intval( $_GET[ 'cat_ID' ] ), 'category' ) );
- exit;
- } elseif ( isset( $_GET[ 'tag' ] ) ) {
- wp_redirect( get_term_link( $_GET[ 'tag' ], 'post_tag' ) );
- exit;
- } else {
- if ( isset( $this->settings[ 'taxonomies_sync_option' ] ) ) {
- $taxs = array_keys( (array)$this->settings[ 'taxonomies_sync_option' ] );
- foreach ( $taxs as $t ) {
- if ( isset( $_GET[ $t ] ) ) {
- $term_obj = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON t.term_id = x.term_id
- WHERE t.slug=%s AND x.taxonomy=%s", $_GET[ $t ], $t ) );
- $term_link = get_term_link( $term_obj, $t );
- $term_link = str_replace( '&', '&', $term_link ); // fix
- if ( $term_link && !is_wp_error( $term_link ) ) {
- wp_redirect( $term_link );
- exit;
- }
- }
- }
- }
- }
- }
- if ( empty( $this->this_lang ) ) {
- $this->this_lang = $default_language;
- }
- }
- // allow forcing the current language when it can't be decoded from the URL
- $this->this_lang = apply_filters( 'icl_set_current_language', $this->this_lang );
- }
- //reorder active language to put 'this_lang' in front
- foreach ( $this->active_languages as $k => $al ) {
- if ( $al[ 'code' ] == $this->this_lang ) {
- unset( $this->active_languages[ $k ] );
- $this->active_languages = array_merge( array( $k => $al ), $this->active_languages );
- }
- }
- // filter some queries
- add_filter( 'query', array( $this, 'filter_queries' ) );
- add_filter( 'option_rewrite_rules', array( $this, 'rewrite_rules_filter' ) );
- $this->set_language_cookie();
- 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' )
- ) {
- if ( version_compare( $GLOBALS[ 'wp_version' ], '3.3', '<' ) ) {
- // Legacy code for admin language switcher
- if ( !$this->is_rtl() && version_compare( $GLOBALS[ 'wp_version' ], '3.3', '>' ) ) {
- add_action( 'admin_notices', 'wpml_set_admin_language_switcher_place', 100 );
- add_action( 'network_admin_notices', 'wpml_set_admin_language_switcher_place', 100 );
- add_action( 'user_admin_notices', 'wpml_set_admin_language_switcher_place', 100 );
- function wpml_set_admin_language_switcher_place()
- {
- echo '<br clear="all" />';
- }
- }
- add_action( 'in_admin_header', array( $this, 'admin_language_switcher_legacy' ) );
- } else {
- // Admin language switcher goes to the WP admin bar
- add_action( 'wp_before_admin_bar_render', array( $this, 'admin_language_switcher' ) );
- }
- }
- if ( !is_admin() && defined( 'DISQUS_VERSION' ) ) {
- include ICL_PLUGIN_PATH . '/modules/disqus.php';
- }
-
- }
-
- /*
- * If user perform bulk taxonomy deletion when displaying non-default
- * language taxonomies, after deletion should stay with same language
- */
- if ( is_admin() &&
- isset($_POST['_wp_http_referer'])
- && false !== strpos($_POST['_wp_http_referer'], 'edit-tags.php')
- && !empty($_POST['delete_tags'])
- && is_array($_POST['delete_tags'])
- && !empty($_GET['lang'])
- && (
- $_POST['action'] == 'delete' || $_POST['action2'] == 'delete'
- ) ) {
- add_filter('wp_redirect', array($this, 'preserve_lang_param_after_bulk_category_delete'));
- }
- if ( $this->is_rtl() ) {
- $GLOBALS[ 'text_direction' ] = 'rtl';
- }
- if (!wpml_is_ajax() && is_admin() && empty( $this->settings[ 'dont_show_help_admin_notice' ] ) ) {
- if ( !$this->get_setting('setup_wizard_step') ) {
- if(SitePress::check_settings_integrity()) {
- add_action( 'admin_notices', array( $this, 'help_admin_notice' ) );
- }
- }
- }
- $short_v = implode( '.', array_slice( explode( '.', ICL_SITEPRESS_VERSION ), 0, 3 ) );
- if ( is_admin() && ( !isset( $this->settings[ 'hide_upgrade_notice' ] ) || $this->settings[ 'hide_upgrade_notice' ] != $short_v ) ) {
- add_action( 'admin_notices', array( $this, 'upgrade_notice' ) );
- }
- require ICL_PLUGIN_PATH . '/inc/template-constants.php';
- if ( defined( 'WPML_LOAD_API_SUPPORT' ) ) {
- require ICL_PLUGIN_PATH . '/inc/wpml-api.php';
- }
- add_action( 'wp_footer', array( $this, 'display_wpml_footer' ), 20 );
- if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
- add_action( 'xmlrpc_call', array( $this, 'xmlrpc_call_actions' ) );
- add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
- }
- if ( defined( 'WPML_TM_VERSION' ) && is_admin() ) {
- require ICL_PLUGIN_PATH . '/inc/quote.php';
- }
- add_action( 'init', array( $this, 'set_up_language_selector' ) );
- global $pagenow;
- if ( $pagenow == 'admin-ajax.php' ) {
- if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'wpml_tt_show_terms') {
- $default_language = $this->get_default_language();
- $this->switch_lang($default_language, true);
- }
- }
- // Disable the Admin language switcher when in Taxonomy Translation page
- // If the page uses AJAX and the language must be forced to default, please use the
- // if ( $pagenow == 'admin-ajax.php' )above
- if ( is_admin() && isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == ICL_PLUGIN_FOLDER . '/menu/taxonomy-translation.php' ) {
- $this->switch_lang($default_language, true);
- add_action( 'init', array( $this, 'remove_admin_language_switcher'));
- }
- //Code to run when reactivating the plugin
- $recently_activated = $this->get_setting('just_reactivated');
- if($recently_activated) {
- add_action( 'init', array( $this, 'rebuild_language_information' ), 1000 );
- }
- }
- function icl_redirect_canonical_wrapper()
- {
- global $_icl_server_request_uri;
- $requested_url = ( !empty( $_SERVER[ 'HTTPS' ] ) && strtolower( $_SERVER[ 'HTTPS' ] ) == 'on' ) ? 'https://' : 'http://';
- $requested_url .= $this->get_server_host_name();
- $requested_url .= $_icl_server_request_uri;
- redirect_canonical( $requested_url );
- }
- /**
- * If user perform bulk taxonomy deletion when displaying non-default
- * language taxonomies, after deletion should stay with same language
- *
- * @param string $location Url where browser will redirect
- * @return string Url where browser will redirect
- */
- function preserve_lang_param_after_bulk_category_delete($location) {
- if (empty($_GET['lang'])) {
- return $location;
- }
- $location = add_query_arg( 'lang', $_GET['lang'], $location );
- return $location;
- }
- function remove_admin_language_switcher() {
- remove_action( 'wp_before_admin_bar_render', array( $this, 'admin_language_switcher' ) );
- }
- function rebuild_language_information() {
- $this->set_setting('just_reactivated', 0);
- $this->save_settings();
- global $iclTranslationManagement;
- if ( isset( $iclTranslationManagement ) ) {
- $iclTranslationManagement->add_missing_language_information();
- }
- }
- function on_wp_init()
- {
- if ( is_admin() && current_user_can( 'manage_options' ) ) {
- if ( $this->icl_account_configured() ) {
- add_action( 'admin_notices', array( $this, 'icl_reminders' ) );
- }
- }
- include ICL_PLUGIN_PATH . '/inc/translation-management/taxonomy-translation.php';
- }
- function setup()
- {
- $setup_complete = $this->get_setting('setup_complete');
- if(!$setup_complete) {
- $this->set_setting('setup_complete', false);
- }
- return $setup_complete;
- }
- function get_current_user()
- {
- global $current_user;
- if ( did_action( 'set_current_user' ) ) {
- return $current_user;
- } else {
- return $this->current_user; // created early / no authentication
- }
- }
- function ajax_setup()
- {
- require ICL_PLUGIN_PATH . '/ajax.php';
- }
- function configure_custom_column()
- {
- global $pagenow, $wp_post_types;
- $pagenow_ = '';
- $is_ajax = false;
- if ( $pagenow == 'admin-ajax.php' ) {
- if ( isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'inline-save' || isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'fetch-list'
- ) {
- $is_ajax = true;
- }
- }
- if ( ( $pagenow == 'edit.php' || $pagenow_ == 'edit-pages.php' || $is_ajax ) ) {
- $post_type = isset( $_REQUEST[ 'post_type' ] ) ? $_REQUEST[ 'post_type' ] : 'post';
- switch ( $post_type ) {
- case 'post':
- case 'page':
- add_filter( 'manage_' . $post_type . 's_columns', array( $this, 'add_posts_management_column' ) );
- if ( isset( $_GET[ 'post_type' ] ) && $_GET[ 'post_type' ] == 'page' ) {
- add_action( 'manage_' . $post_type . 's_custom_column', array( $this, 'add_content_for_posts_management_column' ) );
- }
- add_action( 'manage_posts_custom_column', array( $this, 'add_content_for_posts_management_column' ) );
- break;
- default:
- if ( in_array( $post_type, array_keys( $this->get_translatable_documents() ) ) ) {
- add_filter( 'manage_' . $post_type . '_posts_columns', array( $this, 'add_posts_management_column' ) );
- if ( $wp_post_types[ $post_type ]->hierarchical ) {
- add_action( 'manage_pages_custom_column', array( $this, 'add_content_for_posts_management_column' ) );
- add_action( 'manage_posts_custom_column', array( $this, 'add_content_for_posts_management_column' ) ); // add this too - for more types plugin
- } else {
- add_action( 'manage_posts_custom_column', array( $this, 'add_content_for_posts_management_column' ) );
- }
- }
- }
- add_action( 'admin_print_scripts', array( $this, '__set_posts_management_column_width' ) );
- }
- }
- function _taxonomy_languages_menu()
- {
- // tags language selection
- global $pagenow;
- if ( $pagenow == 'edit-tags.php' ) {
- // handle case of the tax edit page (after a taxonomy has been added)
- // needs to redirect back to
- if ( isset( $_GET[ 'trid' ] ) && isset( $_GET[ 'source_lang' ] ) ) {
- $translations = $this->get_element_translations( $_GET[ 'trid' ], 'tax_' . $_GET[ 'taxonomy' ] );
- if ( isset( $translations[ $_GET[ 'lang' ] ] ) ) {
- wp_redirect( get_edit_term_link( $translations[ $_GET[ 'lang' ] ]->term_id, $_GET[ 'taxonomy' ] ) );
- exit;
- } else {
- add_action( 'admin_notices', array( $this, '_tax_adding' ) );
- }
- }
- $taxonomy = isset( $_GET[ 'taxonomy' ] ) ? esc_sql( $_GET[ 'taxonomy' ] ) : 'post_tag';
- if ( $this->is_translated_taxonomy( $taxonomy ) ) {
- add_action( 'admin_print_scripts-edit-tags.php', array( $this, 'js_scripts_tags' ) );
- if ( $taxonomy == 'category' ) {
- add_action( 'edit_category_form', array( $this, 'edit_term_form' ) );
- } else {
- add_action( 'add_tag_form', array( $this, 'edit_term_form' ) );
- add_action( 'edit_tag_form', array( $this, 'edit_term_form' ) );
- }
- add_action( 'admin_footer', array( $this, 'terms_language_filter' ) );
- add_filter( 'wp_dropdown_cats', array( $this, 'wp_dropdown_cats_select_parent' ) );
- }
- }
- }
- function _tax_adding()
- {
- $translations = $this->get_element_translations( $_GET[ 'trid' ], 'tax_' . $_GET[ 'taxonomy' ] );
- if ( !empty( $translations ) && isset( $translations[ $_GET[ 'source_lang' ] ]->name ) ) {
- $tax_name = apply_filters( 'the_category', $translations[ $_GET[ 'source_lang' ] ]->name );
- echo '<div id="icl_tax_adding_notice" class="updated fade"><p>' . sprintf( __( 'Adding translation for: %s.', 'sitepress' ), $tax_name ) . '</p></div>';
- }
- }
- /**
- * @param WP_Query $query
- */
- function loop_start($query) {
- if($query->post_count) {
- $this->cache_translations($query->posts);
- }
- }
- /**
- * Cache translated posts
- *
- * @param $posts
- */
- function cache_translations($posts) {
- global $wpdb, $wp_query, $sitepress;
- static $last_query=false;
- if ( isset( $sitepress ) && isset( $wp_query ) && $wp_query->is_main_query() ) {
- if($last_query == $wp_query->query_vars_hash) return;
- $sticky_posts_ids = get_option( 'sticky_posts' );
- if ( $sticky_posts_ids ) {
- if ( count( $sticky_posts_ids ) == 1 ) {
- $sticky_posts_prepared = $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE ID = %d", array( $sticky_posts_ids[0] ) );
- } else {
- $sticky_posts_prepared = "SELECT * FROM {$wpdb->posts} WHERE ID IN (" . implode( ',', array_filter( $sticky_posts_ids ) ) . ")";
- }
- $sticky_posts = $wpdb->get_results( $sticky_posts_prepared );
- $posts_objects = array_map( 'get_post', $sticky_posts );
- if ( !$posts ) {
- $posts = $posts_objects;
- } else {
- $posts = array_merge( $posts, $posts_objects );
- //Remove duplicates
- $posts = array_map( "unserialize", array_unique( array_map( "serialize", $posts ) ) );
- }
- }
- if ( $posts ) {
- $terms = array();
- //Query specific cache
- $cache_key = $wp_query->query_vars_hash;
- $cache_group = 'wp_query:posts_translations';
- $cached_posts_translations = wp_cache_get( $cache_key, $cache_group );
- if ( !$cached_posts_translations ) {
- $post_types = array();
- foreach ( $posts as $post ) {
- $post_types[ $post->post_type ][] = $post->ID;
- }
- $trids = array();
- if ( $post_types ) {
- $trid_cache_group = 'element_trid';
- foreach ( $post_types as $post_type => $posts_ids ) {
- $element_type = 'post_' . $post_type;
- $s_post_type_ids = join( ',', array_filter($posts_ids) );
- $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 ) );
- $post_type_trids_data = $wpdb->get_results( $trids_prepared );
- foreach($post_type_trids_data as $post_type_trid_data) {
- $element_id = $post_type_trid_data->element_id;
- $trid_cache_key = $element_id . ':post_' . $post_type;
- $trid = wp_cache_get($trid_cache_key, $trid_cache_group);
- if(!$trid) {
- $trid = $post_type_trid_data->trid;
- $trids[] = $trid;
- wp_cache_add($trid_cache_key, $trid, $trid_cache_group);
- }
- if($trid) {
- $element_language_details_cache_group = 'element_language_details';
- $element_language_details = wp_cache_get($trid_cache_key, $element_language_details_cache_group);
- if(!$element_language_details) {
- $details = new stdClass();
- $details->trid = $trid;
- $details->language_code = $post_type_trid_data->language_code;
- $details->source_language_code = $post_type_trid_data->source_language_code;
- wp_cache_add($trid_cache_key, $details, $element_language_details_cache_group);
- }
- }
- //Deal with taxonomies
- //$_taxonomies = get_post_taxonomies($element_id);
- $_taxonomies= get_post_taxonomies($element_id);
- foreach($_taxonomies as $_taxonomy) {
- if($sitepress->is_translated_taxonomy($_taxonomy)) {
- $_terms = wp_get_post_terms($element_id, $_taxonomy);
- foreach($_terms as $_term) {
- $terms[$_term->taxonomy][] = $_term->term_id;
- }
- }
- }
- }
- }
- }
- if ( $trids ) {
- if(count($trids)==1) {
- $posts_translations_prepared = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}icl_translations WHERE trid = %d ", array($trids[0]) );
- }else {
- $posts_translations_prepared = "SELECT * FROM {$wpdb->prefix}icl_translations WHERE trid IN (" . join( ',', array_filter($trids) ) . ") ";
- }
- $posts_translations = $wpdb->get_results( $posts_translations_prepared );
- $post_ids = array();
- foreach($posts_translations as $posts_translation) {
- $post_ids[] = $posts_translation->element_id;
- }
- $posts_data = wp_cache_get($cache_key, 'wp_query:posts');
- if(!$posts_data && $post_ids) {
- $posts_prepared = "SELECT * FROM {$wpdb->posts} WHERE ID IN (" . join( ',', array_filter($post_ids) ) . ") ";
- $posts_data = $wpdb->get_results( $posts_prepared );
- wp_cache_set($cache_key, $posts_data, 'wp_query:posts');
- }
- if ( $posts_data ) {
- foreach($posts_data as $post) {
- $_post = wp_cache_get( $post->ID, 'posts' );
- if ( ! $_post ) {
- $_post = $post;
- $_post = sanitize_post( $_post, 'raw' );
- wp_cache_add( $_post->ID, $_post, 'posts' );
- }
- }
- }
- }
- if ( $terms ) {
- $cache_group = 'element_language_details';
- foreach ( $terms as $taxonomy => $term_ids ) {
- $element_type = 'tax_' . $taxonomy;
- $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 ) );
- $terms_translations = $wpdb->get_results( $terms_translations_prepared );
- foreach ( $terms_translations as $terms_translation ) {
- $cache_key = $terms_translation->element_id . ':' . $terms_translation->element_type;
- $cached_details = wp_cache_get( $cache_key, $cache_group );
- if ( !$cached_details ) {
- wp_cache_set( $cache_key, $terms_translation, $cache_group );
- }
- $icl_object_id_cache_group = 'icl_object_id';
- $icl_object_id_cache_key_args = array( $terms_translation->element_id, $taxonomy, false, $terms_translation->language_code );
- $icl_object_id_cache_key = implode( ':', array_filter( $icl_object_id_cache_key_args ) );
- $icl_object_id = wp_cache_get( $cache_key, $cache_group );
- if ( !$icl_object_id ) {
- wp_cache_set( $icl_object_id_cache_key, $terms_translation->element_id, $icl_object_id_cache_group );
- }
- $icl_object_id_cache_key_args = array( $terms_translation->element_id, $taxonomy, true, $terms_translation->language_code );
- $icl_object_id_cache_key = implode( ':', array_filter( $icl_object_id_cache_key_args ) );
- $icl_object_id = wp_cache_get( $cache_key, $cache_group );
- if ( !$icl_object_id ) {
- wp_cache_set( $icl_object_id_cache_key, $terms_translation->element_id, $icl_object_id_cache_group );
- }
- }
- }
- }
- }
- }
- $last_query = $wp_query->query_vars_hash;
- }
- }
- function the_posts( $posts ) {
- if ( !is_admin() && isset( $this->settings[ 'show_untranslated_blog_posts' ] ) && $this->settings[ 'show_untranslated_blog_posts' ] && $this->get_current_language() != $this->get_default_language() ) {
- // show untranslated posts
- global $wpdb, $wp_query;
- $default_language = $this->get_default_language();
- $current_language = $this->get_current_language();
- $debug_backtrace = $this->get_backtrace(4, true); //Limit to first 4 stack frames, since 3 is the highest index we use
- /** @var $custom_wp_query WP_Query */
- $custom_wp_query = isset( $debug_backtrace[ 3 ][ 'object' ] ) ? $debug_backtrace[ 3 ][ 'object' ] : false;
- //exceptions
- if ( ( $current_language == $default_language )
- // original language
- ||
- ( $wp_query != $custom_wp_query )
- // called by a custom query
- ||
- ( !$custom_wp_query->is_posts_page && !$custom_wp_query->is_home )
- // not the blog posts page
- ||
- $wp_query->is_singular
- //is singular
- ||
- !empty( $custom_wp_query->query_vars[ 'category__not_in' ] )
- //|| !empty($custom_wp_query->query_vars['category__in'])
- //|| !empty($custom_wp_query->query_vars['category__and'])
- ||
- !empty( $custom_wp_query->query_vars[ 'tag__not_in' ] ) ||
- !empty( $custom_wp_query->query_vars[ 'post__in' ] ) ||
- !empty( $custom_wp_query->query_vars[ 'post__not_in' ] ) ||
- !empty( $custom_wp_query->query_vars[ 'post_parent' ] )
- ) {
- return $posts;
- }
- // get the posts in the default language instead
- $this_lang = $this->this_lang;
- $this->this_lang = $default_language;
- remove_filter( 'the_posts', array( $this, 'the_posts' ) );
- $custom_wp_query->query_vars[ 'suppress_filters' ] = 0;
- if ( isset( $custom_wp_query->query_vars[ 'pagename' ] ) && !empty( $custom_wp_query->query_vars[ 'pagename' ] ) ) {
- if ( isset( $custom_wp_query->queried_object_id ) && !empty( $custom_wp_query->queried_object_id ) ) {
- $page_id = $custom_wp_query->queried_object_id;
- } else {
- // urlencode added for languages that have urlencoded post_name field value
- $custom_wp_query->query_vars[ 'pagename' ] = urlencode( $custom_wp_query->query_vars[ 'pagename' ] );
- $page_id = $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_name='{$custom_wp_query->query_vars['pagename']}' AND post_type='page'" );
- }
- if ( $page_id ) {
- $tr_page_id = icl_object_id( $page_id, 'page', false, $default_language );
- if ( $tr_page_id ) {
- $custom_wp_query->query_vars[ 'pagename' ] = $wpdb->get_var( "SELECT post_name FROM {$wpdb->posts} WHERE ID={$tr_page_id}" );
- }
- }
- }
- // look for posts without translations
- if ( $posts ) {
- $pids = false;
- foreach ( $posts as $p ) {
- $pids[ ] = $p->ID;
- }
- if ( $pids ) {
- $trids = $wpdb->get_col( "
- SELECT trid
- FROM {$wpdb->prefix}icl_translations
- WHERE element_type='post_post' AND element_id IN (" . join( ',', $pids ) . ") AND language_code = '" . $this_lang . "'" );
- if ( !empty( $trids ) ) {
- $posts_not_translated = $wpdb->get_col( "
- SELECT element_id, COUNT(language_code) AS c
- FROM {$wpdb->prefix}icl_translations
- WHERE trid IN (" . join( ',', $trids ) . ") GROUP BY trid HAVING c = 1
- " );
- if ( !empty( $posts_not_translated ) ) {
- $GLOBALS[ '__icl_the_posts_posts_not_translated' ] = $posts_not_translated;
- add_filter( 'posts_where', array( $this, '_posts_untranslated_extra_posts_where' ), 99 );
- }
- }
- }
- }
- //fix page for posts
- unset( $custom_wp_query->query_vars[ 'pagename' ] );
- unset( $custom_wp_query->query_vars[ 'page_id' ] );
- unset( $custom_wp_query->query_vars[ 'p' ] );
- $my_query = new WP_Query( $custom_wp_query->query_vars );
- add_filter( 'the_posts', array( $this, 'the_posts' ) );
- $this->this_lang = $this_lang;
- // create a map of the translated posts
- foreach ( $posts as $post ) {
- $trans_posts[ $post->ID ] = $post;
- }
- // loop original posts
- foreach ( $my_query->posts as $k => $post ) { // loop posts in the default language
- $trid = $this->get_element_trid( $post->ID );
- $translations = $this->get_element_translations( $trid ); // get translations
- if ( isset( $translations[ $current_language ] ) ) { // if there is a translation in the current language
- if ( isset( $trans_posts[ $translations[ $current_language ]->element_id ] ) ) { //check the map of translated posts
- $my_query->posts[ $k ] = $trans_posts[ $translations[ $current_language ]->element_id ];
- } else { // check if the translated post exists in the database still
- $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d AND post_status='publish' LIMIT 1", $translations[ $current_language ]->element_id ) );
- if ( !empty( $_post ) ) {
- $_post = sanitize_post( $_post );
- $my_query->posts[ $k ] = $_post;
- } else {
- $my_query->posts[ $k ]->original_language = true;
- }
- }
- } else {
- $my_query->posts[ $k ]->original_language = true;
- }
- }
- if ( $custom_wp_query == $wp_query ) {
- $wp_query->max_num_pages = $my_query->max_num_pages;
- }
- $posts = $my_query->posts;
- unset( $GLOBALS[ '__icl_the_posts_posts_not_translated' ] );
- remove_filter( 'posts_where', array( $this, '_posts_untranslated_extra_posts_where' ), 99 );
- }
- // cache translated posts
- $this->cache_translations($posts);
- return $posts;
- }
- function get_pages($pages, $r) {
- $this->cache_translations($pages);
- return $pages;
- }
- function _posts_untranslated_extra_posts_where( $where )
- {
- global $wpdb;
- $where .= ' OR ' . $wpdb->posts . '.ID IN (' . join( ',', $GLOBALS[ '__icl_the_posts_posts_not_translated' ] ) . ')';
- return $where;
- }
- function initialize_cache()
- {
- require_once ICL_PLUGIN_PATH . '/inc/cache.php';
- $this->icl_translations_cache = new icl_cache();
- $this->icl_locale_cache = new icl_cache( 'locale', true );
- $this->icl_flag_cache = new icl_cache( 'flags', true );
- $this->icl_language_name_cache = new icl_cache( 'language_name', true );
- $this->icl_term_taxonomy_cache = new icl_cache();
- }
- function set_admin_language()
- {
- global $wpdb;
- $default_language = $this->get_default_language();
- $found = false;
- $cache_key = "active_languages";
- $cache_group = "set_admin_lang";
- $active_languages = wp_cache_get($cach…
Large files files are truncated, but you can click here to view the full file