| 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 |
if ( 'page' == get_option( 'show_on_front' ) && $id == get_option( 'page_on_front' ) ) |
| 29 |
return $permalink; |
| 30 |
|
| 31 |
$locale = bogo_get_post_locale( $id ); |
| 32 |
$post = get_post( $id ); |
| 33 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 34 |
|
| 35 |
$using_permalinks = $permalink_structure && |
| 36 |
( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| 37 |
|
| 38 |
$permalink = bogo_get_url_with_lang( $permalink, $locale, |
| 39 |
array( 'using_permalinks' => $using_permalinks ) ); |
| 40 |
|
| 41 |
return $permalink; |
| 42 |
} |
| 43 |
|
| 44 |
add_filter( 'post_type_link', 'bogo_post_type_link', 10, 4 ); |
| 45 |
|
| 46 |
function bogo_post_type_link( $permalink, $post, $leavename, $sample ) { |
| 47 |
if ( ! bogo_is_localizable_post_type( $post->post_type ) ) |
| 48 |
return $permalink; |
| 49 |
|
| 50 |
$locale = bogo_get_post_locale( $post->ID ); |
| 51 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 52 |
|
| 53 |
$using_permalinks = $permalink_structure && |
| 54 |
( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ); |
| 55 |
|
| 56 |
$permalink = bogo_get_url_with_lang( $permalink, $locale, |
| 57 |
array( 'using_permalinks' => $using_permalinks ) ); |
| 58 |
|
| 59 |
return $permalink; |
| 60 |
} |
| 61 |
|
| 62 |
add_filter( 'year_link', 'bogo_year_link', 10, 2 ); |
| 63 |
|
| 64 |
function bogo_year_link( $link, $year ) { |
| 65 |
return bogo_get_general_link( $link ); |
| 66 |
} |
| 67 |
|
| 68 |
add_filter( 'month_link', 'bogo_month_link', 10, 3 ); |
| 69 |
|
| 70 |
function bogo_month_link( $link, $year, $month ) { |
| 71 |
return bogo_get_general_link( $link ); |
| 72 |
} |
| 73 |
|
| 74 |
add_filter( 'day_link', 'bogo_day_link', 10, 4 ); |
| 75 |
|
| 76 |
function bogo_day_link( $link, $year, $month, $day ) { |
| 77 |
return bogo_get_general_link( $link ); |
| 78 |
} |
| 79 |
|
| 80 |
add_filter( 'feed_link', 'bogo_feed_link', 10, 2 ); |
| 81 |
|
| 82 |
function bogo_feed_link( $link, $feed ) { |
| 83 |
return bogo_get_general_link( $link ); |
| 84 |
} |
| 85 |
|
| 86 |
add_filter( 'author_feed_link', 'bogo_author_feed_link', 10, 2 ); |
| 87 |
|
| 88 |
function bogo_author_feed_link( $link, $feed ) { |
| 89 |
return bogo_get_general_link( $link ); |
| 90 |
} |
| 91 |
|
| 92 |
add_filter( 'category_feed_link', 'bogo_category_feed_link', 10, 2 ); |
| 93 |
|
| 94 |
function bogo_category_feed_link( $link, $feed ) { |
| 95 |
return bogo_get_general_link( $link ); |
| 96 |
} |
| 97 |
|
| 98 |
add_filter( 'taxonomy_feed_link', 'bogo_taxonomy_feed_link', 10, 3 ); |
| 99 |
|
| 100 |
function bogo_taxonomy_feed_link( $link, $feed, $taxonomy ) { |
| 101 |
return bogo_get_general_link( $link ); |
| 102 |
} |
| 103 |
|
| 104 |
add_filter( 'post_type_archive_link', 'bogo_post_type_archive_link', 10, 2 ); |
| 105 |
|
| 106 |
function bogo_post_type_archive_link( $link, $post_type ) { |
| 107 |
return bogo_get_general_link( $link ); |
| 108 |
} |
| 109 |
|
| 110 |
add_filter( 'post_type_archive_feed_link', 'bogo_post_type_archive_feed_link', 10, 2 ); |
| 111 |
|
| 112 |
function bogo_post_type_archive_feed_link( $link, $feed ) { |
| 113 |
return bogo_get_general_link( $link ); |
| 114 |
} |
| 115 |
|
| 116 |
add_filter( 'term_link', 'bogo_term_link', 10, 3 ); |
| 117 |
|
| 118 |
function bogo_term_link( $link, $term, $taxonomy ) { |
| 119 |
return bogo_get_general_link( $link ); |
| 120 |
} |
| 121 |
|
| 122 |
function bogo_get_general_link( $link ) { |
| 123 |
return bogo_url( $link, get_locale() ); |
| 124 |
} |
| 125 |
|
| 126 |
add_action( 'wp_head', 'bogo_m17n_headers' ); |
| 127 |
|
| 128 |
function bogo_m17n_headers() { |
| 129 |
$languages = array(); |
| 130 |
$locale = get_locale(); |
| 131 |
|
| 132 |
if ( is_singular() ) { |
| 133 |
$post_id = get_queried_object_id(); |
| 134 |
|
| 135 |
if ( ! $post_id || ! $translations = bogo_get_post_translations( $post_id ) ) |
| 136 |
return; |
| 137 |
|
| 138 |
foreach ( $translations as $lang => $translation ) { |
| 139 |
if ( $locale != $lang ) |
| 140 |
$languages[] = array( |
| 141 |
'hreflang' => bogo_language_tag( $lang ), |
| 142 |
'href' => get_permalink( $translation ) ); |
| 143 |
} |
| 144 |
|
| 145 |
} else { |
| 146 |
$available_languages = bogo_available_languages(); |
| 147 |
|
| 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 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if ( ! $languages ) |
| 160 |
return; |
| 161 |
|
| 162 |
foreach ( $languages as $language ) |
| 163 |
echo '<link rel="alternate" hreflang="' . esc_attr( $language['hreflang'] ) . '" href="' . esc_url( $language['href'] ) . '" />' . "\n"; |
| 164 |
} |
| 165 |
|
| 166 |
add_shortcode( 'bogo', 'bogo_language_switcher' ); |
| 167 |
|
| 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 ); |
| 183 |
} |
| 184 |
|
| 185 |
echo '<ul class="language-switcher">'; |
| 186 |
|
| 187 |
$total = count( $available_languages ); |
| 188 |
$count = 0; |
| 189 |
|
| 190 |
foreach ( $available_languages as $code => $name ) { |
| 191 |
$count += 1; |
| 192 |
$class = array(); |
| 193 |
$class[] = bogo_language_tag( $code ); |
| 194 |
$class[] = bogo_lang_slug( $code ); |
| 195 |
|
| 196 |
if ( $locale == $code ) |
| 197 |
$class[] = 'current'; |
| 198 |
|
| 199 |
if ( 1 == $count ) |
| 200 |
$class[] = 'first'; |
| 201 |
|
| 202 |
if ( $total == $count ) |
| 203 |
$class[] = 'last'; |
| 204 |
|
| 205 |
$class = implode( ' ', array_unique( $class ) ); |
| 206 |
|
| 207 |
echo '<li class="' . esc_attr( $class ) . '">'; |
| 208 |
|
| 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>'; |
| 214 |
} 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>'; |
| 219 |
} |
| 220 |
|
| 221 |
echo '</li>'; |
| 222 |
} |
| 223 |
|
| 224 |
echo '</ul>' . "\n"; |
| 225 |
} |
| 226 |
|
| 227 |
?> |