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/language.php +139 -380 3.02.1 View file →
@@ -1,345 +1,125 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
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.
4 + * a language object is made of two terms in 'language' and 'term_language' taxonomies
5 + * manipulating only one object per language instead of two terms should make things easier
9 6 *
7 + * properties:
8 + * term_id => id of term in 'language' taxonomy
9 + * name => language name. Ex: English
10 + * slug => language code used in url. Ex: en
11 + * term_group => order of the language when displayed in a list of languages
12 + * term_taxonomy_id => term taxonomy id in 'language' taxonomy
13 + * taxonomy => 'language'
14 + * description => language locale for backward compatibility
15 + * parent => 0 / not used
16 + * count => number of posts and pages in that language
17 + * tl_term_id => id of the term in 'term_language' taxonomy
18 + * tl_term_taxonomy_id => term taxonomy id in 'term_language' taxonomy
19 + * tl_count => number of terms in that language ( not used by Polylang )
20 + * locale => WordPress language locale. Ex: en_US
21 + * is_rtl => 1 if the language is rtl
22 + * flag_code => code of the flag
23 + * flag_url => url of the flag
24 + * flag => html img of the flag
25 + * custom_flag_url => url of the custom flag if exists, internal use only, moves to flag_url on frontend
26 + * custom_flag => html img of the custom flag if exists, internal use only, moves to flag on frontend
27 + * home_url => home url in this language
28 + * search_url => home url to use in search forms
29 + * host => host of this language
30 + * mo_id => id of the post storing strings translations
31 + * page_on_front => id of the page on front in this language ( set from pll_languages_list filter )
32 + * page_for_posts => id of the page for posts in this language ( set from pll_languages_list filter )
33 + *
10 34 * @since 1.2
11 35 */
12 36 class PLL_Language {
13 - /**
14 - * Id of the term in 'language' taxonomy.
15 - *
16 - * @var int
17 - */
18 - public $term_id;
37 + public $term_id, $name, $slug, $term_group, $term_taxonomy_id, $taxonomy, $description, $parent, $count;
38 + public $tl_term_id, $tl_term_taxonomy_id, $tl_count;
39 + public $locale, $is_rtl;
40 + public $flag_url, $flag;
41 + public $home_url, $search_url;
42 + public $host, $mo_id;
43 + public $page_on_front, $page_for_posts;
19 44
20 45 /**
21 - * Language name. Ex: English.
46 + * constructor: builds a language object given its two corresponding terms in language and term_language taxonomies
22 47 *
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 48 * @since 1.2
185 49 *
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.
50 + * @param object|array $language 'language' term or language object properties stored as an array
51 + * @param object $term_language corresponding 'term_language' term
188 52 */
189 53 public function __construct( $language, $term_language = null ) {
190 - // Build the object from all properties stored as an array.
191 - if ( is_array( $language ) ) {
54 + // build the object from all properties stored as an array
55 + if ( empty( $term_language ) ) {
192 56 foreach ( $language as $prop => $value ) {
193 57 $this->$prop = $value;
194 58 }
195 59 }
196 60
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;
61 + // build the object from taxonomies
62 + else {
63 + foreach ( $language as $prop => $value ) {
64 + $this->$prop = in_array( $prop, array( 'term_id', 'term_taxonomy_id', 'count' ) ) ? (int) $language->$prop : $language->$prop;
65 + }
205 66
67 + // although it would be convenient here, don't assume the term is shared between taxonomies as it may not be the case in future
68 + // http://make.wordpress.org/core/2013/07/28/potential-roadmap-for-taxonomy-meta-and-post-relationships/
206 69 $this->tl_term_id = (int) $term_language->term_id;
207 70 $this->tl_term_taxonomy_id = (int) $term_language->term_taxonomy_id;
208 71 $this->tl_count = (int) $term_language->count;
209 72
210 - // The description field can contain any property.
73 + // the description field can contain any property
74 + // backward compatibility for is_rtl
211 75 $description = maybe_unserialize( $language->description );
212 76 foreach ( $description as $prop => $value ) {
213 77 'rtl' == $prop ? $this->is_rtl = $value : $this->$prop = $value;
214 78 }
215 79
80 + $this->description = &$this->locale; // backward compatibility with Polylang < 1.2
81 +
216 82 $this->mo_id = PLL_MO::get_id( $this );
217 -
218 - $languages = include POLYLANG_DIR . '/settings/languages.php';
219 - $this->w3c = isset( $languages[ $this->locale ]['w3c'] ) ? $languages[ $this->locale ]['w3c'] : str_replace( '_', '-', $this->locale );
220 - if ( isset( $languages[ $this->locale ]['facebook'] ) ) {
221 - $this->facebook = $languages[ $this->locale ]['facebook'];
222 - }
223 83 }
224 84 }
225 85
226 86 /**
227 - * Get the flag informations:
87 + * sets flag_url and flag properties
228 88 *
229 - * @since 2.6
230 - *
231 - * @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 - * }
240 - */
241 - public static function get_flag_informations( $code ) {
242 - $flag = array( 'url' => '' );
243 -
244 - // Polylang builtin flags.
245 - 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 - }
254 - }
255 -
256 - /**
257 - * Filters flag informations:
258 - *
259 - * @since 2.4
260 - *
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.
270 - */
271 - $flag = apply_filters( 'pll_flag', $flag, $code );
272 -
273 - $flag['url'] = esc_url_raw( $flag['url'] );
274 -
275 - if ( empty( $flag['src'] ) ) {
276 - $flag['src'] = esc_url( set_url_scheme( $flag['url'], 'relative' ) );
277 - }
278 -
279 - return $flag;
280 - }
281 -
282 - /**
283 - * Sets flag_url and flag properties.
284 - *
285 89 * @since 1.2
286 - *
287 - * @return void
288 90 */
289 91 public function set_flag() {
290 - $flags = array( 'flag' => self::get_flag_informations( $this->flag_code ) );
92 + $flags['flag']['url'] = '';
291 93
292 - // Custom flags?
293 - $directories = array(
294 - PLL_LOCAL_DIR,
295 - get_stylesheet_directory() . '/polylang',
296 - get_template_directory() . '/polylang',
297 - );
94 + // Polylang builtin flags
95 + if ( ! empty( $this->flag_code ) && file_exists( POLYLANG_DIR . ( $file = '/flags/' . $this->flag_code . '.png' ) ) ) {
96 + $flags['flag']['url'] = esc_url_raw( plugins_url( $file, POLYLANG_FILE ) );
298 97
299 - foreach ( $directories as $dir ) {
300 - if ( file_exists( $file = "{$dir}/{$this->locale}.png" ) || file_exists( $file = "{$dir}/{$this->locale}.jpg" ) || file_exists( $file = "{$dir}/{$this->locale}.svg" ) ) {
301 - $flags['custom_flag']['url'] = content_url( '/' . str_replace( WP_CONTENT_DIR, '', $file ) );
302 - break;
98 + // if base64 encoded flags are preferred
99 + if ( ! defined( 'PLL_ENCODED_FLAGS' ) || PLL_ENCODED_FLAGS ) {
100 + $flags['flag']['src'] = 'data:image/png;base64,' . base64_encode( file_get_contents( POLYLANG_DIR . $file ) );
101 + } else {
102 + $flags['flag']['src'] = esc_url( plugins_url( $file, POLYLANG_FILE ) );
303 103 }
304 104 }
305 105
306 - /**
307 - * Filters the custom flag informations.
308 - *
309 - * @param array $flag {
310 - * Information about the custom flag.
311 - *
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
320 - */
321 - $flags['custom_flag'] = apply_filters( 'pll_custom_flag', empty( $flags['custom_flag'] ) ? null : $flags['custom_flag'], $this->flag_code );
322 -
323 - if ( ! empty( $flags['custom_flag']['url'] ) ) {
324 - if ( empty( $flags['custom_flag']['src'] ) ) {
325 - $flags['custom_flag']['src'] = esc_url( set_url_scheme( $flags['custom_flag']['url'], 'relative' ) );
326 - }
327 -
328 - $flags['custom_flag']['url'] = esc_url_raw( $flags['custom_flag']['url'] );
329 - } else {
330 - unset( $flags['custom_flag'] );
106 + // custom flags ?
107 + if ( file_exists( PLL_LOCAL_DIR . ( $file = '/' . $this->locale . '.png' ) ) || file_exists( PLL_LOCAL_DIR . ( $file = '/' . $this->locale . '.jpg' ) ) ) {
108 + $url = content_url( '/polylang' . $file );
109 + $flags['custom_flag']['url'] = esc_url_raw( $url );
110 + $flags['custom_flag']['src'] = esc_url( $url );
331 111 }
332 112
333 113 /**
334 - * Filters the flag title attribute.
335 - * Defaults to the language name.
114 + * Filter the flag title attribute
115 + * Defaults to the language name
336 116 *
337 117 * @since 0.7
338 118 *
339 - * @param string $title The flag title attribute.
340 - * @param string $slug The language code.
341 - * @param string $locale The language locale.
119 + * @param string $title the flag title attribute
120 + * @param string $slug the language code
121 + * @param string $locale the language locale
342 122 */
343 123 $title = apply_filters( 'pll_flag_title', $this->name, $this->slug, $this->locale );
344 124
345 125 foreach ( $flags as $key => $flag ) {
@@ -345,18 +125,22 @@
345 125 foreach ( $flags as $key => $flag ) {
346 126 $this->{$key . '_url'} = empty( $flag['url'] ) ? '' : $flag['url'];
347 127
348 128 /**
349 - * Filters the html markup of a flag.
129 + * Filter the html markup of a flag
350 130 *
351 131 * @since 1.0.2
352 132 *
353 - * @param string $flag Html markup of the flag or empty string.
354 - * @param string $slug Language code.
133 + * @param string $flag html markup of the flag or empty string
134 + * @param string $slug language code
355 135 */
356 - $this->{$key} = apply_filters(
357 - 'pll_get_flag',
358 - self::get_flag_html( $flag, $title, $this->name ),
136 + $this->{$key} = apply_filters( 'pll_get_flag', empty( $flag['src'] ) ? '' :
137 + sprintf(
138 + '<img src="%s" title="%s" alt="%s" />',
139 + $flag['src'],
140 + esc_attr( $title ),
141 + esc_attr( $this->name )
142 + ),
359 143 $this->slug
360 144 );
361 145 }
362 146 }
@@ -361,91 +145,48 @@
361 145 }
362 146 }
363 147
364 148 /**
365 - * Get HTML code for flag.
149 + * replace flag by custom flag
150 + * takes care of url scheme
366 151 *
367 - * @since 2.7
368 - *
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
152 + * @since 1.7
373 153 */
374 - public static function get_flag_html( $flag, $title = '', $alt = '' ) {
375 - if ( empty( $flag['src'] ) ) {
376 - return '';
154 + public function set_custom_flag() {
155 + // overwrite with custom flags on frontend only
156 + if ( ! empty( $this->custom_flag ) ) {
157 + $this->flag = $this->custom_flag;
158 + $this->flag_url = $this->custom_flag_url;
159 + unset( $this->custom_flag, $this->custom_flag_url ); // hide this
377 160 }
378 161
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 ) );
162 + // set url scheme, also for default flags
163 + if ( is_ssl() ) {
164 + $this->flag = str_replace( 'http://', 'https://', $this->flag );
165 + $this->flag_url = str_replace( 'http://', 'https://', $this->flag_url );
166 + } else {
167 + $this->flag = str_replace( 'https://', 'http://', $this->flag );
168 + $this->flag_url = str_replace( 'https://', 'http://', $this->flag_url );
394 169 }
395 -
396 - return sprintf(
397 - '<img src="%s"%s%s%s%s />',
398 - $flag['src'],
399 - $alt_attr,
400 - $width_attr,
401 - $height_attr,
402 - $style
403 - );
404 170 }
405 171
406 172 /**
407 - * Returns the html of the custom flag if any, or the default flag otherwise.
173 + * updates post and term count
408 174 *
409 - * @since 2.8
410 - *
411 - * @return string
412 - */
413 - public function get_display_flag() {
414 - return empty( $this->custom_flag ) ? $this->flag : $this->custom_flag;
415 - }
416 -
417 - /**
418 - * Returns the url of the custom flag if any, or the default flag otherwise.
419 - *
420 - * @since 2.8
421 - *
422 - * @return string
423 - */
424 - public function get_display_flag_url() {
425 - return empty( $this->custom_flag_url ) ? $this->flag_url : $this->custom_flag_url;
426 - }
427 -
428 - /**
429 - * Updates post and term count.
430 - *
431 175 * @since 1.2
432 - *
433 - * @return void
434 176 */
435 177 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.
178 + wp_update_term_count( $this->term_taxonomy_id, 'language' ); // posts count
179 + wp_update_term_count( $this->tl_term_taxonomy_id, 'term_language' ); // terms count
438 180 }
439 181
440 182 /**
441 - * Set home_url and search_url properties.
183 + * set home_url and search_url properties
442 184 *
443 185 * @since 1.3
444 186 *
445 - * @param string $search_url Home url to use in search forms.
446 - * @param string $home_url Home url.
447 - * @return void
187 + * @param string $search_url
188 + * @param string $home_url
448 189 */
449 190 public function set_home_url( $search_url, $home_url ) {
450 191 $this->search_url = $search_url;
451 192 $this->home_url = $home_url;
@@ -451,36 +192,54 @@
451 192 $this->home_url = $home_url;
452 193 }
453 194
454 195 /**
455 - * Sets the scheme of the home url and the flag urls.
196 + * set home_url scheme
197 + * this can't be cached accross pages
456 198 *
457 - * This can't be cached across pages.
458 - *
459 - * @since 2.8
460 - *
461 - * @return void
199 + * @since 1.6.4
462 200 */
463 - public function set_url_scheme() {
464 - $this->home_url = set_url_scheme( $this->home_url );
465 - $this->search_url = set_url_scheme( $this->search_url );
201 + public function set_home_url_scheme() {
202 + if ( is_ssl() ) {
203 + $this->home_url = str_replace( 'http://', 'https://', $this->home_url );
204 + $this->search_url = str_replace( 'http://', 'https://', $this->search_url );
205 + }
466 206
467 - // Set url scheme, also for the flags.
468 - $this->flag_url = set_url_scheme( $this->flag_url );
469 - if ( ! empty( $this->custom_flag_url ) ) {
470 - $this->custom_flag_url = set_url_scheme( $this->custom_flag_url );
207 + else {
208 + $this->home_url = str_replace( 'https://', 'http://', $this->home_url );
209 + $this->search_url = str_replace( 'https://', 'http://', $this->search_url );
471 210 }
472 211 }
473 212
474 213 /**
475 - * Returns the language locale.
476 - * Converts WP locales to W3C valid locales for display.
214 + * returns the language locale
215 + * converts WP locales to W3C valid locales for display
216 + * @see #33511
477 217 *
478 218 * @since 1.8
479 219 *
480 - * @param string $filter Either 'display' or 'raw', defaults to raw.
220 + * @param string $filter either 'display' or 'raw', defaults to raw
481 221 * @return string
482 222 */
483 223 public function get_locale( $filter = 'raw' ) {
484 - return 'display' === $filter ? $this->w3c : $this->locale;
224 + if ( 'display' == $filter ) {
225 + static $valid_locales = array(
226 + 'bel' => 'be',
227 + 'bre' => 'br',
228 + 'de_CH_informal' => 'de_CH',
229 + 'de_DE_formal' => 'de_DE',
230 + 'dzo' => 'dz',
231 + 'ido' => 'io',
232 + 'kin' => 'rw',
233 + 'oci' => 'oc',
234 + 'mri' => 'mi',
235 + 'nl_NL_formal' => 'nl_NL',
236 + 'roh' => 'rm',
237 + 'srd' => 'sc',
238 + 'tuk' => 'tk',
239 + );
240 + $locale = isset( $valid_locales[ $this->locale ] ) ? $valid_locales[ $this->locale ] : $this->locale;
241 + return str_replace( '_', '-', $locale );
242 + }
243 + return $this->locale;
485 244 }
486 245 }