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