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 +32 -71 2.8.22.1 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Template tag: displays the language switcher
8 5 *
@@ -8,21 +5,19 @@
8 5 *
9 6 * List of parameters accepted in $args:
10 7 *
11 8 * dropdown => displays a dropdown if set to 1, defaults to 0
12 - * echo => echoes the switcher if set to 1 ( default )
9 + * echo => echoes the the switcher if set to 1 ( default )
13 10 * hide_if_empty => hides languages with no posts ( or pages ) if set to 1 ( default )
14 11 * show_flags => shows flags if set to 1, defaults to 0
15 12 * 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
13 + * display_names_as => whether to display the language name or code. valid options are 'slug' and 'name'
17 14 * force_home => forces linking to the home page is set to 1, defaults to 0
18 15 * hide_if_no_translation => hides the link if there is no translation if set to 1, defaults to 0
19 16 * hide_current => hides the current language if set to 1, defaults to 0
20 17 * post_id => if not null, link to translations of post defined by post_id, defaults to null
21 18 * 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 19 *
24 - * @api
25 20 * @since 0.5
26 21 *
27 22 * @param array $args optional
28 23 * @return null|string|array null if displaying, array if raw is requested, string otherwise
@@ -28,9 +23,9 @@
28 23 * @return null|string|array null if displaying, array if raw is requested, string otherwise
29 24 */
30 25 function pll_the_languages( $args = '' ) {
31 26 if ( PLL() instanceof PLL_Frontend ) {
32 - $switcher = new PLL_Switcher();
27 + $switcher = new PLL_Switcher;
33 28 return $switcher->the_languages( PLL()->links, $args );
34 29 }
35 30 return '';
36 31 }
@@ -38,18 +33,14 @@
38 33 /**
39 34 * Returns the current language on frontend
40 35 * Returns the language set in admin language filter on backend ( false if set to all languages )
41 36 *
42 - * @api
43 37 * @since 0.8.1
44 38 *
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
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
47 41 */
48 42 function pll_current_language( $field = 'slug' ) {
49 - if ( OBJECT === $field ) {
50 - return PLL()->curlang;
51 - }
52 43 return isset( PLL()->curlang->$field ) ? PLL()->curlang->$field : false;
53 44 }
54 45
55 46 /**
@@ -54,31 +45,20 @@
54 45
55 46 /**
56 47 * Returns the default language
57 48 *
58 - * @api
59 49 * @since 1.0
60 50 *
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
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
63 53 */
64 54 function pll_default_language( $field = 'slug' ) {
65 - if ( isset( PLL()->options['default_lang'] ) ) {
66 - $lang = PLL()->model->get_language( PLL()->options['default_lang'] );
67 - if ( $lang ) {
68 - if ( OBJECT === $field ) {
69 - return $lang;
70 - }
71 - return isset( $lang->$field ) ? $lang->$field : false;
72 - }
73 - }
74 - return false;
55 + return isset( PLL()->options['default_lang'] ) && ( $lang = PLL()->model->get_language( PLL()->options['default_lang'] ) ) && isset( $lang->$field ) ? $lang->$field : false;
75 56 }
76 57
77 58 /**
78 59 * Among the post and its translations, returns the id of the post which is in the language represented by $slug
79 60 *
80 - * @api
81 61 * @since 0.5
82 62 *
83 63 * @param int $post_id post id
84 64 * @param string $slug optional language code, defaults to current language
@@ -90,9 +70,8 @@
90 70
91 71 /**
92 72 * Among the term and its translations, returns the id of the term which is in the language represented by $slug
93 73 *
94 - * @api
95 74 * @since 0.5
96 75 *
97 76 * @param int $term_id term id
98 77 * @param string $slug optional language code, defaults to current language
@@ -104,9 +83,8 @@
104 83
105 84 /**
106 85 * Returns the home url in the current language
107 86 *
108 - * @api
109 87 * @since 0.8
110 88 *
111 89 * @param string $lang language code ( optional on frontend )
112 90 * @return string
@@ -121,15 +99,14 @@
121 99
122 100 /**
123 101 * Registers a string for translation in the "strings translation" panel
124 102 *
125 - * @api
126 103 * @since 0.6
127 104 *
128 105 * @param string $name a unique name for the string
129 106 * @param string $string the string to register
130 107 * @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
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 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 );
@@ -138,9 +115,8 @@
138 115
139 116 /**
140 117 * Translates a string ( previously registered with pll_register_string )
141 118 *
142 - * @api
143 119 * @since 0.6
144 120 *
145 121 * @param string $string the string to translate
146 122 * @return string the string translation in the current language
@@ -145,15 +121,18 @@
145 121 * @param string $string the string to translate
146 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 137 * @param string $string the string to translate
159 138 * @return string translation in the current language
@@ -164,12 +143,11 @@
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
149 + * @param $string
172 150 * @return string
173 151 */
174 152 function pll_esc_attr__( $string ) {
175 153 return esc_attr( pll__( $string ) );
@@ -176,47 +154,42 @@
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
161 + * @param string $string the string to translate
186 162 */
187 163 function pll_e( $string ) {
188 - echo pll__( $string ); // phpcs:ignore
164 + echo pll__( $string );
189 165 }
190 166
191 167 /**
192 168 * Echoes a translated string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output.
193 169 *
194 - * @api
195 170 * @since 2.1
196 171 *
197 - * @param string $string The string to translate
172 + * @param string $string the string to translate
198 173 */
199 174 function pll_esc_html_e( $string ) {
200 - echo pll_esc_html__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
175 + echo pll_esc_html__( $string );
201 176 }
202 177
203 178 /**
204 179 * Echoes a translated a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML attributes.
205 180 *
206 - * @api
207 181 * @since 2.1
208 182 *
209 - * @param string $string The string to translate
183 + * @param $string
210 184 */
211 185 function pll_esc_attr_e( $string ) {
212 - echo pll_esc_attr__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
186 + echo pll_esc_attr__( $string );
213 187 }
214 188
215 189 /**
216 190 * Translates a string ( previously registered with pll_register_string )
217 191 *
218 - * @api
219 192 * @since 1.5.4
220 193 *
221 194 * @param string $string the string to translate
222 195 * @param string $lang language code
@@ -222,9 +195,9 @@
222 195 * @param string $lang language code
223 196 * @return string the string translation in the requested language
224 197 */
225 198 function pll_translate_string( $string, $lang ) {
226 - if ( PLL() instanceof PLL_Frontend && pll_current_language() == $lang ) {
199 + if ( pll_current_language() == $lang ) {
227 200 return pll__( $string );
228 201 }
229 202
230 203 if ( ! is_scalar( $string ) ) {
@@ -248,12 +221,11 @@
248 221
249 222 /**
250 223 * Returns true if Polylang manages languages and translations for this post type
251 224 *
252 - * @api
253 225 * @since 1.0.1
254 226 *
255 - * @param string $post_type Post type name
227 + * @param string post type name
256 228 * @return bool
257 229 */
258 230 function pll_is_translated_post_type( $post_type ) {
259 231 return PLL()->model->is_translated_post_type( $post_type );
@@ -261,12 +233,11 @@
261 233
262 234 /**
263 235 * Returns true if Polylang manages languages and translations for this taxonomy
264 236 *
265 - * @api
266 237 * @since 1.0.1
267 238 *
268 - * @param string $tax Taxonomy name
239 + * @param string taxonomy name
269 240 * @return bool
270 241 */
271 242 function pll_is_translated_taxonomy( $tax ) {
272 243 return PLL()->model->is_translated_taxonomy( $tax );
@@ -279,9 +250,8 @@
279 250 *
280 251 * hide_empty => hides languages with no posts if set to true ( defaults to false )
281 252 * fields => return only that field if set ( see PLL_Language for a list of fields )
282 253 *
283 - * @api
284 254 * @since 1.5
285 255 *
286 256 * @param array $args list of parameters
287 257 * @return array
@@ -293,9 +263,8 @@
293 263
294 264 /**
295 265 * Set the post language
296 266 *
297 - * @api
298 267 * @since 1.5
299 268 *
300 269 * @param int $id post id
301 270 * @param string $lang language code
@@ -306,9 +275,8 @@
306 275
307 276 /**
308 277 * Set the term language
309 278 *
310 - * @api
311 279 * @since 1.5
312 280 *
313 281 * @param int $id term id
314 282 * @param string $lang language code
@@ -319,9 +287,8 @@
319 287
320 288 /**
321 289 * Save posts translations
322 290 *
323 - * @api
324 291 * @since 1.5
325 292 *
326 293 * @param array $arr an associative array of translations with language code as key and post id as value
327 294 */
@@ -331,9 +298,8 @@
331 298
332 299 /**
333 300 * Save terms translations
334 301 *
335 - * @api
336 302 * @since 1.5
337 303 *
338 304 * @param array $arr an associative array of translations with language code as key and term id as value
339 305 */
@@ -343,14 +309,13 @@
343 309
344 310 /**
345 311 * Returns the post language
346 312 *
347 - * @api
348 313 * @since 1.5.4
349 314 *
350 315 * @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
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
353 318 */
354 319 function pll_get_post_language( $post_id, $field = 'slug' ) {
355 320 return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false;
356 321 }
@@ -357,14 +322,13 @@
357 322
358 323 /**
359 324 * Returns the term language
360 325 *
361 - * @api
362 326 * @since 1.5.4
363 327 *
364 328 * @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
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
367 331 */
368 332 function pll_get_term_language( $term_id, $field = 'slug' ) {
369 333 return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false;
370 334 }
@@ -371,9 +335,8 @@
371 335
372 336 /**
373 337 * Returns an array of translations of a post
374 338 *
375 - * @api
376 339 * @since 1.8
377 340 *
378 341 * @param int $post_id
379 342 * @return array an associative array of translations with language code as key and translation post_id as value
@@ -384,9 +347,8 @@
384 347
385 348 /**
386 349 * Returns an array of translations of a term
387 350 *
388 - * @api
389 351 * @since 1.8
390 352 *
391 353 * @param int $term_id
392 354 * @return array an associative array of translations with language code as key and translation term_id as value
@@ -397,14 +359,13 @@
397 359
398 360 /**
399 361 * Count posts in a language
400 362 *
401 - * @api
402 363 * @since 1.5
403 364 *
404 - * @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 ).
406 - * @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
407 368 */
408 369 function pll_count_posts( $lang, $args = array() ) {
409 370 return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args );
410 371 }
@@ -415,7 +376,7 @@
415 376 * Internal methods may be changed without prior notice
416 377 *
417 378 * @since 1.8
418 379 */
419 -function PLL() { // PHPCS:ignore WordPress.NamingConventions.ValidFunctionName
380 +function PLL() {
420 381 return $GLOBALS['polylang'];
421 382 }