| 1 |
<?php |
| 2 |
|
| 3 |
add_filter( 'post_link', 'bogo_post_link', 10, 3 ); |
| 4 |
|
| 5 |
function bogo_post_link( $permalink, $post, $leavename ) { |
| 6 |
if ( ! bogo_is_localizable_post_type( $post->post_type ) ) |
| 7 |
return $permalink; |
| 8 |
|
| 9 |
$locale = bogo_get_post_locale( $post->ID ); |
| 10 |
$sample = ( isset( $post->filter ) && 'sample' == $post->filter ); |
| 11 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 12 |
|
| 13 |
$using_permalinks = $permalink_structure && |
| 14 |
( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| 15 |
|
| 16 |
$permalink = bogo_get_url_with_lang( $permalink, $locale, |
| 17 |
array( 'using_permalinks' => $using_permalinks ) ); |
| 18 |
|
| 19 |
return $permalink; |
| 20 |
} |
| 21 |
|
| 22 |
add_filter( 'page_link', 'bogo_page_link', 10, 3 ); |
| 23 |
|
| 24 |
function bogo_page_link( $permalink, $id, $sample ) { |
| 25 |
if ( ! bogo_is_localizable_post_type( 'page' ) ) |
| 26 |
return $permalink; |
| 27 |
|
| 28 |
$locale = bogo_get_post_locale( $id ); |
| 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 |
|
| 48 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 49 |
|
| 50 |
$using_permalinks = $permalink_structure && |
| 51 |
( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| 52 |
|
| 53 |
$permalink = bogo_get_url_with_lang( $permalink, $locale, |
| 54 |
array( 'using_permalinks' => $using_permalinks ) ); |
| 55 |
|
| 56 |
return $permalink; |
| 57 |
} |
| 58 |
|
| 59 |
add_filter( 'post_type_link', 'bogo_post_type_link', 10, 4 ); |
| 60 |
|
| 61 |
function bogo_post_type_link( $permalink, $post, $leavename, $sample ) { |
| 62 |
if ( ! bogo_is_localizable_post_type( $post->post_type ) ) |
| 63 |
return $permalink; |
| 64 |
|
| 65 |
$locale = bogo_get_post_locale( $post->ID ); |
| 66 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 67 |
|
| 68 |
$using_permalinks = $permalink_structure && |
| 69 |
( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| 70 |
|
| 71 |
$permalink = bogo_get_url_with_lang( $permalink, $locale, |
| 72 |
array( 'using_permalinks' => $using_permalinks ) ); |
| 73 |
|
| 74 |
return $permalink; |
| 75 |
} |
| 76 |
|
| 77 |
add_filter( 'year_link', 'bogo_year_link', 10, 2 ); |
| 78 |
|
| 79 |
function bogo_year_link( $link, $year ) { |
| 80 |
return bogo_url( $link ); |
| 81 |
} |
| 82 |
|
| 83 |
add_filter( 'month_link', 'bogo_month_link', 10, 3 ); |
| 84 |
|
| 85 |
function bogo_month_link( $link, $year, $month ) { |
| 86 |
return bogo_url( $link ); |
| 87 |
} |
| 88 |
|
| 89 |
add_filter( 'day_link', 'bogo_day_link', 10, 4 ); |
| 90 |
|
| 91 |
function bogo_day_link( $link, $year, $month, $day ) { |
| 92 |
return bogo_url( $link ); |
| 93 |
} |
| 94 |
|
| 95 |
add_filter( 'feed_link', 'bogo_feed_link', 10, 2 ); |
| 96 |
|
| 97 |
function bogo_feed_link( $link, $feed ) { |
| 98 |
return bogo_url( $link ); |
| 99 |
} |
| 100 |
|
| 101 |
add_filter( 'author_feed_link', 'bogo_author_feed_link', 10, 2 ); |
| 102 |
|
| 103 |
function bogo_author_feed_link( $link, $feed ) { |
| 104 |
return bogo_url( $link ); |
| 105 |
} |
| 106 |
|
| 107 |
add_filter( 'category_feed_link', 'bogo_category_feed_link', 10, 2 ); |
| 108 |
|
| 109 |
function bogo_category_feed_link( $link, $feed ) { |
| 110 |
return bogo_url( $link ); |
| 111 |
} |
| 112 |
|
| 113 |
add_filter( 'taxonomy_feed_link', 'bogo_taxonomy_feed_link', 10, 3 ); |
| 114 |
|
| 115 |
function bogo_taxonomy_feed_link( $link, $feed, $taxonomy ) { |
| 116 |
return bogo_url( $link ); |
| 117 |
} |
| 118 |
|
| 119 |
add_filter( 'post_type_archive_link', 'bogo_post_type_archive_link', 10, 2 ); |
| 120 |
|
| 121 |
function bogo_post_type_archive_link( $link, $post_type ) { |
| 122 |
return bogo_url( $link ); |
| 123 |
} |
| 124 |
|
| 125 |
add_filter( 'post_type_archive_feed_link', 'bogo_post_type_archive_feed_link', 10, 2 ); |
| 126 |
|
| 127 |
function bogo_post_type_archive_feed_link( $link, $feed ) { |
| 128 |
return bogo_url( $link ); |
| 129 |
} |
| 130 |
|
| 131 |
add_filter( 'term_link', 'bogo_term_link', 10, 3 ); |
| 132 |
|
| 133 |
function bogo_term_link( $link, $term, $taxonomy ) { |
| 134 |
return bogo_url( $link ); |
| 135 |
} |
| 136 |
|
| 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 ); |
| 144 |
} |
| 145 |
|
| 146 |
add_action( 'wp_head', 'bogo_m17n_headers' ); |
| 147 |
|
| 148 |
function bogo_m17n_headers() { |
| 149 |
$languages = array(); |
| 150 |
|
| 151 |
if ( is_singular() ) { |
| 152 |
$post_id = get_queried_object_id(); |
| 153 |
|
| 154 |
if ( $post_id && $translations = bogo_get_post_translations( $post_id ) ) { |
| 155 |
$locale = get_locale(); |
| 156 |
$translations[$locale] = get_post( $post_id ); |
| 157 |
|
| 158 |
foreach ( $translations as $lang => $translation ) { |
| 159 |
$languages[] = array( |
| 160 |
'hreflang' => bogo_language_tag( $lang ), |
| 161 |
'href' => get_permalink( $translation ) ); |
| 162 |
} |
| 163 |
} |
| 164 |
} else { |
| 165 |
$available_locales = bogo_available_locales(); |
| 166 |
|
| 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 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
$languages = apply_filters( 'bogo_rel_alternate_hreflang', $languages ); |
| 177 |
|
| 178 |
foreach ( (array) $languages as $language ) { |
| 179 |
$hreflang = isset( $language['hreflang'] ) ? $language['hreflang'] : ''; |
| 180 |
$href = isset( $language['href'] ) ? $language['href'] : ''; |
| 181 |
|
| 182 |
if ( $hreflang && $href ) { |
| 183 |
$link = sprintf( '<link rel="alternate" hreflang="%1$s" href="%2$s" />', |
| 184 |
esc_attr( $hreflang ), esc_url( $href ) ); |
| 185 |
|
| 186 |
echo $link . "\n"; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
function bogo_language_switcher( $args = '' ) { |
| 192 |
$args = wp_parse_args( $args, array( |
| 193 |
'echo' => false ) ); |
| 194 |
|
| 195 |
$links = bogo_language_switcher_links( $args ); |
| 196 |
$total = count( $links ); |
| 197 |
$count = 0; |
| 198 |
|
| 199 |
$output = ''; |
| 200 |
|
| 201 |
foreach ( $links as $link ) { |
| 202 |
$count += 1; |
| 203 |
$class = array(); |
| 204 |
$class[] = bogo_language_tag( $link['locale'] ); |
| 205 |
$class[] = bogo_lang_slug( $link['locale'] ); |
| 206 |
|
| 207 |
if ( get_locale() == $link['locale'] ) { |
| 208 |
$class[] = 'current'; |
| 209 |
} |
| 210 |
|
| 211 |
if ( 1 == $count ) { |
| 212 |
$class[] = 'first'; |
| 213 |
} |
| 214 |
|
| 215 |
if ( $total == $count ) { |
| 216 |
$class[] = 'last'; |
| 217 |
} |
| 218 |
|
| 219 |
$class = implode( ' ', array_unique( $class ) ); |
| 220 |
|
| 221 |
$label = $link['native_name'] ? $link['native_name'] : $link['title']; |
| 222 |
$title = $link['title']; |
| 223 |
|
| 224 |
if ( empty( $link['href'] ) ) { |
| 225 |
$li = esc_html( $label ); |
| 226 |
} else { |
| 227 |
$li = sprintf( |
| 228 |
'<a rel="alternate" hreflang="%1$s" href="%2$s" title="%3$s">%4$s</a>', |
| 229 |
$link['lang'], |
| 230 |
esc_url( $link['href'] ), |
| 231 |
esc_attr( $title ), |
| 232 |
esc_html( $label ) ); |
| 233 |
} |
| 234 |
|
| 235 |
$li = sprintf( '<li class="%1$s">%2$s</li>', $class, $li ); |
| 236 |
|
| 237 |
$output .= $li . "\n"; |
| 238 |
} |
| 239 |
|
| 240 |
$output = '<ul class="bogo-language-switcher">' . $output . '</ul>' . "\n"; |
| 241 |
|
| 242 |
$output = apply_filters( 'bogo_language_switcher', $output, $args ); |
| 243 |
|
| 244 |
if ( $args['echo'] ) { |
| 245 |
echo $output; |
| 246 |
} else { |
| 247 |
return $output; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
function bogo_language_switcher_links( $args = '' ) { |
| 252 |
global $wp_query; |
| 253 |
|
| 254 |
$args = wp_parse_args( $args, array() ); |
| 255 |
|
| 256 |
$locale = get_locale(); |
| 257 |
$available_languages = bogo_available_languages(); |
| 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 |
} |
| 331 |
|