| @@ -21,11 +21,10 @@ | ||
| 21 | 21 | |
| 22 | 22 | add_filter( 'page_link', 'bogo_page_link', 10, 3 ); |
| 23 | 23 | |
| 24 | 24 | function bogo_page_link( $permalink, $id, $sample ) { |
| 25 | - if ( ! bogo_is_localizable_post_type( 'page' ) ) { | |
| 25 | + if ( ! bogo_is_localizable_post_type( 'page' ) ) | |
| 26 | 26 | return $permalink; |
| 27 | - } | |
| 28 | 27 | |
| 29 | 28 | $locale = bogo_get_post_locale( $id ); |
| 30 | 29 | $post = get_post( $id ); |
| 31 | 30 | |
| @@ -31,18 +30,19 @@ | ||
| 31 | 30 | |
| 32 | 31 | if ( 'page' == get_option( 'show_on_front' ) ) { |
| 33 | 32 | $front_page_id = get_option( 'page_on_front' ); |
| 34 | 33 | |
| 35 | - if ( $id == $front_page_id ) { | |
| 34 | + if ( $id == $front_page_id ) | |
| 36 | 35 | return $permalink; |
| 37 | - } | |
| 38 | 36 | |
| 39 | - $translation = bogo_get_post_translation( $front_page_id, $locale ); | |
| 37 | + $translations = bogo_get_post_translations( $front_page_id ); | |
| 40 | 38 | |
| 41 | - if ( $translation && $translation->ID == $id ) { | |
| 42 | - $home = set_url_scheme( get_option( 'home' ) ); | |
| 43 | - $home = trailingslashit( $home ); | |
| 44 | - return bogo_url( $home, $locale ); | |
| 39 | + if ( ! empty( $translations[$locale] ) ) { | |
| 40 | + if ( $translations[$locale]->ID == $id ) { | |
| 41 | + $home = set_url_scheme( get_option( 'home' ) ); | |
| 42 | + $home = trailingslashit( $home ); | |
| 43 | + return bogo_url( $home, $locale ); | |
| 44 | + } | |
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | $permalink_structure = get_option( 'permalink_structure' ); |
| @@ -184,8 +184,111 @@ | ||
| 184 | 184 | |
| 185 | 185 | echo $link . "\n"; |
| 186 | 186 | } |
| 187 | 187 | } |
| 188 | +} | |
| 189 | + | |
| 190 | +function bogo_language_switcher( $args = '' ) { | |
| 191 | + $args = wp_parse_args( $args, array( | |
| 192 | + 'echo' => false ) ); | |
| 193 | + | |
| 194 | + $links = bogo_language_switcher_links( $args ); | |
| 195 | + $total = count( $links ); | |
| 196 | + $count = 0; | |
| 197 | + | |
| 198 | + $output = ''; | |
| 199 | + | |
| 200 | + foreach ( $links as $link ) { | |
| 201 | + $count += 1; | |
| 202 | + $class = array(); | |
| 203 | + $class[] = bogo_language_tag( $link['locale'] ); | |
| 204 | + $class[] = bogo_lang_slug( $link['locale'] ); | |
| 205 | + | |
| 206 | + if ( get_locale() == $link['locale'] ) { | |
| 207 | + $class[] = 'current'; | |
| 208 | + } | |
| 209 | + | |
| 210 | + if ( 1 == $count ) { | |
| 211 | + $class[] = 'first'; | |
| 212 | + } | |
| 213 | + | |
| 214 | + if ( $total == $count ) { | |
| 215 | + $class[] = 'last'; | |
| 216 | + } | |
| 217 | + | |
| 218 | + $class = implode( ' ', array_unique( $class ) ); | |
| 219 | + | |
| 220 | + $label = $link['native_name'] ? $link['native_name'] : $link['title']; | |
| 221 | + $title = $link['title']; | |
| 222 | + | |
| 223 | + if ( empty( $link['href'] ) ) { | |
| 224 | + $li = esc_html( $label ); | |
| 225 | + } else { | |
| 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 ) ); | |
| 232 | + } | |
| 233 | + | |
| 234 | + $li = sprintf( '<li class="%1$s">%2$s</li>', $class, $li ); | |
| 235 | + | |
| 236 | + $output .= $li . "\n"; | |
| 237 | + } | |
| 238 | + | |
| 239 | + $output = '<ul class="bogo-language-switcher">' . $output . '</ul>' . "\n"; | |
| 240 | + | |
| 241 | + $output = apply_filters( 'bogo_language_switcher', $output, $args ); | |
| 242 | + | |
| 243 | + if ( $args['echo'] ) { | |
| 244 | + echo $output; | |
| 245 | + } else { | |
| 246 | + return $output; | |
| 247 | + } | |
| 248 | +} | |
| 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 ); | |
| 188 | 291 | } |
| 189 | 292 | |
| 190 | 293 | add_filter( 'get_previous_post_join', 'bogo_adjacent_post_join', 10, 3 ); |
| 191 | 294 | add_filter( 'get_next_post_join', 'bogo_adjacent_post_join', 10, 3 ); |