blob: 98fee4696fb3258a723c397bf79ffb4e14e9d560 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CustomJsTracker\TrackingCode;
use Piwik\Piwik;
use Piwik\Plugin;
use Piwik\Plugins\CustomJsTracker\File;
class PluginTrackerFiles
{
const TRACKER_FILE = 'tracker.js';
const MIN_TRACKER_FILE = 'tracker.min.js';
/**
* @var Plugin\Manager
*/
private $pluginManager;
/**
* @var bool
*/
protected $ignoreMinified = false;
public function __construct()
{
$this->pluginManager = Plugin\Manager::getInstance();
}
public function ignoreMinified()
{
$this->ignoreMinified = true;
}
protected function getDirectoriesToLook()
{
$dirs = array();
$manager = Plugin\Manager::getInstance();
foreach ($manager->getPluginsLoadedAndActivated() as $pluginName => $plugin) {
$dirs[$pluginName] = rtrim(Plugin\Manager::getPluginDirectory($pluginName), '/') . '/';
}
return $dirs;
}
/**
* @return File[]
*/
public function find()
{
$jsFiles = array();
foreach ($this->getDirectoriesToLook() as $pluginName => $pluginDir) {
if (!$this->ignoreMinified && file_exists($pluginDir . self::MIN_TRACKER_FILE)) {
$jsFiles[$pluginName] = new File($pluginDir . self::MIN_TRACKER_FILE);
} elseif (file_exists($pluginDir . self::TRACKER_FILE)) {
$jsFiles[$pluginName] = new File($pluginDir . self::TRACKER_FILE);
}
}
foreach ($jsFiles as $plugin => $file) {
if (!$this->shouldIncludeFile($plugin)) {
unset($jsFiles[$plugin]);
}
}
return $jsFiles;
}
protected function shouldIncludeFile($pluginName)
{
$shouldAddFile = true;
/**
* Detect if a custom tracker file should be added to the piwik.js tracker or not.
*
* This is useful for example if a plugin only wants to add its tracker file when the plugin is configured.
*
* @param bool &$shouldAddFile Decides whether the tracker file belonging to the given plugin should be added or not.
* @param string $pluginName The name of the plugin this file belongs to
*/
Piwik::postEvent('CustomJsTracker.shouldAddTrackerFile', array(&$shouldAddFile, $pluginName));
return $shouldAddFile;
}
protected function isPluginActivated($pluginName)
{
return $this->pluginManager->isPluginActivated($pluginName);
}
}
|