| @@ -161,16 +161,15 @@ | ||
| 161 | 161 | 'href' => get_permalink( $translation ) ); |
| 162 | 162 | } |
| 163 | 163 | } |
| 164 | 164 | } else { |
| 165 | - $available_locales = bogo_available_locales(); | |
| 165 | + $available_locales = bogo_available_locales( array( | |
| 166 | + 'exclude_enus_if_inactive' => true ) ); | |
| 166 | 167 | |
| 167 | - if ( 1 < count( $available_locales ) ) { | |
| 168 | - foreach ( $available_locales as $locale ) { | |
| 169 | - $languages[] = array( | |
| 170 | - 'hreflang' => bogo_language_tag( $locale ), | |
| 171 | - 'href' => bogo_url( null, $locale ) ); | |
| 172 | - } | |
| 168 | + foreach ( $available_locales as $locale ) { | |
| 169 | + $languages[] = array( | |
| 170 | + 'hreflang' => bogo_language_tag( $locale ), | |
| 171 | + 'href' => bogo_url( null, $locale ) ); | |
| 173 | 172 | } |
| 174 | 173 | } |
| 175 | 174 | |
| 176 | 175 | $languages = apply_filters( 'bogo_rel_alternate_hreflang', $languages ); |
| @@ -188,39 +187,24 @@ | ||
| 188 | 187 | } |
| 189 | 188 | } |
| 190 | 189 | |
| 191 | 190 | function bogo_language_switcher( $args = '' ) { |
| 192 | - global $wp_query; | |
| 191 | + $args = wp_parse_args( $args, array( | |
| 192 | + 'echo' => false ) ); | |
| 193 | 193 | |
| 194 | - $defaults = array( | |
| 195 | - 'echo' => false ); | |
| 194 | + $links = bogo_language_switcher_links( $args ); | |
| 195 | + $total = count( $links ); | |
| 196 | + $count = 0; | |
| 196 | 197 | |
| 197 | - $args = wp_parse_args( $args, $defaults ); | |
| 198 | + $output = ''; | |
| 198 | 199 | |
| 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 ); | |
| 214 | - $count = 0; | |
| 215 | - | |
| 216 | - foreach ( $available_languages as $code => $name ) { | |
| 200 | + foreach ( $links as $link ) { | |
| 217 | 201 | $count += 1; |
| 218 | 202 | $class = array(); |
| 219 | - $class[] = bogo_language_tag( $code ); | |
| 220 | - $class[] = bogo_lang_slug( $code ); | |
| 203 | + $class[] = bogo_language_tag( $link['locale'] ); | |
| 204 | + $class[] = bogo_lang_slug( $link['locale'] ); | |
| 221 | 205 | |
| 222 | - if ( $locale == $code ) { | |
| 206 | + if ( get_locale() == $link['locale'] ) { | |
| 223 | 207 | $class[] = 'current'; |
| 224 | 208 | } |
| 225 | 209 | |
| 226 | 210 | if ( 1 == $count ) { |
| @@ -232,29 +216,31 @@ | ||
| 232 | 216 | } |
| 233 | 217 | |
| 234 | 218 | $class = implode( ' ', array_unique( $class ) ); |
| 235 | 219 | |
| 236 | - $output .= '<li class="' . esc_attr( $class ) . '">'; | |
| 220 | + $label = $link['native_name'] ? $link['native_name'] : $link['title']; | |
| 221 | + $title = $link['title']; | |
| 237 | 222 | |
| 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 | + if ( empty( $link['href'] ) ) { | |
| 224 | + $li = esc_html( $label ); | |
| 244 | 225 | } else { |
| 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 | - } | |
| 226 | + $li = sprintf( | |
| 227 | + '<a rel="alternate" hreflang="%1$s" href="%2$s" title="%3$s">%4$s</a>', | |
| 228 | + $link['lang'], | |
| 229 | + esc_url( $link['href'] ), | |
| 230 | + esc_attr( $title ), | |
| 231 | + esc_html( $label ) ); | |
| 250 | 232 | } |
| 251 | 233 | |
| 252 | - $output .= '</li>'; | |
| 234 | + $li = sprintf( '<li class="%1$s">%2$s</li>', $class, $li ); | |
| 235 | + | |
| 236 | + $output .= $li . "\n"; | |
| 253 | 237 | } |
| 254 | 238 | |
| 255 | - $output .= '</ul>' . "\n"; | |
| 239 | + $output = '<ul class="bogo-language-switcher">' . $output . '</ul>' . "\n"; | |
| 256 | 240 | |
| 241 | + $output = apply_filters( 'bogo_language_switcher', $output, $args ); | |
| 242 | + | |
| 257 | 243 | if ( $args['echo'] ) { |
| 258 | 244 | echo $output; |
| 259 | 245 | } else { |
| 260 | 246 | return $output; |
| @@ -260,8 +246,51 @@ | ||
| 260 | 246 | return $output; |
| 261 | 247 | } |
| 262 | 248 | } |
| 263 | 249 | |
| 250 | +function bogo_language_switcher_links( $args = '' ) { | |
| 251 | + global $wp_query; | |
| 252 | + | |
| 253 | + $args = wp_parse_args( $args, array() ); | |
| 254 | + | |
| 255 | + $locale = get_locale(); | |
| 256 | + $available_languages = bogo_available_languages( array( | |
| 257 | + 'exclude_enus_if_inactive' => true ) ); | |
| 258 | + | |
| 259 | + $translations = array(); | |
| 260 | + $is_singular = false; | |
| 261 | + | |
| 262 | + if ( is_singular() || ! empty( $wp_query->is_posts_page ) ) { | |
| 263 | + $translations = bogo_get_post_translations( get_queried_object_id() ); | |
| 264 | + $is_singular = true; | |
| 265 | + } | |
| 266 | + | |
| 267 | + $links = array(); | |
| 268 | + | |
| 269 | + foreach ( $available_languages as $code => $name ) { | |
| 270 | + $link = array( | |
| 271 | + 'locale' => $code, | |
| 272 | + 'lang' => bogo_language_tag( $code ), | |
| 273 | + 'title' => $name, | |
| 274 | + 'native_name' => bogo_get_language_native_name( $code ), | |
| 275 | + 'href' => '' ); | |
| 276 | + | |
| 277 | + if ( $is_singular ) { | |
| 278 | + if ( $locale != $code && ! empty( $translations[$code] ) ) { | |
| 279 | + $link['href'] = get_permalink( $translations[$code] ); | |
| 280 | + } | |
| 281 | + } else { | |
| 282 | + if ( $locale != $code ) { | |
| 283 | + $link['href'] = bogo_url( null, $code ); | |
| 284 | + } | |
| 285 | + } | |
| 286 | + | |
| 287 | + $links[] = $link; | |
| 288 | + } | |
| 289 | + | |
| 290 | + return apply_filters( 'bogo_language_switcher_links', $links, $args ); | |
| 291 | +} | |
| 292 | + | |
| 264 | 293 | add_filter( 'get_previous_post_join', 'bogo_adjacent_post_join', 10, 3 ); |
| 265 | 294 | add_filter( 'get_next_post_join', 'bogo_adjacent_post_join', 10, 3 ); |
| 266 | 295 | |
| 267 | 296 | function bogo_adjacent_post_join( $join, $in_same_term, $excluded_terms ) { |
| @@ -298,6 +327,4 @@ | ||
| 298 | 327 | } |
| 299 | 328 | |
| 300 | 329 | return $where; |
| 301 | 330 | } |
| 302 | - | |
| 303 | -?> | |