blob: f3862989e10f2f62fdcdb22575de6338d5d2a5ea (
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Settings;
use Piwik\Plugin\Manager as PluginManager;
/**
* Settings manager.
*
*/
class Manager
{
private static $settings = array();
private static $numPluginsChecked = 0;
/**
* Returns all available plugin settings, even settings for inactive plugins. A plugin has to specify a file named
* `Settings.php` containing a class named `Settings` that extends `Piwik\Plugin\Settings` in order to be
* considered as a plugin setting. Otherwise the settings for a plugin won't be available.
*
* @return \Piwik\Plugin\Settings[] An array containing array([pluginName] => [setting instance]).
*/
public static function getAllPluginSettings()
{
$numActivatedPlugins = PluginManager::getInstance()->getNumberOfActivatedPlugins();
if (static::$numPluginsChecked != $numActivatedPlugins) {
static::$numPluginsChecked = $numActivatedPlugins;
static::$settings = array();
}
if (empty(static::$settings)) {
$settings = PluginManager::getInstance()->findComponents('Settings', 'Piwik\\Plugin\\Settings');
$byPluginName = array();
foreach ($settings as $setting) {
$byPluginName[$setting->getPluginName()] = $setting;
}
static::$settings = $byPluginName;
}
return static::$settings;
}
private static function isActivatedPlugin($pluginName)
{
return PluginManager::getInstance()->isPluginActivated($pluginName);
}
/**
* Removes all settings made for a specific plugin. Useful while uninstalling a plugin.
*
* @param string $pluginName
*/
public static function cleanupPluginSettings($pluginName)
{
$pluginManager = PluginManager::getInstance();
if (!$pluginManager->isPluginLoaded($pluginName)) {
return;
}
$plugin = $pluginManager->loadPlugin($pluginName);
$settings = $plugin->findComponent('Settings', 'Piwik\\Plugin\\Settings');
if (!empty($settings)) {
$settings->removeAllPluginSettings();
}
}
/**
* Gets all plugins settings that have at least one settings a user is allowed to change. Only the settings for
* activated plugins are returned.
*
* @return \Piwik\Plugin\Settings[] An array containing array([pluginName] => [setting instance]).
*/
public static function getPluginSettingsForCurrentUser()
{
$settings = static::getAllPluginSettings();
$settingsForUser = array();
foreach ($settings as $pluginName => $setting) {
if (!static::isActivatedPlugin($pluginName)) {
continue;
}
$forUser = $setting->getSettingsForCurrentUser();
if (!empty($forUser)) {
$settingsForUser[$pluginName] = $setting;
}
}
return $settingsForUser;
}
public static function hasSystemPluginSettingsForCurrentUser($pluginName)
{
$pluginNames = static::getPluginNamesHavingSystemSettings();
return in_array($pluginName, $pluginNames);
}
/**
* Detects whether there are user settings for activated plugins available that the current user can change.
*
* @return bool
*/
public static function hasUserPluginsSettingsForCurrentUser()
{
$settings = static::getPluginSettingsForCurrentUser();
foreach ($settings as $setting) {
foreach ($setting->getSettingsForCurrentUser() as $set) {
if ($set instanceof UserSetting) {
return true;
}
}
}
return false;
}
public static function getPluginNamesHavingSystemSettings()
{
$settings = static::getPluginSettingsForCurrentUser();
$plugins = array();
foreach ($settings as $pluginName => $setting) {
foreach ($setting->getSettingsForCurrentUser() as $set) {
if ($set instanceof SystemSetting) {
$plugins[] = $pluginName;
}
}
}
return array_unique($plugins);
}
/**
* Detects whether there are system settings for activated plugins available that the current user can change.
*
* @return bool
*/
public static function hasSystemPluginsSettingsForCurrentUser()
{
$settings = static::getPluginNamesHavingSystemSettings();
return !empty($settings);
}
}
|