PluginProbe
Polylang / 3.3
Polylang v3.3
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 +133 -107 2.93.3 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,17 +117,19 @@
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 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,18 +136,22 @@
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 - return is_scalar( $string ) ? __( $string, 'pll_string' ) : $string; // PHPCS:ignore WordPress.WP.I18n
149 + if ( ! is_scalar( $string ) || '' === $string ) {
150 + return $string;
151 + }
152 +
153 + return __( $string, 'pll_string' ); // PHPCS:ignore WordPress.WP.I18n
150 154 }
151 155
152 156 /**
153 157 * Translates a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output.
@@ -154,10 +158,10 @@
154 158 *
155 159 * @api
156 160 * @since 2.1
157 161 *
158 - * @param string $string the string to translate
159 - * @return string translation in the current language
162 + * @param string $string The string to translate.
163 + * @return string The string translated in the current language.
160 164 */
161 165 function pll_esc_html__( $string ) {
162 166 return esc_html( pll__( $string ) );
163 167 }
@@ -167,10 +171,10 @@
167 171 *
168 172 * @api
169 173 * @since 2.1
170 174 *
171 - * @param string $string The string to translate
172 - * @return string
175 + * @param string $string The string to translate.
176 + * @return string The string translated in the current language.
173 177 */
174 178 function pll_esc_attr__( $string ) {
175 179 return esc_attr( pll__( $string ) );
176 180 }
@@ -181,9 +185,10 @@
181 185 *
182 186 * @api
183 187 * @since 0.6
184 188 *
185 - * @param string $string The string to translate
189 + * @param string $string The string to translate.
190 + * @return void
186 191 */
187 192 function pll_e( $string ) {
188 193 echo pll__( $string ); // phpcs:ignore
189 194 }
@@ -193,9 +198,10 @@
193 198 *
194 199 * @api
195 200 * @since 2.1
196 201 *
197 - * @param string $string The string to translate
202 + * @param string $string The string to translate.
203 + * @return void
198 204 */
199 205 function pll_esc_html_e( $string ) {
200 206 echo pll_esc_html__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
201 207 }
@@ -205,9 +211,10 @@
205 211 *
206 212 * @api
207 213 * @since 2.1
208 214 *
209 - * @param string $string The string to translate
215 + * @param string $string The string to translate.
216 + * @return void
210 217 */
211 218 function pll_esc_attr_e( $string ) {
212 219 echo pll_esc_attr__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
213 220 }
@@ -212,16 +219,16 @@
212 219 echo pll_esc_attr__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
213 220 }
214 221
215 222 /**
216 - * Translates a string ( previously registered with pll_register_string )
223 + * Translates a string ( previously registered with pll_register_string ).
217 224 *
218 225 * @api
219 226 * @since 1.5.4
220 227 *
221 - * @param string $string the string to translate
222 - * @param string $lang language code
223 - * @return string the string translation in the requested language
228 + * @param string $string The string to translate.
229 + * @param string $lang Language code.
230 + * @return string The string translated in the requested language.
224 231 */
225 232 function pll_translate_string( $string, $lang ) {
226 233 if ( PLL() instanceof PLL_Frontend && pll_current_language() == $lang ) {
227 234 return pll__( $string );
@@ -226,13 +233,13 @@
226 233 if ( PLL() instanceof PLL_Frontend && pll_current_language() == $lang ) {
227 234 return pll__( $string );
228 235 }
229 236
230 - if ( ! is_scalar( $string ) ) {
237 + if ( ! is_scalar( $string ) || '' === $string ) {
231 238 return $string;
232 239 }
233 240
234 - static $cache; // Cache object to avoid loading the same translations object several times
241 + static $cache; // Cache object to avoid loading the same translations object several times.
235 242
236 243 if ( empty( $cache ) ) {
237 244 $cache = new PLL_Cache();
238 245 }
@@ -246,14 +253,14 @@
246 253 return $mo->translate( $string );
247 254 }
248 255
249 256 /**
250 - * Returns true if Polylang manages languages and translations for this post type
257 + * Returns true if Polylang manages languages and translations for this post type.
251 258 *
252 259 * @api
253 260 * @since 1.0.1
254 261 *
255 - * @param string $post_type Post type name
262 + * @param string $post_type Post type name.
256 263 * @return bool
257 264 */
258 265 function pll_is_translated_post_type( $post_type ) {
259 266 return PLL()->model->is_translated_post_type( $post_type );
@@ -259,14 +266,14 @@
259 266 return PLL()->model->is_translated_post_type( $post_type );
260 267 }
261 268
262 269 /**
263 - * Returns true if Polylang manages languages and translations for this taxonomy
270 + * Returns true if Polylang manages languages and translations for this taxonomy.
264 271 *
265 272 * @api
266 273 * @since 1.0.1
267 274 *
268 - * @param string $tax Taxonomy name
275 + * @param string $tax Taxonomy name.
269 276 * @return bool
270 277 */
271 278 function pll_is_translated_taxonomy( $tax ) {
272 279 return PLL()->model->is_translated_taxonomy( $tax );
@@ -272,20 +279,20 @@
272 279 return PLL()->model->is_translated_taxonomy( $tax );
273 280 }
274 281
275 282 /**
276 - * Returns the list of available languages
283 + * Returns the list of available languages.
277 284 *
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 285 * @api
284 286 * @since 1.5
285 287 *
286 - * @param array $args list of parameters
287 - * @return array
288 + * @param array $args {
289 + * Optional array of arguments.
290 + *
291 + * @type bool $hide_empty Hides languages with no posts if set to true ( defaults to false ).
292 + * @type string $fields Return only that field if set ( @see PLL_Language for a list of fields ), defaults to 'slug'.
293 + * }
294 + * @return string[]
288 295 */
289 296 function pll_languages_list( $args = array() ) {
290 297 $args = wp_parse_args( $args, array( 'fields' => 'slug' ) );
291 298 return PLL()->model->get_languages_list( $args );
@@ -291,15 +298,16 @@
291 298 return PLL()->model->get_languages_list( $args );
292 299 }
293 300
294 301 /**
295 - * Set the post language
302 + * Sets the post language.
296 303 *
297 304 * @api
298 305 * @since 1.5
299 306 *
300 - * @param int $id post id
301 - * @param string $lang language code
307 + * @param int $id Post id.
308 + * @param string $lang Language code.
309 + * @return void
302 310 */
303 311 function pll_set_post_language( $id, $lang ) {
304 312 PLL()->model->post->set_language( $id, $lang );
305 313 }
@@ -304,15 +312,16 @@
304 312 PLL()->model->post->set_language( $id, $lang );
305 313 }
306 314
307 315 /**
308 - * Set the term language
316 + * Sets the term language.
309 317 *
310 318 * @api
311 319 * @since 1.5
312 320 *
313 - * @param int $id term id
314 - * @param string $lang language code
321 + * @param int $id Term id.
322 + * @param string $lang Language code.
323 + * @return void
315 324 */
316 325 function pll_set_term_language( $id, $lang ) {
317 326 PLL()->model->term->set_language( $id, $lang );
318 327 }
@@ -317,14 +326,15 @@
317 326 PLL()->model->term->set_language( $id, $lang );
318 327 }
319 328
320 329 /**
321 - * Save posts translations
330 + * Save posts translations.
322 331 *
323 332 * @api
324 333 * @since 1.5
325 334 *
326 - * @param array $arr an associative array of translations with language code as key and post id as value
335 + * @param int[] $arr An associative array of translations with language code as key and post id as value.
336 + * @return void
327 337 */
328 338 function pll_save_post_translations( $arr ) {
329 339 PLL()->model->post->save_translations( reset( $arr ), $arr );
330 340 }
@@ -334,9 +344,10 @@
334 344 *
335 345 * @api
336 346 * @since 1.5
337 347 *
338 - * @param array $arr an associative array of translations with language code as key and term id as value
348 + * @param int[] $arr An associative array of translations with language code as key and term id as value.
349 + * @return void
339 350 */
340 351 function pll_save_term_translations( $arr ) {
341 352 PLL()->model->term->save_translations( reset( $arr ), $arr );
342 353 }
@@ -341,16 +352,16 @@
341 352 PLL()->model->term->save_translations( reset( $arr ), $arr );
342 353 }
343 354
344 355 /**
345 - * Returns the post language
356 + * Returns the post language.
346 357 *
347 358 * @api
348 359 * @since 1.5.4
349 360 *
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
361 + * @param int $post_id Post id.
362 + * @param string $field Optional, the language field to return ( @see PLL_Language ), defaults to 'slug'.
363 + * @return string|false The requested field for the post language, false if no language is associated to that post.
353 364 */
354 365 function pll_get_post_language( $post_id, $field = 'slug' ) {
355 366 return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false;
356 367 }
@@ -355,16 +366,16 @@
355 366 return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false;
356 367 }
357 368
358 369 /**
359 - * Returns the term language
370 + * Returns the term language.
360 371 *
361 372 * @api
362 373 * @since 1.5.4
363 374 *
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
375 + * @param int $term_id Term id.
376 + * @param string $field Optional, the language field to return ( @see PLL_Language ), defaults to 'slug'.
377 + * @return string|false The requested field for the term language, false if no language is associated to that term.
367 378 */
368 379 function pll_get_term_language( $term_id, $field = 'slug' ) {
369 380 return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false;
370 381 }
@@ -369,15 +380,15 @@
369 380 return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false;
370 381 }
371 382
372 383 /**
373 - * Returns an array of translations of a post
384 + * Returns an array of translations of a post.
374 385 *
375 386 * @api
376 387 * @since 1.8
377 388 *
378 - * @param int $post_id
379 - * @return array an associative array of translations with language code as key and translation post_id as value
389 + * @param int $post_id Post id.
390 + * @return int[] An associative array of translations with language code as key and translation post id as value.
380 391 */
381 392 function pll_get_post_translations( $post_id ) {
382 393 return PLL()->model->post->get_translations( $post_id );
383 394 }
@@ -382,15 +393,15 @@
382 393 return PLL()->model->post->get_translations( $post_id );
383 394 }
384 395
385 396 /**
386 - * Returns an array of translations of a term
397 + * Returns an array of translations of a term.
387 398 *
388 399 * @api
389 400 * @since 1.8
390 401 *
391 - * @param int $term_id
392 - * @return array an associative array of translations with language code as key and translation term_id as value
402 + * @param int $term_id Term id.
403 + * @return int[] An associative array of translations with language code as key and translation term id as value.
393 404 */
394 405 function pll_get_term_translations( $term_id ) {
395 406 return PLL()->model->term->get_translations( $term_id );
396 407 }
@@ -395,15 +406,28 @@
395 406 return PLL()->model->term->get_translations( $term_id );
396 407 }
397 408
398 409 /**
399 - * Count posts in a language
410 + * Counts posts in a language.
400 411 *
401 412 * @api
402 413 * @since 1.5
403 414 *
404 415 * @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 ).
416 + * @param array $args {
417 + * Optional arguments.
418 + * Accepted keys:
419 + *
420 + * @type string $post_type Post type.
421 + * @type int $m YearMonth ( ex: 201307 ).
422 + * @type int $year 4 digit year.
423 + * @type int $monthnum Month number (from 1 to 12).
424 + * @type int $day Day of the month (from 1 to 31).
425 + * @type int $author Author id.
426 + * @type string $author_name Author nicename.
427 + * @type string $post_format Post format.
428 + * @type string $post_status Post status.
429 + * }
406 430 * @return int Posts count.
407 431 */
408 432 function pll_count_posts( $lang, $args = array() ) {
409 433 return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args );
@@ -409,13 +433,15 @@
409 433 return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args );
410 434 }
411 435
412 436 /**
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
437 + * Allows to access the Polylang instance.
438 + * However, it is always preferable to use API functions
439 + * as internal methods may be changed without prior notice.
416 440 *
417 441 * @since 1.8
442 + *
443 + * @return PLL_Frontend|PLL_Admin|PLL_Settings|PLL_REST_Request
418 444 */
419 445 function PLL() { // PHPCS:ignore WordPress.NamingConventions.ValidFunctionName
420 446 return $GLOBALS['polylang'];
421 447 }