PluginProbe
Polylang / 2.6.2
Polylang v2.6.2
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 2.6.2, at include/api.php

381 lines 11.4 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 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 its slug, valid options are 'slug' and 'name', defaults to 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 * item_spacing => whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to preserve
20 *
21 * @since 0.5
22 *
23 * @param array $args optional
24 * @return null|string|array null if displaying, array if raw is requested, string otherwise
25 */
26 function pll_the_languages( $args = '' ) {
27 if ( PLL() instanceof PLL_Frontend ) {
28 $switcher = new PLL_Switcher();
29 return $switcher->the_languages( PLL()->links, $args );
30 }
31 return '';
32 }
33
34 /**
35 * Returns the current language on frontend
36 * Returns the language set in admin language filter on backend ( false if set to all languages )
37 *
38 * @since 0.8.1
39 *
40 * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug'
41 * @return string|bool The requested field for the current language
42 */
43 function pll_current_language( $field = 'slug' ) {
44 return isset( PLL()->curlang->$field ) ? PLL()->curlang->$field : false;
45 }
46
47 /**
48 * Returns the default language
49 *
50 * @since 1.0
51 *
52 * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug'
53 * @return string The requested field for the default language
54 */
55 function pll_default_language( $field = 'slug' ) {
56 return isset( PLL()->options['default_lang'] ) && ( $lang = PLL()->model->get_language( PLL()->options['default_lang'] ) ) && isset( $lang->$field ) ? $lang->$field : false;
57 }
58
59 /**
60 * Among the post and its translations, returns the id of the post which is in the language represented by $slug
61 *
62 * @since 0.5
63 *
64 * @param int $post_id post id
65 * @param string $slug optional language code, defaults to current language
66 * @return int|false|null post id of the translation if exists, false otherwise, null if the current language is not defined yet
67 */
68 function pll_get_post( $post_id, $slug = '' ) {
69 return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->post->get( $post_id, $slug ) : null;
70 }
71
72 /**
73 * Among the term and its translations, returns the id of the term which is in the language represented by $slug
74 *
75 * @since 0.5
76 *
77 * @param int $term_id term id
78 * @param string $slug optional language code, defaults to current language
79 * @return int|false|null term id of the translation if exists, false otherwise, null if the current language is not defined yet
80 */
81 function pll_get_term( $term_id, $slug = '' ) {
82 return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->term->get( $term_id, $slug ) : null;
83 }
84
85 /**
86 * Returns the home url in the current language
87 *
88 * @since 0.8
89 *
90 * @param string $lang language code ( optional on frontend )
91 * @return string
92 */
93 function pll_home_url( $lang = '' ) {
94 if ( empty( $lang ) ) {
95 $lang = pll_current_language();
96 }
97
98 return empty( $lang ) ? home_url( '/' ) : PLL()->links->get_home_url( $lang );
99 }
100
101 /**
102 * Registers a string for translation in the "strings translation" panel
103 *
104 * @since 0.6
105 *
106 * @param string $name a unique name for the string
107 * @param string $string the string to register
108 * @param string $context optional the group in which the string is registered, defaults to 'polylang'
109 * @param bool $multiline optional whether the string table should display a multiline textarea or a single line input, defaults to single line
110 */
111 function pll_register_string( $name, $string, $context = 'polylang', $multiline = false ) {
112 if ( PLL() instanceof PLL_Admin_Base ) {
113 PLL_Admin_Strings::register_string( $name, $string, $context, $multiline );
114 }
115 }
116
117 /**
118 * Translates a string ( previously registered with pll_register_string )
119 *
120 * @since 0.6
121 *
122 * @param string $string the string to translate
123 * @return string the string translation in the current language
124 */
125 function pll__( $string ) {
126 return is_scalar( $string ) ? __( $string, 'pll_string' ) : $string; // PHPCS:ignore WordPress.WP.I18n
127 }
128
129 /**
130 * Translates a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output.
131 *
132 * @since 2.1
133 *
134 * @param string $string the string to translate
135 * @return string translation in the current language
136 */
137 function pll_esc_html__( $string ) {
138 return esc_html( pll__( $string ) );
139 }
140
141 /**
142 * Translates a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML attributes.
143 *
144 * @since 2.1
145 *
146 * @param string $string The string to translate
147 * @return string
148 */
149 function pll_esc_attr__( $string ) {
150 return esc_attr( pll__( $string ) );
151 }
152
153 /**
154 * Echoes a translated string ( previously registered with pll_register_string )
155 * It is an equivalent of _e() and is not escaped.
156 *
157 * @since 0.6
158 *
159 * @param string $string The string to translate
160 */
161 function pll_e( $string ) {
162 echo pll__( $string ); // phpcs:ignore
163 }
164
165 /**
166 * Echoes a translated string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output.
167 *
168 * @since 2.1
169 *
170 * @param string $string The string to translate
171 */
172 function pll_esc_html_e( $string ) {
173 echo pll_esc_html__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
174 }
175
176 /**
177 * Echoes a translated a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML attributes.
178 *
179 * @since 2.1
180 *
181 * @param string $string The string to translate
182 */
183 function pll_esc_attr_e( $string ) {
184 echo pll_esc_attr__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
185 }
186
187 /**
188 * Translates a string ( previously registered with pll_register_string )
189 *
190 * @since 1.5.4
191 *
192 * @param string $string the string to translate
193 * @param string $lang language code
194 * @return string the string translation in the requested language
195 */
196 function pll_translate_string( $string, $lang ) {
197 if ( PLL() instanceof PLL_Frontend && pll_current_language() == $lang ) {
198 return pll__( $string );
199 }
200
201 if ( ! is_scalar( $string ) ) {
202 return $string;
203 }
204
205 static $cache; // Cache object to avoid loading the same translations object several times
206
207 if ( empty( $cache ) ) {
208 $cache = new PLL_Cache();
209 }
210
211 if ( false === $mo = $cache->get( $lang ) ) {
212 $mo = new PLL_MO();
213 $mo->import_from_db( PLL()->model->get_language( $lang ) );
214 $cache->set( $lang, $mo );
215 }
216
217 return $mo->translate( $string );
218 }
219
220 /**
221 * Returns true if Polylang manages languages and translations for this post type
222 *
223 * @since 1.0.1
224 *
225 * @param string $post_type Post type name
226 * @return bool
227 */
228 function pll_is_translated_post_type( $post_type ) {
229 return PLL()->model->is_translated_post_type( $post_type );
230 }
231
232 /**
233 * Returns true if Polylang manages languages and translations for this taxonomy
234 *
235 * @since 1.0.1
236 *
237 * @param string $tax Taxonomy name
238 * @return bool
239 */
240 function pll_is_translated_taxonomy( $tax ) {
241 return PLL()->model->is_translated_taxonomy( $tax );
242 }
243
244 /**
245 * Returns the list of available languages
246 *
247 * List of parameters accepted in $args:
248 *
249 * hide_empty => hides languages with no posts if set to true ( defaults to false )
250 * fields => return only that field if set ( see PLL_Language for a list of fields )
251 *
252 * @since 1.5
253 *
254 * @param array $args list of parameters
255 * @return array
256 */
257 function pll_languages_list( $args = array() ) {
258 $args = wp_parse_args( $args, array( 'fields' => 'slug' ) );
259 return PLL()->model->get_languages_list( $args );
260 }
261
262 /**
263 * Set the post language
264 *
265 * @since 1.5
266 *
267 * @param int $id post id
268 * @param string $lang language code
269 */
270 function pll_set_post_language( $id, $lang ) {
271 PLL()->model->post->set_language( $id, $lang );
272 }
273
274 /**
275 * Set the term language
276 *
277 * @since 1.5
278 *
279 * @param int $id term id
280 * @param string $lang language code
281 */
282 function pll_set_term_language( $id, $lang ) {
283 PLL()->model->term->set_language( $id, $lang );
284 }
285
286 /**
287 * Save posts translations
288 *
289 * @since 1.5
290 *
291 * @param array $arr an associative array of translations with language code as key and post id as value
292 */
293 function pll_save_post_translations( $arr ) {
294 PLL()->model->post->save_translations( reset( $arr ), $arr );
295 }
296
297 /**
298 * Save terms translations
299 *
300 * @since 1.5
301 *
302 * @param array $arr an associative array of translations with language code as key and term id as value
303 */
304 function pll_save_term_translations( $arr ) {
305 PLL()->model->term->save_translations( reset( $arr ), $arr );
306 }
307
308 /**
309 * Returns the post language
310 *
311 * @since 1.5.4
312 *
313 * @param int $post_id
314 * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug'
315 * @return bool|string The requested field for the post language, false if no language is associated to that post
316 */
317 function pll_get_post_language( $post_id, $field = 'slug' ) {
318 return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false;
319 }
320
321 /**
322 * Returns the term language
323 *
324 * @since 1.5.4
325 *
326 * @param int $term_id
327 * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug'
328 * @return bool|string The requested field for the term language, false if no language is associated to that term
329 */
330 function pll_get_term_language( $term_id, $field = 'slug' ) {
331 return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false;
332 }
333
334 /**
335 * Returns an array of translations of a post
336 *
337 * @since 1.8
338 *
339 * @param int $post_id
340 * @return array an associative array of translations with language code as key and translation post_id as value
341 */
342 function pll_get_post_translations( $post_id ) {
343 return PLL()->model->post->get_translations( $post_id );
344 }
345
346 /**
347 * Returns an array of translations of a term
348 *
349 * @since 1.8
350 *
351 * @param int $term_id
352 * @return array an associative array of translations with language code as key and translation term_id as value
353 */
354 function pll_get_term_translations( $term_id ) {
355 return PLL()->model->term->get_translations( $term_id );
356 }
357
358 /**
359 * Count posts in a language
360 *
361 * @since 1.5
362 *
363 * @param string $lang language code
364 * @param array $args ( accepted keys: post_type, m, year, monthnum, day, author, author_name, post_format )
365 * @return int posts count
366 */
367 function pll_count_posts( $lang, $args = array() ) {
368 return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args );
369 }
370
371 /**
372 * Allows to access the Polylang instance
373 * It is always preferable to use API functions
374 * Internal methods may be changed without prior notice
375 *
376 * @since 1.8
377 */
378 function PLL() { // PHPCS:ignore WordPress.NamingConventions.ValidFunctionName
379 return $GLOBALS['polylang'];
380 }
381