/** * Filters the bases used to generate TEC rewrite rules to use WPML managed translations. * * @param array $bases * @param string $method * @param array $domains * * @return array An array of bases each with its (optional) WPML managed translations set. */ public function filter_tribe_events_rewrite_i18n_slugs_raw($bases, $method, $domains) { /** @var SitePress $sitepress */ global $sitepress, $sitepress_settings; if (empty($sitepress) || !is_a($sitepress, 'SitePress')) { return $bases; } $tec = Tribe__Events__Main::instance(); // Grab all languages $langs = $sitepress->get_active_languages(); if (empty($langs)) { return $bases; } foreach ($langs as $lang) { $languages[] = $sitepress->get_locale($lang['code']); } // Prevent Duplicates and Empty langs $languages = array_filter(array_unique($languages)); // Query the Current Language $current_locale = $sitepress->get_locale($sitepress->get_current_language()); // Get the strings on multiple Domains and Languages // remove WPML filter to avoid the locale being set to the default one remove_filter('locale', array($sitepress, 'locale_filter')); $bases = $tec->get_i18n_strings($bases, $languages, $domains, $current_locale); // re-hook WPML filter add_filter('locale', array($sitepress, 'locale_filter')); $string_translation_active = function_exists('wpml_st_load_slug_translation'); $post_slug_translation_on = !empty($sitepress_settings['posts_slug_translation']['on']); if ($string_translation_active && $post_slug_translation_on) { $supported_post_types = array(Tribe__Events__Main::POSTTYPE); foreach ($supported_post_types as $post_type) { // check that translations are active for this CPT $cpt_slug_is_not_translated = empty($sitepress_settings['posts_slug_translation']['types'][$post_type]); if ($cpt_slug_is_not_translated) { continue; } $slug_translations = WPML_Slug_Translation::get_translations($post_type); if (!(is_array($slug_translations) && isset($slug_translations[1]))) { continue; } $bases['single'] = array_merge($bases['single'], wp_list_pluck($slug_translations[1], 'value')); } } return $bases; }
/** * @param $bases * * @return array */ protected function translate_single_slugs(array $bases) { global $sitepress_settings; $supported_post_types = array(Tribe__Events__Main::POSTTYPE); foreach ($supported_post_types as $post_type) { // check that translations are active for this CPT $cpt_slug_is_not_translated = empty($sitepress_settings['posts_slug_translation']['types'][$post_type]); if ($cpt_slug_is_not_translated) { continue; } $slug_translations = WPML_Slug_Translation::get_translations($post_type); if (!isset($slug_translations[1])) { continue; } $bases['single'] = array_merge($bases['single'], wp_list_pluck($slug_translations[1], 'value')); } return $bases; }
function all_rewrite_rules($rewrite) { global $sitepress; if (!class_exists('WPML_Slug_Translation')) { return $rewrite; } $active_languages = $sitepress->get_active_languages(); $current_language = $sitepress->get_current_language(); $default_language = $sitepress->get_default_language(); $cache_keys = array($current_language, $default_language); $cache_keys[] = md5(serialize($active_languages)); $cache_keys[] = md5(serialize($rewrite)); $cache_key = implode(':', $cache_keys); $cache_group = 'all_rewrite_rules'; $cache_found = false; $final_rules = wp_cache_get($cache_key, $cache_group, false, $cache_found); if ($cache_found) { return $final_rules; } $final_rules = $rewrite; foreach ($active_languages as $next_language) { if ($next_language['code'] == $default_language) { continue; } $sitepress->switch_lang($next_language['code']); $translated_rules = WPML_Slug_Translation::rewrite_rules_filter($final_rules); if (is_array($translated_rules) && is_array($final_rules)) { $new_rules = array_diff_assoc($translated_rules, $final_rules); $final_rules = array_merge($new_rules, $final_rules); } } $sitepress->switch_lang($current_language); wp_cache_set($cache_key, $final_rules, $cache_group); return $final_rules; }
public function render($post_type, $custom_post) { $_has_slug = isset($custom_post->rewrite['slug']) && $custom_post->rewrite['slug']; $_on = $this->settings->is_on() && $_has_slug && $this->post_sync_setting->is_sync($post_type); $is_hidden = $_on ? '' : 'hidden'; $_translate = $this->settings->is_translate($post_type); $string_id = null; if ($_has_slug) { list($string_id, $_slug_translations) = WPML_Slug_Translation::get_translations($post_type); if ($this->settings->is_on() && $_translate && !$string_id) { $message = sprintf(__("%s slugs are set to be translated, but they are missing their translation", 'sitepress'), $custom_post->labels->name); ICL_AdminNotifier::displayInstantMessage($message, 'error', 'below-h2', false); } } else { $_slug_translations = false; } if ($_has_slug && $this->settings->is_on()) { ?> <div class="icl_slug_translation_choice <?php echo $is_hidden; ?> "> <p> <label> <input name="translate_slugs[<?php echo $post_type; ?> ][on]" type="checkbox" value="1" <?php checked(1, $_translate, true); ?> /> <?php printf(__('Use different slugs in different languages for %s.', 'sitepress'), $custom_post->labels->name); ?> </label> </p> <table class="js-cpt-slugs <?php if (empty($_translate)) { ?> hidden<?php } ?> "> <?php if ($string_id) { $string = new WPML_ST_String($string_id, $this->wpdb); $string_lang = $string->get_language(); } else { $string_lang = ''; } $string_lang = $string_lang ? $string_lang : $this->sitepress->get_default_language(); $languages = $this->sitepress->get_active_languages(); if (!in_array($string_lang, array_keys($languages))) { $all_languages = $this->sitepress->get_languages(); $languages[$string_lang] = $all_languages[$string_lang]; } $original_slug = WPML_Slug_Translation::get_slug_by_type($post_type); $original_slug = $original_slug ? $original_slug : (isset($custom_post->slug) ? $custom_post->slug : $custom_post->rewrite['slug']); foreach ($languages as $language) { $slug_translation_value = !empty($_slug_translations[$language['code']]['value']) ? $_slug_translations[$language['code']]['value'] : ''; $slug_translation_sample = $original_slug . ' @' . $language['code']; ?> <tr<?php if ($language['code'] == $string_lang) { echo ' style="display:none"'; } ?> > <td> <label for="translate_slugs[<?php echo $post_type; ?> ][langs][<?php echo $language['code']; ?> ]"><?php echo $this->sitepress->get_flag_img($language['code']) . ' ' . $language['display_name']; ?> </label> </td> <td> <input id="translate_slugs[<?php echo $post_type; ?> ][langs][<?php echo $language['code']; ?> ]" class="js-translate-slug" name="translate_slugs[<?php echo $post_type; ?> ][langs][<?php echo $language['code']; ?> ]" type="text" value="<?php echo $slug_translation_value; ?> " placeholder="<?php echo $slug_translation_sample; ?> " data-lang="<?php echo $language['code']; ?> " /> <?php if (isset($_slug_translations[$language['code']]) && $_slug_translations[$language['code']]['status'] != ICL_TM_COMPLETE) { ?> <em class="icl_st_slug_tr_warn"><?php _e("Not marked as 'complete'. Press 'Save' to enable.", 'sitepress'); ?> </em> <?php } ?> </td> </tr> <?php if ($language['code'] == $string_lang) { ?> <tr> <td> <?php $lang_selector = new WPML_Simple_Language_Selector($this->sitepress); $lang_selector->render(array('name' => 'translate_slugs[' . $post_type . '][original]', 'selected' => $language['code'], 'show_please_select' => false, 'echo' => true, 'class' => 'js-translate-slug-original', 'data' => array('slug' => $post_type))); ?> <label for="translate_slugs[<?php echo $post_type; ?> ][langs][<?php echo $language['code']; ?> ]"> <em><?php _e("(original)", 'sitepress'); ?> </em></label> </td> <td><input disabled="disabled" class="disabled" id="translate_slugs[<?php echo $post_type; ?> ][langs][<?php echo $language['code']; ?> ]" name="translate_slugs[<?php echo $post_type; ?> ][langs][<?php echo $language['code']; ?> ]" type="text" value="<?php echo $original_slug; ?> "/> </td> </tr> <?php } } ?> </table> </div> <?php } }
/** * Filters post type archive link if slugs translated. * * @TODO WPML do not filter archive link if post type slugs are translated * * @param type $link * @param type $post_type */ public function post_type_archive_link($link, $post_type) { global $sitepress, $sitepress_settings, $wp_rewrite; $translate = !empty($sitepress_settings['posts_slug_translation']['types'][$post_type]); if ($translate && class_exists('WPML_Slug_Translation')) { $post_type_obj = get_post_type_object($post_type); $translated_slug = WPML_Slug_Translation::get_translated_slug($post_type, $sitepress->get_current_language()); if (get_option('permalink_structure') && is_array($post_type_obj->rewrite)) { $struct = true === $post_type_obj->has_archive ? $translated_slug : $post_type_obj->has_archive; if ($post_type_obj->rewrite['with_front']) { $struct = $wp_rewrite->front . $struct; } else { $struct = $wp_rewrite->root . $struct; } $link = home_url(user_trailingslashit($struct, 'post_type_archive')); } else { $link = home_url('?post_type=' . $translated_slug); } } return $link; }
?> </p> <?php if (defined('WPML_ST_VERSION')) { ?> <?php $_has_slug = isset($custom_post->rewrite['slug']) && $custom_post->rewrite['slug']; $_on = $sitepress_settings['posts_slug_translation']['on'] && $_has_slug && isset($sitepress_settings['custom_posts_sync_option'][$k]) && $sitepress_settings['custom_posts_sync_option'][$k] == 1; $is_hidden = $_on ? '' : 'hidden'; $_translate = !empty($sitepress_settings['posts_slug_translation']['types'][$k]); if ($_has_slug) { $string_id = null; $_slug_translations = false; if (class_exists('WPML_Slug_Translation')) { list($string_id, $_slug_translations) = WPML_Slug_Translation::get_translations(trim($custom_post->rewrite['slug'], '/')); } if ($sitepress_settings['posts_slug_translation']['on'] && $_translate && !$string_id) { $message = sprintf(__("%s slugs are set to be translated, but they are missing their translation", 'sitepress'), $custom_post->labels->name); ICL_AdminNotifier::displayInstantMessage($message, 'error', 'below-h2', false); } } else { $_slug_translations = false; } ?> <?php if ($_has_slug && isset($sitepress_settings['posts_slug_translation']['on']) && $sitepress_settings['posts_slug_translation']['on']) { ?> <div class="icl_slug_translation_choice <?php echo $is_hidden; ?>