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
3 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
400 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 | $warning = ''; |
| 181 | if ( isset( $_GET['page'] ) && substr( sanitize_text_field( wp_unslash( $_GET['page'] ) ), 0, 7 ) === 'matomo-' ) { |
| 182 | $system_report = new \WpMatomo\Admin\SystemReport( $this->settings ); |
| 183 | if ( $system_report->errors_present() ) { |
| 184 | $warning = '<span class="awaiting-mod">!</span>'; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | add_submenu_page( |
| 189 | self::$parent_slug, |
| 190 | __( 'Diagnostics', 'matomo' ), |
| 191 | __( 'Diagnostics', 'matomo' ) . $warning, |
| 192 | Capabilities::KEY_SUPERUSER, |
| 193 | self::SLUG_SYSTEM_REPORT, |
| 194 | [ |
| 195 | $system_report, |
| 196 | 'show', |
| 197 | ] |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | if ( is_plugin_active( 'wp-statistics/wp-statistics.php' ) ) { |
| 202 | add_submenu_page( |
| 203 | self::$parent_slug, |
| 204 | __( 'Import WP Statistics', 'matomo' ), |
| 205 | __( 'Import WP Statistics', 'matomo' ), |
| 206 | Capabilities::KEY_SUPERUSER, |
| 207 | self::SLUG_IMPORTWPS, |
| 208 | [ |
| 209 | $import_wp_s, |
| 210 | 'show', |
| 211 | ] |
| 212 | ); |
| 213 | } |
| 214 | add_submenu_page( |
| 215 | self::$parent_slug, |
| 216 | __( 'About', 'matomo' ), |
| 217 | __( 'About', 'matomo' ), |
| 218 | Capabilities::KEY_VIEW, |
| 219 | self::SLUG_ABOUT, |
| 220 | [ |
| 221 | $info, |
| 222 | 'show', |
| 223 | ] |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | public function menu_external_icons() { |
| 228 | global $submenu; |
| 229 | |
| 230 | if ( isset( $submenu[ self::$parent_slug ] ) ) { |
| 231 | $reporting = __( 'Reporting', 'matomo' ); |
| 232 | $tagmanager = __( 'Tag Manager', 'matomo' ); |
| 233 | foreach ( $submenu[ self::$parent_slug ] as $key => $menu_item ) { |
| 234 | if ( 0 === strpos( $menu_item[0], $reporting ) || 0 === strpos( $menu_item[0], $tagmanager ) ) { |
| 235 | // No other choice |
| 236 | // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 237 | $submenu[ self::$parent_slug ][ $key ][0] .= ' <span class="dashicons-before dashicons-external"></span>'; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | public static function get_matomo_goto_url( $goto ) { |
| 244 | return add_query_arg( [ 'goto' => $goto ], menu_page_url( self::SLUG_REPORTING, false ) ); |
| 245 | } |
| 246 | |
| 247 | public static function get_reporting_url() { |
| 248 | return plugins_url( 'app', MATOMO_ANALYTICS_FILE ) . '/index.php'; |
| 249 | } |
| 250 | |
| 251 | public function tagmanager() { |
| 252 | if ( matomo_has_tag_manager() ) { |
| 253 | $this->go_to_matomo_page( 'TagManager', 'manageContainers', Capabilities::KEY_WRITE ); |
| 254 | } |
| 255 | exit; |
| 256 | } |
| 257 | |
| 258 | public function reporting() { |
| 259 | if ( ! empty( $_GET['goto'] ) ) { |
| 260 | switch ( sanitize_text_field( wp_unslash( $_GET['goto'] ) ) ) { |
| 261 | case self::REPORTING_GOTO_ADMIN: |
| 262 | $this->go_to_matomo_page( 'CoreAdminHome', 'home', Capabilities::KEY_SUPERUSER ); |
| 263 | break; |
| 264 | case self::REPORTING_GOTO_GDPR_TOOLS: |
| 265 | $this->go_to_matomo_page( 'PrivacyManager', 'gdprTools', Capabilities::KEY_SUPERUSER ); |
| 266 | break; |
| 267 | case self::REPORTING_GOTO_GDPR_OVERVIEW: |
| 268 | $this->go_to_matomo_page( 'PrivacyManager', 'gdprOverview', Capabilities::KEY_SUPERUSER ); |
| 269 | break; |
| 270 | case self::REPORTING_GOTO_ASK_CONSENT: |
| 271 | $this->go_to_matomo_page( 'PrivacyManager', 'consent', Capabilities::KEY_SUPERUSER ); |
| 272 | break; |
| 273 | case self::REPORTING_GOTO_OPTOUT: |
| 274 | $this->go_to_matomo_page( 'PrivacyManager', 'usersOptOut', Capabilities::KEY_SUPERUSER ); |
| 275 | break; |
| 276 | case self::REPORTING_GOTO_ANONYMIZE_DATA: |
| 277 | $this->go_to_matomo_page( 'PrivacyManager', 'privacySettings', Capabilities::KEY_SUPERUSER ); |
| 278 | break; |
| 279 | case self::REPORTING_GOTO_DATA_RETENTION: |
| 280 | $this->go_to_matomo_page( 'CoreAdminHome', 'generalSettings', Capabilities::KEY_SUPERUSER ); |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | $url = self::get_reporting_url(); |
| 286 | |
| 287 | $site = new Site(); |
| 288 | $idsite = $site->get_current_matomo_site_id(); |
| 289 | |
| 290 | if ( $idsite ) { |
| 291 | $url = add_query_arg( [ 'idSite' => (int) $idsite ], $url ); |
| 292 | } |
| 293 | |
| 294 | if ( ! empty( $_GET['report_date'] ) ) { |
| 295 | $report_date = sanitize_text_field( wp_unslash( $_GET['report_date'] ) ); |
| 296 | $url = add_query_arg( |
| 297 | [ |
| 298 | 'module' => 'CoreHome', |
| 299 | 'action' => 'index', |
| 300 | ], |
| 301 | $url |
| 302 | ); |
| 303 | |
| 304 | $date = new Dates(); |
| 305 | list( $period, $date ) = $date->detect_period_and_date( $report_date ); |
| 306 | $url = add_query_arg( |
| 307 | [ |
| 308 | 'period' => $period, |
| 309 | 'date' => $date, |
| 310 | ], |
| 311 | $url |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | wp_safe_redirect( $url ); |
| 316 | exit; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * @api |
| 321 | */ |
| 322 | public static function get_matomo_reporting_url( $category, $subcategory, $params = [] ) { |
| 323 | $site = new Site(); |
| 324 | $idsite = $site->get_current_matomo_site_id(); |
| 325 | |
| 326 | if ( ! $idsite ) { |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | $idsite = (int) $idsite; |
| 331 | $params['category'] = $category; |
| 332 | $params['subcategory'] = $subcategory; |
| 333 | $params['idSite'] = $idsite; |
| 334 | |
| 335 | if ( empty( $params['period'] ) ) { |
| 336 | $params['period'] = 'day'; |
| 337 | } |
| 338 | if ( empty( $params['date'] ) ) { |
| 339 | $params['date'] = 'today'; |
| 340 | } |
| 341 | |
| 342 | $url = self::make_matomo_app_base_url(); |
| 343 | $url .= '?module=CoreHome&action=index&idSite=' . (int) $idsite . '&period=' . rawurlencode( $params['period'] ) . '&date=' . rawurlencode( $params['date'] ) . '#?&' . http_build_query( $params ); |
| 344 | |
| 345 | return $url; |
| 346 | } |
| 347 | |
| 348 | private static function make_matomo_app_base_url() { |
| 349 | $url = plugins_url( 'app', MATOMO_ANALYTICS_FILE ); |
| 350 | |
| 351 | return $url . '/index.php'; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * @api |
| 356 | */ |
| 357 | public static function get_matomo_action_url( $module, $action, $params = [] ) { |
| 358 | $site = new Site(); |
| 359 | $idsite = $site->get_current_matomo_site_id(); |
| 360 | |
| 361 | if ( ! $idsite ) { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | $idsite = (int) $idsite; |
| 366 | $params['module'] = $module; |
| 367 | $params['action'] = $action; |
| 368 | $params['idSite'] = $idsite; |
| 369 | |
| 370 | if ( empty( $params['period'] ) ) { |
| 371 | $params['period'] = 'day'; |
| 372 | } |
| 373 | if ( empty( $params['date'] ) ) { |
| 374 | $params['date'] = 'today'; |
| 375 | } |
| 376 | |
| 377 | $url = self::make_matomo_app_base_url() . '?' . http_build_query( $params ); |
| 378 | |
| 379 | return $url; |
| 380 | } |
| 381 | |
| 382 | public function go_to_matomo_page( $module, $action, $cap ) { |
| 383 | if ( ! current_user_can( $cap ) ) { |
| 384 | return; |
| 385 | } |
| 386 | Bootstrap::do_bootstrap(); |
| 387 | |
| 388 | $user_preferences = new UserPreferences(); |
| 389 | $website_id = $user_preferences->getDefaultWebsiteId(); |
| 390 | $default_date = $user_preferences->getDefaultDate(); |
| 391 | $default_period = $user_preferences->getDefaultPeriod( false ); |
| 392 | |
| 393 | $url = self::make_matomo_app_base_url(); |
| 394 | $url .= '?idSite=' . (int) $website_id . '&period=' . rawurlencode( $default_period ) . '&date=' . rawurlencode( $default_date ); |
| 395 | $url .= '&module=' . rawurlencode( $module ) . '&action=' . rawurlencode( $action ); |
| 396 | wp_safe_redirect( $url ); |
| 397 | exit; |
| 398 | } |
| 399 | } |
| 400 |