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