| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * A class to display a language switcher on frontend |
| 8 | 5 | * |
| @@ -15,15 +12,15 @@ | ||
| 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' | |
| 16 | + * @param string $type optional either 'menu' or 'widget', defaults to 'widget' | |
| 20 | 17 | * @param string $key optional either 'string' or 'default', defaults to 'string' |
| 21 | 18 | * @return array list of switcher options strings or default values |
| 22 | 19 | */ |
| 23 | 20 | public static function get_switcher_options( $type = 'widget', $key = 'string' ) { |
| 24 | 21 | $options = array( |
| 25 | - 'dropdown' => array( 'string' => __( 'Displays as a dropdown', 'polylang' ), 'default' => 0 ), | |
| 22 | + 'dropdown' => array( 'string' => __( 'Displays as dropdown', 'polylang' ), 'default' => 0 ), | |
| 26 | 23 | 'show_names' => array( 'string' => __( 'Displays language names', 'polylang' ), 'default' => 1 ), |
| 27 | 24 | 'show_flags' => array( 'string' => __( 'Displays flags', 'polylang' ), 'default' => 0 ), |
| 28 | 25 | 'force_home' => array( 'string' => __( 'Forces link to front page', 'polylang' ), 'default' => 0 ), |
| 29 | 26 | 'hide_current' => array( 'string' => __( 'Hides the current language', 'polylang' ), 'default' => 0 ), |
| @@ -28,8 +25,9 @@ | ||
| 28 | 25 | 'force_home' => array( 'string' => __( 'Forces link to front page', 'polylang' ), 'default' => 0 ), |
| 29 | 26 | 'hide_current' => array( 'string' => __( 'Hides the current language', 'polylang' ), 'default' => 0 ), |
| 30 | 27 | 'hide_if_no_translation' => array( 'string' => __( 'Hides languages with no translation', 'polylang' ), 'default' => 0 ), |
| 31 | 28 | ); |
| 29 | + | |
| 32 | 30 | return wp_list_pluck( $options, $key ); |
| 33 | 31 | } |
| 34 | 32 | |
| 35 | 33 | /** |
| @@ -43,10 +41,10 @@ | ||
| 43 | 41 | * @param array $args |
| 44 | 42 | * @return array |
| 45 | 43 | */ |
| 46 | 44 | protected function get_elements( $links, $args ) { |
| 45 | + | |
| 47 | 46 | $first = true; |
| 48 | - $out = array(); | |
| 49 | 47 | |
| 50 | 48 | foreach ( $links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) { |
| 51 | 49 | $id = (int) $language->term_id; |
| 52 | 50 | $order = (int) $language->term_group; |
| @@ -53,22 +51,26 @@ | ||
| 53 | 51 | $slug = $language->slug; |
| 54 | 52 | $locale = $language->get_locale( 'display' ); |
| 55 | 53 | $classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) ); |
| 56 | 54 | $url = null; // Avoids potential notice |
| 57 | - $curlang = 0 === $args['admin_render'] ? $links->curlang->slug : $args['admin_current_lang']; | |
| 58 | - $current_lang = $curlang == $slug; | |
| 59 | 55 | |
| 60 | - if ( $current_lang ) { | |
| 56 | + if ( $first ) { | |
| 57 | + $classes[] = 'lang-item-first'; | |
| 58 | + $first = false; | |
| 59 | + } | |
| 60 | + | |
| 61 | + if ( $current_lang = $links->curlang->slug == $slug ) { | |
| 61 | 62 | if ( $args['hide_current'] && ! ( $args['dropdown'] && ! $args['raw'] ) ) { |
| 62 | 63 | continue; // Hide current language except for dropdown |
| 63 | - } else { | |
| 64 | + } | |
| 65 | + else { | |
| 64 | 66 | $classes[] = 'current-lang'; |
| 65 | 67 | } |
| 66 | 68 | } |
| 67 | 69 | |
| 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 ) ) { | |
| 70 | + if ( null !== $args['post_id'] && ( $tr_id = $links->model->post->get( $args['post_id'], $language ) ) && $links->current_user_can_read( $tr_id ) ) { | |
| 69 | 71 | $url = get_permalink( $tr_id ); |
| 70 | - } elseif ( null === $args['post_id'] && 0 === $args['admin_render'] ) { | |
| 72 | + } elseif ( null === $args['post_id'] ) { | |
| 71 | 73 | $url = $links->get_translation_url( $language ); |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | 76 | if ( $no_translation = empty( $url ) ) { |
| @@ -93,19 +95,14 @@ | ||
| 93 | 95 | |
| 94 | 96 | $url = empty( $url ) || $args['force_home'] ? $links->get_home_url( $language ) : $url; // If the page is not translated, link to the home page |
| 95 | 97 | |
| 96 | 98 | $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() : '' ); | |
| 99 | + $flag = $args['raw'] && ! $args['show_flags'] ? $language->flag_url : ( $args['show_flags'] ? $language->flag : '' ); | |
| 98 | 100 | |
| 99 | - if ( $first ) { | |
| 100 | - $classes[] = 'lang-item-first'; | |
| 101 | - $first = false; | |
| 102 | - } | |
| 103 | - | |
| 104 | 101 | $out[ $slug ] = compact( 'id', 'order', 'slug', 'locale', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes' ); |
| 105 | 102 | } |
| 106 | 103 | |
| 107 | - return $out; | |
| 104 | + return empty( $out ) ? array() : $out; | |
| 108 | 105 | } |
| 109 | 106 | |
| 110 | 107 | /** |
| 111 | 108 | * Displays a language switcher |
| @@ -124,10 +121,8 @@ | ||
| 124 | 121 | * hide_current => hide the current language if set to 1, defaults to 0 |
| 125 | 122 | * post_id => returns links to translations of post defined by post_id if set, defaults not set |
| 126 | 123 | * raw => return a raw array instead of html markup if set to 1, defaults to 0 |
| 127 | 124 | * 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 | 125 | * |
| 131 | 126 | * @since 0.1 |
| 132 | 127 | * |
| 133 | 128 | * @param object $links instance of PLL_Frontend_Links |
| @@ -148,10 +143,8 @@ | ||
| 148 | 143 | 'hide_current' => 0, // don't hide current language |
| 149 | 144 | 'post_id' => null, // if not null, link to translations of post defined by post_id |
| 150 | 145 | 'raw' => 0, // set this to true to build your own custom language switcher |
| 151 | 146 | '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 | 147 | ); |
| 155 | 148 | $args = wp_parse_args( $args, $defaults ); |
| 156 | 149 | |
| 157 | 150 | /** |
| @@ -176,9 +169,9 @@ | ||
| 176 | 169 | |
| 177 | 170 | if ( $args['dropdown'] ) { |
| 178 | 171 | $args['name'] = 'lang_choice_' . $args['dropdown']; |
| 179 | 172 | $walker = new PLL_Walker_Dropdown(); |
| 180 | - $args['selected'] = 0 === $args['admin_render'] ? $links->curlang->slug : $args['admin_current_lang']; | |
| 173 | + $args['selected'] = $links->curlang->slug; | |
| 181 | 174 | } |
| 182 | 175 | else { |
| 183 | 176 | $walker = new PLL_Walker_List(); |
| 184 | 177 | } |
| @@ -190,12 +183,17 @@ | ||
| 190 | 183 | * |
| 191 | 184 | * @param string $html html returned/outputted by the template tag |
| 192 | 185 | * @param array $args arguments passed to the template tag |
| 193 | 186 | */ |
| 194 | - $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, -1, $args ), $args ); | |
| 187 | + $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, $args ), $args ); | |
| 195 | 188 | |
| 196 | 189 | // Javascript to switch the language when using a dropdown list |
| 197 | - if ( $args['dropdown'] && 0 === $args['admin_render'] ) { | |
| 190 | + if ( $args['dropdown'] ) { | |
| 191 | + foreach ( $links->model->get_languages_list() as $language ) { | |
| 192 | + $url = $links->get_translation_url( $language ); | |
| 193 | + $urls[ $language->slug ] = $args['force_home'] || empty( $url ) ? $links->get_home_url( $language ) : $url; | |
| 194 | + } | |
| 195 | + | |
| 198 | 196 | // Accept only few valid characters for the urls_x variable name ( as the widget id includes '-' which is invalid ) |
| 199 | 197 | $out .= sprintf( |
| 200 | 198 | '<script type="text/javascript"> |
| 201 | 199 | //<![CDATA[ |
| @@ -205,15 +203,15 @@ | ||
| 205 | 203 | } |
| 206 | 204 | //]]> |
| 207 | 205 | </script>', |
| 208 | 206 | 'urls_' . preg_replace( '#[^a-zA-Z0-9]#', '', $args['dropdown'] ), |
| 209 | - wp_json_encode( wp_list_pluck( $elements, 'url' ) ), | |
| 207 | + json_encode( $urls ), | |
| 210 | 208 | esc_js( $args['name'] ) |
| 211 | 209 | ); |
| 212 | 210 | } |
| 213 | 211 | |
| 214 | 212 | if ( $args['echo'] ) { |
| 215 | - echo $out; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 213 | + echo $out; | |
| 216 | 214 | } |
| 217 | 215 | return $out; |
| 218 | 216 | } |
| 219 | 217 | } |