PluginProbe
Bogo / 2.1.3
Bogo v2.1.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.1.3, at includes/link-template.php

260 lines 6.5 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 $locale = get_locale();
151
152 if ( is_singular() ) {
153 $post_id = get_queried_object_id();
154
155 if ( ! $post_id || ! $translations = bogo_get_post_translations( $post_id ) )
156 return;
157
158 foreach ( $translations as $lang => $translation ) {
159 if ( $locale != $lang )
160 $languages[] = array(
161 'hreflang' => bogo_language_tag( $lang ),
162 'href' => get_permalink( $translation ) );
163 }
164
165 } else {
166 $available_languages = bogo_available_languages();
167
168 foreach ( array_keys( $available_languages ) as $lang ) {
169 if ( $locale != $lang ) {
170 $url = bogo_url( null, $lang );
171
172 $languages[] = array(
173 'hreflang' => bogo_language_tag( $lang ),
174 'href' => $url );
175 }
176 }
177 }
178
179 if ( ! $languages )
180 return;
181
182 foreach ( $languages as $language )
183 echo '<link rel="alternate" hreflang="' . esc_attr( $language['hreflang'] ) . '" href="' . esc_url( $language['href'] ) . '" />' . "\n";
184 }
185
186 add_shortcode( 'bogo', 'bogo_language_switcher' );
187
188 function bogo_language_switcher( $args = '' ) {
189 $defaults = array(
190 'echo' => false );
191
192 $args = wp_parse_args( $args, $defaults );
193
194 $locale = get_locale();
195 $available_languages = bogo_available_languages();
196
197 $translations = array();
198
199 if ( is_singular() ) {
200 $post_id = get_queried_object_id();
201
202 if ( $post_id ) {
203 $translations = bogo_get_post_translations( $post_id );
204 }
205 }
206
207 $output = '<ul class="language-switcher">';
208
209 $total = count( $available_languages );
210 $count = 0;
211
212 foreach ( $available_languages as $code => $name ) {
213 $count += 1;
214 $class = array();
215 $class[] = bogo_language_tag( $code );
216 $class[] = bogo_lang_slug( $code );
217
218 if ( $locale == $code ) {
219 $class[] = 'current';
220 }
221
222 if ( 1 == $count ) {
223 $class[] = 'first';
224 }
225
226 if ( $total == $count ) {
227 $class[] = 'last';
228 }
229
230 $class = implode( ' ', array_unique( $class ) );
231
232 $output .= '<li class="' . esc_attr( $class ) . '">';
233
234 if ( is_singular() ) {
235 if ( empty( $translations[$code] ) || $locale == $code ) {
236 $output .= esc_html( $name );
237 } else {
238 $output .= '<a rel="alternate" hreflang="' . bogo_language_tag( $code ) . '" href="' . get_permalink( $translations[$code] ) . '">' . esc_html( $name ) . '</a>';
239 }
240 } else {
241 if ( $locale == $code ) {
242 $output .= esc_html( $name );
243 } else {
244 $output .= '<a rel="alternate" hreflang="' . bogo_language_tag( $code ) . '" href="' . esc_url( bogo_url( null, $code ) ) . '">' . esc_html( $name ) . '</a>';
245 }
246 }
247
248 $output .= '</li>';
249 }
250
251 $output .= '</ul>' . "\n";
252
253 if ( $args['echo'] ) {
254 echo $output;
255 } else {
256 return $output;
257 }
258 }
259
260 ?>