PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
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 / Plugin / Menu.php
matomo / app / core / Plugin Last commit date
ConsoleCommand 2 months ago Dimension 2 months ago API.php 8 months ago AggregatedMetric.php 2 years ago ArchivedMetric.php 2 weeks ago Archiver.php 2 months ago Categories.php 2 years ago ComponentFactory.php 2 weeks ago ComputedMetric.php 1 year ago ConsoleCommand.php 2 months ago Controller.php 2 months ago ControllerAdmin.php 2 weeks ago Dependency.php 2 months ago LogTablesProvider.php 2 weeks ago Manager.php 2 weeks ago Menu.php 2 weeks ago MetadataLoader.php 2 weeks ago Metric.php 2 months ago PluginException.php 1 year ago ProcessedMetric.php 4 months ago ReleaseChannels.php 4 months ago Report.php 2 months ago ReportsProvider.php 2 years ago RequestProcessors.php 5 months ago Segment.php 4 months ago SettingsProvider.php 2 weeks ago Tasks.php 2 months ago ThemeStyles.php 2 weeks ago ViewDataTable.php 2 weeks ago Visualization.php 2 weeks ago WidgetsProvider.php 4 months ago
Menu.php
258 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\Plugin;
10
11 use Piwik\Common;
12 use Piwik\Date;
13 use Piwik\Development;
14 use Piwik\Menu\MenuAdmin;
15 use Piwik\Menu\MenuTop;
16 use Piwik\Period;
17 use Piwik\Plugin\Manager as PluginManager;
18 use Piwik\Plugins\UsersManager\UserPreferences;
19 use Piwik\Site;
20 use Piwik\Url;
21 /**
22 * Base class of all plugin menu providers. Plugins that define their own menu items can extend this class to easily
23 * add new items, to remove or to rename existing items.
24 *
25 * Descendants of this class can overwrite any of these methods. Each method will be executed only once per request
26 * and cached for any further menu requests.
27 *
28 * For an example, see the {@link https://github.com/piwik/piwik/blob/master/plugins/ExampleUI/Menu.php} plugin.
29 *
30 * @api
31 * @since 2.4.0
32 */
33 class Menu
34 {
35 public function __construct()
36 {
37 // Constructor kept for BC (because called in implementations)
38 }
39 private function getModule()
40 {
41 $className = get_class($this);
42 $className = explode('\\', $className);
43 return $className[2];
44 }
45 /**
46 * Generates a URL for the default action of the plugin controller.
47 *
48 * Example:
49 * ```
50 * $menu->addItem('MyPlugin_MyPlugin', '', $this->urlForDefaultAction(), $orderId = 30);
51 * // will add a menu item that leads to the default action of the plugin controller when a user clicks on it.
52 * // The default action is usually the `index` action - meaning the `index()` method the controller -
53 * // but the default action can be customized within a controller
54 * ```
55 *
56 * @param array $additionalParams Optional URL parameters that will be appended to the URL
57 * @return array
58 *
59 * @since 2.7.0
60 * @api
61 */
62 protected function urlForDefaultAction($additionalParams = array())
63 {
64 $params = (array) $additionalParams;
65 $params['action'] = '';
66 $params['module'] = $this->getModule();
67 return $params;
68 }
69 /**
70 * Generates a URL for the given action. In your plugin controller you have to create a method with the same name
71 * as this method will be executed when a user clicks on the menu item. If you want to generate a URL for the
72 * action of another module, meaning not your plugin, you should use the method {@link urlForModuleAction()}.
73 *
74 * @param string $controllerAction The name of the action that should be executed within your controller
75 * @param array $additionalParams Optional URL parameters that will be appended to the URL
76 * @return array
77 *
78 * @since 2.7.0
79 * @api
80 */
81 protected function urlForAction($controllerAction, $additionalParams = array())
82 {
83 $module = $this->getModule();
84 $this->checkisValidCallable($module, $controllerAction);
85 $params = (array) $additionalParams;
86 $params['action'] = $controllerAction;
87 $params['module'] = $module;
88 return $params;
89 }
90 /**
91 * Generates a URL for the given action of the given module. We usually do not recommend to use this method as you
92 * should make sure the method of that module actually exists. If the plugin owner of that module changes the method
93 * in a future version your link might no longer work. If you want to link to an action of your controller use the
94 * method {@link urlForAction()}. Note: We will generate a link only if the given module is installed and activated.
95 *
96 * @param string $module The name of the module/plugin the action belongs to. The module name is case sensitive.
97 * @param string $controllerAction The name of the action that should be executed within your controller
98 * @param array $additionalParams Optional URL parameters that will be appended to the URL
99 * @return array|null Returns null if the given module is either not installed or not activated. Returns the array
100 * of query parameter names and values to the given module action otherwise.
101 *
102 * @since 2.7.0
103 * // not API for now
104 */
105 protected function urlForModuleAction($module, $controllerAction, $additionalParams = array())
106 {
107 $this->checkisValidCallable($module, $controllerAction);
108 $pluginManager = PluginManager::getInstance();
109 if (!$pluginManager->isPluginLoaded($module) || !$pluginManager->isPluginActivated($module)) {
110 return null;
111 }
112 $params = (array) $additionalParams;
113 $params['action'] = $controllerAction;
114 $params['module'] = $module;
115 return $params;
116 }
117 /**
118 * Generates a URL to the given action of the current module, and it will also append some URL query parameters from the
119 * User preferences: idSite, period, date. If you do not need the parameters idSite, period and date to be generated
120 * use {@link urlForAction()} instead.
121 *
122 * @param string $controllerAction The name of the action that should be executed within your controller
123 * @param array $additionalParams Optional URL parameters that will be appended to the URL
124 * @return array Returns the array of query parameter names and values to the given module action and idSite date and period.
125 */
126 protected function urlForActionWithDefaultUserParams($controllerAction, $additionalParams = array())
127 {
128 $module = $this->getModule();
129 return $this->urlForModuleActionWithDefaultUserParams($module, $controllerAction, $additionalParams);
130 }
131 /**
132 * Generates a URL to the given action of the given module, and it will also append some URL query parameters from the
133 * User preferences: idSite, period, date. If you do not need the parameters idSite, period and date to be generated
134 * use {@link urlForModuleAction()} instead.
135 *
136 * @param string $module The name of the module/plugin the action belongs to. The module name is case sensitive.
137 * @param string $controllerAction The name of the action that should be executed within your controller
138 * @param array $additionalParams Optional URL parameters that will be appended to the URL
139 * @return array|null Returns the array of query parameter names and values to the given module action and idSite date and period.
140 * Returns null if the module or action is invalid.
141 */
142 protected function urlForModuleActionWithDefaultUserParams($module, $controllerAction, $additionalParams = array())
143 {
144 $urlModuleAction = $this->urlForModuleAction($module, $controllerAction);
145 $date = Common::getRequestVar('date', \false);
146 if ($date) {
147 $urlModuleAction['date'] = $date;
148 }
149 $period = Common::getRequestVar('period', \false);
150 if ($period) {
151 $urlModuleAction['period'] = $period;
152 }
153 // We want the current query parameters to override the user's defaults
154 return array_merge($this->urlForDefaultUserParams(), $urlModuleAction, $additionalParams);
155 }
156 /**
157 * Builds a top-menu link that opens a reporting section (group) in the reporting single-page-app.
158 * The section is placed in the URL hash rather than the query string, so it does not leak into the
159 * other top-menu links (which are built from the current query string).
160 *
161 * @param array $params query parameters for the link (e.g. from urlForModuleActionWithDefaultUserParams)
162 * @param string $group reporting section id (empty string for the default section)
163 */
164 protected function urlForReportingSection(array $params, string $group) : string
165 {
166 $hashParams = array_merge(array_intersect_key($params, array_flip(['idSite', 'period', 'date'])), ['group' => $group]);
167 return 'index.php?' . Url::getQueryStringFromParameters($params) . '#?' . Url::getQueryStringFromParameters($hashParams);
168 }
169 /**
170 * Returns the &idSite=X&period=Y&date=Z query string fragment,
171 * fetched from current logged-in user's preferences.
172 *
173 * @param int|string|false $websiteId
174 * @param string|false $defaultPeriod
175 * @param string|false $defaultDate
176 * @return array{idSite: int|string, period: string, date: string}
177 * @throws \Exception in case a website was not specified and a default website id could not be found
178 */
179 public function urlForDefaultUserParams($websiteId = \false, $defaultPeriod = \false, $defaultDate = \false)
180 {
181 $userPreferences = new UserPreferences();
182 if (empty($websiteId)) {
183 $websiteId = $userPreferences->getDefaultWebsiteId();
184 }
185 if (empty($websiteId)) {
186 throw new \Exception("A website ID was not specified and a website to default to could not be found.");
187 }
188 if (empty($defaultPeriod)) {
189 $defaultPeriod = $userPreferences->getDefaultPeriod(\false);
190 }
191 if (empty($defaultDate)) {
192 $defaultDate = $userPreferences->getDefaultDate();
193 }
194 if ($defaultPeriod !== 'range' && !empty($defaultDate) && $defaultDate !== 'today') {
195 // not easy to make it work for range... is rarely the default anyway especially when just setting up
196 // Matomo as this logic is basically only applied on the first day a site is created
197 // no need to run logic when today is selected. It basically runs currently only when "yesterday" is selected
198 // as a default date but would also support future new default dates like past month etc.
199 try {
200 $siteCreationDate = Site::getCreationDateFor($websiteId);
201 $siteTimezone = Site::getTimezoneFor($websiteId);
202 if (!empty($siteCreationDate)) {
203 if (is_numeric($defaultDate)) {
204 $defaultDate = (int) $defaultDate;
205 //prevent possible exception should defaultDate be a string timestamp
206 }
207 $siteCreationDate = Date::factory($siteCreationDate, $siteTimezone);
208 $defaultDateObj = Date::factory($defaultDate, $siteTimezone);
209 $period = Period\Factory::build($defaultPeriod, $defaultDateObj);
210 $endDate = $period->getDateEnd();
211 if ($endDate->isEarlier($siteCreationDate)) {
212 // when selected date is before site creation date or it is the site creation day
213 $defaultDate = $siteCreationDate->toString();
214 }
215 }
216 } catch (\Exception $e) {
217 //ignore any error in case site was just deleted or the given date is not valid etc.
218 }
219 }
220 return array('idSite' => $websiteId, 'period' => $defaultPeriod, 'date' => $defaultDate);
221 }
222 /**
223 * Configures the top menu which is supposed to contain analytics related items such as the
224 * "All Websites Dashboard".
225 */
226 public function configureTopMenu(MenuTop $menu)
227 {
228 }
229 /**
230 * Configures the admin menu which is supposed to contain only administration related items such as
231 * "Websites", "Users" or "Settings".
232 */
233 public function configureAdminMenu(MenuAdmin $menu)
234 {
235 }
236 private function checkisValidCallable($module, $action)
237 {
238 if (!Development::isEnabled()) {
239 return;
240 }
241 $prefix = 'Menu item added in ' . get_class($this) . ' will fail when being selected. ';
242 if (!is_string($action)) {
243 Development::error($prefix . 'No valid action is specified. Make sure the defined action that should be executed is a string.');
244 }
245 $reportAction = lcfirst(substr($action, 4));
246 if (\Piwik\Plugin\ReportsProvider::factory($module, $reportAction)) {
247 return;
248 }
249 $controllerClass = '\\Piwik\\Plugins\\' . $module . '\\Controller';
250 if (!Development::methodExists($controllerClass, $action)) {
251 Development::error($prefix . 'The defined action "' . $action . '" does not exist in ' . $controllerClass . '". Make sure to define such a method.');
252 }
253 if (!Development::isCallableMethod($controllerClass, $action)) {
254 Development::error($prefix . 'The defined action "' . $action . '" is not callable on "' . $controllerClass . '". Make sure the method is public.');
255 }
256 }
257 }
258