Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
fonts.php
1<?php
3
8use Bitrix\UI;
9
11{
15 public const DEFAULT_FONTS = [
16 'g-font-open-sans' => [
17 'name' => 'Open Sans',
18 'family' => '"Open Sans", Helvetica, Arial, sans-serif',
19 'url' => 'https://fonts.googleapis.com/css2?family=Open+Sans:wght@100;200;300;400;500;600;700;800;900&subset=cyrillic',
20 ],
21 'g-font-roboto' => [
22 'name' => 'Roboto',
23 'family' => '"Roboto", Arial, sans-serif',
24 'url' => 'https://fonts.googleapis.com/css2?family=Roboto:wght@100;200;300;400;500;600;700;800;900&subset=cyrillic,cyrillic-ext,latin-ext',
25 ],
26 'g-font-roboto-slab' => [
27 'name' => 'Roboto Slab',
28 'family' => '"Roboto Slab", Helvetica, Arial, sans-serif',
29 'url' => 'https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@100;200;300;400;500;600;700;800;900&subset=cyrillic,cyrillic-ext,latin-ext',
30 ],
31 'g-font-montserrat' => [
32 'name' => 'Montserrat',
33 'family' => '"Montserrat", Helvetica, Arial, sans-serif',
34 'url' => 'https://fonts.googleapis.com/css2?family=Montserrat:wght@100;200;300;400;500;600;700;800;900&subset=cyrillic',
35 ],
36 'g-font-alegreya-sans' => [
37 'name' => 'Alegreya Sans',
38 'family' => '"Alegreya Sans", sans-serif',
39 'url' => 'https://fonts.googleapis.com/css2?family=Alegreya+Sans:wght@100;200;300;400;500;600;700;800;900&subset=cyrillic-ext,latin-ext',
40 ],
41 'g-font-cormorant-infant' => [
42 'name' => 'Cormorant Infant',
43 'family' => '"Cormorant Infant", serif',
44 'url' => 'https://fonts.googleapis.com/css2?family=Cormorant+Infant:wght@100;200;300;400;500;600;700;800;900&subset=cyrillic-ext,latin-ext',
45 ],
46 'g-font-pt-sans-caption' => [
47 'name' => 'PT Sans Caption',
48 'family' => '"PT Sans Caption", sans-serif',
49 'url' => 'https://fonts.googleapis.com/css2?family=PT+Sans+Caption:wght@100;200;300;400;500;600;700;800;900&subset=cyrillic-ext,latin-ext',
50 ],
51 'g-font-pt-sans-narrow' => [
52 'name' => 'PT Sans Narrow',
53 'family' => '"PT Sans Narrow", sans-serif',
54 'url' => 'https://fonts.googleapis.com/css2?family=PT+Sans+Narrow:wght@100;200;300;400;500;600;700;800;900&PT+Sans:wght@100;200;300;400;500;600;700;800;900&subset=cyrillic-ext,latin-ext',
55 ],
56 'g-font-pt-sans' => [
57 'name' => 'PT Sans',
58 'family' => '"PT Sans", sans-serif',
59 'url' => 'https://fonts.googleapis.com/css2?family=PT+Sans:wght@100;200;300;400;500;600;700;800;900&subset=cyrillic-ext,latin-ext',
60 ],
61 'g-font-lobster' => [
62 'name' => 'Lobster',
63 'family' => '"Lobster", cursive',
64 'url' => 'https://fonts.googleapis.com/css2?family=Lobster:wght@100;200;300;400;500;600;700;800;900&subset=cyrillic-ext,latin-ext',
65 ],
66 ];
67
72 protected static $setFonts = [];
73
78 protected function getMap()
79 {
80 return [
81 'CODE' => new Field\Textarea(
82 'CODE', [
83 'title' => Loc::getMessage('LNDNGHOOK_FONTS_FONT_BASE'),
84 ]),
85 ];
86 }
87
92 public function enabled()
93 {
94 return true;
95 }
96
102 public static function setFontCode(string $fontCode): void
103 {
104 if (!array_key_exists($fontCode, self::$setFonts))
105 {
106 self::$setFonts[$fontCode] = [];
107 }
108 }
109
114 public function exec()
115 {
116 if (!self::$setFonts)
117 {
118 return;
119 }
120 // @fix for #101643
121
122 $this->fields['CODE'] = str_replace(
123 ['st yle', 'onl oad', 'li nk'],
124 ['style', 'onload', 'link'],
125 $this->fields['CODE']
126 );
127 $styleFound = preg_match_all(
128 '#(<noscript>.*?<style.*?data-id="([^"]+)"[^>]*>[^<]+</style>)#is',
129 $this->fields['CODE'],
130 $matches
131 );
132
133 $fonts = [];
134 if ($styleFound)
135 {
136 $fonts = array_combine($matches[2], $matches[1]);
137 }
138 $this->outputFonts($fonts);
139 }
140
146 protected function outputFonts(array $fonts): void
147 {
148 $setFonts = [];
149
150 foreach (self::$setFonts as $fontCode => $foo)
151 {
152 if (isset($fonts[$fontCode]))
153 {
154 unset(self::$setFonts[$fontCode]);
155 $setFonts[] = self::proxyFontUrl($fonts[$fontCode]);
156 }
157 }
158
159 // set default fonts
160 foreach (self::$setFonts as $fontCode => $foo)
161 {
162 $setFonts[] = self::outputDefaultFont($fontCode);
163 }
164
165 if ($setFonts)
166 {
167 Manager::setPageView(
168 'BeforeHeadClose',
169 implode('', $setFonts)
170 );
171 }
172 }
173
179 public static function outputDefaultFont(string $code): string
180 {
181 if (isset(self::DEFAULT_FONTS[$code]))
182 {
183 $fontUrl = self::DEFAULT_FONTS[$code]['url'];
184 $fontUrl = self::proxyFontUrl($fontUrl);
185
186 return '<link
187 rel="preload"
188 as="style"
189 onload="this.removeAttribute(\'onload\');this.rel=\'stylesheet\'"
190 data-font="' . $code . '"
191 data-protected="true"
192 href="' . $fontUrl . '">
193 <noscript>
194 <link
195 rel="stylesheet"
196 data-font="' . $code . '"
197 data-protected="true"
198 href="' . $fontUrl . '">
199 </noscript>';
200 }
201
202 return '';
203 }
204
210 protected static function proxyFontUrl(string $fontString): string
211 {
212 $defaultDomain = 'fonts.googleapis.com';
213 $proxyDomain = $defaultDomain;
214 if (Loader::includeModule('ui'))
215 {
216 $proxyDomain = UI\Fonts\Proxy::resolveDomain(Manager::getZone());
217 }
218
219 return ($defaultDomain !== $proxyDomain)
220 ? str_replace($defaultDomain, $proxyDomain, $fontString)
221 : $fontString
222 ;
223 }
224}
static outputDefaultFont(string $code)
Definition fonts.php:179
static setFontCode(string $fontCode)
Definition fonts.php:102
static proxyFontUrl(string $fontString)
Definition fonts.php:210
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29