Repository navigation
Expand file tree
/
Copy pathlanguage.php
More file actions
748 lines (677 loc) · 20 KB
/
Copy pathlanguage.php
File metadata and controls
748 lines (677 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
<?php
/**
* @package Polylang
*/
/**
* A language object is made of two terms in 'language' and 'term_language' taxonomies.
* Manipulating only one object per language instead of two terms should make things easier.
*
* @since 1.2
* @immutable
*
* @phpstan-type LanguagePropData array{
* term_id: positive-int,
* term_taxonomy_id: positive-int,
* count: int<0, max>
* }
* @phpstan-type LanguageData array{
* term_props: array{
* language: LanguagePropData,
* }&array<non-empty-string, LanguagePropData>,
* name: non-empty-string,
* slug: non-empty-string,
* locale: non-empty-string,
* w3c: non-empty-string,
* flag_code: string,
* term_group: int,
* is_rtl: int<0, 1>,
* facebook?: string,
* home_url: non-empty-string,
* search_url: non-empty-string,
* host: non-empty-string,
* flag_url: string,
* flag: string,
* custom_flag_url?: string,
* custom_flag?: string,
* page_on_front?: int<0, max>,
* page_for_posts?: int<0, max>,
* active?: bool,
* fallbacks?: array<non-empty-string>,
* is_default: bool,
* admin_flag: array{'aria-hidden': non-empty-string, '': non-empty-string}
* }
*/
class PLL_Language extends PLL_Language_Deprecated {
/**
* Language name. Ex: English.
*
* @var string
*
* @phpstan-var non-empty-string
*/
public $name;
/**
* Language code used in URL. Ex: en.
*
* @var string
*
* @phpstan-var non-empty-string
*/
public $slug;
/**
* Order of the language when displayed in a list of languages.
*
* @var int
*/
public $term_group;
/**
* ID of the term in 'language' taxonomy.
* Duplicated from `$this->term_props['language']['term_id'],
* but kept to facilitate the use of it.
*
* @var int
*
* @phpstan-var int<1, max>
*/
public $term_id;
/**
* WordPress language locale. Ex: en_US.
*
* @var string
*
* @phpstan-var non-empty-string
*/
public $locale;
/**
* 1 if the language is rtl, 0 otherwise.
*
* @var int
*
* @phpstan-var int<0, 1>
*/
public $is_rtl;
/**
* W3C locale.
*
* @var string
*
* @phpstan-var non-empty-string
*/
public $w3c;
/**
* Facebook locale.
*
* @var string
*/
public $facebook;
/**
* Home URL in this language.
*
* @var string
*
* @phpstan-var non-empty-string
*/
private $home_url;
/**
* Home URL to use in search forms.
*
* @var string
*
* @phpstan-var non-empty-string
*/
private $search_url;
/**
* Host corresponding to this language.
*
* @var string
*
* @phpstan-var non-empty-string
*/
public $host;
/**
* ID of the page on front in this language (set from pll_additional_language_data filter).
*
* @var int
*
* @phpstan-var int<0, max>
*/
public $page_on_front;
/**
* ID of the page for posts in this language (set from pll_additional_language_data filter).
*
* @var int
*
* @phpstan-var int<0, max>
*/
public $page_for_posts;
/**
* Code of the flag.
*
* @var string
*/
public $flag_code;
/**
* URL of the flag. Always set to the main domain.
*
* @var string
*/
public $flag_url;
/**
* HTML markup of the flag.
*
* @var string
*/
public $flag;
/**
* URL of the custom flag if it exists. Always set to the main domain.
*
* @var string
*/
public $custom_flag_url;
/**
* HTML markup of the custom flag if it exists.
*
* @var string
*/
public $custom_flag;
/**
* Whether or not the language is active. Default `true`.
*
* @var bool
*/
public $active;
/**
* List of WordPress language locales. Ex: array( 'en_GB' ).
*
* @var string[]
*
* @phpstan-var list<non-empty-string>
*/
public $fallbacks;
/**
* Whether the language is the default one.
*
* @var bool
*/
public $is_default;
/**
* Stores language term properties (like term IDs and counts) for each language taxonomy (`language`,
* `term_language`, etc).
* This stores the values of the properties `$term_id` + `$term_taxonomy_id` + `$count` (`language`), `$tl_term_id`
* + `$tl_term_taxonomy_id` + `$tl_count` (`term_language`), and the `term_id` + `term_taxonomy_id` + `count` for
* other language taxonomies.
*
* @var array[] Array keys are language term names.
*
* @example array(
* 'language' => array(
* 'term_id' => 7,
* 'term_taxonomy_id' => 8,
* 'count' => 11,
* ),
* 'term_language' => array(
* 'term_id' => 11,
* 'term_taxonomy_id' => 12,
* 'count' => 6,
* ),
* 'foo_language' => array(
* 'term_id' => 33,
* 'term_taxonomy_id' => 34,
* 'count' => 0,
* ),
* )
*
* @phpstan-var array{
* language: LanguagePropData,
* }
* &array<non-empty-string, LanguagePropData>
*/
protected $term_props;
/**
* @var array
*
* @phpstan-var array{'aria-hidden': non-empty-string, '': non-empty-string}
*/
private $admin_flag;
/**
* Constructor: builds a language object given the corresponding data.
*
* @since 1.2
* @since 3.4 Only accepts one argument.
*
* @param array $language_data {
* Language object properties stored as an array.
*
* @type array[] $term_props An array of language term properties. Array keys are language taxonomy names
* (`language` and `term_language` are mandatory), array values are arrays of
* language term properties (`term_id`, `term_taxonomy_id`, and `count`).
* @type string $name Language name. Ex: English.
* @type string $slug Language code used in URL. Ex: en.
* @type string $locale WordPress language locale. Ex: en_US.
* @type string $w3c W3C locale.
* @type string $flag_code Code of the flag.
* @type int $term_group Order of the language when displayed in a list of languages.
* @type int $is_rtl `1` if the language is rtl, `0` otherwise.
* @type string $facebook Optional. Facebook locale.
* @type string $home_url Home URL in this language.
* @type string $search_url Home URL to use in search forms.
* @type string $host Host corresponding to this language.
* @type string $flag_url URL of the flag.
* @type string $flag HTML markup of the flag.
* @type string $custom_flag_url Optional. URL of the custom flag if it exists.
* @type string $custom_flag Optional. HTML markup of the custom flag if it exists.
* @type int $page_on_front Optional. ID of the page on front in this language.
* @type int $page_for_posts Optional. ID of the page for posts in this language.
* @type bool $active Optional. Whether or not the language is active. Default `true`.
* @type string[] $fallbacks Optional. List of WordPress language locales. Ex: array( 'en_GB' ).
* @type bool $is_default Whether or not the language is the default one.
* @type array $admin_flag An array containing the keys `''` (empty string) for the "normal" flag, and
* `'aria-hidden'` for the flag hidden to screen readers.
* }
*
* @phpstan-param LanguageData $language_data
*/
public function __construct( array $language_data ) {
// Default values for optional params.
$defaults = array(
'facebook' => '',
'page_on_front' => 0,
'page_for_posts' => 0,
'custom_flag_url' => '',
'custom_flag' => '',
'active' => true,
'fallbacks' => array(),
);
foreach ( array_merge( $defaults, $language_data ) as $prop => $value ) {
$this->$prop = $value;
}
$this->term_id = $this->term_props['language']['term_id'];
}
/**
* Returns a language term property value (term ID, term taxonomy ID, or count).
*
* @since 3.4
*
* @param string $taxonomy_name Name of the taxonomy.
* @param string $prop_name Name of the property: 'term_taxonomy_id', 'term_id', 'count'.
* @return int
*
* @phpstan-param non-empty-string $taxonomy_name
* @phpstan-param 'term_taxonomy_id'|'term_id'|'count' $prop_name
* @phpstan-return int<0, max>
*/
public function get_tax_prop( $taxonomy_name, $prop_name ) {
return $this->term_props[ $taxonomy_name ][ $prop_name ] ?? 0;
}
/**
* Returns the language term props for all content types.
*
* @since 3.4
*
* @param string $property Name of the field to return. An empty string to return them all.
* @return (int[]|int)[] Array keys are taxonomy names, array values depend of `$property`.
*
* @phpstan-param 'term_taxonomy_id'|'term_id'|'count'|'' $property
* @phpstan-return array<non-empty-string, (
* $property is non-empty-string ?
* (
* $property is 'count' ?
* int<0, max> :
* positive-int
* ) :
* LanguagePropData
* )>
*/
public function get_tax_props( $property = '' ) {
if ( empty( $property ) ) {
return $this->term_props;
}
$term_props = array();
foreach ( $this->term_props as $taxonomy_name => $props ) {
$term_props[ $taxonomy_name ] = $props[ $property ];
}
return $term_props;
}
/**
* Returns a predefined HTML flag.
*
* @since 3.4
*
* @param string $flag_code Flag code to render.
* @return string HTML code for the flag.
*/
public static function get_predefined_flag( $flag_code ) {
return self::get_flag_html( self::get_flag_information( $flag_code ) );
}
/**
* Returns the flag information.
*
* @since 2.6
*
* @param string $code Flag code.
* @return array {
* Flag information.
*
* @type string $url Flag url.
* @type string $src Optional, src attribute value if different of the url, for example if base64 encoded.
* @type int $width Optional, flag width in pixels.
* @type int $height Optional, flag height in pixels.
* }
*
* @phpstan-return array{
* url: string,
* src: string,
* width?: positive-int,
* height?: positive-int
* }
*/
public static function get_flag_information( $code ) {
$default_flag = array(
'url' => '',
'src' => '',
);
if ( empty( $code ) ) {
return $default_flag;
}
// Polylang builtin flags.
$file = "/vendor/wpsyntex/flags/{$code}.svg";
if ( is_readable( POLYLANG_ROOT_DIR . $file ) ) {
$default_flag = array(
'url' => plugins_url( $file, POLYLANG_ROOT_FILE ),
'src' => '',
'width' => 18,
'height' => 12,
);
// If base64 encoded flags are preferred.
if ( pll_get_constant( 'PLL_ENCODED_FLAGS', true ) ) {
$content = file_get_contents( POLYLANG_ROOT_DIR . $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
if ( ! empty( $content ) ) {
$default_flag['src'] = 'data:image/svg+xml,' . self::encode_svg( $content );
}
}
}
/**
* Filters flag information:
*
* @since 2.4
*
* @param array $flag {
* Information about the flag.
*
* @type string $url Flag url.
* @type string $src Optional, src attribute value if different of the url, for example if base64 encoded.
* @type int $width Optional, flag width in pixels.
* @type int $height Optional, flag height in pixels.
* }
* @param string $code Flag code.
*/
$flag = apply_filters( 'pll_flag', $default_flag, $code );
$flag['url'] = sanitize_url( $flag['url'] );
if ( empty( $flag['src'] ) || ( $flag['src'] === $default_flag['src'] && $flag['url'] !== $default_flag['url'] ) ) {
$flag['src'] = esc_url( set_url_scheme( $flag['url'], 'relative' ) );
}
if ( isset( $flag['width'] ) ) {
$flag['width'] = absint( $flag['width'] );
}
if ( isset( $flag['height'] ) ) {
$flag['height'] = absint( $flag['height'] );
}
return $flag;
}
/**
* Returns HTML code for flag.
*
* @since 2.7
* @since 3.9 Removed param `$title` and pass `$alt` as second param.
*
* @param array $flag Flag properties: src, width and height.
* @param string $alt Optional alt attribute.
* @return string
*
* @phpstan-param array{
* src: string,
* width?: int|numeric-string,
* height?: int|numeric-string
* } $flag
*/
public static function get_flag_html( $flag, $alt = '' ) {
if ( empty( $flag['src'] ) ) {
return '';
}
// Backward compatibility.
if ( func_num_args() > 2 ) {
_deprecated_argument( __METHOD__ . '()', '3.9', 'The parameter `$title` has been removed and replaced by `$alt` in second position' );
/** @var string */
$alt = func_get_arg( 2 );
}
$alt_attr = empty( $alt ) ? '' : sprintf( ' alt="%s"', esc_attr( $alt ) );
$width_attr = empty( $flag['width'] ) ? '' : sprintf( ' width="%s"', (int) $flag['width'] );
$height_attr = empty( $flag['height'] ) ? '' : sprintf( ' height="%s"', (int) $flag['height'] );
$style = '';
$sizes = array_intersect_key( $flag, array_flip( array( 'width', 'height' ) ) );
if ( ! empty( $sizes ) ) {
array_walk(
$sizes,
function ( &$value, $key ) {
$value = sprintf( '%s: %dpx;', esc_attr( $key ), (int) $value );
}
);
$style = sprintf( ' style="%s"', implode( ' ', $sizes ) );
}
return sprintf(
'<img src="%s"%s%s%s%s />',
$flag['src'],
$alt_attr,
$width_attr,
$height_attr,
$style
);
}
/**
* Returns the language flag or the language slug if there is no flag.
*
* @since 3.9
*
* @param string $mode Optional. Allows to modify the markup depending on how the flag is used. Possible values are:
* - Empty string: the flag can be seen by screen readers,
* - `aria-hidden`: the flag is hidden from screen readers: it is preceded or followed by a
* text (language name for example) that would make the information redundant.
* Default is an empty string.
* @return string
*
* @phpstan-param ''|'aria-hidden' $mode
*/
public function get_admin_flag( string $mode = '' ): string {
return $this->admin_flag[ $mode ];
}
/**
* Returns the html of the custom flag if any, or the default flag otherwise.
*
* @since 2.8
* @since 3.5.3 Added the `$alt` parameter.
*
* @param string $alt Whether or not the alternative text should be set. Accepts 'alt' and 'no-alt'.
*
* @return string
*
* @phpstan-param 'alt'|'no-alt' $alt
*/
public function get_display_flag( $alt = 'alt' ) {
$flag = empty( $this->custom_flag ) ? $this->flag : $this->custom_flag;
if ( 'alt' === $alt ) {
return $flag;
}
return (string) preg_replace( '/(?<=\salt=\")([^"]+)(?=\")/', '', $flag );
}
/**
* Returns the url of the custom flag if any, or the default flag otherwise.
*
* @since 2.8
*
* @return string
*/
public function get_display_flag_url() {
$flag_url = empty( $this->custom_flag_url ) ? $this->flag_url : $this->custom_flag_url;
/**
* Filters `flag_url` property.
*
* @since 3.4.4
*
* @param string $flag_url Flag URL.
* @param PLL_Language $language Current `PLL_language` instance.
*/
return apply_filters( 'pll_language_flag_url', $flag_url, $this );
}
/**
* Updates post and term count.
*
* @since 1.2
*
* @return void
*/
public function update_count() {
foreach ( $this->term_props as $taxonomy => $props ) {
wp_update_term_count( $props['term_taxonomy_id'], $taxonomy );
}
}
/**
* Returns the language locale.
* Converts WP locales to W3C valid locales for display.
*
* @since 1.8
*
* @param string $filter Either 'display' or 'raw', defaults to raw.
* @return string
*
* @phpstan-param 'display'|'raw' $filter
* @phpstan-return non-empty-string
*/
public function get_locale( $filter = 'raw' ) {
return 'display' === $filter ? $this->w3c : $this->locale;
}
/**
* Returns the values of this instance's properties, which can be filtered if required.
*
* @since 3.4
*
* @param string $context Whether or not properties should be filtered. Accepts `db` or `display`.
* Default to `display` which filters some properties.
*
* @return array Array of language object properties.
*
* @phpstan-return LanguageData
*/
public function to_array( $context = 'display' ) {
$language = get_object_vars( $this );
unset( $language['admin_flag'] );
if ( 'db' !== $context ) {
$language['home_url'] = $this->get_home_url();
$language['search_url'] = $this->get_search_url();
}
/** @phpstan-var LanguageData $language */
return $language;
}
/**
* Converts current `PLL_language` into a `stdClass` object. Mostly used to allow dynamic properties.
*
* @since 3.4
*
* @return stdClass Converted `PLL_Language` object.
*/
public function to_std_class() {
return (object) $this->to_array();
}
/**
* Returns language's home URL. Takes care to render it dynamically if no cache is allowed.
*
* @since 3.4
*
* @return string Language home URL.
*/
public function get_home_url() {
if ( ! pll_get_constant( 'PLL_CACHE_LANGUAGES', true ) || ! pll_get_constant( 'PLL_CACHE_HOME_URL', true ) ) {
/**
* Filters current `PLL_Language` instance `home_url` property.
*
* @since 3.4.4
*
* @param string $home_url The `home_url` prop.
* @param array $language Current Array of `PLL_Language` properties.
*/
return apply_filters( 'pll_language_home_url', $this->home_url, $this->to_array( 'db' ) );
}
return $this->home_url;
}
/**
* Returns language's search URL. Takes care to render it dynamically if no cache is allowed.
*
* @since 3.4
*
* @return string Language search URL.
*/
public function get_search_url() {
if ( ! pll_get_constant( 'PLL_CACHE_LANGUAGES', true ) || ! pll_get_constant( 'PLL_CACHE_HOME_URL', true ) ) {
/**
* Filters current `PLL_Language` instance `search_url` property.
*
* @since 3.4.4
*
* @param string $search_url The `search_url` prop.
* @param array $language Current Array of `PLL_Language` properties.
*/
return apply_filters( 'pll_language_search_url', $this->search_url, $this->to_array( 'db' ) );
}
return $this->search_url;
}
/**
* Returns the value of a language property.
* This is handy to get a property's value without worrying about triggering a deprecation warning or anything.
*
* @since 3.4
*
* @param string $property A property name. A composite value can be used for language term property values, in the
* form of `{language_taxonomy_name}:{property_name}` (see {@see PLL_Language::get_tax_prop()}
* for the possible values). Ex: `term_language:term_taxonomy_id`.
* @return string|int|bool|string[] The requested property for the language, `false` if the property doesn't exist.
*
* @phpstan-return (
* $property is 'slug' ? non-empty-string : string|int|bool|list<non-empty-string>
* )
*/
public function get_prop( $property ) {
// Deprecated property.
if ( $this->is_deprecated_term_property( $property ) ) {
return $this->get_deprecated_term_property( $property );
}
if ( $this->is_deprecated_url_property( $property ) ) {
return $this->get_deprecated_url_property( $property );
}
// Composite property like 'term_language:term_taxonomy_id'.
if ( preg_match( '/^(?<tax>.{1,32}):(?<field>term_id|term_taxonomy_id|count)$/', $property, $matches ) ) {
/** @var array{tax:non-empty-string, field:'term_id'|'term_taxonomy_id'|'count'} $matches */
return $this->get_tax_prop( $matches['tax'], $matches['field'] );
}
return $this->$property ?? false;
}
/**
* Prepares a SVG image for use in data uri.
*
* @see https://codepen.io/tigt/post/optimizing-svgs-in-data-uris.
* @since 3.9
*
* @param string $svg A string representing an SVG image.
* @return string Encode SVG.
*/
protected static function encode_svg( string $svg ): string {
$to_replace = array(
'"' => "'",
'<' => '%3C',
'>' => '%3E',
'#' => '%23',
"\n" => '',
"\r" => '',
' ' => '%20',
);
return str_replace( array_keys( $to_replace ), array_values( $to_replace ), $svg );
}
}