PluginProbe
Polylang / 2.0.3
Polylang v2.0.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 +36 -110 2.82.0.3 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,78 +121,40 @@
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
150 -}
125 + static $cache; // Cache object to avoid translating the same string several times
151 126
152 -/**
153 - * Translates a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output.
154 - *
155 - * @api
156 - * @since 2.1
157 - *
158 - * @param string $string the string to translate
159 - * @return string translation in the current language
160 - */
161 -function pll_esc_html__( $string ) {
162 - return esc_html( pll__( $string ) );
163 -}
127 + if ( ! did_action( 'pll_language_defined' ) || ! is_scalar( $string ) ) { // No need for translation
128 + return $string;
129 + }
164 130
165 -/**
166 - * Translates a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML attributes.
167 - *
168 - * @api
169 - * @since 2.1
170 - *
171 - * @param string $string The string to translate
172 - * @return string
173 - */
174 -function pll_esc_attr__( $string ) {
175 - return esc_attr( pll__( $string ) );
131 + if ( empty( $cache ) ) {
132 + $cache = new PLL_Cache();
133 + }
134 +
135 + if ( false === $str = $cache->get( $string ) ) {
136 + $str = __( $string, 'pll_string' );
137 + $cache->set( $string, $str );
138 + }
139 +
140 + return $str;
176 141 }
177 142
178 143 /**
179 144 * Echoes a translated string ( previously registered with pll_register_string )
180 - * It is an equivalent of _e() and is not escaped.
181 145 *
182 - * @api
183 146 * @since 0.6
184 147 *
185 - * @param string $string The string to translate
148 + * @param string $string the string to translate
186 149 */
187 150 function pll_e( $string ) {
188 - echo pll__( $string ); // phpcs:ignore
151 + echo pll__( $string );
189 152 }
190 153
191 154 /**
192 - * Echoes a translated string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output.
193 - *
194 - * @api
195 - * @since 2.1
196 - *
197 - * @param string $string The string to translate
198 - */
199 -function pll_esc_html_e( $string ) {
200 - echo pll_esc_html__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
201 -}
202 -
203 -/**
204 - * Echoes a translated a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML attributes.
205 - *
206 - * @api
207 - * @since 2.1
208 - *
209 - * @param string $string The string to translate
210 - */
211 -function pll_esc_attr_e( $string ) {
212 - echo pll_esc_attr__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
213 -}
214 -
215 -/**
216 155 * Translates a string ( previously registered with pll_register_string )
217 156 *
218 - * @api
219 157 * @since 1.5.4
220 158 *
221 159 * @param string $string the string to translate
222 160 * @param string $lang language code
@@ -222,9 +160,9 @@
222 160 * @param string $lang language code
223 161 * @return string the string translation in the requested language
224 162 */
225 163 function pll_translate_string( $string, $lang ) {
226 - if ( PLL() instanceof PLL_Frontend && pll_current_language() == $lang ) {
164 + if ( pll_current_language() == $lang ) {
227 165 return pll__( $string );
228 166 }
229 167
230 168 if ( ! is_scalar( $string ) ) {
@@ -248,12 +186,11 @@
248 186
249 187 /**
250 188 * Returns true if Polylang manages languages and translations for this post type
251 189 *
252 - * @api
253 190 * @since 1.0.1
254 191 *
255 - * @param string $post_type Post type name
192 + * @param string post type name
256 193 * @return bool
257 194 */
258 195 function pll_is_translated_post_type( $post_type ) {
259 196 return PLL()->model->is_translated_post_type( $post_type );
@@ -261,12 +198,11 @@
261 198
262 199 /**
263 200 * Returns true if Polylang manages languages and translations for this taxonomy
264 201 *
265 - * @api
266 202 * @since 1.0.1
267 203 *
268 - * @param string $tax Taxonomy name
204 + * @param string taxonomy name
269 205 * @return bool
270 206 */
271 207 function pll_is_translated_taxonomy( $tax ) {
272 208 return PLL()->model->is_translated_taxonomy( $tax );
@@ -279,9 +215,8 @@
279 215 *
280 216 * hide_empty => hides languages with no posts if set to true ( defaults to false )
281 217 * fields => return only that field if set ( see PLL_Language for a list of fields )
282 218 *
283 - * @api
284 219 * @since 1.5
285 220 *
286 221 * @param array $args list of parameters
287 222 * @return array
@@ -293,9 +228,8 @@
293 228
294 229 /**
295 230 * Set the post language
296 231 *
297 - * @api
298 232 * @since 1.5
299 233 *
300 234 * @param int $id post id
301 235 * @param string $lang language code
@@ -306,9 +240,8 @@
306 240
307 241 /**
308 242 * Set the term language
309 243 *
310 - * @api
311 244 * @since 1.5
312 245 *
313 246 * @param int $id term id
314 247 * @param string $lang language code
@@ -319,9 +252,8 @@
319 252
320 253 /**
321 254 * Save posts translations
322 255 *
323 - * @api
324 256 * @since 1.5
325 257 *
326 258 * @param array $arr an associative array of translations with language code as key and post id as value
327 259 */
@@ -331,9 +263,8 @@
331 263
332 264 /**
333 265 * Save terms translations
334 266 *
335 - * @api
336 267 * @since 1.5
337 268 *
338 269 * @param array $arr an associative array of translations with language code as key and term id as value
339 270 */
@@ -343,14 +274,13 @@
343 274
344 275 /**
345 276 * Returns the post language
346 277 *
347 - * @api
348 278 * @since 1.5.4
349 279 *
350 280 * @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
281 + * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
282 + * @return bool|string the requested field for the post language, false if no language is associated to that post
353 283 */
354 284 function pll_get_post_language( $post_id, $field = 'slug' ) {
355 285 return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false;
356 286 }
@@ -357,14 +287,13 @@
357 287
358 288 /**
359 289 * Returns the term language
360 290 *
361 - * @api
362 291 * @since 1.5.4
363 292 *
364 293 * @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
294 + * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
295 + * @return bool|string the requested field for the term language, false if no language is associated to that term
367 296 */
368 297 function pll_get_term_language( $term_id, $field = 'slug' ) {
369 298 return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false;
370 299 }
@@ -371,9 +300,8 @@
371 300
372 301 /**
373 302 * Returns an array of translations of a post
374 303 *
375 - * @api
376 304 * @since 1.8
377 305 *
378 306 * @param int $post_id
379 307 * @return array an associative array of translations with language code as key and translation post_id as value
@@ -384,9 +312,8 @@
384 312
385 313 /**
386 314 * Returns an array of translations of a term
387 315 *
388 - * @api
389 316 * @since 1.8
390 317 *
391 318 * @param int $term_id
392 319 * @return array an associative array of translations with language code as key and translation term_id as value
@@ -397,14 +324,13 @@
397 324
398 325 /**
399 326 * Count posts in a language
400 327 *
401 - * @api
402 328 * @since 1.5
403 329 *
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.
330 + * @param string $lang language code
331 + * @param array $args ( accepted keys: post_type, m, year, monthnum, day, author, author_name, post_format )
332 + * @return int posts count
407 333 */
408 334 function pll_count_posts( $lang, $args = array() ) {
409 335 return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args );
410 336 }
@@ -415,7 +341,7 @@
415 341 * Internal methods may be changed without prior notice
416 342 *
417 343 * @since 1.8
418 344 */
419 -function PLL() { // PHPCS:ignore WordPress.NamingConventions.ValidFunctionName
345 +function PLL() {
420 346 return $GLOBALS['polylang'];
421 347 }