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

239 lines 6.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 return bogo_url( home_url( '/' ), $locale );
42 }
43 }
44
45 $permalink_structure = get_option( 'permalink_structure' );
46
47 $using_permalinks = $permalink_structure &&
48 ( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) );
49
50 $permalink = bogo_get_url_with_lang( $permalink, $locale,
51 array( 'using_permalinks' => $using_permalinks ) );
52
53 return $permalink;
54 }
55
56 add_filter( 'post_type_link', 'bogo_post_type_link', 10, 4 );
57
58 function bogo_post_type_link( $permalink, $post, $leavename, $sample ) {
59 if ( ! bogo_is_localizable_post_type( $post->post_type ) )
60 return $permalink;
61
62 $locale = bogo_get_post_locale( $post->ID );
63 $permalink_structure = get_option( 'permalink_structure' );
64
65 $using_permalinks = $permalink_structure &&
66 ( $sample || ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) );
67
68 $permalink = bogo_get_url_with_lang( $permalink, $locale,
69 array( 'using_permalinks' => $using_permalinks ) );
70
71 return $permalink;
72 }
73
74 add_filter( 'year_link', 'bogo_year_link', 10, 2 );
75
76 function bogo_year_link( $link, $year ) {
77 return bogo_get_general_link( $link );
78 }
79
80 add_filter( 'month_link', 'bogo_month_link', 10, 3 );
81
82 function bogo_month_link( $link, $year, $month ) {
83 return bogo_get_general_link( $link );
84 }
85
86 add_filter( 'day_link', 'bogo_day_link', 10, 4 );
87
88 function bogo_day_link( $link, $year, $month, $day ) {
89 return bogo_get_general_link( $link );
90 }
91
92 add_filter( 'feed_link', 'bogo_feed_link', 10, 2 );
93
94 function bogo_feed_link( $link, $feed ) {
95 return bogo_get_general_link( $link );
96 }
97
98 add_filter( 'author_feed_link', 'bogo_author_feed_link', 10, 2 );
99
100 function bogo_author_feed_link( $link, $feed ) {
101 return bogo_get_general_link( $link );
102 }
103
104 add_filter( 'category_feed_link', 'bogo_category_feed_link', 10, 2 );
105
106 function bogo_category_feed_link( $link, $feed ) {
107 return bogo_get_general_link( $link );
108 }
109
110 add_filter( 'taxonomy_feed_link', 'bogo_taxonomy_feed_link', 10, 3 );
111
112 function bogo_taxonomy_feed_link( $link, $feed, $taxonomy ) {
113 return bogo_get_general_link( $link );
114 }
115
116 add_filter( 'post_type_archive_link', 'bogo_post_type_archive_link', 10, 2 );
117
118 function bogo_post_type_archive_link( $link, $post_type ) {
119 return bogo_get_general_link( $link );
120 }
121
122 add_filter( 'post_type_archive_feed_link', 'bogo_post_type_archive_feed_link', 10, 2 );
123
124 function bogo_post_type_archive_feed_link( $link, $feed ) {
125 return bogo_get_general_link( $link );
126 }
127
128 add_filter( 'term_link', 'bogo_term_link', 10, 3 );
129
130 function bogo_term_link( $link, $term, $taxonomy ) {
131 return bogo_get_general_link( $link );
132 }
133
134 function bogo_get_general_link( $link ) {
135 return bogo_url( $link, get_locale() );
136 }
137
138 add_action( 'wp_head', 'bogo_m17n_headers' );
139
140 function bogo_m17n_headers() {
141 $languages = array();
142 $locale = get_locale();
143
144 if ( is_singular() ) {
145 $post_id = get_queried_object_id();
146
147 if ( ! $post_id || ! $translations = bogo_get_post_translations( $post_id ) )
148 return;
149
150 foreach ( $translations as $lang => $translation ) {
151 if ( $locale != $lang )
152 $languages[] = array(
153 'hreflang' => bogo_language_tag( $lang ),
154 'href' => get_permalink( $translation ) );
155 }
156
157 } else {
158 $available_languages = bogo_available_languages();
159
160 foreach ( array_keys( $available_languages ) as $lang ) {
161 if ( $locale != $lang ) {
162 $url = bogo_url( null, $lang );
163
164 $languages[] = array(
165 'hreflang' => bogo_language_tag( $lang ),
166 'href' => $url );
167 }
168 }
169 }
170
171 if ( ! $languages )
172 return;
173
174 foreach ( $languages as $language )
175 echo '<link rel="alternate" hreflang="' . esc_attr( $language['hreflang'] ) . '" href="' . esc_url( $language['href'] ) . '" />' . "\n";
176 }
177
178 add_shortcode( 'bogo', 'bogo_language_switcher' );
179
180 function bogo_language_switcher( $args = '' ) {
181 $defaults = array();
182
183 $args = wp_parse_args( $args, $defaults );
184
185 $locale = get_locale();
186 $available_languages = bogo_available_languages();
187
188 $translations = array();
189
190 if ( is_singular() ) {
191 $post_id = get_queried_object_id();
192
193 if ( $post_id )
194 $translations = bogo_get_post_translations( $post_id );
195 }
196
197 echo '<ul class="language-switcher">';
198
199 $total = count( $available_languages );
200 $count = 0;
201
202 foreach ( $available_languages as $code => $name ) {
203 $count += 1;
204 $class = array();
205 $class[] = bogo_language_tag( $code );
206 $class[] = bogo_lang_slug( $code );
207
208 if ( $locale == $code )
209 $class[] = 'current';
210
211 if ( 1 == $count )
212 $class[] = 'first';
213
214 if ( $total == $count )
215 $class[] = 'last';
216
217 $class = implode( ' ', array_unique( $class ) );
218
219 echo '<li class="' . esc_attr( $class ) . '">';
220
221 if ( is_singular() ) {
222 if ( empty( $translations[$code] ) || $locale == $code )
223 echo esc_html( $name );
224 else
225 echo '<a rel="alternate" hreflang="' . bogo_language_tag( $code ) . '" href="' . get_permalink( $translations[$code] ) . '">' . esc_html( $name ) . '</a>';
226 } else {
227 if ( $locale == $code )
228 echo esc_html( $name );
229 else
230 echo '<a rel="alternate" hreflang="' . bogo_language_tag( $code ) . '" href="' . esc_url( bogo_url( null, $code ) ) . '">' . esc_html( $name ) . '</a>';
231 }
232
233 echo '</li>';
234 }
235
236 echo '</ul>' . "\n";
237 }
238
239 ?>