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