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