PluginProbe
Polylang / 1.8.5
Polylang v1.8.5
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / api.php

api.php in Polylang 1.8.5, at include/api.php

344 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * template tag: displays the language switcher
5 *
6 * list of parameters accepted in $args:
7 *
8 * dropdown => displays a dropdown if set to 1, defaults to 0
9 * echo => echoes the the switcher if set to 1 ( default )
10 * hide_if_empty => hides languages with no posts ( or pages ) if set to 1 ( default )
11 * show_flags => shows flags if set to 1, defaults to 0
12 * show_names => shows languages names if set to 1 ( default )
13 * display_names_as => whether to display the language name or code. valid options are 'slug' and 'name'
14 * force_home => forces linking to the home page is set to 1, defaults to 0
15 * hide_if_no_translation => hides the link if there is no translation if set to 1, defaults to 0
16 * hide_current => hides the current language if set to 1, defaults to 0
17 * post_id => if not null, link to translations of post defined by post_id, defaults to null
18 * raw => set this to true to build your own custom language switcher, defaults to 0
19 *
20 * @since 0.5
21 *
22 * @param array $args optional
23 * @return null|string|array null if displaying, array if raw is requested, string otherwise
24 */
25 function pll_the_languages( $args = '' ) {
26 if ( PLL_ADMIN ) {
27 return '';
28 }
29
30 $switcher = new PLL_Switcher;
31 return $switcher->the_languages( PLL()->links, $args );
32 }
33
34 /*
35 * returns the current language
36 *
37 * @since 0.8.1
38 *
39 * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
40 * @return string the requested field for the current language
41 */
42 function pll_current_language( $field = 'slug' ) {
43 return isset( PLL()->curlang->$field ) ? PLL()->curlang->$field : false;
44 }
45
46 /*
47 * returns the default language
48 *
49 * @since 1.0
50 *
51 * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
52 * @return string the requested field for the default language
53 */
54 function pll_default_language( $field = 'slug' ) {
55 return isset( PLL()->options['default_lang'] ) && ( $lang = PLL()->model->get_language( PLL()->options['default_lang'] ) ) && isset( $lang->$field ) ? $lang->$field : false;
56 }
57
58 /*
59 * among the post and its translations, returns the id of the post which is in the language represented by $slug
60 *
61 * @since 0.5
62 *
63 * @param int $post_id post id
64 * @param string $slug optional language code, defaults to current language
65 * @return int post id of the translation if exists
66 */
67 function pll_get_post( $post_id, $slug = '' ) {
68 return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->post->get( $post_id, $slug ) : null;
69 }
70
71 /*
72 * among the term and its translations, returns the id of the term which is in the language represented by $slug
73 *
74 * @since 0.5
75 *
76 * @param int $term_id term id
77 * @param string $slug optional language code, defaults to current language
78 * @return int term id of the translation if exists
79 */
80 function pll_get_term( $term_id, $slug = '' ) {
81 return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->term->get( $term_id, $slug ) : null;
82 }
83
84 /*
85 * returns the home url in the current language
86 *
87 * @since 0.8
88 *
89 * @param string $lang language code ( optional on frontend )
90 * @return string
91 */
92 function pll_home_url( $lang = '' ) {
93 if ( empty( $lang ) ) {
94 $lang = pll_current_language();
95 }
96
97 return empty( $lang ) ? home_url( '/' ) : PLL()->links->get_home_url( $lang );
98 }
99
100 /*
101 * registers a string for translation in the "strings translation" panel
102 *
103 * @since 0.6
104 *
105 * @param string $name a unique name for the string
106 * @param string $string the string to register
107 * @param string $context optional the group in which the string is registered, defaults to 'polylang'
108 * @param bool $multiline optional wether the string table should display a multiline textarea or a single line input, defaults to single line
109 */
110 function pll_register_string( $name, $string, $context = 'polylang', $multiline = false ) {
111 if ( PLL_ADMIN ) {
112 PLL_Admin_Strings::register_string( $name, $string, $context, $multiline );
113 }
114 }
115
116 /*
117 * translates a string ( previously registered with pll_register_string )
118 *
119 * @since 0.6
120 *
121 * @param string $string the string to translate
122 * @return string the string translation in the current language
123 */
124 function pll__( $string ) {
125 static $cache; // cache object to avoid translating the same string several times
126
127 if ( ! did_action( 'pll_language_defined' ) ) { // no need for translation
128 return $string;
129 }
130
131 if ( empty( $cache ) ) {
132 $cache = new PLL_Cache();
133 }
134
135 if ( false === $str = $cache->get( $string ) ) {
136 $str = __( $string, 'pll_string' );
137 $cache->set( $string, $str );
138 }
139
140 return $str;
141 }
142
143 /*
144 * echoes a translated string ( previously registered with pll_register_string )
145 *
146 * @since 0.6
147 *
148 * @param string $string the string to translate
149 */
150 function pll_e( $string ) {
151 echo pll__( $string );
152 }
153
154 /*
155 * translates a string ( previously registered with pll_register_string )
156 *
157 * @since 1.5.4
158 *
159 * @param string $string the string to translate
160 * @param string $lang language code
161 * @return string the string translation in the requested language
162 */
163 function pll_translate_string( $string, $lang ) {
164 if ( pll_current_language() == $lang ) {
165 return pll__( $string );
166 }
167
168 static $cache; // cache object to avoid loading the same translations object several times
169
170 if ( empty( $cache ) ) {
171 $cache = new PLL_Cache();
172 }
173
174 if ( false === $mo = $cache->get( $lang ) ) {
175 $mo = new PLL_MO();
176 $mo->import_from_db( PLL()->model->get_language( $lang ) );
177 $cache->set( $lang, $mo );
178 }
179
180 return $mo->translate( $string );
181 }
182
183 /*
184 * returns true if Polylang manages languages and translations for this post type
185 *
186 * @since 1.0.1
187 *
188 * @param string post type name
189 * @return bool
190 */
191 function pll_is_translated_post_type( $post_type ) {
192 return PLL()->model->is_translated_post_type( $post_type );
193 }
194
195 /*
196 * returns true if Polylang manages languages and translations for this taxonomy
197 *
198 * @since 1.0.1
199 *
200 * @param string taxonomy name
201 * @return bool
202 */
203 function pll_is_translated_taxonomy( $tax ) {
204 return PLL()->model->is_translated_taxonomy( $tax );
205 }
206
207 /*
208 * returns the list of available languages
209 *
210 * list of parameters accepted in $args:
211 *
212 * hide_empty => hides languages with no posts if set to true ( defaults to false )
213 * fields => return only that field if set ( see PLL_Language for a list of fields )
214 *
215 * @since 1.5
216 *
217 * @param array $args list of parameters
218 * @return array
219 */
220 function pll_languages_list( $args = array() ) {
221 $args = wp_parse_args( $args, array( 'fields' => 'slug' ) );
222 return PLL()->model->get_languages_list( $args );
223 }
224
225 /*
226 * set the post language
227 *
228 * @since 1.5
229 *
230 * @param int $post_id post id
231 * @param string $lang language code
232 */
233 function pll_set_post_language( $id, $lang ) {
234 PLL()->model->post->set_language( $id, $lang );
235 }
236
237 /*
238 * set the term language
239 *
240 * @since 1.5
241 *
242 * @param int $id term id
243 * @param string $lang language code
244 */
245 function pll_set_term_language( $id, $lang ) {
246 PLL()->model->term->set_language( $id, $lang );
247 }
248
249 /*
250 * save posts translations
251 *
252 * @since 1.5
253 *
254 * @param array $arr an associative array of translations with language code as key and post id as value
255 */
256 function pll_save_post_translations( $arr ) {
257 PLL()->model->post->save_translations( reset( $arr ), $arr );
258 }
259
260 /*
261 * save terms translations
262 *
263 * @since 1.5
264 *
265 * @param array $arr an associative array of translations with language code as key and term id as value
266 */
267 function pll_save_term_translations( $arr ) {
268 PLL()->model->term->save_translations( reset( $arr ), $arr );
269 }
270
271 /*
272 * returns the post language
273 *
274 * @since 1.5.4
275 *
276 * @param int $post_id
277 * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
278 * @return bool|string the requested field for the post language, false if no language is associated to that post
279 */
280 function pll_get_post_language( $post_id, $field = 'slug' ) {
281 return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false;
282 }
283
284 /*
285 * returns the term language
286 *
287 * @since 1.5.4
288 *
289 * @param int $term_id
290 * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
291 * @return bool|string the requested field for the term language, false if no language is associated to that term
292 */
293 function pll_get_term_language( $term_id, $field = 'slug' ) {
294 return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false;
295 }
296
297 /*
298 * returns an array of translations of a post
299 *
300 * @since 1.8
301 *
302 * @param int $post_id
303 * @return array an associative array of translations with language code as key and translation post_id as value
304 */
305 function pll_get_post_translations( $post_id ) {
306 return PLL()->model->post->get_translations( $post_id );
307 }
308
309 /*
310 * returns an array of translations of a term
311 *
312 * @since 1.8
313 *
314 * @param int $term_id
315 * @return array an associative array of translations with language code as key and translation term_id as value
316 */
317 function pll_get_term_translations( $term_id ) {
318 return PLL()->model->term->get_translations( $term_id );
319 }
320
321 /*
322 * count posts in a language
323 *
324 * @since 1.5
325 *
326 * @param string $lang language code
327 * @param array $args ( accepted keys: post_type, m, year, monthnum, day, author, author_name, post_format )
328 * @return int posts count
329 */
330 function pll_count_posts( $lang, $args = array() ) {
331 return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args );
332 }
333
334 /*
335 * allows to access the Polylang instance
336 * it is always preferable to use API functions
337 * internal methods may be changed without prior notice
338 *
339 * @since 1.8
340 */
341 function PLL() {
342 return $GLOBALS['polylang'];
343 }
344