| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* a class to display a language switcher on frontend |
| 5 |
* |
| 6 |
* @since 1.2 |
| 7 |
*/ |
| 8 |
class PLL_Switcher { |
| 9 |
|
| 10 |
/* |
| 11 |
* returns options available for the language switcher - menu or widget |
| 12 |
* either strings to display the options or default values |
| 13 |
* |
| 14 |
* @since 0.7 |
| 15 |
* |
| 16 |
* @param string $type optional either 'menu' or 'widget', defaults to 'widget' |
| 17 |
* @param string $key optional either 'string' or 'default', defaults to 'string' |
| 18 |
* @return array list of switcher options srings or default values |
| 19 |
*/ |
| 20 |
static public function get_switcher_options($type = 'widget', $key ='string') { |
| 21 |
$options = array( |
| 22 |
'show_names' => array('string' => __('Displays language names', 'polylang'), 'default' => 1), |
| 23 |
'show_flags' => array('string' => __('Displays flags', 'polylang'), 'default' => 0), |
| 24 |
'force_home' => array('string' => __('Forces link to front page', 'polylang'), 'default' => 0), |
| 25 |
'hide_current' => array('string' => __('Hides the current language', 'polylang'), 'default' => 0), |
| 26 |
'hide_if_no_translation' => array('string' => __('Hides languages with no translation', 'polylang'), 'default' => 0), |
| 27 |
); |
| 28 |
|
| 29 |
if ($type != 'menu') |
| 30 |
$options = array('dropdown' => array('string' => __('Displays as dropdown', 'polylang'), 'default' => 0)) + $options; |
| 31 |
|
| 32 |
return array_map(create_function('$v', "return \$v['$key'];"), $options); |
| 33 |
} |
| 34 |
|
| 35 |
/* |
| 36 |
* get the language elements for use in a walker |
| 37 |
* |
| 38 |
* list of parameters accepted in $args: |
| 39 |
* @see PLL_Switcher::the_languages |
| 40 |
* |
| 41 |
* @since 1.2 |
| 42 |
* |
| 43 |
* @param object $links instance of PLL_Frontend_Links |
| 44 |
* @param array $args |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
protected function get_elements($links, $args) { |
| 48 |
foreach ($links->model->get_languages_list(array('hide_empty' => $args['hide_if_empty'])) as $language) { |
| 49 |
$id = (int) $language->term_id; |
| 50 |
$slug = $language->slug; |
| 51 |
$classes = array('lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr($slug)); |
| 52 |
|
| 53 |
if ($current_lang = pll_current_language() == $slug) { |
| 54 |
if ($args['hide_current'] && !$args['dropdown']) |
| 55 |
continue; // hide current language except for dropdown |
| 56 |
else |
| 57 |
$classes[] = 'current-lang'; |
| 58 |
} |
| 59 |
|
| 60 |
$url = $args['post_id'] !== null && ($tr_id = $links->model->get_post($args['post_id'], $language)) && $links->current_user_can_read($tr_id) ? get_permalink($tr_id) : |
| 61 |
($args['post_id'] === null && !$args['force_home'] ? $links->get_translation_url($language) : null); |
| 62 |
|
| 63 |
if ($no_translation = empty($url)) |
| 64 |
$classes[] = 'no-translation'; |
| 65 |
|
| 66 |
$url = apply_filters('pll_the_language_link', $url, $slug, $language->locale); |
| 67 |
|
| 68 |
// hide if no translation exists |
| 69 |
if (empty($url) && $args['hide_if_no_translation']) |
| 70 |
continue; |
| 71 |
|
| 72 |
$url = empty($url) ? $links->get_home_url($language) : $url ; // if the page is not translated, link to the home page |
| 73 |
|
| 74 |
$name = $args['show_names'] || !$args['show_flags'] || $args['raw'] ? ($args['display_names_as'] == 'slug' ? $slug : $language->name) : ''; |
| 75 |
$flag = $args['raw'] && !$args['show_flags'] ? $language->flag_url : ($args['show_flags'] ? $language->flag : ''); |
| 76 |
|
| 77 |
$out[] = compact('id', 'slug', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes'); |
| 78 |
} |
| 79 |
return empty($out) ? array() : $out; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
/* |
| 84 |
* displays a language switcher |
| 85 |
* or returns the raw elements to build a custom language switcher |
| 86 |
* |
| 87 |
* list of parameters accepted in $args: |
| 88 |
* |
| 89 |
* dropdown => the list is displayed as dropdown if set, defaults to 0 |
| 90 |
* echo => echoes the list if set to 1, defaults to 1 |
| 91 |
* hide_if_empty => hides languages with no posts (or pages) if set to 1, defaults to 1 |
| 92 |
* show_flags => displays flags if set to 1, defaults to 0 |
| 93 |
* show_names => show language names if set to 1, defaults to 1 |
| 94 |
* display_names_as => wether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name |
| 95 |
* force_home => will always link to home in translated language if set to 1, defaults to 0 |
| 96 |
* hide_if_no_translation => hide the link if there is no translation if set to 1, defaults to 0 |
| 97 |
* hide_current => hide the current language if set to 1, defaults to 0 |
| 98 |
* post_id => returns links to translations of post defined by post_id if set, defaults not set |
| 99 |
* raw => return a raw array instead of html markup if set to 1, defaults to 0 |
| 100 |
* |
| 101 |
* @since 0.1 |
| 102 |
* |
| 103 |
* @param object $links instance of PLL_Frontend_Links |
| 104 |
* @param array $args |
| 105 |
* @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher |
| 106 |
*/ |
| 107 |
public function the_languages($links, $args = '') { |
| 108 |
$defaults = array( |
| 109 |
'dropdown' => 0, // display as list and not as dropdown |
| 110 |
'echo' => 1, // echoes the list |
| 111 |
'hide_if_empty' => 1, // hides languages with no posts (or pages) |
| 112 |
'menu' => 0, // not for nav menu (this argument is deprecated since v1.1.1) |
| 113 |
'show_flags' => 0, // don't show flags |
| 114 |
'show_names' => 1, // show language names |
| 115 |
'display_names_as' => 'name', // valid options are slug and name |
| 116 |
'force_home' => 0, // tries to find a translation |
| 117 |
'hide_if_no_translation' => 0, // don't hide the link if there is no translation |
| 118 |
'hide_current' => 0, // don't hide current language |
| 119 |
'post_id' => null, // if not null, link to translations of post defined by post_id |
| 120 |
'raw' => 0, // set this to true to build your own custom language switcher |
| 121 |
); |
| 122 |
$args = wp_parse_args($args, $defaults); |
| 123 |
$args = apply_filters('pll_the_languages_args', $args); |
| 124 |
|
| 125 |
// prevents showing empty options in dropdown |
| 126 |
if ($args['dropdown']) |
| 127 |
$args['show_names'] = 1; |
| 128 |
|
| 129 |
$elements = $this->get_elements($links, $args); |
| 130 |
|
| 131 |
if ($args['raw']) |
| 132 |
return $elements; |
| 133 |
|
| 134 |
if ($args['dropdown']) { |
| 135 |
$args['name'] = 'lang_choice_' . $args['dropdown']; |
| 136 |
$walker = new PLL_Walker_Dropdown(); |
| 137 |
$args['selected'] = pll_current_language(); |
| 138 |
} |
| 139 |
else |
| 140 |
$walker = new PLL_Walker_List(); |
| 141 |
|
| 142 |
$out = apply_filters('pll_the_languages', $walker->walk($elements, $args), $args); |
| 143 |
|
| 144 |
// javascript to switch the language when using a dropdown list |
| 145 |
if ($args['dropdown']) { |
| 146 |
foreach ($links->model->get_languages_list() as $language) { |
| 147 |
$urls[$language->slug] = $args['force_home'] || ($url = $links->get_translation_url($language)) == null ? $links->get_home_url($language) : $url; |
| 148 |
} |
| 149 |
|
| 150 |
// accept only few valid characters for the urls_x variable name (as the widget id includes '-' which is invalid) |
| 151 |
$out .= sprintf(' |
| 152 |
<script type="text/javascript"> |
| 153 |
//<![CDATA[ |
| 154 |
var %1$s = %2$s; |
| 155 |
document.getElementById("%3$s").onchange = function() { |
| 156 |
location.href = %1$s[this.value]; |
| 157 |
} |
| 158 |
//]]> |
| 159 |
</script>', |
| 160 |
'urls_' . preg_replace('#[^a-zA-Z0-9]#', '', $args['dropdown']), wp_json_encode($urls), esc_js($args['name']) |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
if ($args['echo']) |
| 165 |
echo $out; |
| 166 |
return $out; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/* |
| 171 |
* displays a language list |
| 172 |
* |
| 173 |
* @since 1.2 |
| 174 |
*/ |
| 175 |
class PLL_Walker_List extends Walker { |
| 176 |
var $db_fields = array ('parent' => 'parent', 'id' => 'id'); |
| 177 |
|
| 178 |
/* |
| 179 |
* outputs one element |
| 180 |
* |
| 181 |
* @since 1.2 |
| 182 |
* |
| 183 |
* @see Walker::start_el |
| 184 |
*/ |
| 185 |
function start_el( &$output, $element, $depth = 0, $args = array(), $current_object_id = 0 ) { |
| 186 |
$output .= sprintf( |
| 187 |
"\t".'<li class="%s"><a hreflang="%s" href="%s">%s</a></li>'."\n", |
| 188 |
implode(' ', $element->classes), |
| 189 |
esc_attr($element->slug), |
| 190 |
esc_url($element->url), |
| 191 |
$args['show_flags'] && $args['show_names'] ? $element->flag.' '.esc_html($element->name) : $element->flag.esc_html($element->name) |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
/* |
| 196 |
* overrides Walker::display_element as it expects an object with a parent property |
| 197 |
* |
| 198 |
* @since 1.2 |
| 199 |
* |
| 200 |
* @see Walker::display_element |
| 201 |
*/ |
| 202 |
function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) { |
| 203 |
$element = (object) $element; // make sure we have an object |
| 204 |
$element->parent = $element->id = 0; // don't care about this |
| 205 |
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
| 206 |
} |
| 207 |
|
| 208 |
/* |
| 209 |
* overrides Walker:walk to set depth argument |
| 210 |
* |
| 211 |
* @since 1.2 |
| 212 |
* |
| 213 |
* @param array $elements elements to display |
| 214 |
* @param array $args |
| 215 |
* @return string |
| 216 |
*/ |
| 217 |
function walk($elements, $args = array()) { |
| 218 |
return parent::walk($elements, -1, $args); |
| 219 |
} |
| 220 |
} |
| 221 |
|