| 1 |
<?php |
| 2 |
|
| 3 |
add_shortcode( 'bogo', 'bogo_language_switcher' ); |
| 4 |
|
| 5 |
function bogo_language_switcher( $args = '' ) { |
| 6 |
$args = wp_parse_args( $args, array( |
| 7 |
'echo' => false ) ); |
| 8 |
|
| 9 |
$links = bogo_language_switcher_links( $args ); |
| 10 |
$total = count( $links ); |
| 11 |
$count = 0; |
| 12 |
|
| 13 |
$output = ''; |
| 14 |
|
| 15 |
foreach ( $links as $link ) { |
| 16 |
$count += 1; |
| 17 |
$class = array(); |
| 18 |
$class[] = bogo_language_tag( $link['locale'] ); |
| 19 |
$class[] = bogo_lang_slug( $link['locale'] ); |
| 20 |
|
| 21 |
if ( get_locale() == $link['locale'] ) { |
| 22 |
$class[] = 'current'; |
| 23 |
} |
| 24 |
|
| 25 |
if ( 1 == $count ) { |
| 26 |
$class[] = 'first'; |
| 27 |
} |
| 28 |
|
| 29 |
if ( $total == $count ) { |
| 30 |
$class[] = 'last'; |
| 31 |
} |
| 32 |
|
| 33 |
$class = implode( ' ', array_unique( $class ) ); |
| 34 |
|
| 35 |
$label = $link['native_name'] ? $link['native_name'] : $link['title']; |
| 36 |
$title = $link['title']; |
| 37 |
|
| 38 |
if ( empty( $link['href'] ) ) { |
| 39 |
$li = esc_html( $label ); |
| 40 |
} else { |
| 41 |
$li = sprintf( |
| 42 |
'<a rel="alternate" hreflang="%1$s" href="%2$s" title="%3$s">%4$s</a>', |
| 43 |
$link['lang'], |
| 44 |
esc_url( $link['href'] ), |
| 45 |
esc_attr( $title ), |
| 46 |
esc_html( $label ) ); |
| 47 |
} |
| 48 |
|
| 49 |
$li = sprintf( '<li class="%1$s">%2$s</li>', $class, $li ); |
| 50 |
|
| 51 |
$output .= $li . "\n"; |
| 52 |
} |
| 53 |
|
| 54 |
$output = '<ul class="bogo-language-switcher">' . $output . '</ul>' . "\n"; |
| 55 |
|
| 56 |
$output = apply_filters( 'bogo_language_switcher', $output, $args ); |
| 57 |
|
| 58 |
if ( $args['echo'] ) { |
| 59 |
echo $output; |
| 60 |
} else { |
| 61 |
return $output; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
function bogo_language_switcher_links( $args = '' ) { |
| 66 |
global $wp_query; |
| 67 |
|
| 68 |
$args = wp_parse_args( $args, array() ); |
| 69 |
|
| 70 |
$locale = get_locale(); |
| 71 |
$available_languages = bogo_available_languages( array( |
| 72 |
'exclude_enus_if_inactive' => true ) ); |
| 73 |
|
| 74 |
$translations = array(); |
| 75 |
$is_singular = false; |
| 76 |
|
| 77 |
if ( is_singular() || ! empty( $wp_query->is_posts_page ) ) { |
| 78 |
$translations = bogo_get_post_translations( get_queried_object_id() ); |
| 79 |
$is_singular = true; |
| 80 |
} |
| 81 |
|
| 82 |
$links = array(); |
| 83 |
|
| 84 |
foreach ( $available_languages as $code => $name ) { |
| 85 |
$link = array( |
| 86 |
'locale' => $code, |
| 87 |
'lang' => bogo_language_tag( $code ), |
| 88 |
'title' => $name, |
| 89 |
'native_name' => bogo_get_language_native_name( $code ), |
| 90 |
'href' => '' ); |
| 91 |
|
| 92 |
if ( $is_singular ) { |
| 93 |
if ( $locale != $code |
| 94 |
&& ! empty( $translations[$code] ) |
| 95 |
&& 'publish' == get_post_status( $translations[$code] ) ) { |
| 96 |
$link['href'] = get_permalink( $translations[$code] ); |
| 97 |
} |
| 98 |
} else { |
| 99 |
if ( $locale != $code ) { |
| 100 |
$link['href'] = bogo_url( null, $code ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$links[] = $link; |
| 105 |
} |
| 106 |
|
| 107 |
return apply_filters( 'bogo_language_switcher_links', $links, $args ); |
| 108 |
} |
| 109 |
|