PluginProbe
Bogo / 2.6
Bogo v2.6
3.9.3 trunk 1.0 1.1 2.0 2.0.1 2.1 2.1.1 2.1.2 2.1.3 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5 2.6 2.6.1 2.7 2.8 2.8.1 2.9 3.0 3.0.1 All 52 releases
← All changes | includes/link-template.php +132 -65 2.22.6 View file →
@@ -146,80 +146,65 @@
146 146 add_action( 'wp_head', 'bogo_m17n_headers' );
147 147
148 148 function bogo_m17n_headers() {
149 149 $languages = array();
150 - $locale = get_locale();
151 150
152 151 if ( is_singular() ) {
153 - if ( ! $post_id = get_queried_object_id() ) {
154 - return;
155 - }
152 + $post_id = get_queried_object_id();
156 153
157 - if ( ! $translations = bogo_get_post_translations( $post_id ) ) {
158 - return;
159 - }
154 + if ( $post_id && $translations = bogo_get_post_translations( $post_id ) ) {
155 + $locale = get_locale();
156 + $translations[$locale] = get_post( $post_id );
160 157
161 - $translations[$locale] = get_post( $post_id );
162 -
163 - foreach ( $translations as $lang => $translation ) {
164 - $languages[] = array(
165 - 'hreflang' => bogo_language_tag( $lang ),
166 - 'href' => get_permalink( $translation ) );
158 + foreach ( $translations as $lang => $translation ) {
159 + $languages[] = array(
160 + 'hreflang' => bogo_language_tag( $lang ),
161 + 'href' => get_permalink( $translation ) );
162 + }
167 163 }
168 -
169 164 } else {
170 - $available_languages = bogo_available_languages();
165 + $available_locales = bogo_available_locales( array(
166 + 'exclude_enus_if_inactive' => true ) );
171 167
172 - if ( count( $available_languages ) < 2 ) {
173 - return;
174 - }
175 -
176 - foreach ( array_keys( $available_languages ) as $lang ) {
168 + foreach ( $available_locales as $locale ) {
177 169 $languages[] = array(
178 - 'hreflang' => bogo_language_tag( $lang ),
179 - 'href' => bogo_url( null, $lang ) );
170 + 'hreflang' => bogo_language_tag( $locale ),
171 + 'href' => bogo_url( null, $locale ) );
180 172 }
181 173 }
182 174
183 - if ( ! $languages )
184 - return;
175 + $languages = apply_filters( 'bogo_rel_alternate_hreflang', $languages );
185 176
186 - foreach ( $languages as $language )
187 - echo '<link rel="alternate" hreflang="' . esc_attr( $language['hreflang'] ) . '" href="' . esc_url( $language['href'] ) . '" />' . "\n";
188 -}
177 + foreach ( (array) $languages as $language ) {
178 + $hreflang = isset( $language['hreflang'] ) ? $language['hreflang'] : '';
179 + $href = isset( $language['href'] ) ? $language['href'] : '';
189 180
190 -function bogo_language_switcher( $args = '' ) {
191 - global $wp_query;
181 + if ( $hreflang && $href ) {
182 + $link = sprintf( '<link rel="alternate" hreflang="%1$s" href="%2$s" />',
183 + esc_attr( $hreflang ), esc_url( $href ) );
192 184
193 - $defaults = array(
194 - 'echo' => false );
195 -
196 - $args = wp_parse_args( $args, $defaults );
197 -
198 - $locale = get_locale();
199 - $available_languages = bogo_available_languages();
200 -
201 - $translations = array();
202 - $is_singular = false;
203 - $post_id = get_queried_object_id();
204 -
205 - if ( is_singular() || ! empty( $wp_query->is_posts_page ) ) {
206 - $translations = bogo_get_post_translations( $post_id );
207 - $is_singular = true;
185 + echo $link . "\n";
186 + }
208 187 }
188 +}
209 189
210 - $output = '<ul class="language-switcher">';
190 +function bogo_language_switcher( $args = '' ) {
191 + $args = wp_parse_args( $args, array(
192 + 'echo' => false ) );
211 193
212 - $total = count( $available_languages );
194 + $links = bogo_language_switcher_links( $args );
195 + $total = count( $links );
213 196 $count = 0;
214 197
215 - foreach ( $available_languages as $code => $name ) {
198 + $output = '';
199 +
200 + foreach ( $links as $link ) {
216 201 $count += 1;
217 202 $class = array();
218 - $class[] = bogo_language_tag( $code );
219 - $class[] = bogo_lang_slug( $code );
203 + $class[] = bogo_language_tag( $link['locale'] );
204 + $class[] = bogo_lang_slug( $link['locale'] );
220 205
221 - if ( $locale == $code ) {
206 + if ( get_locale() == $link['locale'] ) {
222 207 $class[] = 'current';
223 208 }
224 209
225 210 if ( 1 == $count ) {
@@ -231,29 +216,31 @@
231 216 }
232 217
233 218 $class = implode( ' ', array_unique( $class ) );
234 219
235 - $output .= '<li class="' . esc_attr( $class ) . '">';
220 + $label = $link['native_name'] ? $link['native_name'] : $link['title'];
221 + $title = $link['title'];
236 222
237 - if ( $is_singular ) {
238 - if ( empty( $translations[$code] ) || $locale == $code ) {
239 - $output .= esc_html( $name );
240 - } else {
241 - $output .= '<a rel="alternate" hreflang="' . bogo_language_tag( $code ) . '" href="' . get_permalink( $translations[$code] ) . '">' . esc_html( $name ) . '</a>';
242 - }
223 + if ( empty( $link['href'] ) ) {
224 + $li = esc_html( $label );
243 225 } else {
244 - if ( $locale == $code ) {
245 - $output .= esc_html( $name );
246 - } else {
247 - $output .= '<a rel="alternate" hreflang="' . bogo_language_tag( $code ) . '" href="' . esc_url( bogo_url( null, $code ) ) . '">' . esc_html( $name ) . '</a>';
248 - }
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 ) );
249 232 }
250 233
251 - $output .= '</li>';
234 + $li = sprintf( '<li class="%1$s">%2$s</li>', $class, $li );
235 +
236 + $output .= $li . "\n";
252 237 }
253 238
254 - $output .= '</ul>' . "\n";
239 + $output = '<ul class="bogo-language-switcher">' . $output . '</ul>' . "\n";
255 240
241 + $output = apply_filters( 'bogo_language_switcher', $output, $args );
242 +
256 243 if ( $args['echo'] ) {
257 244 echo $output;
258 245 } else {
259 246 return $output;
@@ -259,5 +246,85 @@
259 246 return $output;
260 247 }
261 248 }
262 249
263 -?>
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 +}