PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
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 / AssetManager.php
matomo / app / core Last commit date
API 3 months ago Access 3 months ago Application 3 months ago Archive 3 months ago ArchiveProcessor 3 months ago Archiver 2 years ago AssetManager 3 months ago Auth 6 months ago Category 6 months ago Changes 3 months ago CliMulti 1 year ago Columns 3 months ago Concurrency 3 months ago Config 3 months ago Container 3 months ago CronArchive 3 months ago DataAccess 3 months ago DataFiles 2 years ago DataTable 3 months ago Db 3 months ago DeviceDetector 1 year ago Email 2 years ago Exception 4 months ago Http 4 months ago Intl 3 months ago Log 2 years ago Mail 1 year ago Measurable 6 months ago Menu 3 months ago Metrics 3 months ago Notification 6 months ago Period 3 months ago Plugin 3 months ago Policy 3 months ago ProfessionalServices 1 year ago Report 1 year ago ReportRenderer 3 months ago Request 3 months ago Scheduler 3 months ago Segment 3 months ago Session 3 months ago Settings 3 months ago Tracker 3 months ago Translation 3 months ago Twig 1 year ago UpdateCheck 3 months ago Updater 4 months ago Updates 3 months ago Validators 1 year ago View 6 months ago ViewDataTable 3 months ago Visualization 1 year ago Widget 3 months ago .htaccess 2 years ago Access.php 3 months ago Archive.php 3 months ago ArchiveProcessor.php 4 months ago AssetManager.php 3 months ago Auth.php 6 months ago AuthResult.php 6 months ago BaseFactory.php 2 years ago Cache.php 2 years ago CacheId.php 4 months ago CliMulti.php 3 months ago Common.php 3 months ago Config.php 3 months ago Console.php 3 months ago Context.php 2 years ago Cookie.php 1 year ago CronArchive.php 3 months ago DI.php 3 months ago DataArray.php 5 months ago DataTable.php 3 months ago Date.php 3 months ago Db.php 3 months ago DbHelper.php 3 months ago Development.php 1 year ago ErrorHandler.php 6 months ago EventDispatcher.php 1 year ago ExceptionHandler.php 4 months ago FileIntegrity.php 3 months ago Filechecks.php 1 year ago Filesystem.php 3 months ago FrontController.php 4 months ago Http.php 4 months ago IP.php 1 year ago Log.php 3 months ago LogDeleter.php 1 year ago Mail.php 1 year ago Metrics.php 3 months ago NoAccessException.php 2 years ago Nonce.php 6 months ago Notification.php 6 months ago NumberFormatter.php 5 months ago Option.php 5 months ago Period.php 3 months ago Piwik.php 3 months ago Plugin.php 3 months ago Process.php 1 year ago Profiler.php 6 months ago ProxyHeaders.php 4 months ago ProxyHttp.php 5 months ago QuickForm2.php 3 months ago RankingQuery.php 5 months ago ReportRenderer.php 3 months ago Request.php 3 months ago Segment.php 3 months ago Sequence.php 6 months ago Session.php 3 months ago SettingsPiwik.php 3 months ago SettingsServer.php 1 year ago Singleton.php 2 years ago Site.php 4 months ago SiteContentDetector.php 3 months ago SupportedBrowser.php 2 years ago TCPDF.php 1 year ago Theme.php 1 year ago Timer.php 2 years ago Tracker.php 3 months ago Twig.php 3 months ago Unzip.php 1 year ago UpdateCheck.php 3 months ago Updater.php 3 months ago UpdaterErrorException.php 2 years ago Updates.php 3 months ago Url.php 3 months ago UrlHelper.php 3 months ago Version.php 3 months ago View.php 3 months ago bootstrap.php 1 year ago dispatch.php 2 years ago testMinimumPhpVersion.php 6 months ago
AssetManager.php
431 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;
10
11 use Exception;
12 use Piwik\AssetManager\UIAsset;
13 use Piwik\AssetManager\UIAsset\InMemoryUIAsset;
14 use Piwik\AssetManager\UIAsset\OnDiskUIAsset;
15 use Piwik\AssetManager\UIAssetCacheBuster;
16 use Piwik\AssetManager\UIAssetFetcher\JScriptUIAssetFetcher;
17 use Piwik\AssetManager\UIAssetFetcher\StaticUIAssetFetcher;
18 use Piwik\AssetManager\UIAssetFetcher\StylesheetUIAssetFetcher;
19 use Piwik\AssetManager\UIAssetFetcher\PluginUmdAssetFetcher;
20 use Piwik\AssetManager\UIAssetFetcher;
21 use Piwik\AssetManager\UIAssetMerger\JScriptUIAssetMerger;
22 use Piwik\AssetManager\UIAssetMerger\StylesheetUIAssetMerger;
23 use Piwik\Container\StaticContainer;
24 use Piwik\Plugin\Manager;
25 /**
26 * AssetManager is the class used to manage the inclusion of UI assets:
27 * JavaScript and CSS files.
28 *
29 * It performs the following actions:
30 * - Identifies required assets
31 * - Includes assets in the rendered HTML page
32 * - Manages asset merging and minifying
33 * - Manages server-side cache
34 *
35 * Whether assets are included individually or as merged files is defined by
36 * the global option 'disable_merged_assets'. See the documentation in the global
37 * config for more information.
38 */
39 class AssetManager extends \Piwik\Singleton
40 {
41 public const MERGED_CSS_FILE = "asset_manager_global_css.css";
42 public const MERGED_CORE_JS_FILE = "asset_manager_core_js.js";
43 public const MERGED_NON_CORE_JS_FILE = "asset_manager_non_core_js.js";
44 public const CSS_IMPORT_DIRECTIVE = "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" />\n";
45 public const JS_IMPORT_DIRECTIVE = "<script type=\"text/javascript\" src=\"%s\"></script>\n";
46 public const JS_DEFER_IMPORT_DIRECTIVE = "<script type=\"text/javascript\" src=\"%s\" defer></script>\n";
47 public const GET_CSS_MODULE_ACTION = "index.php?module=Proxy&action=getCss";
48 public const GET_CORE_JS_MODULE_ACTION = "index.php?module=Proxy&action=getCoreJs";
49 public const GET_NON_CORE_JS_MODULE_ACTION = "index.php?module=Proxy&action=getNonCoreJs";
50 public const GET_JS_UMD_MODULE_ACTION = "index.php?module=Proxy&action=getUmdJs&chunk=";
51 /**
52 * @var UIAssetCacheBuster
53 */
54 private $cacheBuster;
55 /**
56 * @var UIAssetFetcher
57 */
58 private $minimalStylesheetFetcher;
59 /**
60 * @var Theme
61 */
62 private $theme;
63 public function __construct()
64 {
65 $this->cacheBuster = UIAssetCacheBuster::getInstance();
66 $this->minimalStylesheetFetcher = new StaticUIAssetFetcher(array(), array(), $this->theme);
67 $theme = Manager::getInstance()->getThemeEnabled();
68 if (!empty($theme)) {
69 $this->theme = new \Piwik\Theme();
70 }
71 }
72 /**
73 * @inheritDoc
74 * @return AssetManager
75 */
76 public static function getInstance()
77 {
78 $assetManager = parent::getInstance();
79 /**
80 * Triggered when creating an instance of the asset manager. Lets you overwrite the
81 * asset manager behavior.
82 *
83 * @param AssetManager &$assetManager
84 *
85 * @ignore
86 * This event is not a public event since we don't want to make the asset manager itself public
87 * API
88 */
89 \Piwik\Piwik::postEvent('AssetManager.makeNewAssetManagerObject', array(&$assetManager));
90 return $assetManager;
91 }
92 /**
93 * @param UIAssetCacheBuster $cacheBuster
94 */
95 public function setCacheBuster($cacheBuster)
96 {
97 $this->cacheBuster = $cacheBuster;
98 }
99 /**
100 * @param UIAssetFetcher $minimalStylesheetFetcher
101 */
102 public function setMinimalStylesheetFetcher($minimalStylesheetFetcher)
103 {
104 $this->minimalStylesheetFetcher = $minimalStylesheetFetcher;
105 }
106 /**
107 * @param Theme $theme
108 */
109 public function setTheme($theme)
110 {
111 $this->theme = $theme;
112 }
113 /**
114 * Return CSS file inclusion directive(s) using the markup <link>
115 *
116 * @return string
117 */
118 public function getCssInclusionDirective()
119 {
120 return sprintf(self::CSS_IMPORT_DIRECTIVE, self::GET_CSS_MODULE_ACTION);
121 }
122 /**
123 * Return JS file inclusion directive(s) using the markup <script>
124 *
125 */
126 public function getJsInclusionDirective(bool $deferJS = \false) : string
127 {
128 $result = "<script type=\"text/javascript\">\n" . StaticContainer::get('Piwik\\Translation\\Translator')->getJavascriptTranslations() . "\n</script>";
129 if ($this->isMergedAssetsDisabled()) {
130 $this->getMergedCoreJSAsset()->delete();
131 $this->getMergedNonCoreJSAsset()->delete();
132 $result .= $this->getIndividualCoreAndNonCoreJsIncludes();
133 } else {
134 $result .= sprintf($deferJS ? self::JS_DEFER_IMPORT_DIRECTIVE : self::JS_IMPORT_DIRECTIVE, self::GET_CORE_JS_MODULE_ACTION);
135 $result .= sprintf($deferJS ? self::JS_DEFER_IMPORT_DIRECTIVE : self::JS_IMPORT_DIRECTIVE, self::GET_NON_CORE_JS_MODULE_ACTION);
136 $result .= $this->getPluginUmdChunks();
137 }
138 return $result;
139 }
140 protected function getPluginUmdChunks()
141 {
142 $fetcher = $this->getPluginUmdJScriptFetcher();
143 $chunks = $fetcher->getChunkFiles();
144 $result = '';
145 foreach ($chunks as $chunk) {
146 $src = self::GET_JS_UMD_MODULE_ACTION . urlencode($chunk->getChunkName());
147 $result .= sprintf(self::JS_DEFER_IMPORT_DIRECTIVE, $src);
148 }
149 return $result;
150 }
151 /**
152 * Return the base.less compiled to css
153 *
154 * @return UIAsset
155 */
156 public function getCompiledBaseCss()
157 {
158 $mergedAsset = new InMemoryUIAsset();
159 $assetMerger = new StylesheetUIAssetMerger($mergedAsset, $this->minimalStylesheetFetcher, $this->cacheBuster);
160 $assetMerger->generateFile();
161 return $mergedAsset;
162 }
163 /**
164 * Return the css merged file absolute location.
165 * If there is none, the generation process will be triggered.
166 *
167 * @return UIAsset
168 */
169 public function getMergedStylesheet()
170 {
171 $mergedAsset = $this->getMergedStylesheetAsset();
172 $assetFetcher = new StylesheetUIAssetFetcher(Manager::getInstance()->getLoadedPluginsName(), $this->theme);
173 $assetMerger = new StylesheetUIAssetMerger($mergedAsset, $assetFetcher, $this->cacheBuster);
174 $assetMerger->generateFile();
175 return $mergedAsset;
176 }
177 /**
178 * Return the core js merged file absolute location.
179 * If there is none, the generation process will be triggered.
180 *
181 * @return UIAsset
182 */
183 public function getMergedCoreJavaScript()
184 {
185 return $this->getMergedJavascript($this->getCoreJScriptFetcher(), $this->getMergedCoreJSAsset());
186 }
187 /**
188 * Return the non core js merged file absolute location.
189 * If there is none, the generation process will be triggered.
190 *
191 * @return UIAsset
192 */
193 public function getMergedNonCoreJavaScript()
194 {
195 return $this->getMergedJavascript($this->getNonCoreJScriptFetcher(), $this->getMergedNonCoreJSAsset());
196 }
197 /**
198 * Return a chunk JS merged file absolute location.
199 * If there is none, the generation process will be triggered.
200 *
201 * @param string $chunk The name of the chunk. Will either be a plugin name or an integer.
202 * @return UIAsset
203 */
204 public function getMergedJavaScriptChunk($chunk)
205 {
206 $assetFetcher = $this->getPluginUmdJScriptFetcher($chunk);
207 $outputFile = $assetFetcher->getRequestedChunkOutputFile();
208 return $this->getMergedJavascript($assetFetcher, $this->getMergedUIAsset($outputFile));
209 }
210 /**
211 * @param boolean|"all" $core
212 * @return string[]
213 */
214 public function getLoadedPlugins($core)
215 {
216 $loadedPlugins = array();
217 foreach (Manager::getInstance()->getPluginsLoadedAndActivated() as $plugin) {
218 $pluginName = $plugin->getPluginName();
219 $pluginIsCore = Manager::getInstance()->isPluginBundledWithCore($pluginName);
220 if ($core === 'all' || $pluginIsCore && $core || !$pluginIsCore && !$core) {
221 $loadedPlugins[] = $pluginName;
222 }
223 }
224 return $loadedPlugins;
225 }
226 /**
227 * Remove previous merged assets
228 */
229 public function removeMergedAssets($pluginName = \false)
230 {
231 $assetsToRemove = array($this->getMergedStylesheetAsset());
232 if ($pluginName) {
233 if ($this->pluginContainsJScriptAssets($pluginName)) {
234 if (Manager::getInstance()->isPluginBundledWithCore($pluginName)) {
235 $assetsToRemove[] = $this->getMergedCoreJSAsset();
236 } else {
237 $assetsToRemove[] = $this->getMergedNonCoreJSAsset();
238 }
239 $assetFetcher = $this->getPluginUmdJScriptFetcher();
240 foreach ($assetFetcher->getChunkFiles() as $chunk) {
241 $files = $chunk->getFiles();
242 $foundInChunk = \false;
243 foreach ($files as $file) {
244 if (strpos($file, "/{$pluginName}.umd.") !== \false) {
245 $foundInChunk = \true;
246 }
247 }
248 if ($foundInChunk) {
249 $outputFile = $chunk->getOutputFile();
250 $asset = $this->getMergedUIAsset($outputFile);
251 if ($asset->exists()) {
252 $assetsToRemove[] = $asset;
253 }
254 break;
255 }
256 }
257 }
258 } else {
259 $assetsToRemove[] = $this->getMergedCoreJSAsset();
260 $assetsToRemove[] = $this->getMergedNonCoreJSAsset();
261 $assetFetcher = $this->getPluginUmdJScriptFetcher();
262 foreach ($assetFetcher->getChunkFiles() as $chunk) {
263 $outputFile = $chunk->getOutputFile();
264 $asset = $this->getMergedUIAsset($outputFile);
265 if ($asset->exists()) {
266 $assetsToRemove[] = $asset;
267 }
268 }
269 }
270 $this->removeAssets($assetsToRemove);
271 }
272 /**
273 * Check if the merged file directory exists and is writable.
274 *
275 * @return string The directory location
276 * @throws Exception if directory is not writable.
277 */
278 public function getAssetDirectory()
279 {
280 $mergedFileDirectory = StaticContainer::get('path.tmp') . '/assets';
281 if (!is_dir($mergedFileDirectory)) {
282 \Piwik\Filesystem::mkdir($mergedFileDirectory);
283 }
284 if (!is_writable($mergedFileDirectory)) {
285 throw new Exception("Directory " . $mergedFileDirectory . " has to be writable.");
286 }
287 return $mergedFileDirectory;
288 }
289 /**
290 * Return the global option disable_merged_assets
291 *
292 * @return boolean
293 */
294 public function isMergedAssetsDisabled()
295 {
296 if (\Piwik\Config::getInstance()->Development['disable_merged_assets'] == 1) {
297 return \true;
298 }
299 if (isset($_GET['disable_merged_assets']) && $_GET['disable_merged_assets'] == 1) {
300 return \true;
301 }
302 return \false;
303 }
304 /**
305 * @param UIAssetFetcher $assetFetcher
306 * @param UIAsset $mergedAsset
307 * @return UIAsset
308 */
309 private function getMergedJavascript($assetFetcher, $mergedAsset)
310 {
311 $assetMerger = new JScriptUIAssetMerger($mergedAsset, $assetFetcher, $this->cacheBuster);
312 $assetMerger->generateFile();
313 return $mergedAsset;
314 }
315 /**
316 * Return individual JS file inclusion directive(s) using the markup <script>
317 *
318 */
319 protected function getIndividualCoreAndNonCoreJsIncludes() : string
320 {
321 return $this->getIndividualJsIncludesFromAssetFetcher($this->getCoreJScriptFetcher()) . $this->getIndividualJsIncludesFromAssetFetcher($this->getNonCoreJScriptFetcher()) . $this->getIndividualJsIncludesFromAssetFetcher($this->getPluginUmdJScriptFetcher());
322 }
323 /**
324 * @param UIAssetFetcher $assetFetcher
325 */
326 protected function getIndividualJsIncludesFromAssetFetcher($assetFetcher) : string
327 {
328 $jsIncludeString = '';
329 $assets = $assetFetcher->getCatalog()->getAssets();
330 foreach ($assets as $jsFile) {
331 $jsFile->validateFile();
332 $jsIncludeString = $jsIncludeString . sprintf(self::JS_IMPORT_DIRECTIVE, $jsFile->getRelativeLocation());
333 }
334 return $jsIncludeString;
335 }
336 private function getCoreJScriptFetcher()
337 {
338 return new JScriptUIAssetFetcher($this->getLoadedPlugins(\true), $this->theme);
339 }
340 protected function getNonCoreJScriptFetcher()
341 {
342 return new JScriptUIAssetFetcher($this->getLoadedPlugins(\false), $this->theme);
343 }
344 protected function getPluginUmdJScriptFetcher($chunk = null)
345 {
346 return new PluginUmdAssetFetcher($this->getLoadedPlugins('all'), $this->theme, $chunk);
347 }
348 /**
349 * @param string $pluginName
350 * @return boolean
351 */
352 private function pluginContainsJScriptAssets($pluginName)
353 {
354 $fetcher = new JScriptUIAssetFetcher(array($pluginName), $this->theme);
355 try {
356 $assets = $fetcher->getCatalog()->getAssets();
357 } catch (\Exception $e) {
358 // This can happen when a plugin is not valid (eg. Piwik 1.x format)
359 // When posting the event to the plugin, it returns an exception "Plugin has not been loaded"
360 return \false;
361 }
362 $pluginManager = Manager::getInstance();
363 $plugin = null;
364 if ($pluginManager->isPluginLoaded($pluginName)) {
365 $plugin = $pluginManager->getLoadedPlugin($pluginName);
366 }
367 if ($plugin && $plugin->isTheme()) {
368 $theme = $pluginManager->getTheme($pluginName);
369 $javaScriptFiles = $theme->getJavaScriptFiles();
370 if (!empty($javaScriptFiles)) {
371 $assets = array_merge($assets, $javaScriptFiles);
372 }
373 }
374 return !empty($assets);
375 }
376 /**
377 * @param UIAsset[] $uiAssets
378 */
379 public function removeAssets($uiAssets)
380 {
381 foreach ($uiAssets as $uiAsset) {
382 $uiAsset->delete();
383 }
384 }
385 /**
386 * @return UIAsset
387 */
388 public function getMergedStylesheetAsset()
389 {
390 return $this->getMergedUIAsset(self::MERGED_CSS_FILE);
391 }
392 /**
393 * @return UIAsset
394 */
395 private function getMergedCoreJSAsset()
396 {
397 return $this->getMergedUIAsset(self::MERGED_CORE_JS_FILE);
398 }
399 /**
400 * @return UIAsset
401 */
402 protected function getMergedNonCoreJSAsset()
403 {
404 return $this->getMergedUIAsset(self::MERGED_NON_CORE_JS_FILE);
405 }
406 /**
407 * @param string $fileName
408 * @return UIAsset
409 */
410 private function getMergedUIAsset($fileName)
411 {
412 return new OnDiskUIAsset($this->getAssetDirectory(), $fileName);
413 }
414 public static function compileCustomStylesheets($files)
415 {
416 $assetManager = new \Piwik\AssetManager();
417 $fetcher = new StaticUIAssetFetcher($files, $priorityOrder = array(), $theme = null);
418 $assetManager->setMinimalStylesheetFetcher($fetcher);
419 return $assetManager->getCompiledBaseCss()->getContent();
420 }
421 public static function compileCustomJs($files)
422 {
423 $mergedAsset = new InMemoryUIAsset();
424 $fetcher = new StaticUIAssetFetcher($files, $priorityOrder = array(), $theme = null);
425 $cacheBuster = UIAssetCacheBuster::getInstance();
426 $assetMerger = new JScriptUIAssetMerger($mergedAsset, $fetcher, $cacheBuster);
427 $assetMerger->generateFile();
428 return $mergedAsset->getContent();
429 }
430 }
431