| 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 |
global $wp_query; |
| 193 |
|
| 194 |
$defaults = array( |
| 195 |
'echo' => false ); |
| 196 |
|
| 197 |
$args = wp_parse_args( $args, $defaults ); |
| 198 |
|
| 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 ) { |
| 217 |
$count += 1; |
| 218 |
$class = array(); |
| 219 |
$class[] = bogo_language_tag( $code ); |
| 220 |
$class[] = bogo_lang_slug( $code ); |
| 221 |
|
| 222 |
if ( $locale == $code ) { |
| 223 |
$class[] = 'current'; |
| 224 |
} |
| 225 |
|
| 226 |
if ( 1 == $count ) { |
| 227 |
$class[] = 'first'; |
| 228 |
} |
| 229 |
|
| 230 |
if ( $total == $count ) { |
| 231 |
$class[] = 'last'; |
| 232 |
} |
| 233 |
|
| 234 |
$class = implode( ' ', array_unique( $class ) ); |
| 235 |
|
| 236 |
$output .= '<li class="' . esc_attr( $class ) . '">'; |
| 237 |
|
| 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 |
} |
| 244 |
} 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 |
} |
| 250 |
} |
| 251 |
|
| 252 |
$output .= '</li>'; |
| 253 |
} |
| 254 |
|
| 255 |
$output .= '</ul>' . "\n"; |
| 256 |
|
| 257 |
if ( $args['echo'] ) { |
| 258 |
echo $output; |
| 259 |
} else { |
| 260 |
return $output; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
add_filter( 'get_previous_post_join', 'bogo_adjacent_post_join', 10, 3 ); |
| 265 |
add_filter( 'get_next_post_join', 'bogo_adjacent_post_join', 10, 3 ); |
| 266 |
|
| 267 |
function bogo_adjacent_post_join( $join, $in_same_term, $excluded_terms ) { |
| 268 |
global $wpdb; |
| 269 |
|
| 270 |
$post = get_post(); |
| 271 |
|
| 272 |
if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) { |
| 273 |
$join .= " LEFT JOIN $wpdb->postmeta AS postmeta_bogo ON (p.ID = postmeta_bogo.post_id AND postmeta_bogo.meta_key = '_locale')"; |
| 274 |
} |
| 275 |
|
| 276 |
return $join; |
| 277 |
} |
| 278 |
|
| 279 |
add_filter( 'get_previous_post_where', 'bogo_adjacent_post_where', 10, 3 ); |
| 280 |
add_filter( 'get_next_post_where', 'bogo_adjacent_post_where', 10, 3 ); |
| 281 |
|
| 282 |
function bogo_adjacent_post_where( $where, $in_same_term, $excluded_terms ) { |
| 283 |
global $wpdb; |
| 284 |
|
| 285 |
$post = get_post(); |
| 286 |
|
| 287 |
if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) { |
| 288 |
$locale = bogo_get_post_locale( $post->ID ); |
| 289 |
|
| 290 |
$where .= " AND (1=0"; |
| 291 |
$where .= $wpdb->prepare( " OR postmeta_bogo.meta_value LIKE %s", $locale ); |
| 292 |
|
| 293 |
if ( bogo_is_default_locale( $locale ) ) { |
| 294 |
$where .= " OR postmeta_bogo.meta_id IS NULL"; |
| 295 |
} |
| 296 |
|
| 297 |
$where .= ")"; |
| 298 |
} |
| 299 |
|
| 300 |
return $where; |
| 301 |
} |
| 302 |
|
| 303 |
?> |