PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
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 2 months ago Dimension 2 months ago API.php 8 months ago AggregatedMetric.php 2 years ago ArchivedMetric.php 2 weeks ago Archiver.php 2 months ago Categories.php 2 years ago ComponentFactory.php 2 weeks ago ComputedMetric.php 1 year ago ConsoleCommand.php 2 months ago Controller.php 2 months ago ControllerAdmin.php 2 weeks ago Dependency.php 2 months ago LogTablesProvider.php 2 weeks ago Manager.php 2 weeks ago Menu.php 2 weeks ago MetadataLoader.php 2 weeks ago Metric.php 2 months ago PluginException.php 1 year ago ProcessedMetric.php 4 months ago ReleaseChannels.php 4 months ago Report.php 2 months ago ReportsProvider.php 2 years ago RequestProcessors.php 5 months ago Segment.php 4 months ago SettingsProvider.php 2 weeks ago Tasks.php 2 months ago ThemeStyles.php 2 weeks ago ViewDataTable.php 2 weeks ago Visualization.php 2 weeks ago WidgetsProvider.php 4 months ago
ThemeStyles.php
306 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', 'colorNewBrand' => 'theme-color-new-brand', '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', 'colorTextPlaceholder' => 'theme-color-text-placeholder', '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', 'colorBorderAlternative' => 'theme-color-border-alternative', 'colorBorderLight' => 'theme-color-border-light', 'colorBoxShadow' => 'theme-color-boxShadow', 'shadowOverlay' => 'theme-shadow-overlay', '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 $colorNewBrand = '#00b4be';
39 /**
40 * @var string|array<string>
41 */
42 public $colorFocusRing = '#0969da';
43 /**
44 * @var string|array<string>
45 */
46 public $colorFocusRingAlternative = '#00b4bE';
47 /**
48 * @var string|array<string>
49 */
50 public $colorTextHighContrast = ['#000', '#d9d9d9'];
51 /**
52 * @var string|array<string>
53 */
54 public $colorText = ['#212121', '#ccc'];
55 /**
56 * @var string|array<string>
57 */
58 public $colorTextContrast = ['#37474f', '#bbb'];
59 /**
60 * @var string|array<string>
61 */
62 public $colorTextLight = ['#444', '#aaa'];
63 /**
64 * @var string|array<string>
65 */
66 public $colorTextLighter = ['#646464', '#999'];
67 /**
68 * @var string|array<string>
69 */
70 public $colorTextOnDisabled = ['#666666', '#999'];
71 /**
72 * @var string|array<string>
73 * @since Matomo 5.12.0
74 */
75 public $colorTextPlaceholder = ['#d1d1d1', '#565656'];
76 /**
77 * @var string|array<string>
78 */
79 public $colorTextInvert = ['#ccc', '#555'];
80 /**
81 * @var string|array<string>
82 */
83 public $colorTextInvertContrast = ['#fff', '#000'];
84 /**
85 * @var string|array<string>
86 */
87 public $colorTextInvertLight = ['#b9b9b9', '#666'];
88 /**
89 * @var string|array<string>
90 */
91 public $colorTextDisabled = ['#aaa', '#666'];
92 /**
93 * @var string|array<string>
94 */
95 public $colorLink = ['#1976D2', '#778fd4'];
96 /**
97 * @var string|array<string>
98 */
99 public $colorBaseSeries = '#ee3024';
100 /**
101 * @var string|array<string>
102 */
103 public $colorHeadlineAlternative = ['#4E4E4E', '#aaa'];
104 /**
105 * @var string|array<string>
106 */
107 public $colorHeaderBackground;
108 /**
109 * @var string|array<string>
110 */
111 public $colorHeaderText;
112 /**
113 * @var string|array<string>
114 */
115 public $colorMenuContrastText;
116 /**
117 * @var string|array<string>
118 */
119 public $colorMenuContrastTextSelected;
120 /**
121 * @var string|array<string>
122 */
123 public $colorMenuContrastTextActive = ['#1976D2', '#fff'];
124 /**
125 * @var string|array<string>
126 */
127 public $colorMenuContrastBackgroundHover = ['#eff0f1', '#151819'];
128 /**
129 * @var string|array<string>
130 */
131 public $colorMenuContrastBackground;
132 /**
133 * @var string|array<string>
134 */
135 public $colorWidgetExportedBackgroundBase;
136 /**
137 * @var string|array<string>
138 */
139 public $colorWidgetTitleText;
140 /**
141 * @var string|array<string>
142 */
143 public $colorWidgetTitleBackground;
144 /**
145 * @var string|array<string>
146 */
147 public $colorBackgroundBase = ['#f5f5f5', '#151819'];
148 /**
149 * @var string|array<string>
150 */
151 public $colorBackgroundTinyContrast = ['#f2f2f2', '#182c32'];
152 /**
153 * @var string|array<string>
154 */
155 public $colorBackgroundLowContrast = ['#d9d9d9', '#192d33'];
156 /**
157 * @var string|array<string>
158 */
159 public $colorBackgroundContrast = ['#fff', '#202329'];
160 /**
161 * @var string|array<string>
162 */
163 public $colorBackgroundHighContrast = ['#202020', '#404349'];
164 /**
165 * @var string|array<string>
166 */
167 public $colorBackgroundDisabled = ['#d9d9d9', '#303339'];
168 /**
169 * @var string|array<string>
170 */
171 public $colorBorderLight = ['#a9a399', '#645e54'];
172 /**
173 * @var string|array<string>
174 * @deprecated Use $colorBorderAlternative instead. Retained only for legacy use and will be
175 * phased out once everything has moved to the alternative.
176 */
177 public $colorBorder = ['#cccccc', '#555555'];
178 /**
179 * Use this as the new border color. $colorBorder only remains so that we don't need to update
180 * screenshots unless we really need to
181 *
182 * @var string|array<string>
183 */
184 public $colorBorderAlternative = ['#E0E0E0', '#555555'];
185 /**
186 * @var string|array<string>
187 */
188 public $colorBoxShadow = ['rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)'];
189 /**
190 * @var string|array<string>
191 */
192 public $shadowOverlay = ['0 0 3px #A5B1CA4D, 0 10px 40px #A5B1CA4D', '0 0 3px #00000080, 0 10px 40px #00000080'];
193 /**
194 * @var string|array<string>
195 */
196 public $colorCode = '#f3f3f3';
197 /**
198 * @var string|array<string>
199 */
200 public $colorCodeBackground = '#4d4d4d';
201 /**
202 * @var string|array<string>
203 */
204 public $colorWidgetBackground;
205 /**
206 * @var string|array<string>
207 */
208 public $colorWidgetBorder;
209 /**
210 * @var string|array<string>
211 */
212 public $filterOnIllustration = ['none', 'brightness(89%) invert(100%) hue-rotate(180deg)'];
213 public function __construct(string $themeMode)
214 {
215 $this->themeMode = $themeMode;
216 $this->colorMenuContrastText = $this->colorText;
217 $this->colorMenuContrastTextSelected = $this->colorMenuContrastText;
218 $this->colorMenuContrastBackground = $this->colorBackgroundContrast;
219 $this->colorWidgetExportedBackgroundBase = $this->colorBackgroundContrast;
220 $this->colorWidgetTitleText = $this->colorText;
221 $this->colorWidgetTitleBackground = $this->colorBackgroundContrast;
222 $this->colorWidgetBackground = $this->colorBackgroundContrast;
223 $this->colorWidgetBorder = $this->colorBackgroundTinyContrast;
224 $this->colorHeaderBackground = $this->colorBackgroundContrast;
225 $this->colorHeaderText = $this->colorTextLighter;
226 }
227 /**
228 * @return ThemeStyles
229 */
230 public static function get(string $mode = self::AUTO_MODE)
231 {
232 $result = new self($mode);
233 /**
234 * @ignore
235 */
236 Piwik::postEvent('Theme.configureThemeVariables', [$result]);
237 return $result;
238 }
239 public function getIsLightMode() : bool
240 {
241 return $this->themeMode === self::LIGHT_MODE;
242 }
243 public function getIsDarkMode() : bool
244 {
245 return $this->themeMode === self::DARK_MODE;
246 }
247 public function getThemeMode() : string
248 {
249 return $this->themeMode;
250 }
251 public function getPropertyValue(string $name) : string
252 {
253 if (!property_exists($this, $name)) {
254 return '';
255 }
256 return $this->resolvePropertyValue($this->{$name}, $this->getIsDarkMode() ? 1 : 0);
257 }
258 public function toLessCode()
259 {
260 $rootCssVars = [];
261 $darkCssVars = [];
262 foreach (get_object_vars($this) as $name => $value) {
263 $varName = isset(self::$propertyNamesToLessVariableNames[$name]) ? self::$propertyNamesToLessVariableNames[$name] : $this->getGenericThemeVarName($name);
264 if (is_array($value)) {
265 $rootCssVars[] = " --{$varName}: " . $this->resolvePropertyValue($value, 0) . ";\n";
266 $darkCssVars[] = " --{$varName}: " . $this->resolvePropertyValue($value, 1) . ";\n";
267 } else {
268 $rootCssVars[] = " --{$varName}: " . $this->resolvePropertyValue($value, 0) . ";\n";
269 }
270 }
271 $result = ":root {\n color-scheme: light;\n" . implode('', $rootCssVars) . "}\n\n";
272 if (!empty($darkCssVars)) {
273 $result .= "[data-theme-mode=\"dark\"] {\n color-scheme: dark;\n" . implode('', $darkCssVars) . "}\n\n";
274 $result .= "@media (prefers-color-scheme: dark) {\n";
275 $result .= " [data-theme-mode=\"auto\"] {\n color-scheme: dark;\n" . implode('', $darkCssVars) . " }\n";
276 $result .= "}\n\n";
277 }
278 foreach (get_object_vars($this) as $name => $_) {
279 $varName = isset(self::$propertyNamesToLessVariableNames[$name]) ? self::$propertyNamesToLessVariableNames[$name] : $this->getGenericThemeVarName($name);
280 $result .= "@{$varName}: ~\"var(--{$varName})\";\n";
281 }
282 return $result;
283 }
284 private function getGenericThemeVarName($propertyName)
285 {
286 return 'theme-' . $propertyName;
287 }
288 /**
289 * @param mixed $value
290 */
291 private function resolvePropertyValue($value, int $preferredIndex) : string
292 {
293 if (!is_array($value)) {
294 return is_string($value) ? $value : '';
295 }
296 $fallbackIndex = $preferredIndex === 1 ? 0 : 1;
297 if (isset($value[$preferredIndex]) && is_string($value[$preferredIndex])) {
298 return $value[$preferredIndex];
299 }
300 if (isset($value[$fallbackIndex]) && is_string($value[$fallbackIndex])) {
301 return $value[$fallbackIndex];
302 }
303 return '';
304 }
305 }
306