PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
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 1 month ago Dimension 1 month ago API.php 6 months ago AggregatedMetric.php 2 years ago ArchivedMetric.php 1 year ago Archiver.php 1 month ago Categories.php 2 years ago ComponentFactory.php 3 months ago ComputedMetric.php 1 year ago ConsoleCommand.php 1 month ago Controller.php 1 month ago ControllerAdmin.php 2 weeks ago Dependency.php 1 month ago LogTablesProvider.php 2 years ago Manager.php 1 month ago Menu.php 1 month ago MetadataLoader.php 1 month ago Metric.php 1 month ago PluginException.php 1 year ago ProcessedMetric.php 3 months ago ReleaseChannels.php 3 months ago Report.php 1 month ago ReportsProvider.php 2 years ago RequestProcessors.php 4 months ago Segment.php 3 months ago SettingsProvider.php 1 month ago Tasks.php 1 month ago ThemeStyles.php 2 weeks ago ViewDataTable.php 3 months ago Visualization.php 1 year ago WidgetsProvider.php 3 months ago
Menu.php
244 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 protected function urlForActionWithDefaultUserParams($controllerAction, $additionalParams = array())
126 {
127 $module = $this->getModule();
128 return $this->urlForModuleActionWithDefaultUserParams($module, $controllerAction, $additionalParams);
129 }
130 /**
131 * Generates a URL to the given action of the given module, and it will also append some URL query parameters from the
132 * User preferences: idSite, period, date. If you do not need the parameters idSite, period and date to be generated
133 * use {@link urlForModuleAction()} instead.
134 *
135 * @param string $module The name of the module/plugin the action belongs to. The module name is case sensitive.
136 * @param string $controllerAction The name of the action that should be executed within your controller
137 * @param array $additionalParams Optional URL parameters that will be appended to the URL
138 * @return array|null Returns the array of query parameter names and values to the given module action and idSite date and period.
139 * Returns null if the module or action is invalid.
140 */
141 protected function urlForModuleActionWithDefaultUserParams($module, $controllerAction, $additionalParams = array())
142 {
143 $urlModuleAction = $this->urlForModuleAction($module, $controllerAction);
144 $date = Common::getRequestVar('date', \false);
145 if ($date) {
146 $urlModuleAction['date'] = $date;
147 }
148 $period = Common::getRequestVar('period', \false);
149 if ($period) {
150 $urlModuleAction['period'] = $period;
151 }
152 // We want the current query parameters to override the user's defaults
153 return array_merge($this->urlForDefaultUserParams(), $urlModuleAction, $additionalParams);
154 }
155 /**
156 * Returns the &idSite=X&period=Y&date=Z query string fragment,
157 * fetched from current logged-in user's preferences.
158 *
159 * @param int|string|false $websiteId
160 * @param string|false $defaultPeriod
161 * @param string|false $defaultDate
162 * @return array{idSite: int|string, period: string, date: string}
163 * @throws \Exception in case a website was not specified and a default website id could not be found
164 */
165 public function urlForDefaultUserParams($websiteId = \false, $defaultPeriod = \false, $defaultDate = \false)
166 {
167 $userPreferences = new UserPreferences();
168 if (empty($websiteId)) {
169 $websiteId = $userPreferences->getDefaultWebsiteId();
170 }
171 if (empty($websiteId)) {
172 throw new \Exception("A website ID was not specified and a website to default to could not be found.");
173 }
174 if (empty($defaultPeriod)) {
175 $defaultPeriod = $userPreferences->getDefaultPeriod(\false);
176 }
177 if (empty($defaultDate)) {
178 $defaultDate = $userPreferences->getDefaultDate();
179 }
180 if ($defaultPeriod !== 'range' && !empty($defaultDate) && $defaultDate !== 'today') {
181 // not easy to make it work for range... is rarely the default anyway especially when just setting up
182 // Matomo as this logic is basically only applied on the first day a site is created
183 // no need to run logic when today is selected. It basically runs currently only when "yesterday" is selected
184 // as a default date but would also support future new default dates like past month etc.
185 try {
186 $siteCreationDate = Site::getCreationDateFor($websiteId);
187 $siteTimezone = Site::getTimezoneFor($websiteId);
188 if (!empty($siteCreationDate)) {
189 if (is_numeric($defaultDate)) {
190 $defaultDate = (int) $defaultDate;
191 //prevent possible exception should defaultDate be a string timestamp
192 }
193 $siteCreationDate = Date::factory($siteCreationDate, $siteTimezone);
194 $defaultDateObj = Date::factory($defaultDate, $siteTimezone);
195 $period = Period\Factory::build($defaultPeriod, $defaultDateObj);
196 $endDate = $period->getDateEnd();
197 if ($endDate->isEarlier($siteCreationDate)) {
198 // when selected date is before site creation date or it is the site creation day
199 $defaultDate = $siteCreationDate->toString();
200 }
201 }
202 } catch (\Exception $e) {
203 //ignore any error in case site was just deleted or the given date is not valid etc.
204 }
205 }
206 return array('idSite' => $websiteId, 'period' => $defaultPeriod, 'date' => $defaultDate);
207 }
208 /**
209 * Configures the top menu which is supposed to contain analytics related items such as the
210 * "All Websites Dashboard".
211 */
212 public function configureTopMenu(MenuTop $menu)
213 {
214 }
215 /**
216 * Configures the admin menu which is supposed to contain only administration related items such as
217 * "Websites", "Users" or "Settings".
218 */
219 public function configureAdminMenu(MenuAdmin $menu)
220 {
221 }
222 private function checkisValidCallable($module, $action)
223 {
224 if (!Development::isEnabled()) {
225 return;
226 }
227 $prefix = 'Menu item added in ' . get_class($this) . ' will fail when being selected. ';
228 if (!is_string($action)) {
229 Development::error($prefix . 'No valid action is specified. Make sure the defined action that should be executed is a string.');
230 }
231 $reportAction = lcfirst(substr($action, 4));
232 if (\Piwik\Plugin\ReportsProvider::factory($module, $reportAction)) {
233 return;
234 }
235 $controllerClass = '\\Piwik\\Plugins\\' . $module . '\\Controller';
236 if (!Development::methodExists($controllerClass, $action)) {
237 Development::error($prefix . 'The defined action "' . $action . '" does not exist in ' . $controllerClass . '". Make sure to define such a method.');
238 }
239 if (!Development::isCallableMethod($controllerClass, $action)) {
240 Development::error($prefix . 'The defined action "' . $action . '" is not callable on "' . $controllerClass . '". Make sure the method is public.');
241 }
242 }
243 }
244