PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
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 6 years ago API.php 6 years ago AggregatedMetric.php 6 years ago ArchivedMetric.php 6 years ago Archiver.php 6 years ago Categories.php 6 years ago ComponentFactory.php 6 years ago ComputedMetric.php 6 years ago ConsoleCommand.php 6 years ago Controller.php 6 years ago ControllerAdmin.php 6 years ago Dependency.php 6 years ago LogTablesProvider.php 6 years ago Manager.php 6 years ago Menu.php 6 years ago MetadataLoader.php 6 years ago Metric.php 6 years ago PluginException.php 6 years ago ProcessedMetric.php 6 years ago ReleaseChannels.php 6 years ago Report.php 6 years ago ReportsProvider.php 6 years ago RequestProcessors.php 6 years ago Segment.php 6 years ago SettingsProvider.php 6 years ago Tasks.php 6 years ago ThemeStyles.php 6 years ago ViewDataTable.php 6 years ago Visualization.php 6 years ago WidgetsProvider.php 6 years ago
Menu.php
254 lines
1 <?php
2 /**
3 * Piwik - 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\Development;
13 use Piwik\Menu\MenuAdmin;
14 use Piwik\Menu\MenuTop;
15 use Piwik\Plugin\Manager as PluginManager;
16 use Piwik\Plugins\UsersManager\UserPreferences;
17
18 /**
19 * Base class of all plugin menu providers. Plugins that define their own menu items can extend this class to easily
20 * add new items, to remove or to rename existing items.
21 *
22 * Descendants of this class can overwrite any of these methods. Each method will be executed only once per request
23 * and cached for any further menu requests.
24 *
25 * For an example, see the {@link https://github.com/piwik/piwik/blob/master/plugins/ExampleUI/Menu.php} plugin.
26 *
27 * @api
28 * @since 2.4.0
29 */
30 class Menu
31 {
32 public function __construct()
33 {
34 // Constructor kept for BC (because called in implementations)
35 }
36
37 private function getModule()
38 {
39 $className = get_class($this);
40 $className = explode('\\', $className);
41
42 return $className[2];
43 }
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
68 return $params;
69 }
70
71 /**
72 * Generates a URL for the given action. In your plugin controller you have to create a method with the same name
73 * as this method will be executed when a user clicks on the menu item. If you want to generate a URL for the
74 * action of another module, meaning not your plugin, you should use the method {@link urlForModuleAction()}.
75 *
76 * @param string $controllerAction The name of the action that should be executed within your controller
77 * @param array $additionalParams Optional URL parameters that will be appended to the URL
78 * @return array
79 *
80 * @since 2.7.0
81 * @api
82 */
83 protected function urlForAction($controllerAction, $additionalParams = array())
84 {
85 $module = $this->getModule();
86 $this->checkisValidCallable($module, $controllerAction);
87
88 $params = (array) $additionalParams;
89 $params['action'] = $controllerAction;
90 $params['module'] = $module;
91
92 return $params;
93 }
94
95 /**
96 * Generates a URL for the given action of the given module. We usually do not recommend to use this method as you
97 * should make sure the method of that module actually exists. If the plugin owner of that module changes the method
98 * in a future version your link might no longer work. If you want to link to an action of your controller use the
99 * method {@link urlForAction()}. Note: We will generate a link only if the given module is installed and activated.
100 *
101 * @param string $module The name of the module/plugin the action belongs to. The module name is case sensitive.
102 * @param string $controllerAction The name of the action that should be executed within your controller
103 * @param array $additionalParams Optional URL parameters that will be appended to the URL
104 * @return array|null Returns null if the given module is either not installed or not activated. Returns the array
105 * of query parameter names and values to the given module action otherwise.
106 *
107 * @since 2.7.0
108 * // not API for now
109 */
110 protected function urlForModuleAction($module, $controllerAction, $additionalParams = array())
111 {
112 $this->checkisValidCallable($module, $controllerAction);
113
114 $pluginManager = PluginManager::getInstance();
115
116 if (!$pluginManager->isPluginLoaded($module) ||
117 !$pluginManager->isPluginActivated($module)) {
118 return null;
119 }
120
121 $params = (array) $additionalParams;
122 $params['action'] = $controllerAction;
123 $params['module'] = $module;
124
125 return $params;
126 }
127
128 /**
129 * Generates a URL to the given action of the current module, and it will also append some URL query parameters from the
130 * User preferences: idSite, period, date. If you do not need the parameters idSite, period and date to be generated
131 * use {@link urlForAction()} instead.
132 *
133 * @param string $controllerAction The name of the action that should be executed within your controller
134 * @param array $additionalParams Optional URL parameters that will be appended to the URL
135 * @return array Returns the array of query parameter names and values to the given module action and idSite date and period.
136 *
137 */
138 protected function urlForActionWithDefaultUserParams($controllerAction, $additionalParams = array())
139 {
140 $module = $this->getModule();
141
142 return $this->urlForModuleActionWithDefaultUserParams($module, $controllerAction, $additionalParams);
143 }
144
145 /**
146 * Generates a URL to the given action of the given module, and it will also append some URL query parameters from the
147 * User preferences: idSite, period, date. If you do not need the parameters idSite, period and date to be generated
148 * use {@link urlForModuleAction()} instead.
149 *
150 * @param string $module The name of the module/plugin the action belongs to. The module name is case sensitive.
151 * @param string $controllerAction The name of the action that should be executed within your controller
152 * @param array $additionalParams Optional URL parameters that will be appended to the URL
153 * @return array|null Returns the array of query parameter names and values to the given module action and idSite date and period.
154 * Returns null if the module or action is invalid.
155 *
156 */
157 protected function urlForModuleActionWithDefaultUserParams($module, $controllerAction, $additionalParams = array())
158 {
159 $urlModuleAction = $this->urlForModuleAction($module, $controllerAction);
160
161 $date = Common::getRequestVar('date', false);
162 if ($date) {
163 $urlModuleAction['date'] = $date;
164 }
165 $period = Common::getRequestVar('period', false);
166 if ($period) {
167 $urlModuleAction['period'] = $period;
168 }
169
170 // We want the current query parameters to override the user's defaults
171 return array_merge(
172 $this->urlForDefaultUserParams(),
173 $urlModuleAction,
174 $additionalParams
175 );
176 }
177
178 /**
179 * Returns the &idSite=X&period=Y&date=Z query string fragment,
180 * fetched from current logged-in user's preferences.
181 *
182 * @param bool $websiteId
183 * @param bool $defaultPeriod
184 * @param bool $defaultDate
185 * @return array eg ['idSite' => 1, 'period' => 'day', 'date' => '2012-02-03']
186 * @throws \Exception in case a website was not specified and a default website id could not be found
187 */
188 public function urlForDefaultUserParams($websiteId = false, $defaultPeriod = false, $defaultDate = false)
189 {
190 $userPreferences = new UserPreferences();
191 if (empty($websiteId)) {
192 $websiteId = $userPreferences->getDefaultWebsiteId();
193 }
194 if (empty($websiteId)) {
195 throw new \Exception("A website ID was not specified and a website to default to could not be found.");
196 }
197 if (empty($defaultDate)) {
198 $defaultDate = $userPreferences->getDefaultDate();
199 }
200 if (empty($defaultPeriod)) {
201 $defaultPeriod = $userPreferences->getDefaultPeriod(false);
202 }
203 return array(
204 'idSite' => $websiteId,
205 'period' => $defaultPeriod,
206 'date' => $defaultDate,
207 );
208 }
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 /**
219 * Configures the admin menu which is supposed to contain only administration related items such as
220 * "Websites", "Users" or "Settings".
221 */
222 public function configureAdminMenu(MenuAdmin $menu)
223 {
224 }
225
226 private function checkisValidCallable($module, $action)
227 {
228 if (!Development::isEnabled()) {
229 return;
230 }
231
232 $prefix = 'Menu item added in ' . get_class($this) . ' will fail when being selected. ';
233
234 if (!is_string($action)) {
235 Development::error($prefix . 'No valid action is specified. Make sure the defined action that should be executed is a string.');
236 }
237
238 $reportAction = lcfirst(substr($action, 4));
239 if (ReportsProvider::factory($module, $reportAction)) {
240 return;
241 }
242
243 $controllerClass = '\\Piwik\\Plugins\\' . $module . '\\Controller';
244
245 if (!Development::methodExists($controllerClass, $action)) {
246 Development::error($prefix . 'The defined action "' . $action . '" does not exist in ' . $controllerClass . '". Make sure to define such a method.');
247 }
248
249 if (!Development::isCallableMethod($controllerClass, $action)) {
250 Development::error($prefix . 'The defined action "' . $action . '" is not callable on "' . $controllerClass . '". Make sure the method is public.');
251 }
252 }
253 }
254