Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented option to show only available language translations links #329

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions admin/qtx_configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ function qtranxf_conf() {
<br/>
<label for="show_alternative_content"><input type="checkbox" name="show_alternative_content" id="show_alternative_content" value="1"<?php checked($q_config['show_alternative_content']) ?>/> <?php _e('Show content in an alternative language when translation is not available for the selected language.', 'qtranslate') ?></label>
<p class="qtranxs_notes"><?php echo __('If this option is on, then the content of the first available language will also be shown, instead of the expected language, for the sake of user convenience.', 'qtranslate') ?></p>
<br/>
<label for="show_only_available_translations"><input type="checkbox" name="show_only_available_translations" id="show_only_available_translations" value="1"<?php checked($q_config['show_only_available_translations']) ?>/> <?php _e('Show links to available language translations only.', 'qtranslate') ?></label>
<p class="qtranxs_notes"><?php echo __('Posts or pages with available translations are only shown.', 'qtranslate') ?></p>
</td>
</tr>
<tr valign="top">
Expand Down
2 changes: 1 addition & 1 deletion qtranslate_frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function qtranxf_add_language_menu_item(&$items, &$menu_order, &$itemid, $key, $
unset($items[$k]);
}

foreach($q_config['enabled_languages'] as $lang)
foreach(qtranxf_getPageAvailableLanguages() as $lang)
{
if($type=='AL'){
if($lang==$language) continue;
Expand Down
1 change: 1 addition & 0 deletions qtranslate_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function qtranxf_set_default_options(&$ops){
'show_displayed_language_prefix' => true,
'show_alternative_content_message' => true,
'show_alternative_content' => false,
'show_only_available_translations' => false,
'hide_default_language' => true,// hide language tag for default language in urls
'use_secure_cookie' => false,
'header_css_on' => true,
Expand Down
31 changes: 31 additions & 0 deletions qtranslate_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,3 +960,34 @@ function qtranxf_convert_strftime2date($format) {
return $format;
}
}// END DATE TIME FUNCTIONS

function qtranxf_getPageAvailableLanguages(){
global $q_config;
$sortedLanguages = qtranxf_getSortedLanguages();
if (!empty($q_config['show_only_available_translations']) && $q_config['show_only_available_translations']) {
$postId = get_queried_object_id();
$pageLanguages = get_metadata('post', $postId, 'availablePageLanguages', true);
$availablePageLanguages = [];

if (empty($pageLanguages) || $pageLanguages === false) {
return $sortedLanguages;
}

foreach ($sortedLanguages as $language) {
if (in_array($language, $pageLanguages))
$availablePageLanguages[] = $language;
}
return $availablePageLanguages;
}
return $sortedLanguages;
}
function qtranxf_setPageAvailableLanguages($newStatus, $oldStatus, $post) {
$translatedLanguages = qtranxf_getAvailableLanguages($post->post_content);
if ($newStatus == 'publish') {
update_post_meta($post->ID, 'availablePageLanguages', $translatedLanguages);
}
else if ($newStatus == 'trash') {
delete_post_meta($post->ID, 'availablePageLanguages', $translatedLanguages);
}
}
add_action('transition_post_status', 'qtranxf_setPageAvailableLanguages', 10, 3);
4 changes: 2 additions & 2 deletions qtranslate_widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function widget($args, $instance) {
$title=apply_filters('qtranslate_widget_title',$title,$this);
echo $before_title . $title . $after_title;
}
qtranxf_generateLanguageSelectCode($instance,$this->id);
qtranxf_getPageAvailableLanguages($instance,$this->id);
echo $after_widget;
}

Expand Down Expand Up @@ -162,7 +162,7 @@ function qtranxf_generateLanguageSelectCode($args = array(), $id='') {
case 'text':
case 'css_only':
case 'dropdown': {
foreach(qtranxf_getSortedLanguages() as $language) {
foreach(qtranxf_getPageAvailableLanguages() as $language) {
$alt = $q_config['language_name'][$language].' ('.$language.')';
$classes = array('lang-'.$language);
if($language == $q_config['language']) $classes[] = 'active';
Expand Down