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