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