PluginProbe
Polylang / 2.8
Polylang v2.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/language.php +115 -288 3.02.8 View file →
@@ -3,217 +3,86 @@
3 3 * @package Polylang
4 4 */
5 5
6 6 /**
7 - * A language object is made of two terms in 'language' and 'term_language' taxonomies.
8 - * Manipulating only one object per language instead of two terms should make things easier.
7 + * A language object is made of two terms in 'language' and 'term_language' taxonomies
8 + * manipulating only one object per language instead of two terms should make things easier
9 9 *
10 + * Properties:
11 + * term_id => id of term in 'language' taxonomy
12 + * name => language name. Ex: English
13 + * slug => language code used in url. Ex: en
14 + * term_group => order of the language when displayed in a list of languages
15 + * term_taxonomy_id => term taxonomy id in 'language' taxonomy
16 + * taxonomy => 'language'
17 + * description => language locale for backward compatibility
18 + * parent => 0 / not used
19 + * count => number of posts and pages in that language
20 + * tl_term_id => id of the term in 'term_language' taxonomy
21 + * tl_term_taxonomy_id => term taxonomy id in 'term_language' taxonomy
22 + * tl_count => number of terms in that language ( not used by Polylang )
23 + * locale => WordPress language locale. Ex: en_US
24 + * is_rtl => 1 if the language is rtl
25 + * w3c => W3C locale
26 + * flag_code => code of the flag
27 + * flag_url => url of the flag
28 + * flag => html img of the flag
29 + * custom_flag_url => url of the custom flag if exists, internal use only, moves to flag_url on frontend
30 + * custom_flag => html img of the custom flag if exists, internal use only, moves to flag on frontend
31 + * home_url => home url in this language
32 + * search_url => home url to use in search forms
33 + * host => host of this language
34 + * mo_id => id of the post storing strings translations
35 + * page_on_front => id of the page on front in this language ( set from pll_languages_list filter )
36 + * page_for_posts => id of the page for posts in this language ( set from pll_languages_list filter )
37 + *
10 38 * @since 1.2
11 39 */
12 40 class PLL_Language {
13 - /**
14 - * Id of the term in 'language' taxonomy.
15 - *
16 - * @var int
17 - */
18 - public $term_id;
41 + public $term_id, $name, $slug, $term_group, $term_taxonomy_id, $taxonomy, $description, $parent, $count;
42 + public $tl_term_id, $tl_term_taxonomy_id, $tl_count;
43 + public $locale, $is_rtl;
44 + public $w3c, $facebook;
45 + public $flag_url, $flag;
46 + public $home_url, $search_url;
47 + public $host, $mo_id;
48 + public $page_on_front, $page_for_posts;
19 49
20 50 /**
21 - * Language name. Ex: English.
51 + * Constructor: builds a language object given its two corresponding terms in language and term_language taxonomies
22 52 *
23 - * @var string
24 - */
25 - public $name;
26 -
27 - /**
28 - * Language code used in url. Ex: en.
29 - *
30 - * @var string
31 - */
32 - public $slug;
33 -
34 - /**
35 - * Order of the language when displayed in a list of languages.
36 - *
37 - * @var int
38 - */
39 - public $term_group;
40 -
41 - /**
42 - * Term taxonomy id in 'language' taxonomy.
43 - *
44 - * @var int
45 - */
46 - public $term_taxonomy_id;
47 -
48 - /**
49 - * Number of posts and pages in that language.
50 - *
51 - * @var int
52 - */
53 - public $count;
54 -
55 - /**
56 - * Id of the term in 'term_language' taxonomy.
57 - *
58 - * @var int
59 - */
60 - public $tl_term_id;
61 -
62 - /**
63 - * Term taxonomy id in 'term_language' taxonomy.
64 - *
65 - * @var int
66 - */
67 - public $tl_term_taxonomy_id;
68 -
69 - /**
70 - * Number of terms in that language.
71 - *
72 - * @var int
73 - */
74 - public $tl_count;
75 -
76 - /**
77 - * WordPress language locale. Ex: en_US.
78 - *
79 - * @var string
80 - */
81 - public $locale;
82 -
83 - /**
84 - * 1 if the language is rtl, 0 otherwise.
85 - *
86 - * @var int
87 - */
88 - public $is_rtl;
89 -
90 - /**
91 - * W3C locale.
92 - *
93 - * @var string.
94 - */
95 - public $w3c;
96 -
97 - /**
98 - * Facebook locale.
99 - *
100 - * @var string.
101 - */
102 - public $facebook;
103 -
104 - /**
105 - * Home url in this language.
106 - *
107 - * @var string
108 - */
109 - public $home_url;
110 -
111 - /**
112 - * Home url to use in search forms.
113 - *
114 - * @var string
115 - */
116 - public $search_url;
117 -
118 - /**
119 - * Host corresponding to this language.
120 - *
121 - * @var string
122 - */
123 - public $host;
124 -
125 - /**
126 - * Id of the post storing strings translations.
127 - *
128 - * @var int
129 - */
130 - public $mo_id;
131 -
132 - /**
133 - * Id of the page on front in this language ( set from pll_languages_list filter ).
134 - *
135 - * @var int
136 - */
137 - public $page_on_front;
138 -
139 - /**
140 - * Id of the page for posts in this language ( set from pll_languages_list filter ).
141 - *
142 - * @var int
143 - */
144 - public $page_for_posts;
145 -
146 - /**
147 - * Code of the flag.
148 - *
149 - * @var string
150 - */
151 - public $flag_code;
152 -
153 - /**
154 - * Url of the flag.
155 - *
156 - * @var string
157 - */
158 - public $flag_url;
159 -
160 - /**
161 - * Html markup of the flag.
162 - *
163 - * @var string
164 - */
165 - public $flag;
166 -
167 - /**
168 - * Url of the custom flag if it exists.
169 - *
170 - * @var string
171 - */
172 - protected $custom_flag_url;
173 -
174 - /**
175 - * Html markup of the custom flag if it exists.
176 - *
177 - * @var string
178 - */
179 - protected $custom_flag;
180 -
181 - /**
182 - * Constructor: builds a language object given its two corresponding terms in 'language' and 'term_language' taxonomies.
183 - *
184 53 * @since 1.2
185 54 *
186 - * @param WP_Term|array $language Term in 'language' taxonomy or language object properties stored as an array.
187 - * @param WP_Term $term_language Corresponding 'term_language' term.
55 + * @param object|array $language 'language' term or language object properties stored as an array
56 + * @param object $term_language Corresponding 'term_language' term
188 57 */
189 58 public function __construct( $language, $term_language = null ) {
190 - // Build the object from all properties stored as an array.
191 - if ( is_array( $language ) ) {
59 + // Build the object from all properties stored as an array
60 + if ( empty( $term_language ) ) {
192 61 foreach ( $language as $prop => $value ) {
193 62 $this->$prop = $value;
194 63 }
195 64 }
196 65
197 - // Build the object from taxonomy terms.
198 - elseif ( ! empty( $term_language ) ) {
199 - $this->term_id = (int) $language->term_id;
200 - $this->name = $language->name;
201 - $this->slug = $language->slug;
202 - $this->term_group = (int) $language->term_group;
203 - $this->term_taxonomy_id = (int) $language->term_taxonomy_id;
204 - $this->count = (int) $language->count;
66 + // Build the object from taxonomies
67 + else {
68 + foreach ( $language as $prop => $value ) {
69 + $this->$prop = in_array( $prop, array( 'term_id', 'term_taxonomy_id', 'count' ) ) ? (int) $language->$prop : $language->$prop;
70 + }
205 71
206 72 $this->tl_term_id = (int) $term_language->term_id;
207 73 $this->tl_term_taxonomy_id = (int) $term_language->term_taxonomy_id;
208 74 $this->tl_count = (int) $term_language->count;
209 75
210 - // The description field can contain any property.
76 + // The description field can contain any property
77 + // Backward compatibility for is_rtl
211 78 $description = maybe_unserialize( $language->description );
212 79 foreach ( $description as $prop => $value ) {
213 80 'rtl' == $prop ? $this->is_rtl = $value : $this->$prop = $value;
214 81 }
215 82
83 + $this->description = &$this->locale; // Backward compatibility with Polylang < 1.2
84 +
216 85 $this->mo_id = PLL_MO::get_id( $this );
217 86
218 87 $languages = include POLYLANG_DIR . '/settings/languages.php';
219 88 $this->w3c = isset( $languages[ $this->locale ]['w3c'] ) ? $languages[ $this->locale ]['w3c'] : str_replace( '_', '-', $this->locale );
@@ -223,74 +92,66 @@
223 92 }
224 93 }
225 94
226 95 /**
227 - * Get the flag informations:
96 + * Get the flag informations
97 + * 'url' => Flag url
98 + * 'src' => Optional, src attribute value if different of the url, for example if base64 encoded
99 + * 'width' => Optional, flag width in pixels
100 + * 'height' => Optional, flag height in pixels
228 101 *
229 102 * @since 2.6
230 103 *
231 104 * @param string $code Flag code.
232 - * @return array {
233 - * Flag informations.
234 - *
235 - * @type string $url Flag url.
236 - * @type string $src Optional, src attribute value if different of the url, for example if base64 encoded.
237 - * @type int $width Optional, flag width in pixels.
238 - * @type int $height Optional, flag height in pixels.
239 - * }
105 + * @return array Flag informations.
240 106 */
241 107 public static function get_flag_informations( $code ) {
242 108 $flag = array( 'url' => '' );
243 109
244 - // Polylang builtin flags.
110 + // Polylang builtin flags
245 111 if ( ! empty( $code ) && file_exists( POLYLANG_DIR . ( $file = '/flags/' . $code . '.png' ) ) ) {
246 - $flag['url'] = plugins_url( $file, POLYLANG_FILE );
247 -
248 - // If base64 encoded flags are preferred.
249 - if ( ! defined( 'PLL_ENCODED_FLAGS' ) || PLL_ENCODED_FLAGS ) {
250 - list( $flag['width'], $flag['height'] ) = getimagesize( POLYLANG_DIR . $file );
251 - $file_contents = file_get_contents( POLYLANG_DIR . $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
252 - $flag['src'] = 'data:image/png;base64,' . base64_encode( $file_contents ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
253 - }
112 + $flag['url'] = $_url = plugins_url( $file, POLYLANG_FILE );
254 113 }
255 114
256 115 /**
257 - * Filters flag informations:
116 + * Filter flag informations
117 + * 'url' => Flag url
118 + * 'src' => Optional, src attribute value if different of the url, for example if base64 encoded
119 + * 'width' => Optional, flag width in pixels
120 + * 'height' => Optional, flag height in pixels
258 121 *
259 122 * @since 2.4
260 123 *
261 - * @param array $flag {
262 - * Information about the flag.
263 - *
264 - * @type string $url Flag url.
265 - * @type string $src Optional, src attribute value if different of the url, for example if base64 encoded.
266 - * @type int $width Optional, flag width in pixels.
267 - * @type int $height Optional, flag height in pixels.
268 - * }
269 - * @param string $code Flag code.
124 + * @param array $flag Information about the flag
125 + * @param string $code Flag code
270 126 */
271 127 $flag = apply_filters( 'pll_flag', $flag, $code );
272 128
273 - $flag['url'] = esc_url_raw( $flag['url'] );
274 -
275 129 if ( empty( $flag['src'] ) ) {
276 - $flag['src'] = esc_url( set_url_scheme( $flag['url'], 'relative' ) );
130 + // If using predefined flags and base64 encoded flags are preferred
131 + if ( isset( $_url ) && $flag['url'] === $_url && ( ! defined( 'PLL_ENCODED_FLAGS' ) || PLL_ENCODED_FLAGS ) ) {
132 + list( $flag['width'], $flag['height'] ) = getimagesize( POLYLANG_DIR . $file );
133 + $file_contents = file_get_contents( POLYLANG_DIR . $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
134 + $flag['src'] = 'data:image/png;base64,' . base64_encode( $file_contents ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
135 + } else {
136 + $flag['src'] = esc_url( set_url_scheme( $flag['url'], 'relative' ) );
137 + }
277 138 }
278 139
140 + $flag['url'] = esc_url_raw( $flag['url'] );
141 +
279 142 return $flag;
280 143 }
281 144
282 145 /**
283 - * Sets flag_url and flag properties.
146 + * Sets flag_url and flag properties
284 147 *
285 148 * @since 1.2
286 - *
287 - * @return void
288 149 */
289 150 public function set_flag() {
290 151 $flags = array( 'flag' => self::get_flag_informations( $this->flag_code ) );
291 152
292 - // Custom flags?
153 + // Custom flags ?
293 154 $directories = array(
294 155 PLL_LOCAL_DIR,
295 156 get_stylesheet_directory() . '/polylang',
296 157 get_template_directory() . '/polylang',
@@ -303,21 +164,18 @@
303 164 }
304 165 }
305 166
306 167 /**
307 - * Filters the custom flag informations.
168 + * Filter the custom flag informations
169 + * 'url' => Flag url
170 + * 'src' => Optional, src attribute value if different of the url, for example if base64 encoded
171 + * 'width' => Optional, flag width in pixels
172 + * 'height' => Optional, flag height in pixels
308 173 *
309 - * @param array $flag {
310 - * Information about the custom flag.
174 + * @since 2.4
311 175 *
312 - * @type string $url Flag url.
313 - * @type string $src Optional, src attribute value if different of the url, for example if base64 encoded.
314 - * @type int $width Optional, flag width in pixels.
315 - * @type int $height Optional, flag height in pixels.
316 - * }
317 - * @param string $code Flag code.
318 - *
319 - * @since 2.4
176 + * @param array $flag Information about the custom flag
177 + * @param string $code Flag code
320 178 */
321 179 $flags['custom_flag'] = apply_filters( 'pll_custom_flag', empty( $flags['custom_flag'] ) ? null : $flags['custom_flag'], $this->flag_code );
322 180
323 181 if ( ! empty( $flags['custom_flag']['url'] ) ) {
@@ -330,16 +188,16 @@
330 188 unset( $flags['custom_flag'] );
331 189 }
332 190
333 191 /**
334 - * Filters the flag title attribute.
335 - * Defaults to the language name.
192 + * Filter the flag title attribute
193 + * Defaults to the language name
336 194 *
337 195 * @since 0.7
338 196 *
339 - * @param string $title The flag title attribute.
340 - * @param string $slug The language code.
341 - * @param string $locale The language locale.
197 + * @param string $title the flag title attribute
198 + * @param string $slug the language code
199 + * @param string $locale the language locale
342 200 */
343 201 $title = apply_filters( 'pll_flag_title', $this->name, $this->slug, $this->locale );
344 202
345 203 foreach ( $flags as $key => $flag ) {
@@ -345,14 +203,14 @@
345 203 foreach ( $flags as $key => $flag ) {
346 204 $this->{$key . '_url'} = empty( $flag['url'] ) ? '' : $flag['url'];
347 205
348 206 /**
349 - * Filters the html markup of a flag.
207 + * Filter the html markup of a flag
350 208 *
351 209 * @since 1.0.2
352 210 *
353 - * @param string $flag Html markup of the flag or empty string.
354 - * @param string $slug Language code.
211 + * @param string $flag html markup of the flag or empty string
212 + * @param string $slug language code
355 213 */
356 214 $this->{$key} = apply_filters(
357 215 'pll_get_flag',
358 216 self::get_flag_html( $flag, $title, $this->name ),
@@ -361,46 +219,24 @@
361 219 }
362 220 }
363 221
364 222 /**
365 - * Get HTML code for flag.
223 + * Get HTML code for flag
366 224 *
367 225 * @since 2.7
368 226 *
369 - * @param array $flag Flag properties: src, width and height.
370 - * @param string $title Optional title attribute.
371 - * @param string $alt Optional alt attribute.
372 - * @return string
227 + * @param array $flag flag properties: src, width and height
228 + * @param string $title optional title attribute
229 + * @param string $alt optional alt attribute
373 230 */
374 231 public static function get_flag_html( $flag, $title = '', $alt = '' ) {
375 - if ( empty( $flag['src'] ) ) {
376 - return '';
377 - }
378 -
379 - $alt_attr = empty( $alt ) ? '' : sprintf( ' alt="%s"', esc_attr( $alt ) );
380 - $width_attr = empty( $flag['width'] ) ? '' : sprintf( ' width="%s"', (int) $flag['width'] );
381 - $height_attr = empty( $flag['height'] ) ? '' : sprintf( ' height="%s"', (int) $flag['height'] );
382 -
383 - $style = '';
384 - $sizes = array_intersect_key( $flag, array_flip( array( 'width', 'height' ) ) );
385 -
386 - if ( ! empty( $sizes ) ) {
387 - array_walk(
388 - $sizes,
389 - function ( &$value, $key ) {
390 - $value = sprintf( '%s: %dpx;', esc_attr( $key ), (int) $value );
391 - }
392 - );
393 - $style = sprintf( ' style="%s"', implode( ' ', $sizes ) );
394 - }
395 -
396 - return sprintf(
232 + return empty( $flag['src'] ) ? '' : sprintf(
397 233 '<img src="%s"%s%s%s%s />',
398 234 $flag['src'],
399 - $alt_attr,
400 - $width_attr,
401 - $height_attr,
402 - $style
235 + empty( $title ) ? '' : sprintf( ' title="%s"', esc_attr( $title ) ),
236 + empty( $alt ) ? '' : sprintf( ' alt="%s"', esc_attr( $alt ) ),
237 + empty( $flag['width'] ) ? '' : sprintf( ' width="%s"', (int) $flag['width'] ),
238 + empty( $flag['height'] ) ? '' : sprintf( ' height="%s"', (int) $flag['height'] )
403 239 );
404 240 }
405 241
406 242 /**
@@ -406,10 +242,8 @@
406 242 /**
407 243 * Returns the html of the custom flag if any, or the default flag otherwise.
408 244 *
409 245 * @since 2.8
410 - *
411 - * @return string
412 246 */
413 247 public function get_display_flag() {
414 248 return empty( $this->custom_flag ) ? $this->flag : $this->custom_flag;
415 249 }
@@ -417,35 +251,30 @@
417 251 /**
418 252 * Returns the url of the custom flag if any, or the default flag otherwise.
419 253 *
420 254 * @since 2.8
421 - *
422 - * @return string
423 255 */
424 256 public function get_display_flag_url() {
425 - return empty( $this->custom_flag_url ) ? $this->flag_url : $this->custom_flag_url;
257 + return empty( $this->custom_flag_url ) ? $this->flag : $this->custom_flag_url;
426 258 }
427 259
428 260 /**
429 - * Updates post and term count.
261 + * Updates post and term count
430 262 *
431 263 * @since 1.2
432 - *
433 - * @return void
434 264 */
435 265 public function update_count() {
436 - wp_update_term_count( $this->term_taxonomy_id, 'language' ); // Posts count.
437 - wp_update_term_count( $this->tl_term_taxonomy_id, 'term_language' ); // Terms count.
266 + wp_update_term_count( $this->term_taxonomy_id, 'language' ); // posts count
267 + wp_update_term_count( $this->tl_term_taxonomy_id, 'term_language' ); // terms count
438 268 }
439 269
440 270 /**
441 - * Set home_url and search_url properties.
271 + * Set home_url and search_url properties
442 272 *
443 273 * @since 1.3
444 274 *
445 - * @param string $search_url Home url to use in search forms.
446 - * @param string $home_url Home url.
447 - * @return void
275 + * @param string $search_url
276 + * @param string $home_url
448 277 */
449 278 public function set_home_url( $search_url, $home_url ) {
450 279 $this->search_url = $search_url;
451 280 $this->home_url = $home_url;
@@ -451,15 +280,13 @@
451 280 $this->home_url = $home_url;
452 281 }
453 282
454 283 /**
455 - * Sets the scheme of the home url and the flag urls.
284 + * Sets the scheme of the home url and the flag urls
456 285 *
457 286 * This can't be cached across pages.
458 287 *
459 288 * @since 2.8
460 - *
461 - * @return void
462 289 */
463 290 public function set_url_scheme() {
464 291 $this->home_url = set_url_scheme( $this->home_url );
465 292 $this->search_url = set_url_scheme( $this->search_url );
@@ -471,14 +298,14 @@
471 298 }
472 299 }
473 300
474 301 /**
475 - * Returns the language locale.
476 - * Converts WP locales to W3C valid locales for display.
302 + * Returns the language locale
303 + * Converts WP locales to W3C valid locales for display
477 304 *
478 305 * @since 1.8
479 306 *
480 - * @param string $filter Either 'display' or 'raw', defaults to raw.
307 + * @param string $filter either 'display' or 'raw', defaults to raw
481 308 * @return string
482 309 */
483 310 public function get_locale( $filter = 'raw' ) {
484 311 return 'display' === $filter ? $this->w3c : $this->locale;