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