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