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