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