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 / classes / WpMatomo / Admin / Menu.php
matomo / classes / WpMatomo / Admin Last commit date
PluginSuggestions 2 months ago TrackingSettings 2 months ago views 2 months ago AccessSettings.php 4 years ago AdBlockDetector.php 6 months ago Admin.php 2 months ago AdminSettings.php 2 months ago AdminSettingsInterface.php 6 years ago AdvancedSettings.php 10 months ago Chart.php 2 months ago CookieConsent.php 4 years ago Dashboard.php 2 months ago ExclusionSettings.php 6 months ago GeolocationSettings.php 2 years ago GetStarted.php 2 months ago ImportWpStatistics.php 2 months ago Info.php 2 months ago InvalidIpException.php 4 years ago Marketplace.php 2 months ago MarketplaceSetupWizard.php 2 months ago MarketplaceSetupWizardBody.php 3 months ago MatomoPage.php 2 months ago MatomoPageContent.php 2 months ago Menu.php 2 months ago PluginMeasurableSettings.php 2 years ago PrivacySettings.php 1 year ago SafeModeMenu.php 2 months ago Summary.php 2 months ago SystemReport.php 2 months ago TrackingSettings.php 4 months ago WhatsNewNotifications.php 2 months ago
Menu.php
405 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 do_action( 'matomo_before_add_menu' );
69
70 $info = new MatomoPage( new Info() );
71 $get_started = new MatomoPage( new GetStarted( $this->settings ) );
72 $marketplace = new MatomoPage( new Marketplace( $this->settings ) );
73 $summary = new MatomoPage( new Summary( $this->settings ) );
74 $import_wp_s = new MatomoPage( new ImportWpStatistics() );
75 $admin_settings = new MatomoPage( new AdminSettings( $this->settings ) );
76
77 $matomo_logo_url = plugins_url( 'assets/img/matomo-logo-light-grey.svg', MATOMO_ANALYTICS_FILE );
78
79 add_menu_page( 'Matomo Analytics', 'Matomo Analytics', self::CAP_NOT_EXISTS, 'matomo', null, $matomo_logo_url, 2 );
80
81 if ( $this->settings->get_global_option( Settings::SHOW_GET_STARTED_PAGE ) && $get_started->get_content()->can_user_manage() ) {
82 if ( ! is_multisite() || ! is_network_admin() ) {
83 add_submenu_page(
84 self::$parent_slug,
85 __( 'Get Started', 'matomo' ),
86 __( 'Get Started', 'matomo' ),
87 Capabilities::KEY_SUPERUSER,
88 self::SLUG_GET_STARTED,
89 [
90 $get_started,
91 'show',
92 ]
93 );
94 }
95 }
96
97 if ( is_network_admin() ) {
98 $info_multisite = new MatomoPage( new Info( true ), 'show_multisite' );
99
100 add_submenu_page(
101 self::$parent_slug,
102 __( 'Multi Site', 'matomo' ),
103 __( 'Multi Site', 'matomo' ),
104 Capabilities::KEY_SUPERUSER,
105 'matomo-multisite',
106 [
107 $info_multisite,
108 'show',
109 ]
110 );
111 } else {
112 add_submenu_page(
113 self::$parent_slug,
114 __( 'Summary', 'matomo' ),
115 __( 'Summary', 'matomo' ),
116 Capabilities::KEY_VIEW,
117 self::SLUG_REPORT_SUMMARY,
118 [
119 $summary,
120 'show',
121 ]
122 );
123
124 // the network itself is not a blog
125 add_submenu_page(
126 self::$parent_slug,
127 __( 'Reporting', 'matomo' ),
128 __( 'Reporting', 'matomo' ),
129 Capabilities::KEY_VIEW,
130 self::SLUG_REPORTING,
131 [
132 $this,
133 'reporting',
134 ]
135 );
136 // the network itself is not a blog
137 if ( matomo_has_tag_manager() ) {
138 add_submenu_page(
139 self::$parent_slug,
140 __( 'Tag Manager', 'matomo' ),
141 __( 'Tag Manager', 'matomo' ),
142 Capabilities::KEY_WRITE,
143 self::SLUG_TAGMANAGER,
144 [
145 $this,
146 'tagmanager',
147 ]
148 );
149 }
150 }
151
152 // we always show settings except when multi site is used, plugin is not network enabled, and we are in network admin
153 $can_matomo_be_managed = ( ! is_multisite() || $this->settings->is_network_enabled() || ! is_network_admin() );
154
155 if ( $can_matomo_be_managed ) {
156 add_submenu_page(
157 self::$parent_slug,
158 __( 'Settings', 'matomo' ),
159 __( 'Settings', 'matomo' ),
160 Capabilities::KEY_SUPERUSER,
161 self::SLUG_SETTINGS,
162 [
163 $admin_settings,
164 'show',
165 ]
166 );
167 }
168
169 if ( ! is_plugin_active( MATOMO_MARKETPLACE_PLUGIN_NAME ) ) {
170 add_submenu_page(
171 self::$parent_slug,
172 __( 'Marketplace', 'matomo' ),
173 __( 'Marketplace', 'matomo' ),
174 Capabilities::KEY_VIEW,
175 self::SLUG_MARKETPLACE,
176 [
177 $marketplace,
178 'show',
179 ]
180 );
181 }
182
183 if ( $this->settings->is_network_enabled() || ! is_network_admin() ) {
184 $system_report = new MatomoPage( new SystemReport( $this->settings ) );
185
186 $warning = '';
187 if ( Admin::is_matomo_admin() ) {
188 if ( ! get_user_meta( get_current_user_id(), \WpMatomo\ErrorNotice::OPTION_NAME_SYSTEM_REPORT_ERRORS_DISMISSED ) && $system_report->get_content()->errors_present() ) {
189 $warning = '<span class="awaiting-mod">!</span>';
190 }
191 }
192
193 add_submenu_page(
194 self::$parent_slug,
195 __( 'Diagnostics', 'matomo' ),
196 __( 'Diagnostics', 'matomo' ) . $warning,
197 Capabilities::KEY_SUPERUSER,
198 self::SLUG_SYSTEM_REPORT,
199 [
200 $system_report,
201 'show',
202 ]
203 );
204 }
205
206 if ( is_plugin_active( 'wp-statistics/wp-statistics.php' ) ) {
207 add_submenu_page(
208 self::$parent_slug,
209 __( 'Import WP Statistics', 'matomo' ),
210 __( 'Import WP Statistics', 'matomo' ),
211 Capabilities::KEY_SUPERUSER,
212 self::SLUG_IMPORTWPS,
213 [
214 $import_wp_s,
215 'show',
216 ]
217 );
218 }
219 add_submenu_page(
220 self::$parent_slug,
221 __( 'Help', 'matomo' ),
222 __( 'Help', 'matomo' ),
223 Capabilities::KEY_VIEW,
224 self::SLUG_ABOUT,
225 [
226 $info,
227 'show',
228 ]
229 );
230 }
231
232 public function menu_external_icons() {
233 global $submenu;
234
235 if ( isset( $submenu[ self::$parent_slug ] ) ) {
236 $reporting = __( 'Reporting', 'matomo' );
237 $tagmanager = __( 'Tag Manager', 'matomo' );
238 foreach ( $submenu[ self::$parent_slug ] as $key => $menu_item ) {
239 if ( 0 === strpos( $menu_item[0], $reporting ) || 0 === strpos( $menu_item[0], $tagmanager ) ) {
240 // No other choice
241 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
242 $submenu[ self::$parent_slug ][ $key ][0] .= ' <span class="dashicons-before dashicons-external"></span>';
243 }
244 }
245 }
246 }
247
248 public static function get_matomo_goto_url( $goto ) {
249 return add_query_arg( [ 'goto' => $goto ], menu_page_url( self::SLUG_REPORTING, false ) );
250 }
251
252 public static function get_reporting_url() {
253 return plugins_url( 'app', MATOMO_ANALYTICS_FILE ) . '/index.php';
254 }
255
256 public function tagmanager() {
257 if ( matomo_has_tag_manager() ) {
258 $this->go_to_matomo_page( 'TagManager', 'manageContainers', Capabilities::KEY_WRITE );
259 }
260 exit;
261 }
262
263 public function reporting() {
264 if ( ! empty( $_GET['goto'] ) ) {
265 switch ( sanitize_text_field( wp_unslash( $_GET['goto'] ) ) ) {
266 case self::REPORTING_GOTO_ADMIN:
267 $this->go_to_matomo_page( 'CoreAdminHome', 'home', Capabilities::KEY_SUPERUSER );
268 break;
269 case self::REPORTING_GOTO_GDPR_TOOLS:
270 $this->go_to_matomo_page( 'PrivacyManager', 'gdprTools', Capabilities::KEY_SUPERUSER );
271 break;
272 case self::REPORTING_GOTO_GDPR_OVERVIEW:
273 $this->go_to_matomo_page( 'PrivacyManager', 'gdprOverview', Capabilities::KEY_SUPERUSER );
274 break;
275 case self::REPORTING_GOTO_ASK_CONSENT:
276 $this->go_to_matomo_page( 'PrivacyManager', 'consent', Capabilities::KEY_SUPERUSER );
277 break;
278 case self::REPORTING_GOTO_OPTOUT:
279 $this->go_to_matomo_page( 'PrivacyManager', 'usersOptOut', Capabilities::KEY_SUPERUSER );
280 break;
281 case self::REPORTING_GOTO_ANONYMIZE_DATA:
282 $this->go_to_matomo_page( 'PrivacyManager', 'privacySettings', Capabilities::KEY_SUPERUSER );
283 break;
284 case self::REPORTING_GOTO_DATA_RETENTION:
285 $this->go_to_matomo_page( 'CoreAdminHome', 'generalSettings', Capabilities::KEY_SUPERUSER );
286 break;
287 }
288 }
289
290 $url = self::get_reporting_url();
291
292 $site = new Site();
293 $idsite = $site->get_current_matomo_site_id();
294
295 if ( $idsite ) {
296 $url = add_query_arg( [ 'idSite' => (int) $idsite ], $url );
297 }
298
299 if ( ! empty( $_GET['report_date'] ) ) {
300 $report_date = sanitize_text_field( wp_unslash( $_GET['report_date'] ) );
301 $url = add_query_arg(
302 [
303 'module' => 'CoreHome',
304 'action' => 'index',
305 ],
306 $url
307 );
308
309 $date = new Dates();
310 list( $period, $date ) = $date->detect_period_and_date( $report_date );
311 $url = add_query_arg(
312 [
313 'period' => $period,
314 'date' => $date,
315 ],
316 $url
317 );
318 }
319
320 wp_safe_redirect( $url );
321 exit;
322 }
323
324 /**
325 * @api
326 */
327 public static function get_matomo_reporting_url( $category, $subcategory, $params = [] ) {
328 $site = new Site();
329 $idsite = $site->get_current_matomo_site_id();
330
331 if ( ! $idsite ) {
332 return;
333 }
334
335 $idsite = (int) $idsite;
336 $params['category'] = $category;
337 $params['subcategory'] = $subcategory;
338 $params['idSite'] = $idsite;
339
340 if ( empty( $params['period'] ) ) {
341 $params['period'] = 'day';
342 }
343 if ( empty( $params['date'] ) ) {
344 $params['date'] = 'today';
345 }
346
347 $url = self::make_matomo_app_base_url();
348 $url .= '?module=CoreHome&action=index&idSite=' . (int) $idsite . '&period=' . rawurlencode( $params['period'] ) . '&date=' . rawurlencode( $params['date'] ) . '#?&' . http_build_query( $params );
349
350 return $url;
351 }
352
353 private static function make_matomo_app_base_url() {
354 $url = plugins_url( 'app', MATOMO_ANALYTICS_FILE );
355
356 return $url . '/index.php';
357 }
358
359 /**
360 * @api
361 */
362 public static function get_matomo_action_url( $module, $action, $params = [] ) {
363 $site = new Site();
364 $idsite = $site->get_current_matomo_site_id();
365
366 if ( ! $idsite ) {
367 return;
368 }
369
370 $idsite = (int) $idsite;
371 $params['module'] = $module;
372 $params['action'] = $action;
373 $params['idSite'] = $idsite;
374
375 if ( empty( $params['period'] ) ) {
376 $params['period'] = 'day';
377 }
378 if ( empty( $params['date'] ) ) {
379 $params['date'] = 'today';
380 }
381
382 $url = self::make_matomo_app_base_url() . '?' . http_build_query( $params );
383
384 return $url;
385 }
386
387 public function go_to_matomo_page( $module, $action, $cap ) {
388 if ( ! current_user_can( $cap ) ) {
389 return;
390 }
391 Bootstrap::do_bootstrap();
392
393 $user_preferences = new UserPreferences();
394 $website_id = $user_preferences->getDefaultWebsiteId();
395 $default_date = $user_preferences->getDefaultDate();
396 $default_period = $user_preferences->getDefaultPeriod( false );
397
398 $url = self::make_matomo_app_base_url();
399 $url .= '?idSite=' . (int) $website_id . '&period=' . rawurlencode( $default_period ) . '&date=' . rawurlencode( $default_date );
400 $url .= '&module=' . rawurlencode( $module ) . '&action=' . rawurlencode( $action );
401 wp_safe_redirect( $url );
402 exit;
403 }
404 }
405