PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
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 / Widget / WidgetsList.php
matomo / app / core / Widget Last commit date
Widget.php 6 years ago WidgetConfig.php 6 years ago WidgetContainerConfig.php 6 years ago WidgetsList.php 6 years ago
WidgetsList.php
250 lines
1 <?php
2 /**
3 * Piwik - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9 namespace Piwik\Widget;
10
11 use Piwik\Cache as PiwikCache;
12 use Piwik\Container\StaticContainer;
13 use Piwik\Development;
14 use Piwik\Piwik;
15 use Piwik\Report\ReportWidgetFactory;
16
17 /**
18 * Manages the global list of reports that can be displayed as dashboard widgets.
19 *
20 * Widgets are added through the {@hook WidgetsList.addWidgets} and filtered through the {@hook Widgets.filterWidgets}
21 * event. Observers for this event should call the {@link addWidget()} method to add widgets or use any of the other
22 * methods to remove widgets.
23 *
24 * @api since Piwik 3.0.0
25 */
26 class WidgetsList
27 {
28 /**
29 * List of widgets
30 *
31 * @var WidgetConfig[]
32 */
33 private $widgets = array();
34
35 /**
36 * @var WidgetContainerConfig[]
37 */
38 private $container;
39
40 /**
41 * @var array
42 */
43 private $containerWidgets;
44
45 /**
46 * Adds a new widget to the widget config. Please make sure the widget is enabled before adding a widget as
47 * no such checks will be performed.
48 *
49 * @param WidgetConfig $widget
50 */
51 public function addWidgetConfig(WidgetConfig $widget)
52 {
53 if ($widget instanceof WidgetContainerConfig) {
54 $this->addContainer($widget);
55 } elseif (Development::isEnabled()) {
56 $this->checkIsValidWidget($widget);
57 }
58
59 $this->widgets[] = $widget;
60 }
61
62 /**
63 * Add multiple widget configs at once. See {@link addWidgetConfig()}.
64 *
65 * @param WidgetConfig[] $widgets
66 */
67 public function addWidgetConfigs($widgets)
68 {
69 foreach ($widgets as $widget) {
70 $this->addWidgetConfig($widget);
71 }
72 }
73
74 private function addContainer(WidgetContainerConfig $containerWidget)
75 {
76 $widgetId = $containerWidget->getId();
77
78 $this->container[$widgetId] = $containerWidget;
79
80 // widgets were added to this container, but the container did not exist yet.
81 if (isset($this->containerWidgets[$widgetId])) {
82 foreach ($this->containerWidgets[$widgetId] as $widget) {
83 $containerWidget->addWidgetConfig($widget);
84 }
85 unset($this->containerWidgets[$widgetId]);
86 }
87 }
88
89 /**
90 * Get all added widget configs.
91 *
92 * @return WidgetConfig[]
93 */
94 public function getWidgetConfigs()
95 {
96 return $this->widgets;
97 }
98
99 private function checkIsValidWidget(WidgetConfig $widget)
100 {
101 if (!$widget->getModule()) {
102 Development::error('No module is defined for added widget having name "' . $widget->getName());
103 }
104
105 if (!$widget->getAction()) {
106 Development::error('No action is defined for added widget having name "' . $widget->getName());
107 }
108 }
109
110 /**
111 * Add a widget to a widget container. It doesn't matter whether the container was added to this list already
112 * or whether the container is added later. As long as a container having the same containerId is added at
113 * some point the widget will be added to that container. If no container having this id is added the widget
114 * will not be recognized.
115 *
116 * @param string $containerId eg 'Products' or 'Contents'. See {@link WidgetContainerConfig::setId}
117 * @param WidgetConfig $widget
118 */
119 public function addToContainerWidget($containerId, WidgetConfig $widget)
120 {
121 if (isset($this->container[$containerId])) {
122 $this->container[$containerId]->addWidgetConfig($widget);
123 } else {
124 if (!isset($this->containerWidgets[$containerId])) {
125 $this->containerWidgets[$containerId] = array();
126 }
127
128 $this->containerWidgets[$containerId][] = $widget;
129 }
130 }
131
132 /**
133 * Removes one or more widgets from the widget list.
134 *
135 * @param string $widgetCategoryId The widget category id. Can be a translation token eg 'General_Visits'
136 * see {@link WidgetConfig::setCategoryId()}.
137 * @param string|false $widgetName The name of the widget to remove eg 'VisitTime_ByServerTimeWidgetName'.
138 * If not supplied, all widgets within that category will be removed.
139 */
140 public function remove($widgetCategoryId, $widgetName = false)
141 {
142 foreach ($this->widgets as $index => $widget) {
143 if ($widget->getCategoryId() === $widgetCategoryId) {
144 if (!$widgetName || $widget->getName() === $widgetName) {
145 unset($this->widgets[$index]);
146 }
147 }
148 }
149 }
150
151 /**
152 * Returns `true` if a widget exists in the widget list, `false` if otherwise.
153 *
154 * @param string $module The controller name of the widget.
155 * @param string $action The controller action of the widget.
156 * @return bool
157 */
158 public function isDefined($module, $action)
159 {
160 foreach ($this->widgets as $widget) {
161 if ($widget->getModule() === $module && $widget->getAction() === $action) {
162 return true;
163 }
164 }
165
166 return false;
167 }
168
169 /**
170 * Get all widgets defined in the Piwik platform.
171 * @ignore
172 * @return static
173 */
174 public static function get()
175 {
176 $list = new static;
177
178 $widgets = StaticContainer::get('Piwik\Plugin\WidgetsProvider');
179
180 $widgetContainerConfigs = $widgets->getWidgetContainerConfigs();
181 foreach ($widgetContainerConfigs as $config) {
182 if ($config->isEnabled()) {
183 $list->addWidgetConfig($config);
184 }
185 }
186
187 $widgetConfigs = $widgets->getWidgetConfigs();
188 foreach ($widgetConfigs as $widget) {
189 if ($widget->isEnabled()) {
190 $list->addWidgetConfig($widget);
191 }
192 }
193
194 $reports = StaticContainer::get('Piwik\Plugin\ReportsProvider');
195 $reports = $reports->getAllReports();
196 foreach ($reports as $report) {
197 if ($report->isEnabled()) {
198 $factory = new ReportWidgetFactory($report);
199 $report->configureWidgets($list, $factory);
200 }
201 }
202
203 /**
204 * Triggered to filter widgets.
205 *
206 * **Example**
207 *
208 * public function removeWidgetConfigs(Piwik\Widget\WidgetsList $list)
209 * {
210 * $list->remove($category='General_Visits'); // remove all widgets having this category
211 * }
212 *
213 * @param WidgetsList $list An instance of the WidgetsList. You can change the list of widgets this way.
214 */
215 Piwik::postEvent('Widget.filterWidgets', array($list));
216
217 return $list;
218 }
219
220 /**
221 * CAUTION! If you ever change this method, existing updates will fail as they currently use that method!
222 * If you change the output the uniqueId for existing widgets would not be found anymore
223 *
224 * Returns the unique id of an widget with the given parameters
225 *
226 * @param $controllerName
227 * @param $controllerAction
228 * @param array $customParameters
229 * @return string
230 */
231 public static function getWidgetUniqueId($controllerName, $controllerAction, $customParameters = array())
232 {
233 $widgetUniqueId = 'widget' . $controllerName . $controllerAction;
234
235 foreach ($customParameters as $name => $value) {
236 if (is_array($value)) {
237 // use 'Array' for backward compatibility;
238 // could we switch to using $value[0]?
239 $value = 'Array';
240 }
241 $value = urlencode($value);
242 $value = str_replace('%', '', $value);
243 $widgetUniqueId .= $name . $value;
244 }
245
246 return $widgetUniqueId;
247 }
248
249 }
250