PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.2.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.2.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 / AssetManager.php
matomo / app / core Last commit date
API 1 year ago Access 1 year ago Application 1 year ago Archive 1 year ago ArchiveProcessor 1 year ago Archiver 2 years ago AssetManager 1 year ago Auth 1 year ago Category 2 years ago Changes 1 year ago CliMulti 1 year ago Columns 1 year ago Concurrency 1 year ago Config 1 year ago Container 1 year ago CronArchive 1 year ago DataAccess 1 year ago DataFiles 2 years ago DataTable 1 year ago Db 1 year ago DeviceDetector 1 year ago Email 2 years ago Exception 1 year ago Http 1 year ago Intl 1 year ago Log 2 years ago Mail 1 year ago Measurable 1 year ago Menu 1 year ago Metrics 1 year ago Notification 1 year ago Period 1 year ago Plugin 1 year ago ProfessionalServices 1 year ago Report 1 year ago ReportRenderer 1 year ago Scheduler 1 year ago Segment 1 year ago Session 1 year ago Settings 1 year ago Tracker 1 year ago Translation 1 year ago Twig 1 year ago UpdateCheck 1 year ago Updater 1 year ago Updates 1 year ago Validators 1 year ago View 1 year ago ViewDataTable 1 year ago Visualization 1 year ago Widget 1 year ago .htaccess 2 years ago Access.php 1 year ago Archive.php 1 year ago ArchiveProcessor.php 1 year ago AssetManager.php 1 year ago Auth.php 2 years ago AuthResult.php 2 years ago BaseFactory.php 2 years ago Cache.php 2 years ago CacheId.php 1 year ago CliMulti.php 1 year ago Common.php 1 year ago Config.php 1 year ago Console.php 1 year ago Context.php 2 years ago Cookie.php 1 year ago CronArchive.php 1 year ago DI.php 1 year ago DataArray.php 1 year ago DataTable.php 1 year ago Date.php 1 year ago Db.php 1 year ago DbHelper.php 1 year ago Development.php 1 year ago ErrorHandler.php 1 year ago EventDispatcher.php 1 year ago ExceptionHandler.php 1 year ago FileIntegrity.php 1 year ago Filechecks.php 1 year ago Filesystem.php 1 year ago FrontController.php 1 year ago Http.php 1 year ago IP.php 1 year ago Log.php 2 years ago LogDeleter.php 1 year ago Mail.php 1 year ago Metrics.php 1 year ago NoAccessException.php 2 years ago Nonce.php 1 year ago Notification.php 1 year ago NumberFormatter.php 1 year ago Option.php 1 year ago Period.php 1 year ago Piwik.php 1 year ago Plugin.php 1 year ago Process.php 1 year ago Profiler.php 1 year ago ProxyHeaders.php 2 years ago ProxyHttp.php 1 year ago QuickForm2.php 1 year ago RankingQuery.php 1 year ago ReportRenderer.php 1 year ago Request.php 1 year ago Segment.php 1 year ago Sequence.php 2 years ago Session.php 1 year ago SettingsPiwik.php 1 year ago SettingsServer.php 1 year ago Singleton.php 2 years ago Site.php 1 year ago SiteContentDetector.php 1 year ago SupportedBrowser.php 2 years ago TCPDF.php 1 year ago Theme.php 1 year ago Timer.php 2 years ago Tracker.php 1 year ago Twig.php 1 year ago Unzip.php 1 year ago UpdateCheck.php 1 year ago Updater.php 1 year ago UpdaterErrorException.php 2 years ago Updates.php 1 year ago Url.php 1 year ago UrlHelper.php 1 year ago Version.php 1 year ago View.php 1 year ago bootstrap.php 1 year ago dispatch.php 2 years ago testMinimumPhpVersion.php 2 years ago
AssetManager.php
435 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 * @param bool $deferJS
126 * @return string
127 */
128 public function getJsInclusionDirective(bool $deferJS = \false) : string
129 {
130 $result = "<script type=\"text/javascript\">\n" . StaticContainer::get('Piwik\\Translation\\Translator')->getJavascriptTranslations() . "\n</script>";
131 if ($this->isMergedAssetsDisabled()) {
132 $this->getMergedCoreJSAsset()->delete();
133 $this->getMergedNonCoreJSAsset()->delete();
134 $result .= $this->getIndividualCoreAndNonCoreJsIncludes();
135 } else {
136 $result .= sprintf($deferJS ? self::JS_DEFER_IMPORT_DIRECTIVE : self::JS_IMPORT_DIRECTIVE, self::GET_CORE_JS_MODULE_ACTION);
137 $result .= sprintf($deferJS ? self::JS_DEFER_IMPORT_DIRECTIVE : self::JS_IMPORT_DIRECTIVE, self::GET_NON_CORE_JS_MODULE_ACTION);
138 $result .= $this->getPluginUmdChunks();
139 }
140 return $result;
141 }
142 protected function getPluginUmdChunks()
143 {
144 $fetcher = $this->getPluginUmdJScriptFetcher();
145 $chunks = $fetcher->getChunkFiles();
146 $result = '';
147 foreach ($chunks as $chunk) {
148 $src = self::GET_JS_UMD_MODULE_ACTION . urlencode($chunk->getChunkName());
149 $result .= sprintf(self::JS_DEFER_IMPORT_DIRECTIVE, $src);
150 }
151 return $result;
152 }
153 /**
154 * Return the base.less compiled to css
155 *
156 * @return UIAsset
157 */
158 public function getCompiledBaseCss()
159 {
160 $mergedAsset = new InMemoryUIAsset();
161 $assetMerger = new StylesheetUIAssetMerger($mergedAsset, $this->minimalStylesheetFetcher, $this->cacheBuster);
162 $assetMerger->generateFile();
163 return $mergedAsset;
164 }
165 /**
166 * Return the css merged file absolute location.
167 * If there is none, the generation process will be triggered.
168 *
169 * @return UIAsset
170 */
171 public function getMergedStylesheet()
172 {
173 $mergedAsset = $this->getMergedStylesheetAsset();
174 $assetFetcher = new StylesheetUIAssetFetcher(Manager::getInstance()->getLoadedPluginsName(), $this->theme);
175 $assetMerger = new StylesheetUIAssetMerger($mergedAsset, $assetFetcher, $this->cacheBuster);
176 $assetMerger->generateFile();
177 return $mergedAsset;
178 }
179 /**
180 * Return the core js merged file absolute location.
181 * If there is none, the generation process will be triggered.
182 *
183 * @return UIAsset
184 */
185 public function getMergedCoreJavaScript()
186 {
187 return $this->getMergedJavascript($this->getCoreJScriptFetcher(), $this->getMergedCoreJSAsset());
188 }
189 /**
190 * Return the non core js merged file absolute location.
191 * If there is none, the generation process will be triggered.
192 *
193 * @return UIAsset
194 */
195 public function getMergedNonCoreJavaScript()
196 {
197 return $this->getMergedJavascript($this->getNonCoreJScriptFetcher(), $this->getMergedNonCoreJSAsset());
198 }
199 /**
200 * Return a chunk JS merged file absolute location.
201 * If there is none, the generation process will be triggered.
202 *
203 * @param string $chunk The name of the chunk. Will either be a plugin name or an integer.
204 * @return UIAsset
205 */
206 public function getMergedJavaScriptChunk($chunk)
207 {
208 $assetFetcher = $this->getPluginUmdJScriptFetcher($chunk);
209 $outputFile = $assetFetcher->getRequestedChunkOutputFile();
210 return $this->getMergedJavascript($assetFetcher, $this->getMergedUIAsset($outputFile));
211 }
212 /**
213 * @param boolean|"all" $core
214 * @return string[]
215 */
216 public function getLoadedPlugins($core)
217 {
218 $loadedPlugins = array();
219 foreach (Manager::getInstance()->getPluginsLoadedAndActivated() as $plugin) {
220 $pluginName = $plugin->getPluginName();
221 $pluginIsCore = Manager::getInstance()->isPluginBundledWithCore($pluginName);
222 if ($core === 'all' || $pluginIsCore && $core || !$pluginIsCore && !$core) {
223 $loadedPlugins[] = $pluginName;
224 }
225 }
226 return $loadedPlugins;
227 }
228 /**
229 * Remove previous merged assets
230 */
231 public function removeMergedAssets($pluginName = \false)
232 {
233 $assetsToRemove = array($this->getMergedStylesheetAsset());
234 if ($pluginName) {
235 if ($this->pluginContainsJScriptAssets($pluginName)) {
236 if (Manager::getInstance()->isPluginBundledWithCore($pluginName)) {
237 $assetsToRemove[] = $this->getMergedCoreJSAsset();
238 } else {
239 $assetsToRemove[] = $this->getMergedNonCoreJSAsset();
240 }
241 $assetFetcher = $this->getPluginUmdJScriptFetcher();
242 foreach ($assetFetcher->getChunkFiles() as $chunk) {
243 $files = $chunk->getFiles();
244 $foundInChunk = \false;
245 foreach ($files as $file) {
246 if (strpos($file, "/{$pluginName}.umd.") !== \false) {
247 $foundInChunk = \true;
248 }
249 }
250 if ($foundInChunk) {
251 $outputFile = $chunk->getOutputFile();
252 $asset = $this->getMergedUIAsset($outputFile);
253 if ($asset->exists()) {
254 $assetsToRemove[] = $asset;
255 }
256 break;
257 }
258 }
259 }
260 } else {
261 $assetsToRemove[] = $this->getMergedCoreJSAsset();
262 $assetsToRemove[] = $this->getMergedNonCoreJSAsset();
263 $assetFetcher = $this->getPluginUmdJScriptFetcher();
264 foreach ($assetFetcher->getChunkFiles() as $chunk) {
265 $outputFile = $chunk->getOutputFile();
266 $asset = $this->getMergedUIAsset($outputFile);
267 if ($asset->exists()) {
268 $assetsToRemove[] = $asset;
269 }
270 }
271 }
272 $this->removeAssets($assetsToRemove);
273 }
274 /**
275 * Check if the merged file directory exists and is writable.
276 *
277 * @return string The directory location
278 * @throws Exception if directory is not writable.
279 */
280 public function getAssetDirectory()
281 {
282 $mergedFileDirectory = StaticContainer::get('path.tmp') . '/assets';
283 if (!is_dir($mergedFileDirectory)) {
284 \Piwik\Filesystem::mkdir($mergedFileDirectory);
285 }
286 if (!is_writable($mergedFileDirectory)) {
287 throw new Exception("Directory " . $mergedFileDirectory . " has to be writable.");
288 }
289 return $mergedFileDirectory;
290 }
291 /**
292 * Return the global option disable_merged_assets
293 *
294 * @return boolean
295 */
296 public function isMergedAssetsDisabled()
297 {
298 if (\Piwik\Config::getInstance()->Development['disable_merged_assets'] == 1) {
299 return \true;
300 }
301 if (isset($_GET['disable_merged_assets']) && $_GET['disable_merged_assets'] == 1) {
302 return \true;
303 }
304 return \false;
305 }
306 /**
307 * @param UIAssetFetcher $assetFetcher
308 * @param UIAsset $mergedAsset
309 * @return UIAsset
310 */
311 private function getMergedJavascript($assetFetcher, $mergedAsset)
312 {
313 $assetMerger = new JScriptUIAssetMerger($mergedAsset, $assetFetcher, $this->cacheBuster);
314 $assetMerger->generateFile();
315 return $mergedAsset;
316 }
317 /**
318 * Return individual JS file inclusion directive(s) using the markup <script>
319 *
320 * @return string
321 */
322 protected function getIndividualCoreAndNonCoreJsIncludes() : string
323 {
324 return $this->getIndividualJsIncludesFromAssetFetcher($this->getCoreJScriptFetcher()) . $this->getIndividualJsIncludesFromAssetFetcher($this->getNonCoreJScriptFetcher()) . $this->getIndividualJsIncludesFromAssetFetcher($this->getPluginUmdJScriptFetcher());
325 }
326 /**
327 * @param UIAssetFetcher $assetFetcher
328 * @return string
329 */
330 protected function getIndividualJsIncludesFromAssetFetcher($assetFetcher) : string
331 {
332 $jsIncludeString = '';
333 $assets = $assetFetcher->getCatalog()->getAssets();
334 foreach ($assets as $jsFile) {
335 $jsFile->validateFile();
336 $jsIncludeString = $jsIncludeString . sprintf(self::JS_IMPORT_DIRECTIVE, $jsFile->getRelativeLocation());
337 }
338 return $jsIncludeString;
339 }
340 private function getCoreJScriptFetcher()
341 {
342 return new JScriptUIAssetFetcher($this->getLoadedPlugins(\true), $this->theme);
343 }
344 protected function getNonCoreJScriptFetcher()
345 {
346 return new JScriptUIAssetFetcher($this->getLoadedPlugins(\false), $this->theme);
347 }
348 protected function getPluginUmdJScriptFetcher($chunk = null)
349 {
350 return new PluginUmdAssetFetcher($this->getLoadedPlugins('all'), $this->theme, $chunk);
351 }
352 /**
353 * @param string $pluginName
354 * @return boolean
355 */
356 private function pluginContainsJScriptAssets($pluginName)
357 {
358 $fetcher = new JScriptUIAssetFetcher(array($pluginName), $this->theme);
359 try {
360 $assets = $fetcher->getCatalog()->getAssets();
361 } catch (\Exception $e) {
362 // This can happen when a plugin is not valid (eg. Piwik 1.x format)
363 // When posting the event to the plugin, it returns an exception "Plugin has not been loaded"
364 return \false;
365 }
366 $pluginManager = Manager::getInstance();
367 $plugin = null;
368 if ($pluginManager->isPluginLoaded($pluginName)) {
369 $plugin = $pluginManager->getLoadedPlugin($pluginName);
370 }
371 if ($plugin && $plugin->isTheme()) {
372 $theme = $pluginManager->getTheme($pluginName);
373 $javaScriptFiles = $theme->getJavaScriptFiles();
374 if (!empty($javaScriptFiles)) {
375 $assets = array_merge($assets, $javaScriptFiles);
376 }
377 }
378 return !empty($assets);
379 }
380 /**
381 * @param UIAsset[] $uiAssets
382 */
383 public function removeAssets($uiAssets)
384 {
385 foreach ($uiAssets as $uiAsset) {
386 $uiAsset->delete();
387 }
388 }
389 /**
390 * @return UIAsset
391 */
392 public function getMergedStylesheetAsset()
393 {
394 return $this->getMergedUIAsset(self::MERGED_CSS_FILE);
395 }
396 /**
397 * @return UIAsset
398 */
399 private function getMergedCoreJSAsset()
400 {
401 return $this->getMergedUIAsset(self::MERGED_CORE_JS_FILE);
402 }
403 /**
404 * @return UIAsset
405 */
406 protected function getMergedNonCoreJSAsset()
407 {
408 return $this->getMergedUIAsset(self::MERGED_NON_CORE_JS_FILE);
409 }
410 /**
411 * @param string $fileName
412 * @return UIAsset
413 */
414 private function getMergedUIAsset($fileName)
415 {
416 return new OnDiskUIAsset($this->getAssetDirectory(), $fileName);
417 }
418 public static function compileCustomStylesheets($files)
419 {
420 $assetManager = new \Piwik\AssetManager();
421 $fetcher = new StaticUIAssetFetcher($files, $priorityOrder = array(), $theme = null);
422 $assetManager->setMinimalStylesheetFetcher($fetcher);
423 return $assetManager->getCompiledBaseCss()->getContent();
424 }
425 public static function compileCustomJs($files)
426 {
427 $mergedAsset = new InMemoryUIAsset();
428 $fetcher = new StaticUIAssetFetcher($files, $priorityOrder = array(), $theme = null);
429 $cacheBuster = UIAssetCacheBuster::getInstance();
430 $assetMerger = new JScriptUIAssetMerger($mergedAsset, $fetcher, $cacheBuster);
431 $assetMerger->generateFile();
432 return $mergedAsset->getContent();
433 }
434 }
435