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