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