wpml_helper = new WPML_Helper( $wpdb );
parent::__construct( $wpdb );
$this->locale_utils = new WPML_Locale( $wpdb, $this, $locale );
$sitepress_settings = get_option('icl_sitepress_settings');
$this->settings = &$sitepress_settings;
$this->always_translatable_post_types = array( 'post', 'page' );
$this->always_translatable_taxonomies = array( 'category', 'post_tag' );
$this->post_translation = &$wpml_post_translations;
$this->term_translation = &$wpml_term_translations;
//TODO: [WPML 3.5] To remove in WPML 3.5
//@since 3.1
if(is_admin() && !$this->get_setting('icl_capabilities_verified')) {
icl_enable_capabilities();
$sitepress_settings = get_option('icl_sitepress_settings');
}
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[ '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' ) );
}
$this->initialize_cache( );
$this->flags = new WPML_Flags( $wpdb );
add_action( 'plugins_loaded', array( $this, 'init' ), 1 );
add_action( 'wp_loaded', array( $this, 'maybe_set_this_lang' ) );
add_action( 'switch_blog', array( $this, 'init_settings' ), 10, 1 );
// 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' ) );
if ( $this->get_setting('existing_content_language_verified') && ( $this->get_setting('setup_complete') || ( !empty($_GET[ 'page' ]) && $this->get_setting('setup_wizard_step')==3 && $_GET[ 'page' ] == ICL_PLUGIN_FOLDER . '/menu/languages.php' ) ) ) {
// Post/page language box
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_filter( 'the_posts', array( $this, 'the_posts' ), 10 );
if ( $pagenow == 'edit.php' ) {
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' ) );
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'] ) && 'ajax-tag-search' === $_GET['action'] ) || ( isset( $_POST['action'] ) && 'get-tagcloud' === $_POST['action'] ) ) {
add_filter( 'get_terms', array( 'WPML_Terms_Translations', 'get_terms_filter' ), 10, 2 );
}
$this->set_term_filters_and_hooks();
add_action( 'parse_query', array( $this, 'parse_query' ) );
// The delete filter only ensures the synchronizing of delete actions between translations of a term.
add_action( 'delete_term', array( $this, 'delete_term' ), 1, 3 );
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_save_term', array( 'WPML_Post_Edit_Ajax', 'wpml_save_term' ) );
add_action( 'wp_ajax_wpml_switch_post_language', array( 'WPML_Post_Edit_Ajax', 'wpml_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_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' ) );
// 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 );
// custom hook for adding the language selector to the template
//@deprecated see 'wpml_add_language_selector' action in $this->api_hooks();)
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' ) );
// 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' ) );
// language negotiation
add_action( 'query_vars', array( $this, 'query_vars' ) );
add_filter( 'language_attributes', array( $this, 'language_attributes' ) );
add_filter( 'locale', array( $this, 'locale' ), 10, 1 );
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( 'pre_option_sticky_posts', array( $this, 'option_sticky_posts' ), 10, 2 );
add_action( 'wp_head', array( $this, 'set_wp_query' ) );
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( $this, $wpdb, $wpml_post_translations, $wpml_term_translations );
if ( is_admin() || defined( 'XMLRPC_REQUEST' ) || preg_match( '#wp-comments-post\.php$#', $_SERVER[ 'REQUEST_URI' ] ) ) {
global $iclTranslationManagement;
$iclTranslationManagement = wpml_load_core_tm ();
}
add_action( 'wp_login', array( $this, 'reset_admin_language_cookie' ) );
$this->handle_head_hreflang();
/**
* 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' ) );
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('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'));
$this->api_hooks();
add_action('wpml_loaded', array($this, 'load_dependencies'), 10000);
do_action('wpml_after_startup');
}
/**
* @since 3.2
*/
public function api_hooks() {
//TODO: [WPML 3.5] to deprecate in favour of lowercased namespaces
add_filter( 'WPML_get_setting', array( $this, 'filter_get_setting' ), 10, 2 );
add_filter( 'WPML_get_current_language', array( $this, 'get_current_language' ), 10, 0 );
add_filter( 'WPML_get_user_admin_language', array( $this, 'get_user_admin_language_filter' ), 10, 2 );
add_filter( 'WPML_is_admin_action_from_referer', array( $this, 'check_if_admin_action_from_referer' ), 10, 0 );
add_filter( 'WPML_current_user', array( $this, 'get_current_user' ), 10, 0 );
add_filter( 'wpml_get_setting', array( $this, 'filter_get_setting' ), 10, 2 );
add_action( 'wpml_set_setting', array( $this, 'action_set_setting' ), 10, 3 );
add_filter( 'wpml_get_language_cookie', array( $this, 'get_language_cookie' ), 10, 0 );
add_filter( 'wpml_current_language', array( $this, 'get_current_language' ), 10, 0 );
add_filter( 'wpml_get_user_admin_language', array( $this, 'get_user_admin_language_filter' ), 10, 2 );
add_filter( 'wpml_is_admin_action_from_referer', array( $this, 'check_if_admin_action_from_referer' ), 10, 0 );
add_filter( 'wpml_current_user', array( $this, 'get_current_user' ), 10, 0 );
/**
* @use \SitePress::get_translatable_documents_filter
*/
add_filter( 'wpml_translatable_documents', array( $this, 'get_translatable_documents_filter' ), 10, 2 );
add_filter( 'wpml_is_translated_post_type', array( $this, 'is_translated_post_type_filter' ), 10, 2 );
/**
* @deprecated it has a wrong hook tag
* @since 3.2
*/
add_filter( 'wpml_get_element_translations_filter', array( $this, 'get_element_translations_filter' ), 10, 6 );
add_filter( 'wpml_get_element_translations', array( $this, 'get_element_translations_filter' ), 10, 6 );
add_filter( 'wpml_is_original_content', array( $this, 'is_original_content_filter'), 10, 3 );
add_filter( 'wpml_original_element_id', array( $this, 'get_original_element_id_filter'), 10, 3 );
add_filter( 'wpml_is_rtl', array( $this, 'is_rtl' ) );
add_filter( 'wpml_home_url', 'wpml_get_home_url_filter', 10 );
add_filter( 'wpml_active_languages', 'wpml_get_active_languages_filter', 10, 2 );
add_filter( 'wpml_display_language_names', 'wpml_display_language_names_filter', 10, 5 );
add_filter( 'wpml_display_single_language_name', array($this, 'get_display_single_language_name_filter'), 10, 2 );
add_filter( 'wpml_element_link', 'wpml_link_to_element_filter', 10, 7 );
add_filter( 'wpml_object_id', 'wpml_object_id_filter', 10, 4 );
add_filter( 'wpml_translated_language_name', 'wpml_translated_language_name_filter', 10, 3 );
add_filter( 'wpml_default_language', 'wpml_get_default_language_filter', 10, 1);
add_filter( 'wpml_post_language_details', 'wpml_get_language_information', 10, 2 );
add_action( 'wpml_add_language_selector', 'wpml_add_language_selector_action' );
add_action( 'wpml_footer_language_selector', 'wpml_footer_language_selector_action' );
add_action( 'wpml_add_language_form_field', 'wpml_add_language_form_field_action' );
add_shortcode( 'wpml_language_form_field', 'wpml_language_form_field_shortcode' );
add_filter( 'wpml_element_translation_type', 'wpml_get_element_translation_type_filter', 10, 3 );
add_filter( 'wpml_element_has_translations', 'wpml_element_has_translations_filter', 10, 3 );
add_filter( 'wpml_content_translations', 'wpml_get_content_translations_filter', 10, 3 );
add_filter( 'wpml_master_post_from_duplicate', 'wpml_get_master_post_from_duplicate_filter' );
add_filter( 'wpml_post_duplicates', 'wpml_get_post_duplicates_filter' );
add_filter( 'wpml_element_type', 'wpml_element_type_filter' );
add_filter( 'wpml_setting', 'wpml_get_setting_filter', 10, 3 );
add_filter( 'wpml_sub_setting', 'wpml_get_sub_setting_filter', 10, 4 );
add_filter( 'wpml_language_is_active', 'wpml_language_is_active_filter', 10, 2 );
add_action( 'wpml_admin_make_post_duplicates', 'wpml_admin_make_post_duplicates_action', 10, 1 );
add_action( 'wpml_make_post_duplicates', 'wpml_make_post_duplicates_action', 10, 1 );
add_filter( 'wpml_element_language_details', 'wpml_element_language_details_filter', 10, 2 );
add_action( 'wpml_set_element_language_details', array($this, 'set_element_language_details_action'), 10, 1 );
add_filter( 'wpml_element_language_code', 'wpml_element_language_code_filter', 10, 2 );
add_filter( 'wpml_elements_without_translations', 'wpml_elements_without_translations_filter', 10, 2 );
add_filter( 'wpml_permalink', 'wpml_permalink_filter', 10, 2 );
add_action( 'wpml_switch_language', 'wpml_switch_language_action', 10, 1 );
}
function init() {
do_action('wpml_before_init');
$this->locale_utils->init();
$this->maybe_set_this_lang();
if ( function_exists( 'w3tc_add_action' ) ) {
w3tc_add_action( 'w3tc_object_cache_key', 'w3tc_translate_cache_key_filter' );
}
$this->get_user_preferences();
$this->set_admin_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;
}
//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'] ) ) {
add_action( 'wpml_verify_post_translations', array(
$this,
'verify_post_translations_action'
), 10, 1 );
if ($this->settings[ 'language_negotiation_type' ] == 2) {
add_filter( 'allowed_redirect_hosts', array( $this, 'allowed_redirect_hosts' ) );
}
//reorder active language to put 'this_lang' in front
$active_languages = $this->get_active_languages();
foreach ( $active_languages as $k => $active_lang ) {
if ( $k === $this->this_lang ) {
unset( $this->active_languages[ $k ] );
$this->active_languages = array_merge( array( $k => $active_lang ), $this->active_languages );
}
}
add_filter( 'mod_rewrite_rules', array( $this, 'rewrite_rules_filter' ), 10 ,1 );
if ( is_admin() &&
$this->get_setting( 'setup_complete' ) &&
( !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' )
) {
// Admin language switcher goes to the WP admin bar
if ( apply_filters( 'wpml_show_admin_language_switcher', true ) ) {
add_action( 'wp_before_admin_bar_render', array( $this, 'admin_language_switcher' ) );
} else {
$this->this_lang = 'all';
}
}
if ( !is_admin() && defined( 'DISQUS_VERSION' ) ) {
include ICL_PLUGIN_PATH . '/modules/disqus.php';
}
}
if ( $this->is_rtl() ) {
$GLOBALS[ 'text_direction' ] = 'rtl';
}
if ( !wpml_is_ajax() && is_admin() && empty( $this->settings[ 'dont_show_help_admin_notice' ] ) ) {
WPML_Troubleshooting_Terms_Menu::display_terms_with_suffix_admin_notice();
if ( !$this->get_setting( 'setup_wizard_step' )
&& strpos( filter_input( INPUT_GET, 'page', FILTER_SANITIZE_URL ), 'menu/languages.php' ) === false
) {
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' ) );
}
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/menu-sync/menus-sync.php'
|| $_GET[ 'page' ] == ICL_PLUGIN_FOLDER . '/menu/term-taxonomy-menus/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( '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 );
}
if ( is_admin() ) {
$this->post_edit_metabox = new WPML_Meta_Boxes_Post_Edit_HTML( $this, $this->post_translation );
}
do_action('wpml_after_init');
do_action('wpml_loaded');
}
/**
* Sets the current language in \SitePress::$this_lang, redirects if
* frontend requests point to incomplete or incorrect urls, un-sets the
* $_GET['lang'] and $_GET['admin_bar'] values so that upload.php is able to
* enqueue 'media-grid' correctly without url parameters breaking its
* functionality.
*/
public function maybe_set_this_lang() {
global $wpml_request_handler, $pagenow, $wpml_language_resolution;
if ( ! defined( 'WP_ADMIN' ) && isset( $_SERVER['HTTP_HOST'] ) && did_action( 'init' ) ) {
require_once ICL_PLUGIN_PATH . '/inc/request-handling/redirection/wpml-frontend-redirection.php';
/** @var WPML_Frontend_Request $wpml_request_handler */
$redirect_helper = _wpml_get_redirect_helper();
$redirection = new WPML_Frontend_Redirection( $this,
$wpml_request_handler, $redirect_helper,
$wpml_language_resolution );
$this->this_lang = $redirection->maybe_redirect();
} else {
$this->this_lang = $wpml_request_handler->get_requested_lang();
}
$wpml_request_handler->set_language_cookie( $this->this_lang );
if ( $pagenow === 'upload.php' ) {
$_GET['lang'] = null;
$_GET['admin_bar'] = null;
}
}
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_action( 'create_term', array( $this, 'create_term' ), 1, 3 );
add_action( 'edit_term', array( $this, 'create_term' ), 1, 3 );
add_filter( 'get_terms_args', array( $this, 'get_terms_args_filter' ), 10, 2 );
add_filter( 'get_edit_term_link', array( $this, 'get_edit_term_link' ), 1, 4 );
add_action( 'deleted_term_relationships', array( $this, 'deleted_term_relationships' ), 10, 2 );
add_action('wp_ajax_icl_repair_broken_type_and_language_assignments', 'icl_repair_broken_type_and_language_assignments');
// adjust queried categories and tags ids according to the language
if ( (bool) $this->get_setting('auto_adjust_ids' ) ) {
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_pages', array( $this, 'get_pages_adjust_ids' ), 1, 2 );
}
}
}
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();
/** @var TranslationManagement $iclTranslationManagement */
global $iclTranslationManagement;
if ( $iclTranslationManagement ) {
$iclTranslationManagement->add_missing_language_information();
}
}
function setup()
{
$setup_complete = $this->get_setting('setup_complete');
if(!$setup_complete) {
$this->set_setting('setup_complete', false);
}
return $setup_complete;
}
public function user_lang_by_authcookie() {
global $current_user;
if ( !isset( $current_user ) ) {
$username = '';
if ( function_exists ( 'wp_parse_auth_cookie' ) ) {
$cookie_data = wp_parse_auth_cookie ();
$username = isset( $cookie_data[ 'username' ] ) ? $cookie_data[ 'username' ] : null;
}
$user_obj = new WP_User( null, $username );
} else {
$user_obj = $current_user;
}
$user_id = isset( $user_obj->ID ) ? $user_obj->ID : 0;
$user_lang = $this->get_user_admin_language ( $user_id );
$user_lang = $user_lang ? $user_lang : $this->get_current_language ();
return $user_lang;
}
function get_current_user() {
global $current_user;
return $current_user !== null ? $current_user : new WP_User();
}
function ajax_setup()
{
require ICL_PLUGIN_PATH . '/ajax.php';
}
function check_if_admin_action_from_referer() {
$referer = isset( $_SERVER[ 'HTTP_REFERER' ] ) ? $_SERVER[ 'HTTP_REFERER' ] : '';
return strpos ( $referer, strtolower ( admin_url () ) ) === 0;
}
function configure_custom_column() {
global $pagenow, $wp_post_types;
if ( $pagenow === 'edit.php'
|| $pagenow === 'edit-pages.php'
|| ( $pagenow === 'admin-ajax.php'
&& ( filter_input ( INPUT_POST, 'action' ) === 'inline-save'
|| filter_input ( INPUT_GET, 'action' ) === 'fetch-list'
) )
) {
$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' ) );
add_filter ( 'manage_' . $post_type . 's_custom_column', array( $this, 'add_content_for_posts_management_column' ) );
break;
default:
if ( in_array ( $post_type, array_keys ( $this->get_translatable_documents () ), true ) ) {
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' )
);
}
}
}
}
}
function the_posts( $posts ) {
global $wpml_post_translations;
$ids = array();
foreach ( $posts as $single_post ) {
$ids[ ] = $single_post->ID;
}
$wpml_post_translations->prefetch_ids ( $ids );
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 $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 = $this->wpdb->get_var(
$this->wpdb->prepare("SELECT ID FROM {$this->wpdb->posts}
WHERE post_name = %s
AND post_type='page'",
$custom_wp_query->query_vars['pagename'] ) );
}
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' ] = $this->wpdb->get_var( $this->wpdb->prepare("SELECT post_name
FROM {$this->wpdb->posts}
WHERE ID = %d ",
$tr_page_id ) );
}
}
}
// look for posts without translations
if ( $posts ) {
$pids = false;
foreach ( $posts as $p ) {
$pids[ ] = $p->ID;
}
if ( $pids ) {
$trids = $this->wpdb->get_col( $this->wpdb->prepare("
SELECT trid
FROM {$this->wpdb->prefix}icl_translations
WHERE element_type='post_post'
AND element_id IN (" . wpml_prepare_in( $pids, '%d' ) . ")
AND language_code = %s", $this_lang ) );
if ( !empty( $trids ) ) {
$posts_not_translated = $this->wpdb->get_col( "
SELECT element_id, COUNT(language_code) AS c
FROM {$this->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 = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT * FROM {$this->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 = array_values( array_unique( array_merge( $my_query->posts, $posts ), SORT_REGULAR ) );
unset( $GLOBALS[ '__icl_the_posts_posts_not_translated' ] );
remove_filter( 'posts_where', array( $this, '_posts_untranslated_extra_posts_where' ), 99 );
}
return $posts;
}
function _posts_untranslated_extra_posts_where( $where ) {
return $where . ' OR ' . $this->wpdb->posts . '.ID IN (' . wpml_prepare_in( $GLOBALS['__icl_the_posts_posts_not_translated'],
'%d' ) . ') ';
}
function initialize_cache() {
require_once ICL_PLUGIN_PATH . '/inc/cache.php';
$this->icl_translations_cache = new icl_cache();
$this->icl_language_name_cache = new icl_cache( 'language_name', true );
}
public function set_admin_language( $admin_language = false ) {
$default_language = $this->get_default_language ();
$this->admin_language = $admin_language ? $admin_language : $this->user_lang_by_authcookie ();
$lang_codes = array_keys ( $this->get_languages () );
if ( (bool) $this->admin_language === true && !in_array ( $this->admin_language, $lang_codes ) ) {
delete_user_meta ( $this->get_current_user ()->ID, 'icl_admin_language' );
}
if ( empty( $this->settings[ 'admin_default_language' ] ) || !in_array (
$this->settings[ 'admin_default_language' ],
$lang_codes
)
) {
$this->settings[ 'admin_default_language' ] = '_default_';
$this->save_settings ();
}
if ( !$this->admin_language ) {
$this->admin_language = $this->settings[ 'admin_default_language' ];
}
if ( $this->admin_language == '_default_' && $default_language ) {
$this->admin_language = $default_language;
}
}
function get_admin_language() {
$current_user = $this->get_current_user();
if ( ! empty( $current_user->ID ) && $this->get_wp_api()->get_user_meta( $current_user->ID,
'icl_admin_language_for_edit',
true ) && $this->is_post_edit_screen()
) {
$admin_language = $this->get_current_language();
} else {
$admin_language = $this->user_lang_by_authcookie();
}
return $admin_language;
}
/**
* @return bool
*/
function is_post_edit_screen() {
global $pagenow;
$action = isset( $_GET['action'] ) ? $_GET['action'] : "";
return $pagenow == 'post-new.php' || ( $pagenow == 'post.php' && ( 0 === strcmp( $action, 'edit' ) ) );
}
function get_user_admin_language_filter( $value, $user_id ) {
$value = $this->get_user_admin_language ( $user_id );
return $value;
}
function get_user_admin_language( $user_id, $reload = false ) {
static $lang = array();
$lang = $reload !== false ? array() : $lang;
if ( !isset( $lang[ $user_id ] ) ) {
$lang[ $user_id ] = get_user_meta ( $user_id, 'icl_admin_language_for_edit', true )
? $this->get_current_language() : get_user_meta ( $user_id, 'icl_admin_language', true );
if ( empty( $lang[ $user_id ] ) ) {
$admin_default_language = $this->get_setting( 'admin_default_language' );
if ( $admin_default_language ) {
$lang[ $user_id ] = $admin_default_language;
}
if ( empty( $lang[ $user_id ] ) || '_default_' == $lang[ $user_id ] ) {
$lang[ $user_id ] = $this->get_default_language();
}
}
}
return $lang[ $user_id ];
}
function administration_menu() {
ICL_AdminNotifier::removeMessage( 'setup-incomplete' );
$main_page = apply_filters( 'icl_menu_main_page', basename( ICL_PLUGIN_PATH ) . '/menu/languages.php' );
$wpml_setup_is_complete = SitePress_Setup::setup_complete();
if ( $wpml_setup_is_complete ) {
add_menu_page( __( 'WPML', 'sitepress' ), __( 'WPML', 'sitepress' ), 'wpml_manage_languages', $main_page, null, ICL_PLUGIN_URL . '/res/img/icon16.png' );
add_submenu_page( $main_page, __( 'Languages', 'sitepress' ), __( 'Languages', 'sitepress' ), 'wpml_manage_languages', basename( ICL_PLUGIN_PATH ) . '/menu/languages.php' );
//By Gen, moved Translation management after language, because problems with permissions
do_action( 'icl_wpml_top_menu_added' );
$wpml_setup_is_complete = $this->get_setting( 'existing_content_language_verified' ) && 2 <= count( $this->get_active_languages() );
if ( $wpml_setup_is_complete ) {
add_submenu_page( $main_page, __( 'Theme and plugins localization', 'sitepress' ), __( 'Theme and plugins localization', 'sitepress' ), 'wpml_manage_theme_and_plugin_localization',
basename( ICL_PLUGIN_PATH )
. '/menu/theme-localization.php' );
if ( ! defined( 'WPML_TM_VERSION' ) ) {
add_submenu_page( $main_page, __( 'Translation options', 'sitepress' ), __( 'Translation options', 'sitepress' ), 'wpml_manage_translation_options', basename( ICL_PLUGIN_PATH ) . '/menu/translation-options.php' );
}
}
$wpml_admin_menus_args = array(
'existing_content_language_verified' => $this->get_setting( 'existing_content_language_verified' ),
'active_languages_count' => count( $this->get_active_languages() ),
'wpml_setup_is_ok' => $wpml_setup_is_complete
);
do_action( 'wpml_admin_menus', $wpml_admin_menus_args );
} else {
$main_page = basename( ICL_PLUGIN_PATH ) . '/menu/languages.php';
add_menu_page( __( 'WPML', 'sitepress' ), __( 'WPML', 'sitepress' ), 'manage_options', $main_page, null, ICL_PLUGIN_URL . '/res/img/icon16.png' );
add_submenu_page( $main_page, __( 'Languages', 'sitepress' ), __( 'Languages', 'sitepress' ), 'wpml_manage_languages', $main_page );
if ( ! $this->is_troubleshooting_page() && ! SitePress_Setup::languages_table_is_complete() ) {
$troubleshooting_url = admin_url( 'admin.php?page=' . ICL_PLUGIN_FOLDER . '/menu/troubleshooting.php' );
$troubleshooting_link = '' . __( 'Troubleshooting', 'sitepress' ) . '';
$message = '';
$message .= __( 'WPML is missing some records in the languages tables and it cannot fully work until this issue is fixed.', 'sitepress' );
$message .= '
';
$message .= sprintf( __( 'Please go to the %s page and click on %s to fix this problem.', 'sitepress' ), $troubleshooting_link, __( 'Fix languages tables', 'sitepress' ) );
$message .= '
';
$message .= '
';
$message .= __( 'This warning will disappear once this issue is fixed.', 'sitepress' );
ICL_AdminNotifier::removeMessage( 'setup-incomplete' );
ICL_AdminNotifier::addMessage( 'setup-incomplete', $message, 'error', false, false, false, 'setup', true );
ICL_AdminNotifier::displayMessages( 'setup' );
}
}
add_submenu_page( $main_page, __( 'Support', 'sitepress' ), __( 'Support', 'sitepress' ), 'wpml_manage_support', ICL_PLUGIN_FOLDER . '/menu/support.php' );
$this->troubleshooting_menu(ICL_PLUGIN_FOLDER . '/menu/support.php');
$this->debug_information_menu(ICL_PLUGIN_FOLDER . '/menu/support.php');
}
private function troubleshooting_menu( $main_page ) {
$submenu_slug = basename( ICL_PLUGIN_PATH ) . '/menu/troubleshooting.php';
add_submenu_page( $main_page, __( 'Troubleshooting', 'sitepress' ), __( 'Troubleshooting', 'sitepress' ), 'wpml_manage_troubleshooting', $submenu_slug );
return $submenu_slug;
}
private function debug_information_menu( $main_page ) {
$submenu_slug = basename( ICL_PLUGIN_PATH ) . '/menu/debug-information.php';
add_submenu_page( $main_page, __( 'Debug information', 'sitepress' ), __( 'Debug information', 'sitepress' ), 'wpml_manage_troubleshooting', $submenu_slug );
return $submenu_slug;
}
// lower priority
function administration_menu2() {
$main_page = apply_filters( 'icl_menu_main_page', ICL_PLUGIN_FOLDER . '/menu/languages.php' );
if ( $this->setup() ) {
add_submenu_page( $main_page, __( 'Taxonomy Translation', 'sitepress' ), __( 'Taxonomy Translation', 'sitepress' ), 'wpml_manage_taxonomy_translation', ICL_PLUGIN_FOLDER . '/menu/taxonomy-translation.php' );
}
}
function init_settings( $blog_id ) {
global $sitepress_settings;
if ( isset( $this->loaded_blog_id ) && $this->loaded_blog_id != $blog_id ) {
remove_action( 'switch_blog', array( $this, 'init_settings' ) );
wp_cache_add( $this->loaded_blog_id . 'icl_sitepress_settings', $sitepress_settings, 'sitepress_ms' );
add_action( 'switch_blog', array( $this, 'init_settings' ), 10, 1 );
$sitepress_settings = wp_cache_get( $blog_id . 'icl_sitepress_settings', 'sitepress_ms' );
$sitepress_settings = (bool) $sitepress_settings === true ? $sitepress_settings : get_option( 'icl_sitepress_settings' );
}
$this->loaded_blog_id = $blog_id;
return $sitepress_settings;
}
function save_settings( $settings = null ) {
if ( ! is_null( $settings ) ) {
foreach ( $settings as $k => $v ) {
if ( is_array( $v ) ) {
foreach ( $v as $k2 => $v2 ) {
$this->settings[ $k ][ $k2 ] = $v2;
}
} else {
$this->settings[ $k ] = $v;
}
}
}
if ( ! empty( $this->settings ) ) {
update_option( 'icl_sitepress_settings', $this->settings );
}
do_action( 'icl_save_settings', $settings );
}
/**
* @since 3.1
*/
function get_settings() {
return $this->settings;
}
function filter_get_setting($value, $key) {
return $this->get_setting($key, $value);
}
/**
* @param string $key
* @param mixed|bool $default
*
* @since 3.1
*
* @return bool|mixed
*/
function get_setting( $key, $default = false ) {
return wpml_get_setting_filter( $default, $key );
}
function action_set_setting($key, $value, $save_now) {
$this->set_setting($key, $value, $save_now);
}
/**
* @param string $key
* @param mixed $value
* @param bool $save_now Immediately update the settings record in the DB
*
* @since 3.1
*/
function set_setting($key, $value, $save_now = false) {
icl_set_setting($key, $value, $save_now);
}
function get_user_preferences() {
if ( ! isset( $this->user_preferences ) || ! $this->user_preferences ) {
$this->user_preferences = get_user_meta( $this->get_current_user()->ID, '_icl_preferences', true );
}
if ( (is_array( $this->user_preferences) && $this->user_preferences == array(0 => false)) || !$this->user_preferences ) {
$this->user_preferences = array();
}
if ( ! is_array( $this->user_preferences ) ) {
$this->user_preferences = (array) $this->user_preferences;
}
return $this->user_preferences;
}
function set_user_preferences($value) {
$this->user_preferences = $value;
}
function save_user_preferences()
{
update_user_meta( $this->get_current_user()->ID, '_icl_preferences', $this->user_preferences );
}
function get_option( $option_name )
{
return isset( $this->settings[ $option_name ] ) ? $this->settings[ $option_name ] : null;
}
function verify_settings() {
$default_settings = array(
'interview_translators' => 1,
'existing_content_language_verified' => 0,
'language_negotiation_type' => 3,
'theme_localization_type' => 1,
'icl_lso_header' => 0,
'icl_lso_link_empty' => 0,
'icl_lso_flags' => 0,
'icl_lso_native_lang' => 1,
'icl_lso_display_lang' => 1,
'sync_page_ordering' => 1,
'sync_page_parent' => 1,
'sync_page_template' => 1,
'sync_ping_status' => 1,
'sync_comment_status' => 1,
'sync_sticky_flag' => 1,
'sync_password' => 1,
'sync_private_flag' => 1,
'sync_post_format' => 1,
'sync_delete' => 0,
'sync_delete_tax' => 0,
'sync_post_taxonomies' => 1,
'sync_post_date' => 0,
'sync_taxonomy_parents' => 0,
'translation_pickup_method' => 0,
'notify_complete' => 1,
'translated_document_status' => 1,
'remote_management' => 0,
'auto_adjust_ids' => 1,
'alert_delay' => 0,
'promote_wpml' => 0,
'troubleshooting_options' => array( 'http_communication' => 1 ),
'automatic_redirect' => 0,
'remember_language' => 24,
'icl_lang_sel_type' => 'dropdown',
'icl_lang_sel_stype' => 'classic',
'icl_lang_sel_orientation' => 'vertical',
'icl_lang_sel_copy_parameters' => '',
'icl_widget_title_show' => 1,
'translated_document_page_url' => 'auto-generate',
'sync_comments_on_duplicates ' => 0,
'seo' => array( 'head_langs' => 1, 'canonicalization_duplicates' => 1 ),
'posts_slug_translation' => array( 'on' => 0 ),
'languages_order' => '',
'urls' => array( 'directory_for_default_language' => 0, 'show_on_root' => '', 'root_html_file_path' => '', 'root_page' => 0, 'hide_language_switchers' => 1 ),
'xdomain_data' => WPML_XDOMAIN_DATA_GET
);
//configured for three levels
$update_settings = false;
foreach ( $default_settings as $key => $value ) {
if ( is_array( $value ) ) {
foreach ( $value as $k2 => $v2 ) {
if ( is_array( $v2 ) ) {
foreach ( $v2 as $k3 => $v3 ) {
if ( !isset( $this->settings[ $key ][ $k2 ][ $k3 ] ) ) {
$this->settings[ $key ][ $k2 ][ $k3 ] = $v3;
$update_settings = true;
}
}
} else {
if ( !isset( $this->settings[ $key ][ $k2 ] ) ) {
$this->settings[ $key ][ $k2 ] = $v2;
$update_settings = true;
}
}
}
} else {
if ( !isset( $this->settings[ $key ] ) ) {
$this->settings[ $key ] = $value;
$update_settings = true;
}
}
}
if ( $update_settings ) {
$this->save_settings();
}
}
function save_language_pairs()
{
// clear existing languages
$lang_pairs = $this->settings[ 'language_pairs' ];
if ( is_array( $lang_pairs ) ) {
foreach ( $lang_pairs as $from => $to ) {
$lang_pairs[ $from ] = array();
}
}
// get the from languages
$from_languages = array();
foreach ( $_POST as $k => $v ) {
if ( 0 === strpos( $k, 'icl_lng_from_' ) ) {
$f = str_replace( 'icl_lng_from_', '', $k );
$from_languages[ ] = $f;
}
}
foreach ( $_POST as $k => $v ) {
if ( 0 !== strpos( $k, 'icl_lng_' ) ) {
continue;
}
if ( 0 === strpos( $k, 'icl_lng_to' ) ) {
$t = str_replace( 'icl_lng_to_', '', $k );
$exp = explode( '_', $t );
if ( in_array( $exp[ 0 ], $from_languages ) ) {
$lang_pairs[ $exp[ 0 ] ][ $exp[ 1 ] ] = 1;
}
}
}
$iclsettings[ 'language_pairs' ] = $lang_pairs;
$this->save_settings( $iclsettings );
}
function get_active_languages( $refresh = false ) {
/** @var WPML_Request $wpml_request_handler*/
global $wpml_request_handler;
$in_language = defined( 'WP_ADMIN' ) && $this->admin_language ? $this->admin_language : null;
$in_language = $in_language === null ? $this->get_current_language() : $in_language;
$in_language = $in_language ? $in_language : $this->get_default_language();
$active_languages = $this->get_languages( $in_language, true, $refresh );
$active_languages = isset($active_languages[$in_language]) ? $active_languages
: $this->get_languages( $in_language, true, true );
$active_languages = $active_languages ? $active_languages : array();
$this->active_languages = $wpml_request_handler->show_hidden()
? $active_languages
: array_diff_key( $active_languages, array_fill_keys($this->get_setting ( 'hidden_languages', array()), 1) );
return $this->active_languages;
}
/**
* Returns an input array of languages, that are in the form of associative arrays,
* ordered by the user-chosen language order
*
* @param array[] $languages
*
* @return array[]
*/
function order_languages( $languages ) {
$ordered_languages = array();
if ( is_array( $this->settings[ 'languages_order' ] ) ) {
foreach ( $this->settings[ 'languages_order' ] as $code ) {
if ( isset( $languages[ $code ] ) ) {
$ordered_languages[ $code ] = $languages[ $code ];
unset( $languages[ $code ] );
}
}
} else {
// initial save
$iclsettings[ 'languages_order' ] = array_keys( $languages );
$this->save_settings( $iclsettings );
}
if ( ! empty( $languages ) ) {
foreach ( $languages as $code => $lang ) {
$ordered_languages[ $code ] = $lang;
}
}
return $ordered_languages;
}
/**
* @param $lang_code
* Checks if a given language code belongs to a currently active language.
* @return bool
*/
function is_active_language( $lang_code ) {
$result = false;
$active_languages = $this->get_active_languages();
foreach ( $active_languages as $lang ) {
if ( $lang_code == $lang[ 'code' ] ) {
$result = true;
break;
}
}
return $result;
}
public function get_languages( $lang = false, $active_only = false, $refresh = false ) {
if ( !$lang ) {
$lang = $this->get_default_language();
}
if ( $active_only && !$refresh && $res = $this->icl_language_name_cache->get( 'in_language_' . $lang ) ) {
return $res;
}
if ( !$active_only && !$refresh && $res = $this->icl_language_name_cache->get( 'all_language_' . $lang ) ) {
return $res;
}
$setup_instance = wpml_get_setup_instance();
return $setup_instance->refresh_active_lang_cache($lang, $active_only);
}
function get_language_details( $code ) {
if ( defined( 'WP_ADMIN' ) ) {
$dcode = $this->admin_language;
} else {
$dcode = $code;
}
$details = $this->icl_language_name_cache->get( 'language_details_' . $code . $dcode );
if ( !$details ) {
$language_details = $this->get_languages( $dcode );
$details = isset( $language_details[ $code ] ) ? $language_details[ $code ] : false;
}
return $details;
}
function get_language_code( $english_name ) {
$query = $this->wpdb->prepare( " SELECT code FROM {$this->wpdb->prefix}icl_languages WHERE english_name = %s LIMIT 1", $english_name );
$code = $this->wpdb->get_var( $query );
return $code;
}
function get_default_language() {
return isset( $this->settings[ 'default_language' ] ) ? $this->settings[ 'default_language' ] : false;
}
function get_current_language() {
/**
* @var WPML_Request $wpml_request_handler
* @var WPML_Language_Resolution $wpml_language_resolution
*/
global $wpml_request_handler, $wpml_language_resolution;
$this->this_lang = $this->this_lang ? $this->this_lang : $wpml_request_handler->get_requested_lang();
$this->this_lang = $this->this_lang ? $this->this_lang : $this->get_default_language();
return $wpml_language_resolution->current_lang_filter( $this->this_lang );
}
/**
* Switches whole site to the given language or back to the current language
* that was set when first calling this function.
*
* @param null|string $code language code to switch into, will revert to
* initial language if null is given
* @param bool|string $cookie_lang optionally also switch the cookie language
* to the value given
*/
public function switch_lang( $code = null, $cookie_lang = false ) {
/**
* @var WPML_Request $wpml_request_handler
* @var WPML_Language_Resolution $wpml_language_resolution
*/
global $wpml_language_resolution, $wpml_request_handler;
$this->original_language = $this->original_language === null
? $this->get_current_language() : $this->original_language;
if ( is_null( $code ) ) {
$this->this_lang = $this->original_language;
if ( ! empty( $this->original_language_cookie ) ) {
$wpml_request_handler->set_language_cookie( $this->original_language_cookie );
$this->original_language_cookie = false;
}
} else {
if ( $code === 'all' || in_array( $code, $wpml_language_resolution->get_active_language_codes(), true ) ) {
$this->this_lang = $code;
}
if ( $cookie_lang ) {
$this->original_language_cookie = $wpml_request_handler->get_cookie_lang();
$wpml_request_handler->set_language_cookie( $code );
}
}
do_action( 'wpml_language_has_switched' );
}
function set_default_language( $code ) {
$previous_default = $this->get_setting('default_language');
$this->set_setting( 'default_language', $code);
$this->set_setting('admin_default_language', $code);
$this->save_settings();
do_action('icl_after_set_default_language',$code, $previous_default);
// change WP locale
$locale = $this->get_locale( $code );
if ( $locale ) {
update_option( 'WPLANG', $locale );
}
return $code !== 'en' && !file_exists( ABSPATH . LANGDIR . '/' . $locale . '.mo' ) ? 1 : true;
}
function js_load()
{
global $pagenow, $wpdb, $wpml_post_translations, $wpml_term_translations;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
$page = filter_input( INPUT_GET, 'page' );
$page = $page !== null ? basename( $_GET['page'] ) : null;
$page_basename = $page === null ? false : preg_replace( '/[^\w-]/',
'',
str_replace( '.php', '', $page ) );
$this->scripts_handler = new WPML_Admin_Scripts_Setup( $wpdb,
$this,
$wpml_post_translations,
$wpml_term_translations,
$page_basename );
if ( isset( $_SERVER[ 'SCRIPT_NAME' ] ) && ( strpos( $_SERVER[ 'SCRIPT_NAME' ], 'post-new.php' ) || strpos( $_SERVER[ 'SCRIPT_NAME' ], 'post.php' ) ) ) {
wp_register_script( 'sitepress-post-edit-tags', ICL_PLUGIN_URL . '/res/js/post-edit-terms.js', array( 'jquery', 'underscore' ) );
$post_edit_messages = array(
'switch_language_title' => __( 'You are about to change the language of {post_name}.', 'sitepress' ),
'switch_language_alert' => __( 'All categories and tags will be translated if possible.', 'sitepress' ),
'connection_loss_alert' => __( 'The following terms do not have a translation in the chosen language and will be disconnected from this post:', 'sitepress' ),
'loading' => __( 'Loading Language Data for {post_name}', 'sitepress' ),
'switch_language_message' => __( 'Please make sure that you\'ve saved all the changes. We will have to reload the page.', 'sitepress' ),
'switch_language_confirm' => __( 'Do you want to continue?', 'sitepress' ),
'_nonce' => wp_create_nonce('wpml_switch_post_lang_nonce')
);
wp_localize_script( 'sitepress-post-edit-tags', 'icl_post_edit_messages', $post_edit_messages );
wp_enqueue_script( 'sitepress-post-edit-tags' );
}
if ( isset( $_SERVER[ 'SCRIPT_NAME' ] ) && strpos( $_SERVER[ 'SCRIPT_NAME' ], 'edit.php' ) ) {
wp_register_script( 'sitepress-post-list-quickedit', ICL_PLUGIN_URL . '/res/js/post-list-quickedit.js', array( 'jquery') );
wp_enqueue_script( 'sitepress-post-list-quickedit' );
}
wp_enqueue_script( 'sitepress-scripts', ICL_PLUGIN_URL . '/res/js/scripts.js', array( 'jquery' ), ICL_SITEPRESS_VERSION );
if ( isset( $page_basename ) && file_exists( ICL_PLUGIN_PATH . '/res/js/' . $page_basename . '.js' ) ) {
$dependencies = array();
$localization = false;
$color_picker_handler = 'wp-color-picker';
switch ( $page_basename ) {
case 'languages':
$dependencies[ ] = $color_picker_handler;
$dependencies[ ] = 'sitepress-scripts';
$dependencies[ ] = 'wpml-domain-validation';
break;
case 'troubleshooting':
$dependencies [ ] = 'jquery-ui-dialog';
$localization = array(
'object_name' => 'troubleshooting_strings',
'strings' => array(
'success_1' => __( "Post type and source language assignment have been fixed for ", 'sitepress' ),
'success_2' => __( " elements", 'sitepress' ),
'no_problems' => __( "No errors were found in the assignment of post types." ),
'suffixesRemoved' => __( "Language suffixes were removed from the selected terms." ),
'done' => __( 'Done', 'sitepress' ),
'termNamesNonce' => wp_create_nonce('update_term_names_nonce'),
'cacheClearNonce' => wp_create_nonce('cache_clear'),
)
);
wp_enqueue_style("wp-jquery-ui-dialog");
break;
}
$handle = 'sitepress-' . $page_basename;
wp_register_script( $handle, ICL_PLUGIN_URL . '/res/js/' . $page_basename . '.js', $dependencies, ICL_SITEPRESS_VERSION );
if ( $localization ) {
wp_localize_script( $handle, $localization[ 'object_name' ], $localization[ 'strings' ] );
}
if(in_array($color_picker_handler, $dependencies)) {
wp_enqueue_style( $color_picker_handler );
}
wp_enqueue_script( $handle );
}
if ( $pagenow == 'edit.php' ) {
add_action( 'admin_enqueue_scripts', array( $this, 'language_filter' ) );
}
wp_enqueue_style( 'wpml-select-2', ICL_PLUGIN_URL . '/lib/select2/select2.css' );
}
}
function front_end_js()
{
if ( defined( 'ICL_DONT_LOAD_LANGUAGES_JS' ) && ICL_DONT_LOAD_LANGUAGES_JS ) {
return;
}
wp_register_script( 'sitepress', ICL_PLUGIN_URL . '/res/js/sitepress.js', false );
wp_enqueue_script( 'sitepress' );
$vars = array(
'current_language' => $this->this_lang,
'icl_home' => $this->language_url(),
'ajax_url' => $this->convert_url( admin_url('admin-ajax.php'), $this->this_lang ),
'url_type' => $this->settings['language_negotiation_type']
);
wp_localize_script( 'sitepress', 'icl_vars', $vars );
}
function rtl_fix()
{
global $wp_styles;
if ( !empty( $wp_styles ) && $this->is_rtl() ) {
$wp_styles->text_direction = 'rtl';
}
}
function process_forms() {
if ( isset( $_POST[ 'icl_post_action' ] ) ) {
switch ( $_POST[ 'icl_post_action' ] ) {
case 'save_theme_localization':
$locales = array();
foreach ( $_POST as $k => $v ) {
if ( 0 !== strpos( $k, 'locale_file_name_' ) || !trim( $v ) ) {
continue;
}
$locales[ str_replace( 'locale_file_name_', '', $k ) ] = $v;
}
if ( !empty( $locales ) ) {
$this->set_locale_file_names( $locales );
}
break;
}
return;
}
if ( wp_verify_nonce(
(string)filter_input( INPUT_POST, 'icl_initial_languagenonce', FILTER_SANITIZE_STRING ),
'icl_initial_language'
) ) {
$setup_instance = wpml_get_setup_instance ();
$first_lang = filter_input ( INPUT_POST, 'icl_initial_language_code', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$this->admin_language = $first_lang;
$setup_instance->finish_step1($first_lang);
} elseif ( wp_verify_nonce(
(string)filter_input( INPUT_POST, 'icl_language_pairs_formnounce', FILTER_SANITIZE_STRING ),
'icl_language_pairs_form'
) ) {
$this->save_language_pairs();
$this->settings[ 'content_translation_languages_setup' ] = 1;
// Move onto the site description page
$this->settings[ 'content_translation_setup_wizard_step' ] = 2;
$this->settings[ 'website_kind' ] = 2;
$this->settings[ 'interview_translators' ] = 1;
$this->save_settings();
} elseif ( wp_verify_nonce(
(string)filter_input( INPUT_POST, 'icl_site_description_wizardnounce', FILTER_SANITIZE_STRING ),
'icl_site_description_wizard'
) ) {
if ( isset( $_POST[ 'icl_content_trans_setup_back_2' ] ) ) {
// back button.
$this->settings[ 'content_translation_languages_setup' ] = 0;
$this->settings[ 'content_translation_setup_wizard_step' ] = 1;
$this->save_settings();
} elseif ( isset( $_POST[ 'icl_content_trans_setup_next_2' ] ) || isset( $_POST[ 'icl_content_trans_setup_next_2_enter' ] ) ) {
// next button.
$description = $_POST[ 'icl_description' ];
if ( $description == "" ) {
$_POST[ 'icl_form_errors' ] = __( 'Please provide a short description of the website so that translators know what background is required from them.', 'sitepress' );
} else {
$this->settings[ 'icl_site_description' ] = $description;
$this->settings[ 'content_translation_setup_wizard_step' ] = 3;
$this->save_settings();
}
}
}
}
function post_edit_language_options() {
/** @var TranslationManagement $iclTranslationManagement */
global $post, $iclTranslationManagement, $post_new_file, $post_type_object;
if(!isset($post) || !$this->settings['setup_complete']) return;
if ( current_user_can( 'manage_options' ) ) {
add_meta_box( 'icl_div_config', __( 'Multilingual Content Setup', 'sitepress' ), array( $this, 'meta_box_config' ), $post->post_type, 'normal', 'low' );
}
if ( filter_input(INPUT_POST, 'icl_action' ) === 'icl_mcs_inline' ) {
$custom_post_type = filter_input(INPUT_POST, 'custom_post');
if ( !in_array($custom_post_type, array( 'post', 'page' ) ) ) {
$translate = (int)filter_input(INPUT_POST, 'translate');
$iclsettings[ 'custom_posts_sync_option' ][ $custom_post_type ] = $translate;
if ( $translate ) {
$this->verify_post_translations( $custom_post_type );
}
}
$custom_taxs_off = (array)filter_input(INPUT_POST, 'custom_taxs_off', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY);
$custom_taxs_on = (array)filter_input(INPUT_POST, 'custom_taxs_on', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY);
$tax_sync_options = array_merge(array_fill_keys($custom_taxs_on, 1), array_fill_keys($custom_taxs_off, 0));
foreach ( $tax_sync_options as $key => $setting ) {
$iclsettings[ 'taxonomies_sync_option' ][ $key ] = $setting;
if($setting){
$this->verify_taxonomy_translations( $key );
}
}
$cf_names = (array)filter_input(INPUT_POST, 'cfnames', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY);
$cf_vals = (array)filter_input(INPUT_POST, 'cfvals', FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY);
if ( in_array ( 1, $cf_vals ) === true ) {
global $wpml_post_translations;
$original_post_id = $wpml_post_translations->get_original_element ( $post->ID, true );
$translations = array_diff($wpml_post_translations->get_element_translations ( $original_post_id ), array($original_post_id));
}
foreach ( $cf_names as $k => $v ) {
$custom_field_name = base64_decode ( $v );
$cf_translation_state = isset( $cf_vals[ $k ] ) ? (int) $cf_vals[ $k ] : 0;
$iclTranslationManagement->settings[ 'custom_fields_translation' ][ $custom_field_name ] = $cf_translation_state;
$iclTranslationManagement->save_settings();
// sync the custom fields for the current post
if ( 1 === $cf_translation_state ) {
/**
* @var int[] $translations
* @var int $original_post_id
*/
foreach ( $translations as $translated_id ) {
$this->_sync_custom_field($original_post_id, $translated_id, $custom_field_name);
}
}
}
if ( !empty( $iclsettings ) ) {
$this->save_settings( $iclsettings );
}
}
$post_types = array_keys( $this->get_translatable_documents() );
if ( in_array( $post->post_type, $post_types ) ) {
add_meta_box( 'icl_div', __( 'Language', 'sitepress' ), array( $this, 'meta_box' ), $post->post_type, 'side', 'high' );
}
//Fix the "Add new" button adding the language argument, so to create new content in the same language
if(isset($post_new_file) && isset($post_type_object) && $this->is_translated_post_type($post_type_object->name)) {
$post_language = $this->get_language_for_element($post->ID, 'post_' . $post_type_object->name);
$post_new_file = add_query_arg(array('lang' => $post_language), $post_new_file);
}
}
function set_element_language_details_action( $args ) {
$element_id = $args[ 'element_id' ];
$element_type = isset( $args[ 'element_type' ] ) ? $args[ 'element_type' ] : 'post_post';
$trid = $args[ 'trid' ];
$language_code = $args[ 'language_code' ];
$source_language_code = isset( $args[ 'source_language_code' ] ) ? $args[ 'source_language_code' ] : null;
$check_duplicates = isset( $args[ 'check_duplicates' ] ) ? $args[ 'check_duplicates' ] : true;
$result = $this->set_element_language_details( $element_id, $element_type, $trid, $language_code, $source_language_code, $check_duplicates );
$args[ 'result' ] = $result;
}
/**
* @param int $el_id
* @param string $el_type
* @param int $trid
* @param string $language_code
* @param null|string $src_language_code
* @param bool $check_duplicates
*
* @return bool|int|null|string
*/
public function set_element_language_details(
$el_id,
$el_type = 'post_post',
$trid,
$language_code,
$src_language_code = null,
$check_duplicates = true
) {
if ( ! $this->language_setter ) {
$this->language_setter = new WPML_Set_Language( $this,
$this->wpdb,
$this->post_translation,
$this->term_translation );
}
return $this->language_setter->set(
$el_id,
$el_type,
$trid,
$language_code,
$src_language_code,
$check_duplicates
);
}
function delete_element_translation( $trid, $element_type, $language_code = false ) {
$result = false;
if ( $trid !== false && is_numeric( $trid ) && $element_type !== false && is_string( $trid ) ) {
$delete_where = array( 'trid' => $trid, 'element_type' => $element_type );
$delete_formats = array( '%d', '%s' );
if ( $language_code ) {
$delete_where[ 'language_code' ] = $language_code;
$delete_formats[ ] = '%s';
}
$result = $this->wpdb->delete( $this->wpdb->prefix . 'icl_translations', $delete_where, $delete_formats );
$this->icl_translations_cache->clear();
}
return $result;
}
function get_element_language_details( $el_id, $el_type = 'post_post' ) {
$details = false;
if ( $el_id ) {
if ( strpos( $el_type, 'post_' ) === 0 ) {
$details = $this->post_translation->get_element_language_details( $el_id, OBJECT );
}
if ( strpos( $el_type, 'tax_' ) === 0 ) {
$details = $this->term_translation->get_element_language_details( $el_id, OBJECT );
}
if ( ! $details ) {
$cache_key = $el_id . ':' . $el_type;
$cache_group = 'element_language_details';
$cached_details = wp_cache_get( $cache_key, $cache_group );
if ( $cached_details ) {
return $cached_details;
}
if ( isset( $this->icl_translations_cache ) && $this->icl_translations_cache->has_key( $el_id . $el_type ) ) {
return $this->icl_translations_cache->get( $el_id . $el_type );
}
$details_query = "
SELECT trid, language_code, source_language_code
FROM {$this->wpdb->prefix}icl_translations
WHERE element_id=%d AND element_type=%s
";
$details_prepare = $this->wpdb->prepare( $details_query, array( $el_id, $el_type ) );
$details = $this->wpdb->get_row( $details_prepare );
if ( isset( $this->icl_translations_cache ) ) {
$this->icl_translations_cache->set( $el_id . $el_type, $details );
}
wp_cache_add( $cache_key, $details, $cache_group );
}
}
return $details;
}
public function _sync_custom_field( $post_id_from, $post_id_to, $meta_key ) {
$sql = "SELECT meta_value FROM {$this->wpdb->postmeta} WHERE post_id=%d AND meta_key=%s";
$values_from = $this->wpdb->get_col ( $this->wpdb->prepare ( $sql, array( $post_id_from, $meta_key ) ) );
$values_to = $this->wpdb->get_col ( $this->wpdb->prepare ( $sql, array( $post_id_to, $meta_key ) ) );
$removed = array_diff( $values_to, $values_from );
foreach ( $removed as $v ) {
$delete_prepared = $this->wpdb->prepare( "DELETE FROM {$this->wpdb->postmeta}
WHERE post_id=%d
AND meta_key=%s
AND meta_value=%s",
array($post_id_to, $meta_key, $v) );
$this->wpdb->query( $delete_prepared );
}
$added = array_diff( $values_from, $values_to );
foreach ( $added as $v ) {
$insert_prepared = $this->wpdb->prepare( "INSERT INTO {$this->wpdb->postmeta}(post_id, meta_key, meta_value)
VALUES(%d, %s, %s)",
array($post_id_to, $meta_key, $v) );
$this->wpdb->query( $insert_prepared );
}
}
function copy_custom_fields( $post_id_from, $post_id_to ) {
$cf_copy = array();
if ( isset( $this->settings[ 'translation-management' ][ 'custom_fields_translation' ] ) ) {
foreach ( $this->settings[ 'translation-management' ][ 'custom_fields_translation' ] as $meta_key => $option ) {
if ( $option == WPML_COPY_CUSTOM_FIELD ) {
$cf_copy[ ] = $meta_key;
}
}
}
foreach ( $cf_copy as $meta_key ) {
$meta_from = get_post_meta($post_id_from, $meta_key) ;
$meta_to = get_post_meta($post_id_to, $meta_key) ;
if($meta_from || $meta_to) {
$this->_sync_custom_field( $post_id_from, $post_id_to, $meta_key );
}
}
}
/**
*This method does nothing and is only there as a placeholder for backward compatibility with old Types versions!
*
* @deprecated Since WPML 3.1.9
*
* @param $meta_id
* @param $object_id
* @param $meta_key
* @param $_meta_value
*/
function update_post_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
return;
}
/**
*This method does nothing and is only there as a placeholder for backward compatibility with old Types versions!
*
* @deprecated Since WPML 3.1.9
*
* @param $meta_id
*/
function delete_post_meta( $meta_id ) {
return;
}
/* Custom fields synchronization - END */
function get_element_translations_filter( $value, $trid, $el_type = 'post_post', $skip_empty = false, $all_statuses = false, $skip_cache = false ) {
return $this->get_element_translations( $trid, $el_type, $skip_empty, $all_statuses, $skip_cache );
}
public function get_original_element_id_filter($empty, $element_id, $element_type = 'post_post') {
$original_element_id = $this->get_original_element_id($element_id, $element_type);
return $original_element_id;
}
function is_original_content_filter($default = false, $element_id, $element_type = 'post_post') {
$is_original_content = $default;
$trid = $this->get_element_trid($element_id, $element_type);
$translations = $this->get_element_translations($trid, $element_type);
if($translations) {
foreach($translations as $language_code => $translation) {
if($translation->element_id == $element_id) {
$is_original_content = $translation->original;
break;
}
}
}
return $is_original_content;
}
/**
* @param int $trid
* @param string $el_type Use comment, post, page, {custom post time name}, nav_menu, nav_menu_item, category, post_tag, etc. (prefixed with 'post_', 'tax_', or nothing for 'comment')
* @param bool $skip_empty
* @param bool $all_statuses
* @param bool $skip_cache
*
* @return array|bool|mixed
*/
function get_element_translations( $trid, $el_type = 'post_post', $skip_empty = false, $all_statuses = false, $skip_cache = false ) {
$cache_key_args = array_filter( array( $trid, $el_type, $skip_empty, $all_statuses ) );
$cache_key = md5(wp_json_encode( $cache_key_args ));
$cache_group = 'element_translations';
$cache_found = false;
$cache = new WPML_WP_Cache( $cache_group );
$temp_elements = $cache->get( $cache_key, $cache_found );
if( !$skip_cache && $cache_found ) return $temp_elements;
$translations = array();
$sel_add = '';
$where_add = '';
if ( $trid ) {
if ( 0 === strpos( $el_type, 'post_' ) ) {
$sel_add = ', p.post_title, p.post_status';
$join_add = " LEFT JOIN {$this->wpdb->posts} p ON t.element_id=p.ID";
$groupby_add = "";
if ( ! is_admin() && empty( $all_statuses ) && $el_type !== 'post_attachment' ) {
// the current user may not be the admin but may have read private post/page caps!
if ( current_user_can( 'read_private_pages' ) || current_user_can( 'read_private_posts' ) ) {
$where_add .= " AND (p.post_status = 'publish' OR p.post_status = 'private' OR p.post_status = 'pending')";
$where_add .= " AND (p.post_status = 'publish' OR p.post_status = 'private' OR p.post_status = 'pending')";
} else {
$where_add .= " AND (";
$where_add .= "p.post_status = 'publish' OR p.post_status = 'pending' ";
if ( $uid = $this->get_current_user()->ID ) {
$where_add .= $this->wpdb->prepare(" OR (post_status in ('draft', 'private', 'pending') AND post_author = %d)", $uid);
}
$where_add .= ") ";
}
}
} elseif ( preg_match( '#^tax_(.+)$#', $el_type ) ) {
$sel_add = ', tm.name, tm.term_id, COUNT(tr.object_id) AS instances';
$join_add = " LEFT JOIN {$this->wpdb->term_taxonomy} tt ON t.element_id=tt.term_taxonomy_id
LEFT JOIN {$this->wpdb->terms} tm ON tt.term_id = tm.term_id
LEFT JOIN {$this->wpdb->term_relationships} tr ON tr.term_taxonomy_id=tt.term_taxonomy_id
";
$groupby_add = "GROUP BY tm.term_id";
}
$where_add .= $this->wpdb->prepare( " AND t.trid=%d ", $trid );
if ( !isset( $join_add ) ) {
$join_add = "";
}
if ( !isset( $groupby_add ) ) {
$groupby_add = "";
}
$query = "
SELECT t.translation_id, t.language_code, t.element_id, t.source_language_code, NULLIF(t.source_language_code, '') IS NULL AS original {$sel_add}
FROM {$this->wpdb->prefix}icl_translations t
{$join_add}
WHERE 1 {$where_add}
{$groupby_add}
";
$ret = $this->wpdb->get_results( $query );
foreach ( $ret as $t ) {
if ( ( preg_match( '#^tax_(.+)$#', $el_type ) ) && $t->instances == 0 && !_icl_tax_has_objects_recursive( $t->element_id ) && $skip_empty ) {
continue;
}
$cached_object_key = $t->element_id . '#' . $el_type . '#0#' . $t->language_code;
wp_cache_set( $cached_object_key, $cached_object_key, 'icl_object_id' );
$translations[ $t->language_code ] = $t;
}
}
if($translations) {
$cache->set( $cache_key, $translations );
}
return $translations;
}
static function get_original_element_id($element_id, $element_type = 'post_post', $skip_empty = false, $all_statuses = false, $skip_cache = false) {
$cache_key_args = array_filter( array( $element_id, $element_type, $skip_empty, $all_statuses ) );
$cache_key = md5(wp_json_encode( $cache_key_args ));
$cache_group = 'original_element';
$temp_elements = $skip_cache ? false : wp_cache_get($cache_key, $cache_group);
if($temp_elements) return $temp_elements;
global $sitepress;
$original_element_id = false;
$trid = $sitepress->get_element_trid($element_id, $element_type);
if($trid) {
$element_translations = $sitepress->get_element_translations($trid, $element_type,$skip_empty,$all_statuses,$skip_cache);
foreach($element_translations as $element_translation) {
if($element_translation->original) {
$original_element_id = $element_translation->element_id;
break;
}
}
}
if($original_element_id) {
wp_cache_set($cache_key, $original_element_id, $cache_group);
}
return $original_element_id;
}
/**
* @param int $element_id Use term_taxonomy_id for taxonomies, post_id for posts
* @param string $el_type Use comment, post, page, {custom post time name}, nav_menu, nav_menu_item, category, post_tag, etc. (prefixed with 'post_', 'tax_', or nothing for 'comment')
*
* @return bool|mixed|null|string
*/
function get_element_trid( $element_id, $el_type = 'post_post' ) {
if ( strpos ( $el_type, 'tax_' ) === 0 ) {
/** @var WPML_Term_Translation $wpml_term_translations */
global $wpml_term_translations;
return $wpml_term_translations->get_element_trid ( $element_id );
} elseif ( strpos ( $el_type, 'post_' ) === 0 ) {
global $wpml_post_translations;
return $wpml_post_translations->get_element_trid ( $element_id );
} else {
$cache_key = $element_id . ':' . $el_type;
$cache_group = 'element_trid';
$temp_trid = wp_cache_get( $cache_key, $cache_group );
if ( (bool) $temp_trid === true ) {
return $temp_trid;
}
$trid_prepared = $this->wpdb->prepare(
"SELECT trid FROM {$this->wpdb->prefix}icl_translations WHERE element_id=%d AND element_type=%s",
array( $element_id, $el_type )
);
$trid = $this->wpdb->get_var( $trid_prepared );
if ( $trid ) {
wp_cache_add( $cache_key, $trid, $cache_group );
}
}
return $trid;
}
/**
* @param int $trid
*
* @return int|bool
*/
static function get_original_element_id_by_trid( $trid ) {
global $wpdb;
if ( (bool) $trid === true ) {
$original_element_id_prepared = $wpdb->prepare( "SELECT element_id
FROM {$wpdb->prefix}icl_translations
WHERE trid=%d
AND source_language_code IS NULL
LIMIT 1",
$trid );
$element_id = $wpdb->get_var( $original_element_id_prepared );
} else {
$element_id = false;
}
return $element_id;
}
static function get_source_language_by_trid( $trid ) {
global $wpdb;
$source_language = null;
if ( (bool) $trid === true ) {
$cache = new WPML_WP_Cache( 'get_source_language_by_trid' );
$found = false;
$source_language = $cache->get( $trid, $found );
if ( ! $found ) {
$source_language_prepared = $wpdb->prepare( "
SELECT language_code
FROM {$wpdb->prefix}icl_translations
WHERE trid=%d
AND source_language_code IS NULL
LIMIT 1",
$trid );
$source_language = $wpdb->get_var( $source_language_prepared );
$cache->set( $trid, $source_language );
}
}
return $source_language;
}
/**
* @param int $element_id Use term_taxonomy_id for taxonomies, post_id for posts
* @param string $element_type Use comment, post, page, {custom post time name}, nav_menu, nav_menu_item, category,
* post_tag, etc. (prefixed with 'post_', 'tax_', or nothing for 'comment')
*
* @return null|string
*/
function get_language_for_element( $element_id, $element_type = 'post_post' ) {
$cache_key_array = array( $element_id, $element_type );
$cache_key = md5( serialize( $cache_key_array ) );
$cache_group = 'get_language_for_element';
$cache_found = false;
$cache = new WPML_WP_Cache( $cache_group );
$result = $cache->get( $cache_key, $cache_found );
if ( $cache_found ) {
return $result;
}
$language_for_element_prepared = $this->wpdb->prepare( " SELECT language_code
FROM {$this->wpdb->prefix}icl_translations
WHERE element_id=%d
AND element_type=%s
LIMIT 1",
array( $element_id, $element_type ) );
$result = $this->wpdb->get_var( $language_for_element_prepared );
if ( $result ) {
$cache->set( $cache_key, $result );
}
return apply_filters( 'wpml_language_for_element', $result, $element_type );
}
/**
* @param string $el_type Use comment, post, page, {custom post time name}, nav_menu, nav_menu_item, category, post_tag, etc. (prefixed with 'post_', 'tax_', or nothing for 'comment')
* @param string $target_lang Target language code
* @param string $source_lang Source language code
*
* @return array
*/
function get_elements_without_translations( $el_type, $target_lang, $source_lang ) {
$sql = $this->wpdb->prepare(
"SELECT trid
FROM {$this->wpdb->prefix}icl_translations
WHERE language_code = %s
AND element_type = %s",
$target_lang,
$el_type
);
$trids_for_target = $this->wpdb->get_col( $sql );
if ( sizeof( $trids_for_target ) > 0 ) {
$trids_for_target = wpml_prepare_in( $trids_for_target, '%d' );
$not_trids = 'AND trid NOT IN (' . $trids_for_target . ')';
} else {
$not_trids = '';
}
$join = $where = '';
// exclude trashed posts
if ( 0 === strpos( $el_type, 'post_' ) ) {
$join .= " JOIN {$this->wpdb->posts} ON {$this->wpdb->posts}.ID = {$this->wpdb->prefix}icl_translations.element_id";
$where .= " AND {$this->wpdb->posts}.post_status <> 'trash' AND {$this->wpdb->posts}.post_status <> 'auto-draft'";
}
// Now get all the elements that are in the source language that
// are not already translated into the target language.
$sql = $this->wpdb->prepare(
"SELECT element_id
FROM
{$this->wpdb->prefix}icl_translations
{$join}
WHERE language_code = %s
{$not_trids}
AND element_type= %s
{$where}
",
$source_lang,
$el_type
);
return $this->wpdb->get_col( $sql );
}
/**
* @param string $selected_language
* @param string $default_language
* @param string $post_type
*
* @used_by SitePress:meta_box
*
* @return array
*/
function get_posts_without_translations( $selected_language, $default_language, $post_type = 'post_post' ) {
$untranslated_ids = $this->get_elements_without_translations( $post_type, $selected_language, $default_language );
$untranslated = array();
foreach ( $untranslated_ids as $id ) {
$untranslated[ $id ] = $this->wpdb->get_var(
$this->wpdb->prepare( "SELECT post_title FROM {$this->wpdb->prefix}posts WHERE ID = %d", $id )
);
}
return $untranslated;
}
public function get_orphan_translations( $trid, $post_type = 'post', $source_language ) {
$results = array();
$translations = $this->get_element_translations( $trid, 'post_' . $post_type );
if ( count( $translations ) === 1 ) {
$sql = " SELECT trid, ";
$language_codes = array_keys( $this->get_active_languages() );
$sql_languages = array();
$sql_languages_having = array();
foreach ( $language_codes as $language_code ) {
$sql_languages[] = "SUM(CASE language_code WHEN '" . esc_sql( $language_code ) . "' THEN 1 ELSE 0 END) AS `" . esc_sql( $language_code ) . '`';
if ( $language_code == $source_language ) {
$sql_languages_having[] = '`' . esc_sql( $language_code ) . '`= 0';
}
}
$sql .= implode( ',', $sql_languages );
$sql .= " FROM {$this->wpdb->prefix}icl_translations WHERE element_type = %s ";
$sql .= 'GROUP BY trid ';
$sql .= 'HAVING ' . implode( ' AND ', $sql_languages_having );
$sql .= " ORDER BY trid;";
$sql_prepared = $this->wpdb->prepare( $sql, array( 'post_' . $post_type ) );
$trid_results = $this->wpdb->get_results( $sql_prepared, 'ARRAY_A' );
$trid_list = array_column( $trid_results, 'trid' );
if ( $trid_list ) {
$sql = "SELECT trid AS value, CONCAT('[', t.language_code, '] ', (CASE p.post_title WHEN '' THEN CONCAT(LEFT(p.post_content, 30), '...') ELSE p.post_title END)) AS label
FROM {$this->wpdb->posts} p
INNER JOIN {$this->wpdb->prefix}icl_translations t
ON p.ID = t.element_id
WHERE t.element_type = %s
AND t.language_code <> %s
AND t.trid IN (" . wpml_prepare_in( $trid_list, '%d' ) . ") ";
$sql_prepared = $this->wpdb->prepare( $sql, array( 'post_' . $post_type, $source_language ) );
$results = $this->wpdb->get_results( $sql_prepared );
}
}
return $results;
}
/**
* @param WP_Post $post
*/
function meta_box( $post ) {
do_action('wpml_post_edit_languages', $post);
}
function icl_get_metabox_states() {
global $icl_meta_box_globals;
$translation = false;
$source_id = null;
$translated_id = null;
if ( sizeof( $icl_meta_box_globals[ 'translations' ] ) > 0 ) {
if ( !isset( $icl_meta_box_globals[ 'translations' ][ $icl_meta_box_globals[ 'selected_language' ] ] ) ) {
// We are creating a new translation
$translation = true;
// find the original
foreach ( $icl_meta_box_globals[ 'translations' ] as $trans_data ) {
if ( $trans_data->original == '1' ) {
$source_id = $trans_data->element_id;
break;
}
}
} else {
$trans_data = $icl_meta_box_globals[ 'translations' ][ $icl_meta_box_globals[ 'selected_language' ] ];
// see if this is an original or a translation.
if ( $trans_data->original == '0' ) {
// double check that it's not the original
// This is because the source_language_code field in icl_translations is not always being set to null.
$source_language_code = $this->wpdb->get_var( $this->wpdb->prepare("SELECT source_language_code
FROM {$this->wpdb->prefix}icl_translations
WHERE translation_id = %d",
$trans_data->translation_id ) );
$translation = !( $source_language_code == "" || $source_language_code == null );
if ( $translation ) {
$source_id = $icl_meta_box_globals[ 'translations' ][ $source_language_code ]->element_id;
$translated_id = $trans_data->element_id;
} else {
$source_id = $trans_data->element_id;
}
} else {
$source_id = $trans_data->element_id;
}
}
}
return array( $translation, $source_id, $translated_id );
}
function meta_box_config( $post ) {
/** @var TranslationManagement $iclTranslationManagement */
global $iclTranslationManagement,$wp_taxonomies, $wp_post_types, $sitepress_settings;
if ( ! $this->settings[ 'setup_complete' ] ) {
return false;
}
echo '
| labels->name ?> | |
(settings[ 'show_translations_flag' ] )): ?>style="display:none;">settings[ 'show_translations_flag' ] )): ?>style="display:none;">)
' : '';
$language_selector_mobile .= '
|
' . $n[ 'message' ] . '
'; echo '