PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.13.3
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.13.3
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 / classes / WpMatomo / Admin / Menu.php
matomo / classes / WpMatomo / Admin Last commit date
TrackingSettings 4 years ago views 3 years ago AccessSettings.php 4 years ago Admin.php 4 years ago AdminSettings.php 4 years ago AdminSettingsInterface.php 6 years ago AdvancedSettings.php 4 years ago Chart.php 4 years ago CookieConsent.php 4 years ago Dashboard.php 4 years ago ExclusionSettings.php 4 years ago GeolocationSettings.php 4 years ago GetStarted.php 4 years ago ImportWpStatistics.php 4 years ago Info.php 4 years ago InvalidIpException.php 4 years ago Marketplace.php 4 years ago Menu.php 4 years ago PrivacySettings.php 4 years ago SafeModeMenu.php 4 years ago Summary.php 4 years ago SystemReport.php 3 years ago TrackingSettings.php 4 years ago
Menu.php
392 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 * @package matomo
8 */
9
10 namespace WpMatomo\Admin;
11
12 use Piwik\Plugins\UsersManager\UserPreferences;
13 use WpMatomo\Bootstrap;
14 use WpMatomo\Capabilities;
15 use WpMatomo\Report\Dates;
16 use WpMatomo\Settings;
17 use WpMatomo\Site;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit; // if accessed directly
21 }
22
23 class Menu {
24 /**
25 * @var Settings
26 */
27 private $settings;
28
29 public static $parent_slug = 'matomo';
30
31 const REPORTING_GOTO_ADMIN = 'matomo-admin';
32 const REPORTING_GOTO_GDPR_TOOLS = 'matomo-gdpr-tools';
33 const REPORTING_GOTO_GDPR_OVERVIEW = 'matomo-gdpr-overview';
34 const REPORTING_GOTO_ASK_CONSENT = 'matomo-gdpr-consent';
35 const REPORTING_GOTO_OPTOUT = 'matomo-privacy-optout';
36 const REPORTING_GOTO_ANONYMIZE_DATA = 'matomo-anonymize-date';
37 const REPORTING_GOTO_DATA_RETENTION = 'matomo-data-retention';
38 const SLUG_SYSTEM_REPORT = 'matomo-systemreport';
39 const SLUG_REPORT_SUMMARY = 'matomo-summary';
40 const SLUG_TAGMANAGER = 'matomo-tagmanager';
41 const SLUG_REPORTING = 'matomo-reporting';
42 const SLUG_SETTINGS = 'matomo-settings';
43 const SLUG_GET_STARTED = 'matomo-get-started';
44 const SLUG_ABOUT = 'matomo-about';
45 const SLUG_MARKETPLACE = 'matomo-marketplace';
46 const SLUG_IMPORTWPS = 'matomo-importwps';
47
48 const CAP_NOT_EXISTS = 'unknownfoobar';
49
50 /**
51 * @param Settings $settings
52 */
53 public function __construct( $settings ) {
54 $this->settings = $settings;
55 // Hook for adding admin menus
56 add_action( 'admin_menu', [ $this, 'add_menu' ] );
57 add_action( 'network_admin_menu', [ $this, 'add_menu' ] );
58 add_action( 'admin_head', [ $this, 'menu_external_icons' ] );
59
60 // as we are redirecting we need to perform the redirect as soon as possible before WP has eg echoed the header
61 add_action( 'load-matomo-analytics_page_' . self::SLUG_REPORTING, [ $this, 'reporting' ] );
62 add_action( 'load-' . self::$parent_slug . '_page_' . self::SLUG_REPORTING, [ $this, 'reporting' ] );
63 add_action( 'load-matomo-analytics_page_' . self::SLUG_TAGMANAGER, [ $this, 'tagmanager' ] );
64 add_action( 'load-' . self::$parent_slug . '_page_' . self::SLUG_TAGMANAGER, [ $this, 'tagmanager' ] );
65 }
66
67 public function add_menu() {
68 $info = new Info();
69 $get_started = new GetStarted( $this->settings );
70 $marketplace = new Marketplace( $this->settings );
71 $system_report = new SystemReport( $this->settings );
72 $summary = new Summary( $this->settings );
73 $import_wp_s = new ImportWpStatistics();
74
75 $admin_settings = new AdminSettings( $this->settings );
76
77 add_menu_page( 'Matomo Analytics', 'Matomo Analytics', self::CAP_NOT_EXISTS, 'matomo', null, 'dashicons-analytics' );
78
79 if ( $this->settings->get_global_option( Settings::SHOW_GET_STARTED_PAGE ) && $get_started->can_user_manage() ) {
80 if ( ! is_multisite() || ! is_network_admin() ) {
81 add_submenu_page(
82 self::$parent_slug,
83 __( 'Get Started', 'matomo' ),
84 __( 'Get Started', 'matomo' ),
85 Capabilities::KEY_SUPERUSER,
86 self::SLUG_GET_STARTED,
87 [
88 $get_started,
89 'show',
90 ]
91 );
92 }
93 }
94
95 if ( is_network_admin() ) {
96 add_submenu_page(
97 self::$parent_slug,
98 __( 'Multi Site', 'matomo' ),
99 __( 'Multi Site', 'matomo' ),
100 Capabilities::KEY_SUPERUSER,
101 'matomo-multisite',
102 [
103 $info,
104 'show_multisite',
105 ]
106 );
107 } else {
108 add_submenu_page(
109 self::$parent_slug,
110 __( 'Summary', 'matomo' ),
111 __( 'Summary', 'matomo' ),
112 Capabilities::KEY_VIEW,
113 self::SLUG_REPORT_SUMMARY,
114 [
115 $summary,
116 'show',
117 ]
118 );
119
120 // the network itself is not a blog
121 add_submenu_page(
122 self::$parent_slug,
123 __( 'Reporting', 'matomo' ),
124 __( 'Reporting', 'matomo' ),
125 Capabilities::KEY_VIEW,
126 self::SLUG_REPORTING,
127 [
128 $this,
129 'reporting',
130 ]
131 );
132 // the network itself is not a blog
133 if ( matomo_has_tag_manager() ) {
134 add_submenu_page(
135 self::$parent_slug,
136 __( 'Tag Manager', 'matomo' ),
137 __( 'Tag Manager', 'matomo' ),
138 Capabilities::KEY_WRITE,
139 self::SLUG_TAGMANAGER,
140 [
141 $this,
142 'tagmanager',
143 ]
144 );
145 }
146 }
147
148 // we always show settings except when multi site is used, plugin is not network enabled, and we are in network admin
149 $can_matomo_be_managed = ( ! is_multisite() || $this->settings->is_network_enabled() || ! is_network_admin() );
150
151 if ( $can_matomo_be_managed ) {
152 add_submenu_page(
153 self::$parent_slug,
154 __( 'Settings', 'matomo' ),
155 __( 'Settings', 'matomo' ),
156 Capabilities::KEY_SUPERUSER,
157 self::SLUG_SETTINGS,
158 [
159 $admin_settings,
160 'show',
161 ]
162 );
163 }
164
165 if ( ! is_plugin_active( MATOMO_MARKETPLACE_PLUGIN_NAME ) ) {
166 add_submenu_page(
167 self::$parent_slug,
168 __( 'Marketplace', 'matomo' ),
169 __( 'Marketplace', 'matomo' ),
170 Capabilities::KEY_VIEW,
171 self::SLUG_MARKETPLACE,
172 [
173 $marketplace,
174 'show',
175 ]
176 );
177 }
178
179 if ( $this->settings->is_network_enabled() || ! is_network_admin() ) {
180 add_submenu_page(
181 self::$parent_slug,
182 __( 'Diagnostics', 'matomo' ),
183 __( 'Diagnostics', 'matomo' ),
184 Capabilities::KEY_SUPERUSER,
185 self::SLUG_SYSTEM_REPORT,
186 [
187 $system_report,
188 'show',
189 ]
190 );
191 }
192
193 if ( is_plugin_active( 'wp-statistics/wp-statistics.php' ) ) {
194 add_submenu_page(
195 self::$parent_slug,
196 __( 'Import WP Statistics', 'matomo' ),
197 __( 'Import WP Statistics', 'matomo' ),
198 Capabilities::KEY_SUPERUSER,
199 self::SLUG_IMPORTWPS,
200 [
201 $import_wp_s,
202 'show',
203 ]
204 );
205 }
206 add_submenu_page(
207 self::$parent_slug,
208 __( 'About', 'matomo' ),
209 __( 'About', 'matomo' ),
210 Capabilities::KEY_VIEW,
211 self::SLUG_ABOUT,
212 [
213 $info,
214 'show',
215 ]
216 );
217 }
218
219 public function menu_external_icons() {
220 global $submenu;
221
222 if ( isset( $submenu[ self::$parent_slug ] ) ) {
223 $reporting = __( 'Reporting', 'matomo' );
224 $tagmanager = __( 'Tag Manager', 'matomo' );
225 foreach ( $submenu[ self::$parent_slug ] as $key => $menu_item ) {
226 if ( 0 === strpos( $menu_item[0], $reporting ) || 0 === strpos( $menu_item[0], $tagmanager ) ) {
227 // No other choice
228 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
229 $submenu[ self::$parent_slug ][ $key ][0] .= ' <span class="dashicons-before dashicons-external"></span>';
230 }
231 }
232 }
233 }
234
235 public static function get_matomo_goto_url( $goto ) {
236 return add_query_arg( [ 'goto' => $goto ], menu_page_url( self::SLUG_REPORTING, false ) );
237 }
238
239 public static function get_reporting_url() {
240 return plugins_url( 'app', MATOMO_ANALYTICS_FILE ) . '/index.php';
241 }
242
243 public function tagmanager() {
244 if ( matomo_has_tag_manager() ) {
245 $this->go_to_matomo_page( 'TagManager', 'manageContainers', Capabilities::KEY_WRITE );
246 }
247 exit;
248 }
249
250 public function reporting() {
251 if ( ! empty( $_GET['goto'] ) ) {
252 switch ( sanitize_text_field( wp_unslash( $_GET['goto'] ) ) ) {
253 case self::REPORTING_GOTO_ADMIN:
254 $this->go_to_matomo_page( 'CoreAdminHome', 'home', Capabilities::KEY_SUPERUSER );
255 break;
256 case self::REPORTING_GOTO_GDPR_TOOLS:
257 $this->go_to_matomo_page( 'PrivacyManager', 'gdprTools', Capabilities::KEY_SUPERUSER );
258 break;
259 case self::REPORTING_GOTO_GDPR_OVERVIEW:
260 $this->go_to_matomo_page( 'PrivacyManager', 'gdprOverview', Capabilities::KEY_SUPERUSER );
261 break;
262 case self::REPORTING_GOTO_ASK_CONSENT:
263 $this->go_to_matomo_page( 'PrivacyManager', 'consent', Capabilities::KEY_SUPERUSER );
264 break;
265 case self::REPORTING_GOTO_OPTOUT:
266 $this->go_to_matomo_page( 'PrivacyManager', 'usersOptOut', Capabilities::KEY_SUPERUSER );
267 break;
268 case self::REPORTING_GOTO_ANONYMIZE_DATA:
269 $this->go_to_matomo_page( 'PrivacyManager', 'privacySettings', Capabilities::KEY_SUPERUSER );
270 break;
271 case self::REPORTING_GOTO_DATA_RETENTION:
272 $this->go_to_matomo_page( 'CoreAdminHome', 'generalSettings', Capabilities::KEY_SUPERUSER );
273 break;
274 }
275 }
276
277 $url = self::get_reporting_url();
278
279 $site = new Site();
280 $idsite = $site->get_current_matomo_site_id();
281
282 if ( $idsite ) {
283 $url = add_query_arg( [ 'idSite' => (int) $idsite ], $url );
284 }
285
286 if ( ! empty( $_GET['report_date'] ) ) {
287 $report_date = sanitize_text_field( wp_unslash( $_GET['report_date'] ) );
288 $url = add_query_arg(
289 [
290 'module' => 'CoreHome',
291 'action' => 'index',
292 ],
293 $url
294 );
295
296 $date = new Dates();
297 list( $period, $date ) = $date->detect_period_and_date( $report_date );
298 $url = add_query_arg(
299 [
300 'period' => $period,
301 'date' => $date,
302 ],
303 $url
304 );
305 }
306
307 wp_safe_redirect( $url );
308 exit;
309 }
310
311 /**
312 * @api
313 */
314 public static function get_matomo_reporting_url( $category, $subcategory, $params = [] ) {
315 $site = new Site();
316 $idsite = $site->get_current_matomo_site_id();
317
318 if ( ! $idsite ) {
319 return;
320 }
321
322 $idsite = (int) $idsite;
323 $params['category'] = $category;
324 $params['subcategory'] = $subcategory;
325 $params['idSite'] = $idsite;
326
327 if ( empty( $params['period'] ) ) {
328 $params['period'] = 'day';
329 }
330 if ( empty( $params['date'] ) ) {
331 $params['date'] = 'today';
332 }
333
334 $url = self::make_matomo_app_base_url();
335 $url .= '?module=CoreHome&action=index&idSite=' . (int) $idsite . '&period=' . rawurlencode( $params['period'] ) . '&date=' . rawurlencode( $params['date'] ) . '#?&' . http_build_query( $params );
336
337 return $url;
338 }
339
340 private static function make_matomo_app_base_url() {
341 $url = plugins_url( 'app', MATOMO_ANALYTICS_FILE );
342
343 return $url . '/index.php';
344 }
345
346 /**
347 * @api
348 */
349 public static function get_matomo_action_url( $module, $action, $params = [] ) {
350 $site = new Site();
351 $idsite = $site->get_current_matomo_site_id();
352
353 if ( ! $idsite ) {
354 return;
355 }
356
357 $idsite = (int) $idsite;
358 $params['module'] = $module;
359 $params['action'] = $action;
360 $params['idSite'] = $idsite;
361
362 if ( empty( $params['period'] ) ) {
363 $params['period'] = 'day';
364 }
365 if ( empty( $params['date'] ) ) {
366 $params['date'] = 'today';
367 }
368
369 $url = self::make_matomo_app_base_url() . '?' . http_build_query( $params );
370
371 return $url;
372 }
373
374 public function go_to_matomo_page( $module, $action, $cap ) {
375 if ( ! current_user_can( $cap ) ) {
376 return;
377 }
378 Bootstrap::do_bootstrap();
379
380 $user_preferences = new UserPreferences();
381 $website_id = $user_preferences->getDefaultWebsiteId();
382 $default_date = $user_preferences->getDefaultDate();
383 $default_period = $user_preferences->getDefaultPeriod( false );
384
385 $url = self::make_matomo_app_base_url();
386 $url .= '?idSite=' . (int) $website_id . '&period=' . rawurlencode( $default_period ) . '&date=' . rawurlencode( $default_date );
387 $url .= '&module=' . rawurlencode( $module ) . '&action=' . rawurlencode( $action );
388 wp_safe_redirect( $url );
389 exit;
390 }
391 }
392