PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.10.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.10.0
5.13.0 5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Plugin / ThemeStyles.php
matomo / app / core / Plugin Last commit date
ConsoleCommand 3 months ago Dimension 3 months ago API.php 8 months ago AggregatedMetric.php 2 years ago ArchivedMetric.php 1 year ago Archiver.php 3 months ago Categories.php 2 years ago ComponentFactory.php 5 months ago ComputedMetric.php 1 year ago ConsoleCommand.php 3 months ago Controller.php 3 months ago ControllerAdmin.php 5 months ago Dependency.php 3 months ago LogTablesProvider.php 2 years ago Manager.php 3 months ago Menu.php 3 months ago MetadataLoader.php 3 months ago Metric.php 3 months ago PluginException.php 1 year ago ProcessedMetric.php 5 months ago ReleaseChannels.php 5 months ago Report.php 3 months ago ReportsProvider.php 2 years ago RequestProcessors.php 6 months ago Segment.php 5 months ago SettingsProvider.php 3 months ago Tasks.php 3 months ago ThemeStyles.php 3 months ago ViewDataTable.php 5 months ago Visualization.php 1 year ago WidgetsProvider.php 5 months ago
ThemeStyles.php
285 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik\Plugin;
10
11 use Piwik\Piwik;
12 class ThemeStyles
13 {
14 public const AUTO_MODE = 'auto';
15 public const LIGHT_MODE = 'light';
16 public const DARK_MODE = 'dark';
17 // to maintain BC w/ old names that were defined in LESS
18 private static $propertyNamesToLessVariableNames = ['fontFamilyBase' => 'theme-fontFamily-base', 'colorBrand' => 'theme-color-brand', 'colorBrandContrast' => 'theme-color-brand-contrast', 'colorFocusRing' => 'theme-color-focus-ring', 'colorFocusRingAlternative' => 'theme-color-focus-ring-alternative', 'colorTextHighContrast' => 'theme-color-text-highContrast', 'colorText' => 'theme-color-text', 'colorTextContrast' => 'theme-color-text-contrast', 'colorTextLight' => 'theme-color-text-light', 'colorTextLighter' => 'theme-color-text-lighter', 'colorTextOnDisabled' => 'theme-color-text-on-disabled', 'colorTextInvert' => 'theme-color-text-invert', 'colorTextInvertContrast' => 'theme-color-text-invert-contrast', 'colorTextInvertLight' => 'theme-color-text-invert-light', 'colorTextDisabled' => 'theme-color-text-disabled', 'colorLink' => 'theme-color-link', 'colorBaseSeries' => 'theme-color-base-series', 'colorHeadlineAlternative' => 'theme-color-headline-alternative', 'colorHeaderBackground' => 'theme-color-header-background', 'colorHeaderText' => 'theme-color-header-text', 'colorMenuContrastText' => 'theme-color-menu-contrast-text', 'colorMenuContrastTextSelected' => 'theme-color-menu-contrast-textSelected', 'colorMenuContrastTextActive' => 'theme-color-menu-contrast-textActive', 'colorMenuContrastBackground' => 'theme-color-menu-contrast-background', 'colorWidgetExportedBackgroundBase' => 'theme-color-widget-exported-background-base', 'colorWidgetTitleText' => 'theme-color-widget-title-text', 'colorWidgetTitleBackground' => 'theme-color-widget-title-background', 'colorBackgroundBase' => 'theme-color-background-base', 'colorBackgroundTinyContrast' => 'theme-color-background-tinyContrast', 'colorBackgroundLowContrast' => 'theme-color-background-lowContrast', 'colorBackgroundContrast' => 'theme-color-background-contrast', 'colorBackgroundHighContrast' => 'theme-color-background-highContrast', 'colorBackgroundDisabled' => 'theme-color-background-disabled', 'colorBorder' => 'theme-color-border', 'colorBorderLight' => 'theme-color-border-light', 'colorBoxShadow' => 'theme-color-boxShadow', 'colorCode' => 'theme-color-code', 'colorCodeBackground' => 'theme-color-code-background', 'colorWidgetBackground' => 'theme-color-widget-background', 'colorWidgetBorder' => 'theme-color-widget-border', 'filterOnIllustration' => 'theme-filter-on-illustration', 'colorMenuContrastBackgroundHover' => 'theme-color-menu-contrast-backgroundHover'];
19 /**
20 * @var string
21 */
22 protected $themeMode = self::AUTO_MODE;
23 /**
24 * @var string|array<string>
25 */
26 public $fontFamilyBase = '-apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, Oxygen-Sans, Cantarell, \'Helvetica Neue\', sans-serif';
27 /**
28 * @var string|array<string>
29 */
30 public $colorBrand = ['#43a047', '#778fd4'];
31 /**
32 * @var string|array<string>
33 */
34 public $colorBrandContrast = ['#fff', '#ffffff'];
35 /**
36 * @var string|array<string>
37 */
38 public $colorFocusRing = '#0969da';
39 /**
40 * @var string|array<string>
41 */
42 public $colorFocusRingAlternative;
43 /**
44 * @var string|array<string>
45 */
46 public $colorTextHighContrast = ['#000', '#d9d9d9'];
47 /**
48 * @var string|array<string>
49 */
50 public $colorText = ['#212121', '#ccc'];
51 /**
52 * @var string|array<string>
53 */
54 public $colorTextContrast = ['#37474f', '#bbb'];
55 /**
56 * @var string|array<string>
57 */
58 public $colorTextLight = ['#444', '#aaa'];
59 /**
60 * @var string|array<string>
61 */
62 public $colorTextLighter = ['#646464', '#999'];
63 /**
64 * @var string|array<string>
65 */
66 public $colorTextOnDisabled = ['#666666', '#999'];
67 /**
68 * @var string|array<string>
69 */
70 public $colorTextInvert = ['#ccc', '#555'];
71 /**
72 * @var string|array<string>
73 */
74 public $colorTextInvertContrast = ['#fff', '#000'];
75 /**
76 * @var string|array<string>
77 */
78 public $colorTextInvertLight = ['#b9b9b9', '#666'];
79 /**
80 * @var string|array<string>
81 */
82 public $colorTextDisabled = ['#aaa', '#666'];
83 /**
84 * @var string|array<string>
85 */
86 public $colorLink = ['#1976D2', '#778fd4'];
87 /**
88 * @var string|array<string>
89 */
90 public $colorBaseSeries = '#ee3024';
91 /**
92 * @var string|array<string>
93 */
94 public $colorHeadlineAlternative = ['#4E4E4E', '#aaa'];
95 /**
96 * @var string|array<string>
97 */
98 public $colorHeaderBackground;
99 /**
100 * @var string|array<string>
101 */
102 public $colorHeaderText;
103 /**
104 * @var string|array<string>
105 */
106 public $colorMenuContrastText;
107 /**
108 * @var string|array<string>
109 */
110 public $colorMenuContrastTextSelected;
111 /**
112 * @var string|array<string>
113 */
114 public $colorMenuContrastTextActive = ['#1976D2', '#fff'];
115 /**
116 * @var string|array<string>
117 */
118 public $colorMenuContrastBackgroundHover = ['#eff0f1', '#151819'];
119 /**
120 * @var string|array<string>
121 */
122 public $colorMenuContrastBackground;
123 /**
124 * @var string|array<string>
125 */
126 public $colorWidgetExportedBackgroundBase;
127 /**
128 * @var string|array<string>
129 */
130 public $colorWidgetTitleText;
131 /**
132 * @var string|array<string>
133 */
134 public $colorWidgetTitleBackground;
135 /**
136 * @var string|array<string>
137 */
138 public $colorBackgroundBase = ['#f5f5f5', '#151819'];
139 /**
140 * @var string|array<string>
141 */
142 public $colorBackgroundTinyContrast = ['#f2f2f2', '#182c32'];
143 /**
144 * @var string|array<string>
145 */
146 public $colorBackgroundLowContrast = ['#d9d9d9', '#192d33'];
147 /**
148 * @var string|array<string>
149 */
150 public $colorBackgroundContrast = ['#fff', '#202329'];
151 /**
152 * @var string|array<string>
153 */
154 public $colorBackgroundHighContrast = ['#202020', '#404349'];
155 /**
156 * @var string|array<string>
157 */
158 public $colorBackgroundDisabled = ['#d9d9d9', '#303339'];
159 /**
160 * @var string|array<string>
161 */
162 public $colorBorderLight = ['#a9a399', '#645e54'];
163 /**
164 * @var string|array<string>
165 */
166 public $colorBorder = ['#cccccc', '#555555'];
167 /**
168 * @var string|array<string>
169 */
170 public $colorBoxShadow = ['rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)'];
171 /**
172 * @var string|array<string>
173 */
174 public $colorCode = '#f3f3f3';
175 /**
176 * @var string|array<string>
177 */
178 public $colorCodeBackground = '#4d4d4d';
179 /**
180 * @var string|array<string>
181 */
182 public $colorWidgetBackground;
183 /**
184 * @var string|array<string>
185 */
186 public $colorWidgetBorder;
187 /**
188 * @var string|array<string>
189 */
190 public $filterOnIllustration = ['none', 'brightness(89%) invert(100%) hue-rotate(180deg)'];
191 public function __construct(string $themeMode)
192 {
193 $this->themeMode = $themeMode;
194 $this->colorFocusRingAlternative = $this->colorBrand;
195 $this->colorMenuContrastText = $this->colorText;
196 $this->colorMenuContrastTextSelected = $this->colorMenuContrastText;
197 $this->colorMenuContrastBackground = $this->colorBackgroundContrast;
198 $this->colorWidgetExportedBackgroundBase = $this->colorBackgroundContrast;
199 $this->colorWidgetTitleText = $this->colorText;
200 $this->colorWidgetTitleBackground = $this->colorBackgroundContrast;
201 $this->colorWidgetBackground = $this->colorBackgroundContrast;
202 $this->colorWidgetBorder = $this->colorBackgroundTinyContrast;
203 $this->colorHeaderBackground = $this->colorBackgroundContrast;
204 $this->colorHeaderText = $this->colorTextLighter;
205 }
206 /**
207 * @return ThemeStyles
208 */
209 public static function get(string $mode = self::AUTO_MODE)
210 {
211 $result = new self($mode);
212 /**
213 * @ignore
214 */
215 Piwik::postEvent('Theme.configureThemeVariables', [$result]);
216 return $result;
217 }
218 public function getIsLightMode() : bool
219 {
220 return $this->themeMode === self::LIGHT_MODE;
221 }
222 public function getIsDarkMode() : bool
223 {
224 return $this->themeMode === self::DARK_MODE;
225 }
226 public function getThemeMode() : string
227 {
228 return $this->themeMode;
229 }
230 public function getPropertyValue(string $name) : string
231 {
232 if (!property_exists($this, $name)) {
233 return '';
234 }
235 return $this->resolvePropertyValue($this->{$name}, $this->getIsDarkMode() ? 1 : 0);
236 }
237 public function toLessCode()
238 {
239 $rootCssVars = [];
240 $darkCssVars = [];
241 foreach (get_object_vars($this) as $name => $value) {
242 $varName = isset(self::$propertyNamesToLessVariableNames[$name]) ? self::$propertyNamesToLessVariableNames[$name] : $this->getGenericThemeVarName($name);
243 if (is_array($value)) {
244 $rootCssVars[] = " --{$varName}: " . $this->resolvePropertyValue($value, 0) . ";\n";
245 $darkCssVars[] = " --{$varName}: " . $this->resolvePropertyValue($value, 1) . ";\n";
246 } else {
247 $rootCssVars[] = " --{$varName}: " . $this->resolvePropertyValue($value, 0) . ";\n";
248 }
249 }
250 $result = ":root {\n color-scheme: light;\n" . implode('', $rootCssVars) . "}\n\n";
251 if (!empty($darkCssVars)) {
252 $result .= "[data-theme-mode=\"dark\"] {\n color-scheme: dark;\n" . implode('', $darkCssVars) . "}\n\n";
253 $result .= "@media (prefers-color-scheme: dark) {\n";
254 $result .= " [data-theme-mode=\"auto\"] {\n color-scheme: dark;\n" . implode('', $darkCssVars) . " }\n";
255 $result .= "}\n\n";
256 }
257 foreach (get_object_vars($this) as $name => $_) {
258 $varName = isset(self::$propertyNamesToLessVariableNames[$name]) ? self::$propertyNamesToLessVariableNames[$name] : $this->getGenericThemeVarName($name);
259 $result .= "@{$varName}: ~\"var(--{$varName})\";\n";
260 }
261 return $result;
262 }
263 private function getGenericThemeVarName($propertyName)
264 {
265 return 'theme-' . $propertyName;
266 }
267 /**
268 * @param mixed $value
269 */
270 private function resolvePropertyValue($value, int $preferredIndex) : string
271 {
272 if (!is_array($value)) {
273 return is_string($value) ? $value : '';
274 }
275 $fallbackIndex = $preferredIndex === 1 ? 0 : 1;
276 if (isset($value[$preferredIndex]) && is_string($value[$preferredIndex])) {
277 return $value[$preferredIndex];
278 }
279 if (isset($value[$fallbackIndex]) && is_string($value[$fallbackIndex])) {
280 return $value[$fallbackIndex];
281 }
282 return '';
283 }
284 }
285