PLL_Switcher

A class to display a language switcher on frontend.

Defined (1)

The class is defined in the following location(s).

/include/switcher.php  
  1. class PLL_Switcher { 
  2.  
  3. /** 
  4. * Returns options available for the language switcher - menu or widget 
  5. * either strings to display the options or default values 
  6. * @since 0.7 
  7. * @param string $type optional either 'menu' or 'widget', defaults to 'widget' 
  8. * @param string $key optional either 'string' or 'default', defaults to 'string' 
  9. * @return array list of switcher options srings or default values 
  10. */ 
  11. static public function get_switcher_options( $type = 'widget', $key = 'string' ) { 
  12. $options = array( 
  13. 'dropdown' => array( 'string' => __( 'Displays as dropdown', 'polylang' ), 'default' => 0 ),  
  14. 'show_names' => array( 'string' => __( 'Displays language names', 'polylang' ), 'default' => 1 ),  
  15. 'show_flags' => array( 'string' => __( 'Displays flags', 'polylang' ), 'default' => 0 ),  
  16. 'force_home' => array( 'string' => __( 'Forces link to front page', 'polylang' ), 'default' => 0 ),  
  17. 'hide_current' => array( 'string' => __( 'Hides the current language', 'polylang' ), 'default' => 0 ),  
  18. 'hide_if_no_translation' => array( 'string' => __( 'Hides languages with no translation', 'polylang' ), 'default' => 0 ),  
  19. ); 
  20.  
  21. return wp_list_pluck( $options, $key ); 
  22.  
  23. /** 
  24. * Get the language elements for use in a walker 
  25. * List of parameters accepted in $args: 
  26. * @see PLL_Switcher::the_languages 
  27. * @since 1.2 
  28. * @param object $links instance of PLL_Frontend_Links 
  29. * @param array $args 
  30. * @return array 
  31. */ 
  32. protected function get_elements( $links, $args ) { 
  33.  
  34. $first = true; 
  35.  
  36. foreach ( $links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) { 
  37. $id = (int) $language->term_id; 
  38. $order = (int) $language->term_group; 
  39. $slug = $language->slug; 
  40. $locale = $language->get_locale( 'display' ); 
  41. $classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) ); 
  42. $url = null; // Avoids potential notice 
  43.  
  44. if ( $first ) { 
  45. $classes[] = 'lang-item-first'; 
  46. $first = false; 
  47.  
  48. if ( $current_lang = $links->curlang->slug == $slug ) { 
  49. if ( $args['hide_current'] && ! ( $args['dropdown'] && ! $args['raw'] ) ) { 
  50. continue; // Hide current language except for dropdown 
  51. else { 
  52. $classes[] = 'current-lang'; 
  53.  
  54. if ( null !== $args['post_id'] && ( $tr_id = $links->model->post->get( $args['post_id'], $language ) ) && $links->current_user_can_read( $tr_id ) ) { 
  55. $url = get_permalink( $tr_id ); 
  56. } elseif ( null === $args['post_id'] ) { 
  57. $url = $links->get_translation_url( $language ); 
  58.  
  59. if ( $no_translation = empty( $url ) ) { 
  60. $classes[] = 'no-translation'; 
  61.  
  62. /** 
  63. * Filter the link in the language switcher 
  64. * @since 0.7 
  65. * @param string $url the link 
  66. * @param string $slug language code 
  67. * @param string $locale language locale 
  68. */ 
  69. $url = apply_filters( 'pll_the_language_link', $url, $slug, $language->locale ); 
  70.  
  71. // Hide if no translation exists 
  72. if ( empty( $url ) && $args['hide_if_no_translation'] ) { 
  73. continue; 
  74.  
  75. $url = empty( $url ) || $args['force_home'] ? $links->get_home_url( $language ) : $url ; // If the page is not translated, link to the home page 
  76.  
  77. $name = $args['show_names'] || ! $args['show_flags'] || $args['raw'] ? ( 'slug' == $args['display_names_as'] ? $slug : $language->name ) : ''; 
  78. $flag = $args['raw'] && ! $args['show_flags'] ? $language->flag_url : ( $args['show_flags'] ? $language->flag : '' ); 
  79.  
  80. $out[ $slug ] = compact( 'id', 'order', 'slug', 'locale', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes' ); 
  81.  
  82. return empty( $out ) ? array() : $out; 
  83.  
  84. /** 
  85. * Displays a language switcher 
  86. * or returns the raw elements to build a custom language switcher 
  87. * List of parameters accepted in $args: 
  88. * dropdown => the list is displayed as dropdown if set, defaults to 0 
  89. * echo => echoes the list if set to 1, defaults to 1 
  90. * hide_if_empty => hides languages with no posts ( or pages ) if set to 1, defaults to 1 
  91. * show_flags => displays flags if set to 1, defaults to 0 
  92. * show_names => show language names if set to 1, defaults to 1 
  93. * display_names_as => whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name 
  94. * force_home => will always link to home in translated language if set to 1, defaults to 0 
  95. * hide_if_no_translation => hide the link if there is no translation if set to 1, defaults to 0 
  96. * hide_current => hide the current language if set to 1, defaults to 0 
  97. * post_id => returns links to translations of post defined by post_id if set, defaults not set 
  98. * raw => return a raw array instead of html markup if set to 1, defaults to 0 
  99. * item_spacing => whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to preserve 
  100. * @since 0.1 
  101. * @param object $links instance of PLL_Frontend_Links 
  102. * @param array $args 
  103. * @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher 
  104. */ 
  105. public function the_languages( $links, $args = '' ) { 
  106. $defaults = array( 
  107. 'dropdown' => 0, // display as list and not as dropdown 
  108. 'echo' => 1, // echoes the list 
  109. 'hide_if_empty' => 1, // hides languages with no posts ( or pages ) 
  110. 'menu' => 0, // not for nav menu ( this argument is deprecated since v1.1.1 ) 
  111. 'show_flags' => 0, // don't show flags 
  112. 'show_names' => 1, // show language names 
  113. 'display_names_as' => 'name', // valid options are slug and name 
  114. 'force_home' => 0, // tries to find a translation 
  115. 'hide_if_no_translation' => 0, // don't hide the link if there is no translation 
  116. 'hide_current' => 0, // don't hide current language 
  117. 'post_id' => null, // if not null, link to translations of post defined by post_id 
  118. 'raw' => 0, // set this to true to build your own custom language switcher 
  119. 'item_spacing' => 'preserve', // 'preserve' or 'discard' whitespace between list items 
  120. ); 
  121. $args = wp_parse_args( $args, $defaults ); 
  122.  
  123. /** 
  124. * Filter the arguments of the 'pll_the_languages' template tag 
  125. * @since 1.5 
  126. * @param array $args 
  127. */ 
  128. $args = apply_filters( 'pll_the_languages_args', $args ); 
  129.  
  130. // Prevents showing empty options in dropdown 
  131. if ( $args['dropdown'] ) { 
  132. $args['show_names'] = 1; 
  133.  
  134. $elements = $this->get_elements( $links, $args ); 
  135.  
  136. if ( $args['raw'] ) { 
  137. return $elements; 
  138.  
  139. if ( $args['dropdown'] ) { 
  140. $args['name'] = 'lang_choice_' . $args['dropdown']; 
  141. $walker = new PLL_Walker_Dropdown(); 
  142. $args['selected'] = $links->curlang->slug; 
  143. else { 
  144. $walker = new PLL_Walker_List(); 
  145.  
  146. /** 
  147. * Filter the whole html markup returned by the 'pll_the_languages' template tag 
  148. * @since 0.8 
  149. * @param string $html html returned/outputed by the template tag 
  150. * @param array $args arguments passed to the template tag 
  151. */ 
  152. $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, $args ), $args ); 
  153.  
  154. // Javascript to switch the language when using a dropdown list 
  155. if ( $args['dropdown'] ) { 
  156. foreach ( $links->model->get_languages_list() as $language ) { 
  157. $url = $links->get_translation_url( $language ); 
  158. $urls[ $language->slug ] = $args['force_home'] || empty( $url ) ? $links->get_home_url( $language ) : $url; 
  159.  
  160. // Accept only few valid characters for the urls_x variable name ( as the widget id includes '-' which is invalid ) 
  161. $out .= sprintf( ' 
  162. <script type="text/javascript"> 
  163. //<![CDATA[ 
  164. var %1$s = %2$s; 
  165. document.getElementById( "%3$s" ).onchange = function() { 
  166. location.href = %1$s[this.value]; 
  167. //]]> 
  168. </script>',  
  169. 'urls_' . preg_replace( '#[^a-zA-Z0-9]#', '', $args['dropdown'] ), json_encode( $urls ), esc_js( $args['name'] ) 
  170. ); 
  171.  
  172. if ( $args['echo'] ) { 
  173. echo $out; 
  174. return $out;