PluginProbe
Bogo / 2.4
Bogo v2.4
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 +113 -78 trunk2.4 View file →
@@ -2,14 +2,13 @@
2 2
3 3 add_filter( 'post_link', 'bogo_post_link', 10, 3 );
4 4
5 5 function bogo_post_link( $permalink, $post, $leavename ) {
6 - if ( ! bogo_is_localizable_post_type( $post->post_type ) ) {
6 + if ( ! bogo_is_localizable_post_type( $post->post_type ) )
7 7 return $permalink;
8 - }
9 8
10 9 $locale = bogo_get_post_locale( $post->ID );
11 - $sample = ( isset( $post->filter ) && 'sample' === $post->filter );
10 + $sample = ( isset( $post->filter ) && 'sample' == $post->filter );
12 11 $permalink_structure = get_option( 'permalink_structure' );
13 12
14 13 $using_permalinks = $permalink_structure &&
15 14 ( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) );
@@ -14,38 +13,36 @@
14 13 $using_permalinks = $permalink_structure &&
15 14 ( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) );
16 15
17 16 $permalink = bogo_get_url_with_lang( $permalink, $locale,
18 - array( 'using_permalinks' => $using_permalinks )
19 - );
17 + array( 'using_permalinks' => $using_permalinks ) );
20 18
21 19 return $permalink;
22 20 }
23 21
24 -
25 22 add_filter( 'page_link', 'bogo_page_link', 10, 3 );
26 23
27 -function bogo_page_link( $permalink, $post_id, $sample ) {
28 - if ( ! bogo_is_localizable_post_type( 'page' ) ) {
24 +function bogo_page_link( $permalink, $id, $sample ) {
25 + if ( ! bogo_is_localizable_post_type( 'page' ) )
29 26 return $permalink;
30 - }
31 27
32 - $locale = bogo_get_post_locale( $post_id );
33 - $post = get_post( $post_id );
28 + $locale = bogo_get_post_locale( $id );
29 + $post = get_post( $id );
34 30
35 - if ( 'page' === get_option( 'show_on_front' ) ) {
31 + if ( 'page' == get_option( 'show_on_front' ) ) {
36 32 $front_page_id = get_option( 'page_on_front' );
37 33
38 - if ( $post_id == $front_page_id ) {
34 + if ( $id == $front_page_id )
39 35 return $permalink;
40 - }
41 36
42 - $translation = bogo_get_post_translation( $front_page_id, $locale );
37 + $translations = bogo_get_post_translations( $front_page_id );
43 38
44 - if ( $translation and $translation->ID === $post_id ) {
45 - $home = set_url_scheme( get_option( 'home' ) );
46 - $home = trailingslashit( $home );
47 - return bogo_url( $home, $locale );
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 + }
48 45 }
49 46 }
50 47
51 48 $permalink_structure = get_option( 'permalink_structure' );
@@ -53,21 +50,18 @@
53 50 $using_permalinks = $permalink_structure &&
54 51 ( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) );
55 52
56 53 $permalink = bogo_get_url_with_lang( $permalink, $locale,
57 - array( 'using_permalinks' => $using_permalinks )
58 - );
54 + array( 'using_permalinks' => $using_permalinks ) );
59 55
60 56 return $permalink;
61 57 }
62 58
63 -
64 59 add_filter( 'post_type_link', 'bogo_post_type_link', 10, 4 );
65 60
66 61 function bogo_post_type_link( $permalink, $post, $leavename, $sample ) {
67 - if ( ! bogo_is_localizable_post_type( $post->post_type ) ) {
62 + if ( ! bogo_is_localizable_post_type( $post->post_type ) )
68 63 return $permalink;
69 - }
70 64
71 65 $locale = bogo_get_post_locale( $post->ID );
72 66 $permalink_structure = get_option( 'permalink_structure' );
73 67
@@ -74,15 +68,13 @@
74 68 $using_permalinks = $permalink_structure &&
75 69 ( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) );
76 70
77 71 $permalink = bogo_get_url_with_lang( $permalink, $locale,
78 - array( 'using_permalinks' => $using_permalinks )
79 - );
72 + array( 'using_permalinks' => $using_permalinks ) );
80 73
81 74 return $permalink;
82 75 }
83 76
84 -
85 77 add_filter( 'year_link', 'bogo_year_link', 10, 2 );
86 78
87 79 function bogo_year_link( $link, $year ) {
88 80 return bogo_url( $link );
@@ -87,9 +79,8 @@
87 79 function bogo_year_link( $link, $year ) {
88 80 return bogo_url( $link );
89 81 }
90 82
91 -
92 83 add_filter( 'month_link', 'bogo_month_link', 10, 3 );
93 84
94 85 function bogo_month_link( $link, $year, $month ) {
95 86 return bogo_url( $link );
@@ -94,9 +85,8 @@
94 85 function bogo_month_link( $link, $year, $month ) {
95 86 return bogo_url( $link );
96 87 }
97 88
98 -
99 89 add_filter( 'day_link', 'bogo_day_link', 10, 4 );
100 90
101 91 function bogo_day_link( $link, $year, $month, $day ) {
102 92 return bogo_url( $link );
@@ -101,9 +91,8 @@
101 91 function bogo_day_link( $link, $year, $month, $day ) {
102 92 return bogo_url( $link );
103 93 }
104 94
105 -
106 95 add_filter( 'feed_link', 'bogo_feed_link', 10, 2 );
107 96
108 97 function bogo_feed_link( $link, $feed ) {
109 98 return bogo_url( $link );
@@ -108,9 +97,8 @@
108 97 function bogo_feed_link( $link, $feed ) {
109 98 return bogo_url( $link );
110 99 }
111 100
112 -
113 101 add_filter( 'author_feed_link', 'bogo_author_feed_link', 10, 2 );
114 102
115 103 function bogo_author_feed_link( $link, $feed ) {
116 104 return bogo_url( $link );
@@ -115,9 +103,8 @@
115 103 function bogo_author_feed_link( $link, $feed ) {
116 104 return bogo_url( $link );
117 105 }
118 106
119 -
120 107 add_filter( 'category_feed_link', 'bogo_category_feed_link', 10, 2 );
121 108
122 109 function bogo_category_feed_link( $link, $feed ) {
123 110 return bogo_url( $link );
@@ -122,9 +109,8 @@
122 109 function bogo_category_feed_link( $link, $feed ) {
123 110 return bogo_url( $link );
124 111 }
125 112
126 -
127 113 add_filter( 'taxonomy_feed_link', 'bogo_taxonomy_feed_link', 10, 3 );
128 114
129 115 function bogo_taxonomy_feed_link( $link, $feed, $taxonomy ) {
130 116 return bogo_url( $link );
@@ -129,9 +115,8 @@
129 115 function bogo_taxonomy_feed_link( $link, $feed, $taxonomy ) {
130 116 return bogo_url( $link );
131 117 }
132 118
133 -
134 119 add_filter( 'post_type_archive_link', 'bogo_post_type_archive_link', 10, 2 );
135 120
136 121 function bogo_post_type_archive_link( $link, $post_type ) {
137 122 return bogo_url( $link );
@@ -136,19 +121,14 @@
136 121 function bogo_post_type_archive_link( $link, $post_type ) {
137 122 return bogo_url( $link );
138 123 }
139 124
125 +add_filter( 'post_type_archive_feed_link', 'bogo_post_type_archive_feed_link', 10, 2 );
140 126
141 -add_filter( 'post_type_archive_feed_link',
142 - 'bogo_post_type_archive_feed_link',
143 - 10, 2
144 -);
145 -
146 127 function bogo_post_type_archive_feed_link( $link, $feed ) {
147 128 return bogo_url( $link );
148 129 }
149 130
150 -
151 131 add_filter( 'term_link', 'bogo_term_link', 10, 3 );
152 132
153 133 function bogo_term_link( $link, $term, $taxonomy ) {
154 134 return bogo_url( $link );
@@ -153,22 +133,19 @@
153 133 function bogo_term_link( $link, $term, $taxonomy ) {
154 134 return bogo_url( $link );
155 135 }
156 136
137 +add_filter( 'home_url', 'bogo_home_url' );
157 138
158 -add_filter( 'home_url', 'bogo_home_url', 10, 1 );
159 -
160 139 function bogo_home_url( $url ) {
161 - if ( is_admin() or ! did_action( 'template_redirect' ) ) {
140 + if ( is_admin() || ! did_action( 'template_redirect' ) )
162 141 return $url;
163 - }
164 142
165 143 return bogo_url( $url );
166 144 }
167 145
146 +add_action( 'wp_head', 'bogo_m17n_headers' );
168 147
169 -add_action( 'wp_head', 'bogo_m17n_headers', 10, 0 );
170 -
171 148 function bogo_m17n_headers() {
172 149 $languages = array();
173 150
174 151 if ( is_singular() ) {
@@ -173,9 +150,9 @@
173 150
174 151 if ( is_singular() ) {
175 152 $post_id = get_queried_object_id();
176 153
177 - if ( $post_id and $translations = bogo_get_post_translations( $post_id ) ) {
154 + if ( $post_id && $translations = bogo_get_post_translations( $post_id ) ) {
178 155 $locale = get_locale();
179 156 $translations[$locale] = get_post( $post_id );
180 157
181 158 foreach ( $translations as $lang => $translation ) {
@@ -180,20 +157,20 @@
180 157
181 158 foreach ( $translations as $lang => $translation ) {
182 159 $languages[] = array(
183 160 'hreflang' => bogo_language_tag( $lang ),
184 - 'href' => get_permalink( $translation ),
185 - );
161 + 'href' => get_permalink( $translation ) );
186 162 }
187 163 }
188 164 } else {
189 165 $available_locales = bogo_available_locales();
190 166
191 - foreach ( $available_locales as $locale ) {
192 - $languages[] = array(
193 - 'hreflang' => bogo_language_tag( $locale ),
194 - 'href' => bogo_url( null, $locale ),
195 - );
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 + }
196 173 }
197 174 }
198 175
199 176 $languages = apply_filters( 'bogo_rel_alternate_hreflang', $languages );
@@ -198,36 +175,93 @@
198 175
199 176 $languages = apply_filters( 'bogo_rel_alternate_hreflang', $languages );
200 177
201 178 foreach ( (array) $languages as $language ) {
202 - $hreflang = $language['hreflang'] ?? '';
203 - $href = $language['href'] ?? '';
179 + $hreflang = isset( $language['hreflang'] ) ? $language['hreflang'] : '';
180 + $href = isset( $language['href'] ) ? $language['href'] : '';
204 181
205 - if ( $hreflang and $href ) {
206 - $link = sprintf(
207 - '<link %s />',
208 - bogo_format_atts( array(
209 - 'rel' => 'alternate',
210 - 'href' => esc_url( $href ),
211 - 'hreflang' => $hreflang,
212 - ) )
213 - );
182 + if ( $hreflang && $href ) {
183 + $link = sprintf( '<link rel="alternate" hreflang="%1$s" href="%2$s" />',
184 + esc_attr( $hreflang ), esc_url( $href ) );
214 185
215 - echo wp_kses(
216 - $link,
217 - array(
218 - 'link' => array(
219 - 'rel' => true,
220 - 'href' => true,
221 - 'hreflang' => true,
222 - ),
223 - )
224 - ) . "\n";
186 + echo $link . "\n";
225 187 }
226 188 }
227 189 }
228 190
191 +function bogo_language_switcher( $args = '' ) {
192 + global $wp_query;
229 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 +
230 264 add_filter( 'get_previous_post_join', 'bogo_adjacent_post_join', 10, 3 );
231 265 add_filter( 'get_next_post_join', 'bogo_adjacent_post_join', 10, 3 );
232 266
233 267 function bogo_adjacent_post_join( $join, $in_same_term, $excluded_terms ) {
@@ -234,9 +268,9 @@
234 268 global $wpdb;
235 269
236 270 $post = get_post();
237 271
238 - if ( $post and bogo_is_localizable_post_type( get_post_type( $post ) ) ) {
272 + if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) {
239 273 $join .= " LEFT JOIN $wpdb->postmeta AS postmeta_bogo ON (p.ID = postmeta_bogo.post_id AND postmeta_bogo.meta_key = '_locale')";
240 274 }
241 275
242 276 return $join;
@@ -241,9 +275,8 @@
241 275
242 276 return $join;
243 277 }
244 278
245 -
246 279 add_filter( 'get_previous_post_where', 'bogo_adjacent_post_where', 10, 3 );
247 280 add_filter( 'get_next_post_where', 'bogo_adjacent_post_where', 10, 3 );
248 281
249 282 function bogo_adjacent_post_where( $where, $in_same_term, $excluded_terms ) {
@@ -250,9 +283,9 @@
250 283 global $wpdb;
251 284
252 285 $post = get_post();
253 286
254 - if ( $post and bogo_is_localizable_post_type( get_post_type( $post ) ) ) {
287 + if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) {
255 288 $locale = bogo_get_post_locale( $post->ID );
256 289
257 290 $where .= " AND (1=0";
258 291 $where .= $wpdb->prepare( " OR postmeta_bogo.meta_value LIKE %s", $locale );
@@ -265,4 +298,6 @@
265 298 }
266 299
267 300 return $where;
268 301 }
302 +
303 +?>