PluginProbe
Polylang / 3.7.1
Polylang v3.7.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
polylang / frontend / accept-language.php

accept-language.php in Polylang 3.7.1, at frontend/accept-language.php

113 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Class Accept_Language.
8 *
9 * Represents an Accept-Language HTTP Header, as defined in RFC 2616 Section 14.4 {@see https://tools.ietf.org/html/rfc2616.html#section-14.4}.
10 *
11 * @since 3.0
12 */
13 class PLL_Accept_Language {
14 public const SUBTAG_PATTERNS = array(
15 'language' => '(\b[a-z]{2,3}|[a-z]{4}|[a-z]{5-8}\b)',
16 'language-extension' => '(?:-(\b[a-z]{3}){1,3}\b)?',
17 'script' => '(?:-(\b[a-z]{4})\b)?',
18 'region' => '(?:-(\b[a-z]{2}|[0-9]{3})\b)?',
19 'variant' => '(?:-(\b[0-9][a-z]{1,3}|[a-z][a-z0-9]{4,7})\b)?',
20 'extension' => '(?:-(\b[a-wy-z]-[a-z0-9]{2,8})\b)?',
21 'private-use' => '(?:-(\bx-[a-z0-9]{1,8})\b)?',
22 );
23
24 /**
25 * @var string[] {
26 * @type string $language Usually 2 or three letters (ISO 639).
27 * @type string $language-extension Up to three groups of 3 letters.
28 * @type string $script Four letters.
29 * @type string $region Either two letters of three digits.
30 * @type string $variant Either one digit followed by 1 to 3 letters, or a letter followed by 2 to 7 alphanumerical characters.
31 * @type string $extension One letter that cannot be an 'x', followed by 2 to 8 alphanumerical characters.
32 * @type string $private-use Starts by 'x-', followed by 1 to 8 alphanumerical characters.
33 * }
34 */
35 protected $subtags;
36
37 /**
38 * @var float
39 */
40 protected $quality;
41
42 /**
43 * PLL_Accept_Language constructor.
44 *
45 * @since 3.0
46 *
47 * @param string[] $subtags With subtag name as keys and subtag values as names.
48 * @param mixed $quality Floating point value from 0.0 to 1.0. Higher values indicates a user's preference.
49 */
50 public function __construct( $subtags, $quality = 1.0 ) {
51 $this->subtags = $subtags;
52 $this->quality = is_numeric( $quality ) ? floatval( $quality ) : 1.0;
53 }
54
55 /**
56 * Creates a new instance from an array resulting of a PHP {@see preg_match()} or {@see preg_match_all()} call.
57 *
58 * @since 3.0
59 *
60 * @param string[] $matches Expects first entry to be full match, following entries to be subtags and last entry to be quality factor.
61 * @return PLL_Accept_Language
62 */
63 public static function from_array( $matches ) {
64 $subtags = array_combine(
65 array_keys( array_slice( self::SUBTAG_PATTERNS, 0, count( $matches ) - 1 ) ),
66 array_slice( $matches, 1, count( self::SUBTAG_PATTERNS ) )
67 );
68 $quality = count( $matches ) === 9 ? $matches[8] : 1.0;
69
70 return new PLL_Accept_Language( $subtags, $quality );
71 }
72
73 /**
74 * Returns the full language tag.
75 *
76 * @since 3.0
77 *
78 * @return string
79 */
80 public function __toString() {
81 $subtags = array_filter(
82 $this->subtags,
83 function ( $subtag ) {
84 return ! empty( trim( $subtag ) );
85 }
86 );
87 return implode( '-', $subtags );
88 }
89
90 /**
91 * Returns the quality factor as negotiated by the browser agent.
92 *
93 * @since 3.0
94 *
95 * @return float
96 */
97 public function get_quality() {
98 return $this->quality;
99 }
100
101 /**
102 * Returns a subtag from the language tag.
103 *
104 * @since 3.0
105 *
106 * @param string $name A valid subtag name, {@see PLL_Accept_Language::SUBTAG_PATTERNS} for available subtag names.
107 * @return string
108 */
109 public function get_subtag( $name ) {
110 return $this->subtags[ $name ] ?? '';
111 }
112 }
113