| @@ -24,13 +24,28 @@ | ||
| 24 | 24 | function bogo_page_link( $permalink, $id, $sample ) { |
| 25 | 25 | if ( ! bogo_is_localizable_post_type( 'page' ) ) |
| 26 | 26 | return $permalink; |
| 27 | 27 | |
| 28 | - if ( 'page' == get_option( 'show_on_front' ) && $id == get_option( 'page_on_front' ) ) | |
| 29 | - return $permalink; | |
| 30 | - | |
| 31 | 28 | $locale = bogo_get_post_locale( $id ); |
| 32 | 29 | $post = get_post( $id ); |
| 30 | + | |
| 31 | + if ( 'page' == get_option( 'show_on_front' ) ) { | |
| 32 | + $front_page_id = get_option( 'page_on_front' ); | |
| 33 | + | |
| 34 | + if ( $id == $front_page_id ) | |
| 35 | + return $permalink; | |
| 36 | + | |
| 37 | + $translations = bogo_get_post_translations( $front_page_id ); | |
| 38 | + | |
| 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 | + } | |
| 46 | + } | |
| 47 | + | |
| 33 | 48 | $permalink_structure = get_option( 'permalink_structure' ); |
| 34 | 49 | |
| 35 | 50 | $using_permalinks = $permalink_structure && |
| 36 | 51 | ( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| @@ -61,67 +76,72 @@ | ||
| 61 | 76 | |
| 62 | 77 | add_filter( 'year_link', 'bogo_year_link', 10, 2 ); |
| 63 | 78 | |
| 64 | 79 | function bogo_year_link( $link, $year ) { |
| 65 | - return bogo_get_general_link( $link ); | |
| 80 | + return bogo_url( $link ); | |
| 66 | 81 | } |
| 67 | 82 | |
| 68 | 83 | add_filter( 'month_link', 'bogo_month_link', 10, 3 ); |
| 69 | 84 | |
| 70 | 85 | function bogo_month_link( $link, $year, $month ) { |
| 71 | - return bogo_get_general_link( $link ); | |
| 86 | + return bogo_url( $link ); | |
| 72 | 87 | } |
| 73 | 88 | |
| 74 | 89 | add_filter( 'day_link', 'bogo_day_link', 10, 4 ); |
| 75 | 90 | |
| 76 | 91 | function bogo_day_link( $link, $year, $month, $day ) { |
| 77 | - return bogo_get_general_link( $link ); | |
| 92 | + return bogo_url( $link ); | |
| 78 | 93 | } |
| 79 | 94 | |
| 80 | 95 | add_filter( 'feed_link', 'bogo_feed_link', 10, 2 ); |
| 81 | 96 | |
| 82 | 97 | function bogo_feed_link( $link, $feed ) { |
| 83 | - return bogo_get_general_link( $link ); | |
| 98 | + return bogo_url( $link ); | |
| 84 | 99 | } |
| 85 | 100 | |
| 86 | 101 | add_filter( 'author_feed_link', 'bogo_author_feed_link', 10, 2 ); |
| 87 | 102 | |
| 88 | 103 | function bogo_author_feed_link( $link, $feed ) { |
| 89 | - return bogo_get_general_link( $link ); | |
| 104 | + return bogo_url( $link ); | |
| 90 | 105 | } |
| 91 | 106 | |
| 92 | 107 | add_filter( 'category_feed_link', 'bogo_category_feed_link', 10, 2 ); |
| 93 | 108 | |
| 94 | 109 | function bogo_category_feed_link( $link, $feed ) { |
| 95 | - return bogo_get_general_link( $link ); | |
| 110 | + return bogo_url( $link ); | |
| 96 | 111 | } |
| 97 | 112 | |
| 98 | 113 | add_filter( 'taxonomy_feed_link', 'bogo_taxonomy_feed_link', 10, 3 ); |
| 99 | 114 | |
| 100 | 115 | function bogo_taxonomy_feed_link( $link, $feed, $taxonomy ) { |
| 101 | - return bogo_get_general_link( $link ); | |
| 116 | + return bogo_url( $link ); | |
| 102 | 117 | } |
| 103 | 118 | |
| 104 | 119 | add_filter( 'post_type_archive_link', 'bogo_post_type_archive_link', 10, 2 ); |
| 105 | 120 | |
| 106 | 121 | function bogo_post_type_archive_link( $link, $post_type ) { |
| 107 | - return bogo_get_general_link( $link ); | |
| 122 | + return bogo_url( $link ); | |
| 108 | 123 | } |
| 109 | 124 | |
| 110 | 125 | add_filter( 'post_type_archive_feed_link', 'bogo_post_type_archive_feed_link', 10, 2 ); |
| 111 | 126 | |
| 112 | 127 | function bogo_post_type_archive_feed_link( $link, $feed ) { |
| 113 | - return bogo_get_general_link( $link ); | |
| 128 | + return bogo_url( $link ); | |
| 114 | 129 | } |
| 115 | 130 | |
| 116 | 131 | add_filter( 'term_link', 'bogo_term_link', 10, 3 ); |
| 117 | 132 | |
| 118 | 133 | function bogo_term_link( $link, $term, $taxonomy ) { |
| 119 | - return bogo_get_general_link( $link ); | |
| 134 | + return bogo_url( $link ); | |
| 120 | 135 | } |
| 121 | 136 | |
| 122 | -function bogo_get_general_link( $link ) { | |
| 123 | - return bogo_url( $link, get_locale() ); | |
| 137 | +add_filter( 'home_url', 'bogo_home_url' ); | |
| 138 | + | |
| 139 | +function bogo_home_url( $url ) { | |
| 140 | + if ( is_admin() || ! did_action( 'template_redirect' ) ) | |
| 141 | + return $url; | |
| 142 | + | |
| 143 | + return bogo_url( $url ); | |
| 124 | 144 | } |
| 125 | 145 | |
| 126 | 146 | add_action( 'wp_head', 'bogo_m17n_headers' ); |
| 127 | 147 | |
| @@ -126,102 +146,185 @@ | ||
| 126 | 146 | add_action( 'wp_head', 'bogo_m17n_headers' ); |
| 127 | 147 | |
| 128 | 148 | function bogo_m17n_headers() { |
| 129 | 149 | $languages = array(); |
| 130 | - $locale = get_locale(); | |
| 131 | 150 | |
| 132 | 151 | if ( is_singular() ) { |
| 133 | 152 | $post_id = get_queried_object_id(); |
| 134 | 153 | |
| 135 | - if ( ! $post_id || ! $translations = bogo_get_post_translations( $post_id ) ) | |
| 136 | - return; | |
| 154 | + if ( $post_id && $translations = bogo_get_post_translations( $post_id ) ) { | |
| 155 | + $locale = get_locale(); | |
| 156 | + $translations[$locale] = get_post( $post_id ); | |
| 137 | 157 | |
| 138 | - foreach ( $translations as $lang => $translation ) { | |
| 139 | - if ( $locale != $lang ) | |
| 158 | + foreach ( $translations as $lang => $translation ) { | |
| 140 | 159 | $languages[] = array( |
| 141 | 160 | 'hreflang' => bogo_language_tag( $lang ), |
| 142 | 161 | 'href' => get_permalink( $translation ) ); |
| 162 | + } | |
| 143 | 163 | } |
| 144 | - | |
| 145 | 164 | } else { |
| 146 | - $available_languages = bogo_available_languages(); | |
| 165 | + $available_locales = bogo_available_locales( array( | |
| 166 | + 'exclude_enus_if_inactive' => true ) ); | |
| 147 | 167 | |
| 148 | - foreach ( array_keys( $available_languages ) as $lang ) { | |
| 149 | - if ( $locale != $lang ) { | |
| 150 | - $url = bogo_url( null, $lang ); | |
| 151 | - | |
| 152 | - $languages[] = array( | |
| 153 | - 'hreflang' => bogo_language_tag( $lang ), | |
| 154 | - 'href' => $url ); | |
| 155 | - } | |
| 168 | + foreach ( $available_locales as $locale ) { | |
| 169 | + $languages[] = array( | |
| 170 | + 'hreflang' => bogo_language_tag( $locale ), | |
| 171 | + 'href' => bogo_url( null, $locale ) ); | |
| 156 | 172 | } |
| 157 | 173 | } |
| 158 | 174 | |
| 159 | - if ( ! $languages ) | |
| 160 | - return; | |
| 175 | + $languages = apply_filters( 'bogo_rel_alternate_hreflang', $languages ); | |
| 161 | 176 | |
| 162 | - foreach ( $languages as $language ) | |
| 163 | - echo '<link rel="alternate" hreflang="' . esc_attr( $language['hreflang'] ) . '" href="' . esc_url( $language['href'] ) . '" />' . "\n"; | |
| 164 | -} | |
| 177 | + foreach ( (array) $languages as $language ) { | |
| 178 | + $hreflang = isset( $language['hreflang'] ) ? $language['hreflang'] : ''; | |
| 179 | + $href = isset( $language['href'] ) ? $language['href'] : ''; | |
| 165 | 180 | |
| 166 | -add_shortcode( 'bogo', 'bogo_language_switcher' ); | |
| 181 | + if ( $hreflang && $href ) { | |
| 182 | + $link = sprintf( '<link rel="alternate" hreflang="%1$s" href="%2$s" />', | |
| 183 | + esc_attr( $hreflang ), esc_url( $href ) ); | |
| 167 | 184 | |
| 168 | -function bogo_language_switcher( $args = '' ) { | |
| 169 | - $defaults = array(); | |
| 170 | - | |
| 171 | - $args = wp_parse_args( $args, $defaults ); | |
| 172 | - | |
| 173 | - $locale = get_locale(); | |
| 174 | - $available_languages = bogo_available_languages(); | |
| 175 | - | |
| 176 | - $translations = array(); | |
| 177 | - | |
| 178 | - if ( is_singular() ) { | |
| 179 | - $post_id = get_queried_object_id(); | |
| 180 | - | |
| 181 | - if ( $post_id ) | |
| 182 | - $translations = bogo_get_post_translations( $post_id ); | |
| 185 | + echo $link . "\n"; | |
| 186 | + } | |
| 183 | 187 | } |
| 188 | +} | |
| 184 | 189 | |
| 185 | - echo '<ul class="language-switcher">'; | |
| 190 | +function bogo_language_switcher( $args = '' ) { | |
| 191 | + $args = wp_parse_args( $args, array( | |
| 192 | + 'echo' => false ) ); | |
| 186 | 193 | |
| 187 | - $total = count( $available_languages ); | |
| 194 | + $links = bogo_language_switcher_links( $args ); | |
| 195 | + $total = count( $links ); | |
| 188 | 196 | $count = 0; |
| 189 | 197 | |
| 190 | - foreach ( $available_languages as $code => $name ) { | |
| 198 | + $output = ''; | |
| 199 | + | |
| 200 | + foreach ( $links as $link ) { | |
| 191 | 201 | $count += 1; |
| 192 | 202 | $class = array(); |
| 193 | - $class[] = bogo_language_tag( $code ); | |
| 194 | - $class[] = bogo_lang_slug( $code ); | |
| 203 | + $class[] = bogo_language_tag( $link['locale'] ); | |
| 204 | + $class[] = bogo_lang_slug( $link['locale'] ); | |
| 195 | 205 | |
| 196 | - if ( $locale == $code ) | |
| 206 | + if ( get_locale() == $link['locale'] ) { | |
| 197 | 207 | $class[] = 'current'; |
| 208 | + } | |
| 198 | 209 | |
| 199 | - if ( 1 == $count ) | |
| 210 | + if ( 1 == $count ) { | |
| 200 | 211 | $class[] = 'first'; |
| 212 | + } | |
| 201 | 213 | |
| 202 | - if ( $total == $count ) | |
| 214 | + if ( $total == $count ) { | |
| 203 | 215 | $class[] = 'last'; |
| 216 | + } | |
| 204 | 217 | |
| 205 | 218 | $class = implode( ' ', array_unique( $class ) ); |
| 206 | 219 | |
| 207 | - echo '<li class="' . esc_attr( $class ) . '">'; | |
| 220 | + $label = $link['native_name'] ? $link['native_name'] : $link['title']; | |
| 221 | + $title = $link['title']; | |
| 208 | 222 | |
| 209 | - if ( is_singular() ) { | |
| 210 | - if ( empty( $translations[$code] ) || $locale == $code ) | |
| 211 | - echo esc_html( $name ); | |
| 212 | - else | |
| 213 | - echo '<a rel="alternate" hreflang="' . bogo_language_tag( $code ) . '" href="' . get_permalink( $translations[$code] ) . '">' . esc_html( $name ) . '</a>'; | |
| 223 | + if ( empty( $link['href'] ) ) { | |
| 224 | + $li = esc_html( $label ); | |
| 214 | 225 | } else { |
| 215 | - if ( $locale == $code ) | |
| 216 | - echo esc_html( $name ); | |
| 217 | - else | |
| 218 | - echo '<a rel="alternate" hreflang="' . bogo_language_tag( $code ) . '" href="' . esc_url( bogo_url( null, $code ) ) . '">' . esc_html( $name ) . '</a>'; | |
| 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 ) ); | |
| 219 | 232 | } |
| 220 | 233 | |
| 221 | - echo '</li>'; | |
| 234 | + $li = sprintf( '<li class="%1$s">%2$s</li>', $class, $li ); | |
| 235 | + | |
| 236 | + $output .= $li . "\n"; | |
| 222 | 237 | } |
| 223 | 238 | |
| 224 | - echo '</ul>' . "\n"; | |
| 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 | + } | |
| 225 | 248 | } |
| 226 | 249 | |
| 227 | -?> | |
| 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 | + | |
| 293 | +add_filter( 'get_previous_post_join', 'bogo_adjacent_post_join', 10, 3 ); | |
| 294 | +add_filter( 'get_next_post_join', 'bogo_adjacent_post_join', 10, 3 ); | |
| 295 | + | |
| 296 | +function bogo_adjacent_post_join( $join, $in_same_term, $excluded_terms ) { | |
| 297 | + global $wpdb; | |
| 298 | + | |
| 299 | + $post = get_post(); | |
| 300 | + | |
| 301 | + if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) { | |
| 302 | + $join .= " LEFT JOIN $wpdb->postmeta AS postmeta_bogo ON (p.ID = postmeta_bogo.post_id AND postmeta_bogo.meta_key = '_locale')"; | |
| 303 | + } | |
| 304 | + | |
| 305 | + return $join; | |
| 306 | +} | |
| 307 | + | |
| 308 | +add_filter( 'get_previous_post_where', 'bogo_adjacent_post_where', 10, 3 ); | |
| 309 | +add_filter( 'get_next_post_where', 'bogo_adjacent_post_where', 10, 3 ); | |
| 310 | + | |
| 311 | +function bogo_adjacent_post_where( $where, $in_same_term, $excluded_terms ) { | |
| 312 | + global $wpdb; | |
| 313 | + | |
| 314 | + $post = get_post(); | |
| 315 | + | |
| 316 | + if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) { | |
| 317 | + $locale = bogo_get_post_locale( $post->ID ); | |
| 318 | + | |
| 319 | + $where .= " AND (1=0"; | |
| 320 | + $where .= $wpdb->prepare( " OR postmeta_bogo.meta_value LIKE %s", $locale ); | |
| 321 | + | |
| 322 | + if ( bogo_is_default_locale( $locale ) ) { | |
| 323 | + $where .= " OR postmeta_bogo.meta_id IS NULL"; | |
| 324 | + } | |
| 325 | + | |
| 326 | + $where .= ")"; | |
| 327 | + } | |
| 328 | + | |
| 329 | + return $where; | |
| 330 | +} | |