PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
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
← All changes | include/api.php +127 -107 2.9.13.1.4 View file →
@@ -3,50 +3,46 @@
3 3 * @package Polylang
4 4 */
5 5
6 6 /**
7 - * Template tag: displays the language switcher
7 + * Template tag: displays the language switcher.
8 + * The function does nothing if used outside the frontend.
8 9 *
9 - * List of parameters accepted in $args:
10 - *
11 - * - dropdown => displays a dropdown if set to 1, defaults to 0
12 - * - echo => echoes the switcher if set to 1 ( default )
13 - * - hide_if_empty => hides languages with no posts ( or pages ) if set to 1 ( default )
14 - * - show_flags => shows flags if set to 1, defaults to 0
15 - * - show_names => shows languages names if set to 1 ( default )
16 - * - display_names_as => whether to display the language name or its slug, valid options are 'slug' and 'name',
17 - * defaults to name
18 - * - force_home => forces linking to the home page is set to 1, defaults to 0
19 - * - hide_if_no_translation => hides the link if there is no translation if set to 1, defaults to 0
20 - * - hide_current => hides the current language if set to 1, defaults to 0
21 - * - post_id => if not null, link to translations of post defined by post_id, defaults to null
22 - * - raw => set this to true to build your own custom language switcher, defaults to 0
23 - * - item_spacing => whether to preserve or discard whitespace between list items, valid options are
24 - * 'preserve' and 'discard', defaults to preserve
25 - *
26 10 * @api
27 11 * @since 0.5
28 12 *
29 - * @param array $args optional
30 - * @return null|string|array null if displaying, array if raw is requested, string otherwise
13 + * @param array $args {
14 + * Optional array of arguments.
15 + *
16 + * @type int $dropdown The list is displayed as dropdown if set to 1, defaults to 0.
17 + * @type int $echo Echoes the list if set to 1, defaults to 1.
18 + * @type int $hide_if_empty Hides languages with no posts ( or pages ) if set to 1, defaults to 1.
19 + * @type int $show_flags Displays flags if set to 1, defaults to 0.
20 + * @type int $show_names Shows language names if set to 1, defaults to 1.
21 + * @type string $display_names_as Whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name.
22 + * @type int $force_home Will always link to the homepage in the translated language if set to 1, defaults to 0.
23 + * @type int $hide_if_no_translation Hides the link if there is no translation if set to 1, defaults to 0.
24 + * @type int $hide_current Hides the current language if set to 1, defaults to 0.
25 + * @type int $post_id Returns links to the translations of the post defined by post_id if set, defaults to not set.
26 + * @type int $raw Return a raw array instead of html markup if set to 1, defaults to 0.
27 + * @type string $item_spacing Whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to 'preserve'.
28 + * }
29 + * @return string|array Either the html markup of the switcher or the raw elements to build a custom language switcher.
31 30 */
32 -function pll_the_languages( $args = '' ) {
33 - if ( PLL() instanceof PLL_Frontend ) {
34 - $switcher = new PLL_Switcher();
35 - return $switcher->the_languages( PLL()->links, $args );
36 - }
37 - return '';
31 +function pll_the_languages( $args = array() ) {
32 + $switcher = new PLL_Switcher();
33 + return $switcher->the_languages( PLL()->links, $args );
38 34 }
39 35
40 36 /**
41 - * Returns the current language on frontend
42 - * Returns the language set in admin language filter on backend ( false if set to all languages )
37 + * Returns the current language on frontend.
38 + * Returns the language set in admin language filter on backend ( false if set to all languages ).
43 39 *
44 40 * @api
45 41 * @since 0.8.1
46 42 *
47 - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug', pass OBJECT constant to get the language object.
48 - * @return string|PLL_Language|bool The requested field for the current language
43 + * @param string $field Optional, the language field to return ( @see PLL_Language ), defaults to 'slug'. Pass OBJECT constant to get the language object.
44 + * @return string|PLL_Language|false The requested field for the current language.
49 45 */
50 46 function pll_current_language( $field = 'slug' ) {
51 47 if ( OBJECT === $field ) {
52 48 return PLL()->curlang;
@@ -54,15 +50,15 @@
54 50 return isset( PLL()->curlang->$field ) ? PLL()->curlang->$field : false;
55 51 }
56 52
57 53 /**
58 - * Returns the default language
54 + * Returns the default language.
59 55 *
60 56 * @api
61 57 * @since 1.0
62 58 *
63 - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug', pass OBJECT constant to get the language object.
64 - * @return string|PLL_Language|bool The requested field for the default language
59 + * @param string $field Optional, the language field to return ( @see PLL_Language ), defaults to 'slug'. Pass OBJECT constant to get the language object.
60 + * @return string|PLL_Language|false The requested field for the default language.
65 61 */
66 62 function pll_default_language( $field = 'slug' ) {
67 63 if ( isset( PLL()->options['default_lang'] ) ) {
68 64 $lang = PLL()->model->get_language( PLL()->options['default_lang'] );
@@ -76,42 +72,42 @@
76 72 return false;
77 73 }
78 74
79 75 /**
80 - * Among the post and its translations, returns the id of the post which is in the language represented by $slug
76 + * Among the post and its translations, returns the id of the post which is in the language represented by $lang.
81 77 *
82 78 * @api
83 79 * @since 0.5
84 80 *
85 - * @param int $post_id post id
86 - * @param string $slug optional language code, defaults to current language
87 - * @return int|false|null post id of the translation if exists, false otherwise, null if the current language is not defined yet
81 + * @param int $post_id Post id.
82 + * @param string $lang Optional language code, defaults to the current language.
83 + * @return int|false|null Post id of the translation if it exists, false otherwise, null if the current language is not defined yet.
88 84 */
89 -function pll_get_post( $post_id, $slug = '' ) {
90 - return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->post->get( $post_id, $slug ) : null;
85 +function pll_get_post( $post_id, $lang = '' ) {
86 + return ( $lang = $lang ? $lang : pll_current_language() ) ? PLL()->model->post->get( $post_id, $lang ) : null;
91 87 }
92 88
93 89 /**
94 - * Among the term and its translations, returns the id of the term which is in the language represented by $slug
90 + * Among the term and its translations, returns the id of the term which is in the language represented by $lang.
95 91 *
96 92 * @api
97 93 * @since 0.5
98 94 *
99 - * @param int $term_id term id
100 - * @param string $slug optional language code, defaults to current language
101 - * @return int|false|null term id of the translation if exists, false otherwise, null if the current language is not defined yet
95 + * @param int $term_id Term id.
96 + * @param string $lang Optional language code, defaults to the current language.
97 + * @return int|false|null Term id of the translation if it exists, false otherwise, null if the current language is not defined yet.
102 98 */
103 -function pll_get_term( $term_id, $slug = '' ) {
104 - return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->term->get( $term_id, $slug ) : null;
99 +function pll_get_term( $term_id, $lang = '' ) {
100 + return ( $lang = $lang ? $lang : pll_current_language() ) ? PLL()->model->term->get( $term_id, $lang ) : null;
105 101 }
106 102
107 103 /**
108 - * Returns the home url in the current language
104 + * Returns the home url in a language.
109 105 *
110 106 * @api
111 107 * @since 0.8
112 108 *
113 - * @param string $lang language code ( optional on frontend )
109 + * @param string $lang Optional language code, defaults to the current language.
114 110 * @return string
115 111 */
116 112 function pll_home_url( $lang = '' ) {
117 113 if ( empty( $lang ) ) {
@@ -121,17 +117,19 @@
121 117 return empty( $lang ) ? home_url( '/' ) : PLL()->links->get_home_url( $lang );
122 118 }
123 119
124 120 /**
125 - * Registers a string for translation in the "strings translation" panel
121 + * Registers a string for translation in the "strings translation" panel.
126 122 *
127 123 * @api
128 124 * @since 0.6
129 125 *
130 - * @param string $name a unique name for the string
131 - * @param string $string the string to register
132 - * @param string $context optional the group in which the string is registered, defaults to 'polylang'
133 - * @param bool $multiline optional whether the string table should display a multiline textarea or a single line input, defaults to single line
126 + * @param string $name A unique name for the string.
127 + * @param string $string The string to register.
128 + * @param string $context Optional, the group in which the string is registered, defaults to 'polylang'.
129 + * @param bool $multiline Optional, true if the string table should display a multiline textarea,
130 + * false if should display a single line input, defaults to false.
131 + * @return void
134 132 */
135 133 function pll_register_string( $name, $string, $context = 'Polylang', $multiline = false ) {
136 134 if ( PLL() instanceof PLL_Admin_Base ) {
137 135 PLL_Admin_Strings::register_string( $name, $string, $context, $multiline );
@@ -138,15 +136,15 @@
138 136 }
139 137 }
140 138
141 139 /**
142 - * Translates a string ( previously registered with pll_register_string )
140 + * Translates a string ( previously registered with pll_register_string ).
143 141 *
144 142 * @api
145 143 * @since 0.6
146 144 *
147 - * @param string $string the string to translate
148 - * @return string the string translation in the current language
145 + * @param string $string The string to translate.
146 + * @return string The string translated in the current language.
149 147 */
150 148 function pll__( $string ) {
151 149 return is_scalar( $string ) ? __( $string, 'pll_string' ) : $string; // PHPCS:ignore WordPress.WP.I18n
152 150 }
@@ -156,10 +154,10 @@
156 154 *
157 155 * @api
158 156 * @since 2.1
159 157 *
160 - * @param string $string the string to translate
161 - * @return string translation in the current language
158 + * @param string $string The string to translate.
159 + * @return string The string translated in the current language.
162 160 */
163 161 function pll_esc_html__( $string ) {
164 162 return esc_html( pll__( $string ) );
165 163 }
@@ -169,10 +167,10 @@
169 167 *
170 168 * @api
171 169 * @since 2.1
172 170 *
173 - * @param string $string The string to translate
174 - * @return string
171 + * @param string $string The string to translate.
172 + * @return string The string translated in the current language.
175 173 */
176 174 function pll_esc_attr__( $string ) {
177 175 return esc_attr( pll__( $string ) );
178 176 }
@@ -183,9 +181,10 @@
183 181 *
184 182 * @api
185 183 * @since 0.6
186 184 *
187 - * @param string $string The string to translate
185 + * @param string $string The string to translate.
186 + * @return void
188 187 */
189 188 function pll_e( $string ) {
190 189 echo pll__( $string ); // phpcs:ignore
191 190 }
@@ -195,9 +194,10 @@
195 194 *
196 195 * @api
197 196 * @since 2.1
198 197 *
199 - * @param string $string The string to translate
198 + * @param string $string The string to translate.
199 + * @return void
200 200 */
201 201 function pll_esc_html_e( $string ) {
202 202 echo pll_esc_html__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
203 203 }
@@ -207,9 +207,10 @@
207 207 *
208 208 * @api
209 209 * @since 2.1
210 210 *
211 - * @param string $string The string to translate
211 + * @param string $string The string to translate.
212 + * @return void
212 213 */
213 214 function pll_esc_attr_e( $string ) {
214 215 echo pll_esc_attr__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
215 216 }
@@ -214,16 +215,16 @@
214 215 echo pll_esc_attr__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
215 216 }
216 217
217 218 /**
218 - * Translates a string ( previously registered with pll_register_string )
219 + * Translates a string ( previously registered with pll_register_string ).
219 220 *
220 221 * @api
221 222 * @since 1.5.4
222 223 *
223 - * @param string $string the string to translate
224 - * @param string $lang language code
225 - * @return string the string translation in the requested language
224 + * @param string $string The string to translate.
225 + * @param string $lang Language code.
226 + * @return string The string translated in the requested language.
226 227 */
227 228 function pll_translate_string( $string, $lang ) {
228 229 if ( PLL() instanceof PLL_Frontend && pll_current_language() == $lang ) {
229 230 return pll__( $string );
@@ -232,9 +233,9 @@
232 233 if ( ! is_scalar( $string ) ) {
233 234 return $string;
234 235 }
235 236
236 - static $cache; // Cache object to avoid loading the same translations object several times
237 + static $cache; // Cache object to avoid loading the same translations object several times.
237 238
238 239 if ( empty( $cache ) ) {
239 240 $cache = new PLL_Cache();
240 241 }
@@ -248,14 +249,14 @@
248 249 return $mo->translate( $string );
249 250 }
250 251
251 252 /**
252 - * Returns true if Polylang manages languages and translations for this post type
253 + * Returns true if Polylang manages languages and translations for this post type.
253 254 *
254 255 * @api
255 256 * @since 1.0.1
256 257 *
257 - * @param string $post_type Post type name
258 + * @param string $post_type Post type name.
258 259 * @return bool
259 260 */
260 261 function pll_is_translated_post_type( $post_type ) {
261 262 return PLL()->model->is_translated_post_type( $post_type );
@@ -261,14 +262,14 @@
261 262 return PLL()->model->is_translated_post_type( $post_type );
262 263 }
263 264
264 265 /**
265 - * Returns true if Polylang manages languages and translations for this taxonomy
266 + * Returns true if Polylang manages languages and translations for this taxonomy.
266 267 *
267 268 * @api
268 269 * @since 1.0.1
269 270 *
270 - * @param string $tax Taxonomy name
271 + * @param string $tax Taxonomy name.
271 272 * @return bool
272 273 */
273 274 function pll_is_translated_taxonomy( $tax ) {
274 275 return PLL()->model->is_translated_taxonomy( $tax );
@@ -274,20 +275,20 @@
274 275 return PLL()->model->is_translated_taxonomy( $tax );
275 276 }
276 277
277 278 /**
278 - * Returns the list of available languages
279 + * Returns the list of available languages.
279 280 *
280 - * List of parameters accepted in $args:
281 - *
282 - * hide_empty => hides languages with no posts if set to true ( defaults to false )
283 - * fields => return only that field if set ( see PLL_Language for a list of fields )
284 - *
285 281 * @api
286 282 * @since 1.5
287 283 *
288 - * @param array $args list of parameters
289 - * @return array
284 + * @param array $args {
285 + * Optional array of arguments.
286 + *
287 + * @type bool $hide_empty Hides languages with no posts if set to true ( defaults to false ).
288 + * @type string $fields Return only that field if set ( @see PLL_Language for a list of fields ), defaults to 'slug'.
289 + * }
290 + * @return string[]
290 291 */
291 292 function pll_languages_list( $args = array() ) {
292 293 $args = wp_parse_args( $args, array( 'fields' => 'slug' ) );
293 294 return PLL()->model->get_languages_list( $args );
@@ -293,15 +294,16 @@
293 294 return PLL()->model->get_languages_list( $args );
294 295 }
295 296
296 297 /**
297 - * Set the post language
298 + * Sets the post language.
298 299 *
299 300 * @api
300 301 * @since 1.5
301 302 *
302 - * @param int $id post id
303 - * @param string $lang language code
303 + * @param int $id Post id.
304 + * @param string $lang Language code.
305 + * @return void
304 306 */
305 307 function pll_set_post_language( $id, $lang ) {
306 308 PLL()->model->post->set_language( $id, $lang );
307 309 }
@@ -306,15 +308,16 @@
306 308 PLL()->model->post->set_language( $id, $lang );
307 309 }
308 310
309 311 /**
310 - * Set the term language
312 + * Sets the term language.
311 313 *
312 314 * @api
313 315 * @since 1.5
314 316 *
315 - * @param int $id term id
316 - * @param string $lang language code
317 + * @param int $id Term id.
318 + * @param string $lang Language code.
319 + * @return void
317 320 */
318 321 function pll_set_term_language( $id, $lang ) {
319 322 PLL()->model->term->set_language( $id, $lang );
320 323 }
@@ -319,14 +322,15 @@
319 322 PLL()->model->term->set_language( $id, $lang );
320 323 }
321 324
322 325 /**
323 - * Save posts translations
326 + * Save posts translations.
324 327 *
325 328 * @api
326 329 * @since 1.5
327 330 *
328 - * @param array $arr an associative array of translations with language code as key and post id as value
331 + * @param int[] $arr An associative array of translations with language code as key and post id as value.
332 + * @return void
329 333 */
330 334 function pll_save_post_translations( $arr ) {
331 335 PLL()->model->post->save_translations( reset( $arr ), $arr );
332 336 }
@@ -336,9 +340,10 @@
336 340 *
337 341 * @api
338 342 * @since 1.5
339 343 *
340 - * @param array $arr an associative array of translations with language code as key and term id as value
344 + * @param int[] $arr An associative array of translations with language code as key and term id as value.
345 + * @return void
341 346 */
342 347 function pll_save_term_translations( $arr ) {
343 348 PLL()->model->term->save_translations( reset( $arr ), $arr );
344 349 }
@@ -343,16 +348,16 @@
343 348 PLL()->model->term->save_translations( reset( $arr ), $arr );
344 349 }
345 350
346 351 /**
347 - * Returns the post language
352 + * Returns the post language.
348 353 *
349 354 * @api
350 355 * @since 1.5.4
351 356 *
352 - * @param int $post_id
353 - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug'
354 - * @return bool|string The requested field for the post language, false if no language is associated to that post
357 + * @param int $post_id Post id.
358 + * @param string $field Optional, the language field to return ( @see PLL_Language ), defaults to 'slug'.
359 + * @return string|false The requested field for the post language, false if no language is associated to that post.
355 360 */
356 361 function pll_get_post_language( $post_id, $field = 'slug' ) {
357 362 return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false;
358 363 }
@@ -357,16 +362,16 @@
357 362 return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false;
358 363 }
359 364
360 365 /**
361 - * Returns the term language
366 + * Returns the term language.
362 367 *
363 368 * @api
364 369 * @since 1.5.4
365 370 *
366 - * @param int $term_id
367 - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug'
368 - * @return bool|string The requested field for the term language, false if no language is associated to that term
371 + * @param int $term_id Term id.
372 + * @param string $field Optional, the language field to return ( @see PLL_Language ), defaults to 'slug'.
373 + * @return string|false The requested field for the term language, false if no language is associated to that term.
369 374 */
370 375 function pll_get_term_language( $term_id, $field = 'slug' ) {
371 376 return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false;
372 377 }
@@ -371,15 +376,15 @@
371 376 return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false;
372 377 }
373 378
374 379 /**
375 - * Returns an array of translations of a post
380 + * Returns an array of translations of a post.
376 381 *
377 382 * @api
378 383 * @since 1.8
379 384 *
380 - * @param int $post_id
381 - * @return array an associative array of translations with language code as key and translation post_id as value
385 + * @param int $post_id Post id.
386 + * @return int[] An associative array of translations with language code as key and translation post id as value.
382 387 */
383 388 function pll_get_post_translations( $post_id ) {
384 389 return PLL()->model->post->get_translations( $post_id );
385 390 }
@@ -384,15 +389,15 @@
384 389 return PLL()->model->post->get_translations( $post_id );
385 390 }
386 391
387 392 /**
388 - * Returns an array of translations of a term
393 + * Returns an array of translations of a term.
389 394 *
390 395 * @api
391 396 * @since 1.8
392 397 *
393 - * @param int $term_id
394 - * @return array an associative array of translations with language code as key and translation term_id as value
398 + * @param int $term_id Term id.
399 + * @return int[] An associative array of translations with language code as key and translation term id as value.
395 400 */
396 401 function pll_get_term_translations( $term_id ) {
397 402 return PLL()->model->term->get_translations( $term_id );
398 403 }
@@ -397,15 +402,28 @@
397 402 return PLL()->model->term->get_translations( $term_id );
398 403 }
399 404
400 405 /**
401 - * Count posts in a language
406 + * Counts posts in a language.
402 407 *
403 408 * @api
404 409 * @since 1.5
405 410 *
406 411 * @param string $lang Language code.
407 - * @param array $args WP_Query arguments ( accepted keys: post_type, m, year, monthnum, day, author, author_name, post_format, post_status ).
412 + * @param array $args {
413 + * Optional arguments.
414 + * Accepted keys:
415 + *
416 + * @type string $post_type Post type.
417 + * @type int $m YearMonth ( ex: 201307 ).
418 + * @type int $year 4 digit year.
419 + * @type int $monthnum Month number (from 1 to 12).
420 + * @type int $day Day of the month (from 1 to 31).
421 + * @type int $author Author id.
422 + * @type string $author_name Author nicename.
423 + * @type string $post_format Post format.
424 + * @type string $post_status Post status.
425 + * }
408 426 * @return int Posts count.
409 427 */
410 428 function pll_count_posts( $lang, $args = array() ) {
411 429 return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args );
@@ -411,13 +429,15 @@
411 429 return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args );
412 430 }
413 431
414 432 /**
415 - * Allows to access the Polylang instance
416 - * It is always preferable to use API functions
417 - * Internal methods may be changed without prior notice
433 + * Allows to access the Polylang instance.
434 + * However, it is always preferable to use API functions
435 + * as internal methods may be changed without prior notice.
418 436 *
419 437 * @since 1.8
438 + *
439 + * @return PLL_Frontend|PLL_Admin|PLL_Settings|PLL_REST_Request
420 440 */
421 441 function PLL() { // PHPCS:ignore WordPress.NamingConventions.ValidFunctionName
422 442 return $GLOBALS['polylang'];
423 443 }