| 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 |
); |
| 27 |
|
| 28 |
if ($type != 'menu') |
| 29 |
$options['dropdown'] = array('string' => __('Displays as dropdown', 'polylang'), 'default' => 0); |
| 30 |
|
| 31 |
return array_map(create_function('$v', "return \$v['$key'];"), $options); |
| 32 |
} |
| 33 |
|
| 34 |
/* |
| 35 |
* get the language elements for use in a walker |
| 36 |
* |
| 37 |
* list of parameters accepted in $args: |
| 38 |
* @see PLL_Switcher::the_languages |
| 39 |
* |
| 40 |
* @since 1.2 |
| 41 |
* |
| 42 |
* @param object $links instance of PLL_Frontend_Links |
| 43 |
* @param array $args |
| 44 |
* @return array |
| 45 |
*/ |
| 46 |
protected function get_elements($links, $args) { |
| 47 |
extract($args); |
| 48 |
|
| 49 |
foreach ($links->model->get_languages_list(array('hide_empty' => $hide_if_empty)) as $language) { |
| 50 |
$id = (int) $language->term_id; |
| 51 |
$slug = $language->slug; |
| 52 |
$classes = array('lang-item', 'lang-item-' . esc_attr($id), 'lang-item-' . esc_attr($slug)); |
| 53 |
|
| 54 |
if (pll_current_language() == $slug) { |
| 55 |
if ($hide_current) |
| 56 |
continue; // hide current language |
| 57 |
else |
| 58 |
$classes[] = 'current-lang'; |
| 59 |
} |
| 60 |
|
| 61 |
$url = $post_id !== null && ($tr_id = $links->model->get_post($post_id, $language)) ? get_permalink($tr_id) : |
| 62 |
($post_id === null && !$force_home ? $links->get_translation_url($language) : null); |
| 63 |
|
| 64 |
if (empty($url)) |
| 65 |
$classes[] = 'no-translation'; |
| 66 |
|
| 67 |
$url = apply_filters('pll_the_language_link', $url, $slug, $language->locale); |
| 68 |
|
| 69 |
// hide if no translation exists |
| 70 |
if (empty($url) && $hide_if_no_translation) |
| 71 |
continue; |
| 72 |
|
| 73 |
$url = empty($url) ? $links->get_home_url($language) : $url ; // if the page is not translated, link to the home page |
| 74 |
|
| 75 |
$name = $show_names || !$show_flags || $raw ? ($display_names_as == 'slug' ? $slug : $language->name) : ''; |
| 76 |
$flag = $raw && !$show_flags ? $language->flag_url : ($show_flags ? $language->flag : ''); |
| 77 |
|
| 78 |
$out[] = compact('id', 'slug', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes'); |
| 79 |
} |
| 80 |
return empty($out) ? array() : $out; |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
* displays a language switcher |
| 86 |
* or returns the raw elements to build a custom language switcher |
| 87 |
* |
| 88 |
* list of parameters accepted in $args: |
| 89 |
* |
| 90 |
* dropdown => the list is displayed as dropdown if set to 1, defaults to 0 |
| 91 |
* echo => echoes the list if set to 1, defaults to 1 |
| 92 |
* hide_if_empty => hides languages with no posts (or pages) if set to 1, defaults to 1 |
| 93 |
* show_flags => displays flags if set to 1, defaults to 0 |
| 94 |
* show_names => show language names if set to 1, defaults to 1 |
| 95 |
* display_names_as => wether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name |
| 96 |
* force_home => will always link to home in translated language if set to 1, defaults to 0 |
| 97 |
* hide_if_no_translation => hide the link if there is no translation if set to 1, defaults to 0 |
| 98 |
* hide_current => hide the current language if set to 1, defaults to 0 |
| 99 |
* post_id => returns links to translations of post defined by post_id if set, defaults not set |
| 100 |
* raw => return a raw array instead of html markup if set to 1, defaults to 0 |
| 101 |
* |
| 102 |
* @since 0.1 |
| 103 |
* |
| 104 |
* @param object $links instance of PLL_Frontend_Links |
| 105 |
* @param array $args |
| 106 |
* @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher |
| 107 |
*/ |
| 108 |
public function the_languages($links, $args = '') { |
| 109 |
$defaults = array( |
| 110 |
'dropdown' => 0, // display as list and not as dropdown |
| 111 |
'echo' => 1, // echoes the list |
| 112 |
'hide_if_empty' => 1, // hides languages with no posts (or pages) |
| 113 |
'menu' => 0, // not for nav menu (this argument is deprecated since v1.1.1) |
| 114 |
'show_flags' => 0, // don't show flags |
| 115 |
'show_names' => 1, // show language names |
| 116 |
'display_names_as' => 'name', // valid options are slug and name |
| 117 |
'force_home' => 0, // tries to find a translation |
| 118 |
'hide_if_no_translation' => 0, // don't hide the link if there is no translation |
| 119 |
'hide_current' => 0, // don't hide current language |
| 120 |
'post_id' => null, // if not null, link to translations of post defined by post_id |
| 121 |
'raw' => 0, // set this to true to build your own custom language switcher |
| 122 |
); |
| 123 |
$args = wp_parse_args($args, $defaults); |
| 124 |
$elements = $this->get_elements($links, $args); |
| 125 |
|
| 126 |
if ($args['raw']) |
| 127 |
return $elements; |
| 128 |
|
| 129 |
if ($args['dropdown']) { |
| 130 |
$walker = new PLL_Walker_Dropdown(); |
| 131 |
$args['selected'] = pll_current_language(); |
| 132 |
} |
| 133 |
else |
| 134 |
$walker = new PLL_Walker_List(); |
| 135 |
|
| 136 |
$out = apply_filters('pll_the_languages', $walker->walk($elements, $args), $args); |
| 137 |
|
| 138 |
if ($args['echo']) |
| 139 |
echo $out; |
| 140 |
return $out; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/* |
| 145 |
* displays a language list |
| 146 |
* |
| 147 |
* @since 1.2 |
| 148 |
*/ |
| 149 |
class PLL_Walker_List extends Walker { |
| 150 |
var $db_fields = array ('parent' => 'parent', 'id' => 'id'); |
| 151 |
|
| 152 |
/* |
| 153 |
* outputs one element |
| 154 |
* |
| 155 |
* @since 1.2 |
| 156 |
* |
| 157 |
* @see Walker::start_el |
| 158 |
*/ |
| 159 |
function start_el( &$output, $element, $depth = 0, $args = array(), $current_object_id = 0 ) { |
| 160 |
$output .= sprintf( |
| 161 |
"\t".'<li class="%s"><a hreflang="%s" href="%s">%s</a></li>'."\n", |
| 162 |
implode(' ', $element->classes), |
| 163 |
esc_attr($element->slug), |
| 164 |
esc_url($element->url), |
| 165 |
$args['show_flags'] && $args['show_names'] ? $element->flag.' '.esc_html($element->name) : $element->flag.esc_html($element->name) |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
/* |
| 170 |
* overrides Walker::display_element as it expects an object with a parent property |
| 171 |
* |
| 172 |
* @since 1.2 |
| 173 |
* |
| 174 |
* @see Walker::display_element |
| 175 |
*/ |
| 176 |
function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) { |
| 177 |
$element = (object) $element; // make sure we have an object |
| 178 |
$element->parent = $element->id = 0; // don't care about this |
| 179 |
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
| 180 |
} |
| 181 |
|
| 182 |
/* |
| 183 |
* overrides Walker:walk to set depth argument |
| 184 |
* |
| 185 |
* @since 1.2 |
| 186 |
* |
| 187 |
* @param array $elements elements to display |
| 188 |
* @param array $args |
| 189 |
* @return string |
| 190 |
*/ |
| 191 |
function walk($elements, $args = array()) { |
| 192 |
return parent::walk($elements, -1, $args); |
| 193 |
} |
| 194 |
} |
| 195 |
|