| @@ -188,24 +188,39 @@ | ||
| 188 | 188 | } |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | function bogo_language_switcher( $args = '' ) { |
| 192 | - $args = wp_parse_args( $args, array( | |
| 193 | - 'echo' => false ) ); | |
| 192 | + global $wp_query; | |
| 194 | 193 | |
| 195 | - $links = bogo_language_switcher_links( $args ); | |
| 196 | - $total = count( $links ); | |
| 194 | + $defaults = array( | |
| 195 | + 'echo' => false ); | |
| 196 | + | |
| 197 | + $args = wp_parse_args( $args, $defaults ); | |
| 198 | + | |
| 199 | + $locale = get_locale(); | |
| 200 | + $available_languages = bogo_available_languages(); | |
| 201 | + | |
| 202 | + $translations = array(); | |
| 203 | + $is_singular = false; | |
| 204 | + $post_id = get_queried_object_id(); | |
| 205 | + | |
| 206 | + if ( is_singular() || ! empty( $wp_query->is_posts_page ) ) { | |
| 207 | + $translations = bogo_get_post_translations( $post_id ); | |
| 208 | + $is_singular = true; | |
| 209 | + } | |
| 210 | + | |
| 211 | + $output = '<ul class="bogo-language-switcher">'; | |
| 212 | + | |
| 213 | + $total = count( $available_languages ); | |
| 197 | 214 | $count = 0; |
| 198 | 215 | |
| 199 | - $output = ''; | |
| 200 | - | |
| 201 | - foreach ( $links as $link ) { | |
| 216 | + foreach ( $available_languages as $code => $name ) { | |
| 202 | 217 | $count += 1; |
| 203 | 218 | $class = array(); |
| 204 | - $class[] = bogo_language_tag( $link['locale'] ); | |
| 205 | - $class[] = bogo_lang_slug( $link['locale'] ); | |
| 219 | + $class[] = bogo_language_tag( $code ); | |
| 220 | + $class[] = bogo_lang_slug( $code ); | |
| 206 | 221 | |
| 207 | - if ( get_locale() == $link['locale'] ) { | |
| 222 | + if ( $locale == $code ) { | |
| 208 | 223 | $class[] = 'current'; |
| 209 | 224 | } |
| 210 | 225 | |
| 211 | 226 | if ( 1 == $count ) { |
| @@ -217,71 +232,34 @@ | ||
| 217 | 232 | } |
| 218 | 233 | |
| 219 | 234 | $class = implode( ' ', array_unique( $class ) ); |
| 220 | 235 | |
| 221 | - if ( empty( $link['href'] ) ) { | |
| 222 | - $li = esc_html( $link['title'] ); | |
| 236 | + $output .= '<li class="' . esc_attr( $class ) . '">'; | |
| 237 | + | |
| 238 | + if ( $is_singular ) { | |
| 239 | + if ( empty( $translations[$code] ) || $locale == $code ) { | |
| 240 | + $output .= esc_html( $name ); | |
| 241 | + } else { | |
| 242 | + $output .= '<a rel="alternate" hreflang="' . bogo_language_tag( $code ) . '" href="' . get_permalink( $translations[$code] ) . '">' . esc_html( $name ) . '</a>'; | |
| 243 | + } | |
| 223 | 244 | } else { |
| 224 | - $li = sprintf( | |
| 225 | - '<a rel="alternate" hreflang="%1$s" href="%2$s">%3$s</a>', | |
| 226 | - $link['lang'], esc_url( $link['href'] ), esc_html( $link['title'] ) ); | |
| 245 | + if ( $locale == $code ) { | |
| 246 | + $output .= esc_html( $name ); | |
| 247 | + } else { | |
| 248 | + $output .= '<a rel="alternate" hreflang="' . bogo_language_tag( $code ) . '" href="' . esc_url( bogo_url( null, $code ) ) . '">' . esc_html( $name ) . '</a>'; | |
| 249 | + } | |
| 227 | 250 | } |
| 228 | 251 | |
| 229 | - $li = sprintf( '<li class="%1$s">%2$s</li>', $class, $li ); | |
| 230 | - | |
| 231 | - $output .= $li . "\n"; | |
| 252 | + $output .= '</li>'; | |
| 232 | 253 | } |
| 233 | 254 | |
| 234 | - $output = '<ul class="bogo-language-switcher">' . $output . '</ul>' . "\n"; | |
| 255 | + $output .= '</ul>' . "\n"; | |
| 235 | 256 | |
| 236 | - $output = apply_filters( 'bogo_language_switcher', $output, $args ); | |
| 237 | - | |
| 238 | 257 | if ( $args['echo'] ) { |
| 239 | 258 | echo $output; |
| 240 | 259 | } else { |
| 241 | 260 | return $output; |
| 242 | 261 | } |
| 243 | -} | |
| 244 | - | |
| 245 | -function bogo_language_switcher_links( $args = '' ) { | |
| 246 | - global $wp_query; | |
| 247 | - | |
| 248 | - $args = wp_parse_args( $args, array() ); | |
| 249 | - | |
| 250 | - $locale = get_locale(); | |
| 251 | - $available_languages = bogo_available_languages(); | |
| 252 | - | |
| 253 | - $translations = array(); | |
| 254 | - $is_singular = false; | |
| 255 | - | |
| 256 | - if ( is_singular() || ! empty( $wp_query->is_posts_page ) ) { | |
| 257 | - $translations = bogo_get_post_translations( get_queried_object_id() ); | |
| 258 | - $is_singular = true; | |
| 259 | - } | |
| 260 | - | |
| 261 | - $links = array(); | |
| 262 | - | |
| 263 | - foreach ( $available_languages as $code => $name ) { | |
| 264 | - $link = array( | |
| 265 | - 'locale' => $code, | |
| 266 | - 'lang' => bogo_language_tag( $code ), | |
| 267 | - 'title' => $name, | |
| 268 | - 'href' => '' ); | |
| 269 | - | |
| 270 | - if ( $is_singular ) { | |
| 271 | - if ( $locale != $code && ! empty( $translations[$code] ) ) { | |
| 272 | - $link['href'] = get_permalink( $translations[$code] ); | |
| 273 | - } | |
| 274 | - } else { | |
| 275 | - if ( $locale != $code ) { | |
| 276 | - $link['href'] = bogo_url( null, $code ); | |
| 277 | - } | |
| 278 | - } | |
| 279 | - | |
| 280 | - $links[] = $link; | |
| 281 | - } | |
| 282 | - | |
| 283 | - return apply_filters( 'bogo_language_switcher_links', $links, $args ); | |
| 284 | 262 | } |
| 285 | 263 | |
| 286 | 264 | add_filter( 'get_previous_post_join', 'bogo_adjacent_post_join', 10, 3 ); |
| 287 | 265 | add_filter( 'get_next_post_join', 'bogo_adjacent_post_join', 10, 3 ); |