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