-
Notifications
You must be signed in to change notification settings - Fork 36
'lang_code' option for link_type is not handled for Mlp_Widget #112
Description
Mlp_Widget advertises lang_code as an option for link text output.
In the function Mlp_Helpers::show_linked_elements the link_text argument is used only to determine display of the flag image.
In order for lang_code to function as an option, it appears it would need to affect how the language item name is gathered, or another key would need to be set. The name is always gathered as native as shown below.
$items[ $site_id ] = array (
'url' => $url,
'http' => $translation->get_language()->get_name( 'http' ),
'name' => $translation->get_language()->get_name( 'native' ),
'priority' => $translation->get_language()->get_priority(),
'icon' => (string) $translation->get_icon_url()
);Mlp_Language::get_name supports the following values
/**
* Get different possible language names
*
* @param string $name Possible values:
* - 'native' (default) ex: Deutsch for German
* - 'english' English name of the language
* - 'http' ex: 'de-AT'
* - 'language_long' alias for 'http'.
* - 'language_short' first part of 'http', ex: 'de' in 'de-AT'
* - 'lang' alias for 'language_short'
* - 'wp_locale' Identifier for translation files used by WordPress
* - 'custom' Language name set in the site preferences
* - 'text' alias for 'custom'
* @return string
*/
public function get_name( $name = '' ) {Recommendation
While this issue intends to highlight an issue with existing code, I also feel that it should be possible to request any of the valid name formats.
Decoupling flag inclusion and the text format, allowing the user to select each independently, would provide the greatest flexibility.
Workaround
This is not a complete workaround, as it does not provide a functioning widget. The following code could however be used to do so.
Be warned that this code goes beyond current functionality. _Use at your own risk_.
/**
* All code in this workaround belongs to the MultilingualPress
* project and is licensed as per the project.
*
* Please see the following URLs.
* https://github.com/inpsyde/multilingual-press
* https://github.com/inpsyde/multilingual-press/blob/master/license.txt
*/
namespace Workaround\Mlp;
/**
* Helper to sort languages.
*
* @param array $a
* @param array $b
* @return int
*/
function strcasecmp_sort_names( Array $a, Array $b ) {
return strcasecmp( $a['name'], $b['name'] );
}
/**
* Helper to sort languages.
*
* @param array $a
* @param array $b
* @return int
*/
function sort_priorities( Array $a, Array $b ) {
if ( $a['priority'] === $b['priority'] )
return 0;
return ( $a['priority'] < $b['priority'] ) ? 1 : -1;
}
/**
* Get the linked elements and display them as a list
* flag from a blogid
*
* @param array $args
* @return string output of the bloglist
*/
function show_linked_elements( $args ) {
$defaults = array (
'link_text' => 'native',
'display_flag' => FALSE, // flag_only or flag_text
'sort' => 'priority',
'show_current_blog' => FALSE,
'strict' => FALSE // get exact translations only
);
$params = wp_parse_args( $args, $defaults );
/** @var Mlp_Language_Api $api */
$api = apply_filters( 'mlp_language_api', NULL );
if ( ! is_a( $api, 'Mlp_Language_Api_Interface' ) ) {
return '';
}
$translations = $api->get_translations(
array (
'strict' => $params[ 'strict' ],
'include_base' => $params[ 'show_current_blog' ]
)
);
if ( empty ( $translations ) )
return '';
$current_id = get_current_blog_id();
$items = array ();
$output = '<div class="mlp_language_box"><ul>';
/** @var Mlp_Translation_Interface $translation */
foreach ( $translations as $site_id => $translation ) {
$url = $translation->get_remote_url();
if ( empty ( $url ) )
continue;
$items[ $site_id ] = array (
'url' => $url,
'http' => $translation->get_language()->get_name( 'http' ),
'name' => $translation->get_language()->get_name( $params[ 'link_text' ] ),
'priority' => $translation->get_language()->get_priority(),
'icon' => (string) $translation->get_icon_url()
);
}
if ( 'blogid' === $params[ 'sort' ] )
ksort( $items );
elseif ( 'priority' === $params[ 'sort' ] )
uasort( $items, __NAMESPACE__ . '\\sort_priorities' );
elseif ( 'name' === $params[ 'sort' ] )
uasort( $items, __NAMESPACE__ . '\\strcasecmp_sort_names' );
foreach ( $items as $site_id => $item ) {
$text = $item[ 'name' ];
if ( ! empty ( $item[ 'icon'] ) ) {
$img = '<img src="' . $item[ 'icon'] . '" alt="' . esc_attr( $item[ 'name' ] ) . '" />';
if ( 'flag_text' === $params[ 'display_flag' ] )
$text = "$img $text";
elseif ( 'flag_only' === $params[ 'display_flag' ] )
$text = $img;
}
if ( $site_id === $current_id ) {
$output .= sprintf( '<li><a class="current-language-item" href="">%s</a></li>', $text );
}
else {
$output .= sprintf(
'<li><a rel="alternate" hreflang="%1$s" href="%2$s">%3$s</a></li>',
$item[ 'http' ],
$item[ 'url' ],
$text
);
}
}
$output .= '</ul></div>';
return $output;
}Usage Example:
use Workaround\Mlp;
echo Mlp\show_linked_elements(array(
'link_text' => 'language_long',
'display_flag' => 'flag_text',
'show_current_blog' => true
));