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