PluginProbe
Bogo / 2.4.3
Bogo v2.4.3
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
bogo / includes / link-template.php

link-template.php in Bogo 2.4.3, at includes/link-template.php

325 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( empty( $link['href'] ) ) {
222 $li = esc_html( $link['title'] );
223 } else {
224 $li = sprintf(
225 '<a rel="alternate" hreflang="%1$s" href="%2$s">%3$s</a>',
226 $link['lang'], esc_url( $link['href'] ), esc_html( $link['title'] ) );
227 }
228
229 $li = sprintf( '<li class="%1$s">%2$s</li>', $class, $li );
230
231 $output .= $li . "\n";
232 }
233
234 $output = '<ul class="bogo-language-switcher">' . $output . '</ul>' . "\n";
235
236 $output = apply_filters( 'bogo_language_switcher', $output, $args );
237
238 if ( $args['echo'] ) {
239 echo $output;
240 } else {
241 return $output;
242 }
243 }
244
245 function bogo_language_switcher_links( $args = '' ) {
246 global $wp_query;
247
248 $args = wp_parse_args( $args, array() );
249
250 $locale = get_locale();
251 $available_languages = bogo_available_languages();
252
253 $translations = array();
254 $is_singular = false;
255
256 if ( is_singular() || ! empty( $wp_query->is_posts_page ) ) {
257 $translations = bogo_get_post_translations( get_queried_object_id() );
258 $is_singular = true;
259 }
260
261 $links = array();
262
263 foreach ( $available_languages as $code => $name ) {
264 $link = array(
265 'locale' => $code,
266 'lang' => bogo_language_tag( $code ),
267 'title' => $name,
268 'href' => '' );
269
270 if ( $is_singular ) {
271 if ( $locale != $code && ! empty( $translations[$code] ) ) {
272 $link['href'] = get_permalink( $translations[$code] );
273 }
274 } else {
275 if ( $locale != $code ) {
276 $link['href'] = bogo_url( null, $code );
277 }
278 }
279
280 $links[] = $link;
281 }
282
283 return apply_filters( 'bogo_language_switcher_links', $links, $args );
284 }
285
286 add_filter( 'get_previous_post_join', 'bogo_adjacent_post_join', 10, 3 );
287 add_filter( 'get_next_post_join', 'bogo_adjacent_post_join', 10, 3 );
288
289 function bogo_adjacent_post_join( $join, $in_same_term, $excluded_terms ) {
290 global $wpdb;
291
292 $post = get_post();
293
294 if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) {
295 $join .= " LEFT JOIN $wpdb->postmeta AS postmeta_bogo ON (p.ID = postmeta_bogo.post_id AND postmeta_bogo.meta_key = '_locale')";
296 }
297
298 return $join;
299 }
300
301 add_filter( 'get_previous_post_where', 'bogo_adjacent_post_where', 10, 3 );
302 add_filter( 'get_next_post_where', 'bogo_adjacent_post_where', 10, 3 );
303
304 function bogo_adjacent_post_where( $where, $in_same_term, $excluded_terms ) {
305 global $wpdb;
306
307 $post = get_post();
308
309 if ( $post && bogo_is_localizable_post_type( get_post_type( $post ) ) ) {
310 $locale = bogo_get_post_locale( $post->ID );
311
312 $where .= " AND (1=0";
313 $where .= $wpdb->prepare( " OR postmeta_bogo.meta_value LIKE %s", $locale );
314
315 if ( bogo_is_default_locale( $locale ) ) {
316 $where .= " OR postmeta_bogo.meta_id IS NULL";
317 }
318
319 $where .= ")";
320 }
321
322 return $where;
323 }
324
325 ?>