/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php
PHP | 11232 lines | 8826 code | 1653 blank | 753 comment | 2565 complexity | 9da0637d29f96643dfb3e6fb5771ee05 MD5 | raw file
Possible License(s): AGPL-1.0, MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0
Large files files are truncated, but you can click here to view the full file
- <?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 $footer_preview = false;
- 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()
- {
- do_action('wpml_before_startup');
- 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' ] && ( $this->settings[ 'setup_complete' ] || ( !empty($_GET[ 'page' ]) && $this->settings['setup_wizard_step']==3 && $_GET[ 'page' ] == ICL_PLUGIN_FOLDER . '/menu/languages.php' ) ) ) {
- // 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' ), 100, 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
- // 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 );
- // Filter links to the root page
- add_filter( 'page_link', array( 'WPML_Root_Page', 'filter_root_permalink' ), 10, 2 );
- add_filter( 'the_preview', array( 'WPML_Root_Page', 'front_page_id_filter' ), 10, 2 );
- if ( $pagenow == 'edit.php' ) {
- add_action( 'admin_footer', array( $this, 'language_filter' ) );
- add_action( 'quick_edit_custom_box', array( 'WPML_Terms_Translations', 'quick_edit_terms_removal' ), 10, 2 );
- }
- 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' ) );
- // filter the saving of terms so that the taxonomy_ids of translated terms are correctly adjusted across taxonomies
- add_action('created_term_translation', array( 'WPML_Terms_Translations', 'sync_ttid_action' ), 10, 3 );
- // filters terms by language for the term/tag-box autoselect
- if ( ( isset( $_GET[ 'action' ] ) && ( $_GET[ 'action' ] == 'ajax-tag-search' )
- || isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] == 'get-tagcloud' ) ) {
- add_filter( 'get_terms', array( 'WPML_Terms_Translations', 'get_terms_filter' ), 10, 2 );
- }
- $this->set_term_filters_and_hooks();
- // The delete filter only ensures the synchronizing of delete actions beween translations of a term.
- add_action( 'delete_term', array( 'WPML_Terms_Translations', 'delete_term_filter' ), 1, 3 );
- add_filter('pre_term_slug', array( 'WPML_Terms_Translations', 'pre_term_slug_filter' ), 10, 2 );
- add_action( 'set_object_terms', array( 'WPML_Terms_Translations', 'set_object_terms_action' ), 10, 6 );
- // AJAX Actions for the post edit screen
- add_action( 'wp_ajax_wpml_get_post_parent_id_by_lang', array( 'WPML_Post_Edit_Ajax', 'wpml_get_post_parent_id_by_lang' ) );
- add_action( 'wp_ajax_wpml_get_taxonomy_terms_json', array( 'WPML_Post_Edit_Ajax', 'wpml_get_taxonomy_terms_json' ) );
- add_action( 'wp_ajax_wpml_save_term', array( 'WPML_Post_Edit_Ajax', 'wpml_save_term' ) );
- add_action( 'wp_ajax_wpml_remove_terms_from_post', array( 'WPML_Post_Edit_Ajax', 'wpml_remove_terms_from_post' ) );
- add_action( 'wp_ajax_wpml_switch_post_language', array( 'WPML_Post_Edit_Ajax', 'wpml_switch_post_language' ) );
- add_action( 'wp_ajax_wpml_before_switch_post_language', array( 'WPML_Post_Edit_Ajax', 'wpml_before_switch_post_language' ) );
- add_action( 'wp_ajax_wpml_set_post_edit_lang', array( 'WPML_Post_Edit_Ajax', 'wpml_set_post_edit_lang' ) );
- add_action( 'wp_ajax_wpml_get_translated_taxonomies', array( 'WPML_Post_Edit_Ajax', 'wpml_get_translated_taxonomies' ) );
- add_action( 'wp_ajax_wpml_get_translations_table_data', array( 'WPML_Post_Edit_Ajax', 'wpml_get_translations_table_data' ) );
- add_action( 'wp_ajax_wpml_get_post_permalink', array( 'WPML_Post_Edit_Ajax', 'wpml_get_post_permalink' ) );
- add_action( 'wp_ajax_wpml_get_default_lang', array( 'WPML_Post_Edit_Ajax', 'wpml_get_default_lang' ) );
- //AJAX Actions for the taxonomy translation screen
- add_action( 'wp_ajax_wpml_get_table_taxonomies', array( 'WPML_Taxonomy_Translation_Table_Display', 'wpml_get_table_taxonomies' ) );
- add_action( 'wp_ajax_wpml_get_terms_and_labels_for_taxonomy_table', array( 'WPML_Taxonomy_Translation_Table_Display', 'wpml_get_terms_and_labels_for_taxonomy_table' ) );
- add_action( 'wp_ajax_wpml_tt_save_labels_translation', array( 'WPML_Taxonomy_Translation', 'save_labels_translation' ) );
- // Ajax Action for the updating of term names on the troubleshooting page
- add_action( 'wp_ajax_wpml_update_term_names_troubleshoot', array( 'WPML_Troubleshooting_Terms_Menu', 'wpml_update_term_names_troubleshoot' ) );
- // 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_action( 'after-category-table', array( 'WPML_Terms_Translations', 'category_display_action' ), 1, 1 );
- // 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
- 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_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' ) );
- }
- /**
- * add extra debug information
- */
- add_filter( 'icl_get_extra_debug_info', array( $this, 'add_extra_debug_info' ) );
- } //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'));
-
- //cron job to update WPML config index file from CDN
- add_action('update_wpml_config_index', array($this,'update_wpml_config_index_event'));
- //update WPML config files
- add_action('wp_ajax_update_wpml_config_index', array($this,'update_wpml_config_index_event_ajax'));
- add_action( 'after_switch_theme', array( $this, 'update_wpml_config_index_event' ) );
- add_action( 'activated_plugin', array( $this, 'update_wpml_config_index_event' ) );
- add_action('core_upgrade_preamble', array($this, 'update_index_screen'));
-
- add_shortcode('wpml_language_selector_widget', 'icl_language_selector');
- add_shortcode('wpml_language_selector_footer', 'icl_language_selector_footer');
-
- add_filter('get_search_form', array($this, 'get_search_form_filter'));
- add_action('plugins_loaded', array($this, 'load_dependencies'), 10000);
- do_action('wpml_after_startup');
- }
- 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()
- {
- do_action('wpml_before_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']);
- }
-
- $current_user = wp_get_current_user();
-
- 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 {
- if ($this->check_if_admin_action_from_referer()){
- $this->this_lang = $this->get_admin_language_cookie();
- } elseif ( isset( $_SERVER[ 'HTTP_REFERER' ] ) ) {
- $this->this_lang = $this->get_language_from_url( $_SERVER[ 'HTTP_REFERER' ] );
- } elseif ( $this->get_language_cookie() ) {
- $this->this_lang = $this->get_language_cookie();
- } else {
- $this->this_lang = $this->get_default_language();
- }
- }
- } 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);
- } elseif(isset($current_user->data)) {
- $lang = $this->get_user_admin_language( $current_user->data->ID );
- $this->this_lang = $lang ? $lang : $default_language;
- }
- $update_admin_language_cookie = false;
- $update_admin_language_cookie |= ( ( is_admin() && ! wpml_is_ajax() ) || ( wpml_is_ajax() && $this->check_if_admin_action_from_referer() ) ) && isset( $_GET[ 'lang' ] ) && ( $this->get_current_language() != $this->get_admin_language_cookie() );
- $update_admin_language_cookie |= isset( $_GET[ 'admin_bar' ] ) && ( $_GET[ 'admin_bar' ] == 1 ) && !isset( $_GET[ 'page' ] );
- $update_admin_language_cookie |= !defined( 'WPML_ST_FOLDER' ) || ( isset($_GET[ 'page' ]) && $_GET[ 'page' ] != WPML_ST_FOLDER . '/menu/string-translation.php' );
- $update_admin_language_cookie |= !$this->get_admin_language_cookie();
- if ( $update_admin_language_cookie ) {
- $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 we access the root url via a non root slug/parameter combination,
- * we redirect accordingly.
- */
- if ( $this->settings[ 'language_negotiation_type' ] == 1 && $this->settings[ 'urls' ][ 'directory_for_default_language' ] && $this->settings[ 'urls' ][ 'show_on_root' ] == 'page' ) {
- $filtered_root_url = WPML_Root_Page::filter_root_permalink( wpml_strip_subdir_from_url( site_url() ) . $_SERVER[ 'REQUEST_URI' ] );
- if ( $filtered_root_url != wpml_strip_subdir_from_url( site_url() ) . $_SERVER[ 'REQUEST_URI' ] ) {
- wp_redirect( $filtered_root_url, 302 );
- exit();
- }
- }
- if ( WPML_Root_Page::is_current_request_root() ) {
- 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' ];
- }
- /** @noinspection PhpIncludeInspection */
- include $html_file;
- exit;
- } else {
- 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 );
- }
- }
- WPML_Troubleshooting_Terms_Menu::display_terms_with_suffix_admin_notice();
- // 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;
- // set language to default and remove 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'
- || $_GET[ 'page' ] == ICL_PLUGIN_FOLDER . '/menu/menus-sync.php'
- || $_GET[ 'page' ] == ICL_PLUGIN_FOLDER . '/menu/taxonomy-translation-display.class.php' )
- || ( $pagenow == 'admin-ajax.php'
- && isset( $_POST[ 'action' ] )
- && $_POST[ 'action' ] == 'wpml_tt_save_labels_translation' ) )
- ) {
- $default_language = $this->get_admin_language();
- $this->switch_lang( $default_language, true );
- add_action( "admin_init", array( "WPML_Taxonomy_Translation_Table_Display", "enqueue_taxonomy_table_js" ) );
- add_action( 'init', array( $this, 'remove_admin_language_switcher' ) );
- }
- /* Posts and new inline created terms, can only be saved in an active language.
- * Also the content of the post-new.php should always be filtered for one specific
- * active language, so to display the correct taxonomy terms for selection.
- */
- if ( $pagenow == 'post-new.php' ) {
- if ( ! $this->is_active_language( $this->get_current_language() ) ) {
- $default_language = $this->get_admin_language();
- $this->switch_lang( $default_language, true );
- }
- }
- //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 );
- }
- do_action('wpml_after_init');
- }
- function load_dependencies() {
- do_action('wpml_load_dependencies');
- }
- /**
- * Sets up all term/taxonomy actions for use outside Translations Management or the Post Edit screen
- */
- function set_term_filters_and_hooks(){
- add_filter( 'terms_clauses', array( $this, 'terms_clauses' ), 10, 4 );
- add_filter( 'list_terms_exclusions', array( $this, 'exclude_other_terms' ), 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_filter( 'get_terms_args', array( $this, 'get_terms_args_filter' ) );
- add_filter( 'get_edit_term_link', array( $this, 'get_edit_term_link' ), 1, 4 );
- add_action('wp_ajax_icl_repair_broken_type_and_language_assignments', 'icl_repair_broken_type_and_language_assignments');
- //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( 'taxonomy_template', array( $this, 'slug_template' ) );
- add_filter( 'category_template', array( $this, 'slug_template' ) );
- //post/page delete taxonomy
- add_action( 'deleted_term_relationships', array( $this, 'deleted_term_relationships' ), 10, 2 );
- // 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 );
- }
- }
- }
- function icl_redirect_canonical_wrapper()
- {
- global $_icl_server_request_uri;
- $requested_url = ( !empty( $_SERVER[ 'HTTPS' ] ) && mb_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 check_if_admin_action_from_referer() {
- $result = false;
- if ( isset( $_SERVER[ 'HTTP_REFERER' ] ) ) {
- $http_referer = parse_url( $_SERVER[ 'HTTP_REFERER' ] );
- $referer_path = $http_referer[ 'path' ];
- $referer_parts = explode( '/', $referer_path );
- if ( in_array( 'wp-admin', $referer_parts ) ) {
- $result = true;
- }
- }
- return $result;
- }
- 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 ( isset( $query->post_count ) && $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 ( defined( 'WPML_DISABLE_CACHE_TRANSLATIONS' ) && is_admin() ){
- return;
- }
- 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_…
Large files files are truncated, but you can click here to view the full file