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