PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.0.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.0.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
Dimension 5 years ago API.php 5 years ago AggregatedMetric.php 5 years ago ArchivedMetric.php 5 years ago Archiver.php 5 years ago Categories.php 5 years ago ComponentFactory.php 5 years ago ComputedMetric.php 5 years ago ConsoleCommand.php 5 years ago Controller.php 5 years ago ControllerAdmin.php 5 years ago Dependency.php 5 years ago LogTablesProvider.php 5 years ago Manager.php 5 years ago Menu.php 5 years ago MetadataLoader.php 5 years ago Metric.php 5 years ago PluginException.php 5 years ago ProcessedMetric.php 5 years ago ReleaseChannels.php 5 years ago Report.php 5 years ago ReportsProvider.php 5 years ago RequestProcessors.php 5 years ago Segment.php 5 years ago SettingsProvider.php 5 years ago Tasks.php 5 years ago ThemeStyles.php 5 years ago ViewDataTable.php 5 years ago Visualization.php 5 years ago WidgetsProvider.php 5 years ago
Menu.php
286 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
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 /**
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
40 private function getModule()
41 {
42 $className = get_class($this);
43 $className = explode('\\', $className);
44
45 return $className[2];
46 }
47
48 /**
49 * Generates a URL for the default action of the plugin controller.
50 *
51 * Example:
52 * ```
53 * $menu->addItem('MyPlugin_MyPlugin', '', $this->urlForDefaultAction(), $orderId = 30);
54 * // will add a menu item that leads to the default action of the plugin controller when a user clicks on it.
55 * // The default action is usually the `index` action - meaning the `index()` method the controller -
56 * // but the default action can be customized within a controller
57 * ```
58 *
59 * @param array $additionalParams Optional URL parameters that will be appended to the URL
60 * @return array
61 *
62 * @since 2.7.0
63 * @api
64 */
65 protected function urlForDefaultAction($additionalParams = array())
66 {
67 $params = (array) $additionalParams;
68 $params['action'] = '';
69 $params['module'] = $this->getModule();
70
71 return $params;
72 }
73
74 /**
75 * Generates a URL for the given action. In your plugin controller you have to create a method with the same name
76 * as this method will be executed when a user clicks on the menu item. If you want to generate a URL for the
77 * action of another module, meaning not your plugin, you should use the method {@link urlForModuleAction()}.
78 *
79 * @param string $controllerAction The name of the action that should be executed within your controller
80 * @param array $additionalParams Optional URL parameters that will be appended to the URL
81 * @return array
82 *
83 * @since 2.7.0
84 * @api
85 */
86 protected function urlForAction($controllerAction, $additionalParams = array())
87 {
88 $module = $this->getModule();
89 $this->checkisValidCallable($module, $controllerAction);
90
91 $params = (array) $additionalParams;
92 $params['action'] = $controllerAction;
93 $params['module'] = $module;
94
95 return $params;
96 }
97
98 /**
99 * Generates a URL for the given action of the given module. We usually do not recommend to use this method as you
100 * should make sure the method of that module actually exists. If the plugin owner of that module changes the method
101 * in a future version your link might no longer work. If you want to link to an action of your controller use the
102 * method {@link urlForAction()}. Note: We will generate a link only if the given module is installed and activated.
103 *
104 * @param string $module The name of the module/plugin the action belongs to. The module name is case sensitive.
105 * @param string $controllerAction The name of the action that should be executed within your controller
106 * @param array $additionalParams Optional URL parameters that will be appended to the URL
107 * @return array|null Returns null if the given module is either not installed or not activated. Returns the array
108 * of query parameter names and values to the given module action otherwise.
109 *
110 * @since 2.7.0
111 * // not API for now
112 */
113 protected function urlForModuleAction($module, $controllerAction, $additionalParams = array())
114 {
115 $this->checkisValidCallable($module, $controllerAction);
116
117 $pluginManager = PluginManager::getInstance();
118
119 if (!$pluginManager->isPluginLoaded($module) ||
120 !$pluginManager->isPluginActivated($module)) {
121 return null;
122 }
123
124 $params = (array) $additionalParams;
125 $params['action'] = $controllerAction;
126 $params['module'] = $module;
127
128 return $params;
129 }
130
131 /**
132 * Generates a URL to the given action of the current 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 urlForAction()} instead.
135 *
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 Returns the array of query parameter names and values to the given module action and idSite date and period.
139 *
140 */
141 protected function urlForActionWithDefaultUserParams($controllerAction, $additionalParams = array())
142 {
143 $module = $this->getModule();
144
145 return $this->urlForModuleActionWithDefaultUserParams($module, $controllerAction, $additionalParams);
146 }
147
148 /**
149 * Generates a URL to the given action of the given module, and it will also append some URL query parameters from the
150 * User preferences: idSite, period, date. If you do not need the parameters idSite, period and date to be generated
151 * use {@link urlForModuleAction()} instead.
152 *
153 * @param string $module The name of the module/plugin the action belongs to. The module name is case sensitive.
154 * @param string $controllerAction The name of the action that should be executed within your controller
155 * @param array $additionalParams Optional URL parameters that will be appended to the URL
156 * @return array|null Returns the array of query parameter names and values to the given module action and idSite date and period.
157 * Returns null if the module or action is invalid.
158 *
159 */
160 protected function urlForModuleActionWithDefaultUserParams($module, $controllerAction, $additionalParams = array())
161 {
162 $urlModuleAction = $this->urlForModuleAction($module, $controllerAction);
163
164 $date = Common::getRequestVar('date', false);
165 if ($date) {
166 $urlModuleAction['date'] = $date;
167 }
168 $period = Common::getRequestVar('period', false);
169 if ($period) {
170 $urlModuleAction['period'] = $period;
171 }
172
173 // We want the current query parameters to override the user's defaults
174 return array_merge(
175 $this->urlForDefaultUserParams(),
176 $urlModuleAction,
177 $additionalParams
178 );
179 }
180
181 /**
182 * Returns the &idSite=X&period=Y&date=Z query string fragment,
183 * fetched from current logged-in user's preferences.
184 *
185 * @param bool $websiteId
186 * @param bool $defaultPeriod
187 * @param bool $defaultDate
188 * @return array eg ['idSite' => 1, 'period' => 'day', 'date' => '2012-02-03']
189 * @throws \Exception in case a website was not specified and a default website id could not be found
190 */
191 public function urlForDefaultUserParams($websiteId = false, $defaultPeriod = false, $defaultDate = false)
192 {
193 $userPreferences = new UserPreferences();
194 if (empty($websiteId)) {
195 $websiteId = $userPreferences->getDefaultWebsiteId();
196 }
197 if (empty($websiteId)) {
198 throw new \Exception("A website ID was not specified and a website to default to could not be found.");
199 }
200 if (empty($defaultPeriod)) {
201 $defaultPeriod = $userPreferences->getDefaultPeriod(false);
202 }
203 if (empty($defaultDate)) {
204 $defaultDate = $userPreferences->getDefaultDate();
205 }
206
207 if ($defaultPeriod !== 'range' && !empty($defaultDate) && $defaultDate !== 'today') {
208 // not easy to make it work for range... is rarely the default anyway especially when just setting up
209 // Matomo as this logic is basically only applied on the first day a site is created
210 // no need to run logic when today is selected. It basically runs currently only when "yesterday" is selected
211 // as a default date but would also support future new default dates like past month etc.
212 try {
213 $siteCreationDate = Site::getCreationDateFor($websiteId);
214 $siteTimezone = Site::getTimezoneFor($websiteId);
215
216 if (!empty($siteCreationDate)) {
217 if (is_numeric($defaultDate)) {
218 $defaultDate = (int) $defaultDate; //prevent possible exception should defaultDate be a string timestamp
219 }
220 $siteCreationDate = Date::factory($siteCreationDate, $siteTimezone);
221 $defaultDateObj = Date::factory($defaultDate, $siteTimezone);
222
223 $period = Period\Factory::build($defaultPeriod, $defaultDateObj);
224 $endDate = $period->getDateEnd();
225
226 if ($endDate->isEarlier($siteCreationDate)) {
227 // when selected date is before site creation date or it is the site creation day
228 $defaultDate = $siteCreationDate->toString();
229 }
230 }
231 } catch (\Exception $e) {
232 //ignore any error in case site was just deleted or the given date is not valid etc.
233 }
234 }
235 return array(
236 'idSite' => $websiteId,
237 'period' => $defaultPeriod,
238 'date' => $defaultDate,
239 );
240 }
241
242 /**
243 * Configures the top menu which is supposed to contain analytics related items such as the
244 * "All Websites Dashboard".
245 */
246 public function configureTopMenu(MenuTop $menu)
247 {
248 }
249
250 /**
251 * Configures the admin menu which is supposed to contain only administration related items such as
252 * "Websites", "Users" or "Settings".
253 */
254 public function configureAdminMenu(MenuAdmin $menu)
255 {
256 }
257
258 private function checkisValidCallable($module, $action)
259 {
260 if (!Development::isEnabled()) {
261 return;
262 }
263
264 $prefix = 'Menu item added in ' . get_class($this) . ' will fail when being selected. ';
265
266 if (!is_string($action)) {
267 Development::error($prefix . 'No valid action is specified. Make sure the defined action that should be executed is a string.');
268 }
269
270 $reportAction = lcfirst(substr($action, 4));
271 if (ReportsProvider::factory($module, $reportAction)) {
272 return;
273 }
274
275 $controllerClass = '\\Piwik\\Plugins\\' . $module . '\\Controller';
276
277 if (!Development::methodExists($controllerClass, $action)) {
278 Development::error($prefix . 'The defined action "' . $action . '" does not exist in ' . $controllerClass . '". Make sure to define such a method.');
279 }
280
281 if (!Development::isCallableMethod($controllerClass, $action)) {
282 Development::error($prefix . 'The defined action "' . $action . '" is not callable on "' . $controllerClass . '". Make sure the method is public.');
283 }
284 }
285 }
286