| 1 |
<?php |
| 2 |
|
| 3 |
function bogo_language_switcher( $args = '' ) { |
| 4 |
$args = wp_parse_args( $args, array( |
| 5 |
'echo' => false, |
| 6 |
) ); |
| 7 |
|
| 8 |
$links = bogo_language_switcher_links( $args ); |
| 9 |
$total = count( $links ); |
| 10 |
$count = 0; |
| 11 |
|
| 12 |
$output = ''; |
| 13 |
|
| 14 |
foreach ( $links as $link ) { |
| 15 |
$count += 1; |
| 16 |
$class = array(); |
| 17 |
$class[] = bogo_language_tag( $link['locale'] ); |
| 18 |
$class[] = bogo_lang_slug( $link['locale'] ); |
| 19 |
|
| 20 |
if ( get_locale() === $link['locale'] ) { |
| 21 |
$class[] = 'current'; |
| 22 |
} |
| 23 |
|
| 24 |
if ( 1 === $count ) { |
| 25 |
$class[] = 'first'; |
| 26 |
} |
| 27 |
|
| 28 |
if ( $total === $count ) { |
| 29 |
$class[] = 'last'; |
| 30 |
} |
| 31 |
|
| 32 |
$class = implode( ' ', array_unique( $class ) ); |
| 33 |
|
| 34 |
$label = $link['native_name'] ? $link['native_name'] : $link['title']; |
| 35 |
$title = $link['title']; |
| 36 |
|
| 37 |
if ( empty( $link['href'] ) ) { |
| 38 |
$li = esc_html( $label ); |
| 39 |
} else { |
| 40 |
$atts = array( |
| 41 |
'rel' => 'alternate', |
| 42 |
'hreflang' => $link['lang'], |
| 43 |
'href' => esc_url( $link['href'] ), |
| 44 |
'title' => $title, |
| 45 |
); |
| 46 |
|
| 47 |
if ( get_locale() === $link['locale'] ) { |
| 48 |
$atts += array( |
| 49 |
'class' => 'current', |
| 50 |
'aria-current' => 'page', |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
$li = sprintf( |
| 55 |
'<a %1$s>%2$s</a>', |
| 56 |
bogo_format_atts( $atts ), |
| 57 |
esc_html( $label ) |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
$li = sprintf( |
| 62 |
'<span class="bogo-language-name">%s</span>', |
| 63 |
$li |
| 64 |
); |
| 65 |
|
| 66 |
if ( apply_filters( 'bogo_use_flags', true ) ) { |
| 67 |
$country_code = bogo_get_country_code( $link['locale'] ); |
| 68 |
|
| 69 |
$flag = sprintf( |
| 70 |
'<span class="bogoflags bogoflags-%s"></span>', |
| 71 |
$country_code ? strtolower( $country_code ) : 'zz' |
| 72 |
); |
| 73 |
|
| 74 |
$li = sprintf( '<li class="%1$s">%3$s %2$s</li>', $class, $li, $flag ); |
| 75 |
} else { |
| 76 |
$li = sprintf( '<li class="%1$s">%2$s</li>', $class, $li ); |
| 77 |
} |
| 78 |
|
| 79 |
$output .= $li . "\n"; |
| 80 |
} |
| 81 |
|
| 82 |
$output = sprintf( |
| 83 |
'<ul class="bogo-language-switcher list-view">%s</ul>', |
| 84 |
$output |
| 85 |
); |
| 86 |
|
| 87 |
$output = apply_filters( 'bogo_language_switcher', $output, $args ); |
| 88 |
|
| 89 |
if ( $args['echo'] ) { |
| 90 |
echo wp_kses_post( $output ); |
| 91 |
} else { |
| 92 |
return $output; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
function bogo_language_suggestion( $args = '' ) { |
| 98 |
$args = wp_parse_args( $args, array( |
| 99 |
'echo' => false, |
| 100 |
) ); |
| 101 |
|
| 102 |
$locale_to_suggest = false; |
| 103 |
|
| 104 |
foreach ( bogo_http_accept_languages() as $locale ) { |
| 105 |
$locale = bogo_get_closest_locale( $locale ); |
| 106 |
|
| 107 |
// A locale is available and it is not the current locale. |
| 108 |
if ( $locale and $locale !== determine_locale() ) { |
| 109 |
$locale_to_suggest = $locale; |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
$translations = array(); |
| 115 |
|
| 116 |
if ( $locale_to_suggest ) { |
| 117 |
$translations = array_filter( |
| 118 |
bogo_language_switcher_links( $args ), |
| 119 |
static function ( $link ) use ( $locale_to_suggest ) { |
| 120 |
return $link['locale'] === $locale_to_suggest && '' !== $link['href']; |
| 121 |
} |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
$output = ''; |
| 126 |
|
| 127 |
if ( $translations and $translation = reset( $translations ) ) { |
| 128 |
switch_to_locale( $locale_to_suggest ); |
| 129 |
|
| 130 |
$lang_name = bogo_get_language( $locale_to_suggest ); |
| 131 |
|
| 132 |
if ( $lang_name ) { |
| 133 |
$lang_name = bogo_get_short_name( $lang_name ); |
| 134 |
} else { |
| 135 |
$lang_name = sprintf( '[%s]', $locale_to_suggest ); |
| 136 |
} |
| 137 |
|
| 138 |
$link = sprintf( |
| 139 |
'<a %1$s>%2$s</a>', |
| 140 |
bogo_format_atts( array( |
| 141 |
'rel' => 'alternate', |
| 142 |
'hreflang' => $translation['lang'], |
| 143 |
'href' => $translation['href'], |
| 144 |
'title' => $translation['title'], |
| 145 |
) ), |
| 146 |
esc_html( $lang_name ) |
| 147 |
); |
| 148 |
|
| 149 |
$output = sprintf( |
| 150 |
/* translators: %s: Language name */ |
| 151 |
esc_html( __( 'This page is also available in %s.', 'bogo' ) ), |
| 152 |
$link |
| 153 |
); |
| 154 |
|
| 155 |
$output = sprintf( |
| 156 |
'<p class="bogo-language-switcher suggestion-view">%s</p>', |
| 157 |
$output |
| 158 |
); |
| 159 |
|
| 160 |
restore_previous_locale(); |
| 161 |
} |
| 162 |
|
| 163 |
$output = apply_filters( 'bogo_language_suggestion', $output, $args ); |
| 164 |
|
| 165 |
if ( $args['echo'] ) { |
| 166 |
echo wp_kses_post( $output ); |
| 167 |
} else { |
| 168 |
return $output; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
function bogo_language_switcher_links( $args = '' ) { |
| 174 |
global $wp_query; |
| 175 |
|
| 176 |
$args = wp_parse_args( $args, array() ); |
| 177 |
|
| 178 |
$locale = get_locale(); |
| 179 |
|
| 180 |
$available_languages = bogo_available_languages(); |
| 181 |
|
| 182 |
$translations = array(); |
| 183 |
$is_singular = false; |
| 184 |
|
| 185 |
if ( is_singular() or ! empty( $wp_query->is_posts_page ) ) { |
| 186 |
$translations = bogo_get_post_translations( get_queried_object_id() ); |
| 187 |
$is_singular = true; |
| 188 |
} |
| 189 |
|
| 190 |
$links = array(); |
| 191 |
|
| 192 |
foreach ( $available_languages as $code => $name ) { |
| 193 |
$native_name = bogo_get_language_native_name( $code ); |
| 194 |
|
| 195 |
if ( bogo_locale_is_alone( $code ) ) { |
| 196 |
$native_name = bogo_get_short_name( $native_name ); |
| 197 |
} |
| 198 |
|
| 199 |
$link = array( |
| 200 |
'locale' => $code, |
| 201 |
'lang' => bogo_language_tag( $code ), |
| 202 |
'title' => $name, |
| 203 |
'native_name' => trim( $native_name ), |
| 204 |
'href' => '', |
| 205 |
); |
| 206 |
|
| 207 |
if ( $is_singular ) { |
| 208 |
if ( $locale === $code ) { |
| 209 |
$link['href'] = get_permalink( get_queried_object_id() ); |
| 210 |
} elseif ( |
| 211 |
! empty( $translations[$code] ) and |
| 212 |
'publish' === get_post_status( $translations[$code] ) |
| 213 |
) { |
| 214 |
$link['href'] = get_permalink( $translations[$code] ); |
| 215 |
} |
| 216 |
} else { |
| 217 |
$link['href'] = bogo_url( null, $code ); |
| 218 |
} |
| 219 |
|
| 220 |
$links[] = $link; |
| 221 |
} |
| 222 |
|
| 223 |
return apply_filters( 'bogo_language_switcher_links', $links, $args ); |
| 224 |
} |
| 225 |
|