TrackingSettings
5 years ago
views
5 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
Dashboard.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
5 years ago
PrivacySettings.php
5 years ago
SafeModeMenu.php
6 years ago
Summary.php
5 years ago
SystemReport.php
5 years ago
TrackingSettings.php
5 years ago
Menu.php
377 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 | add_menu_page( 'Matomo Analytics', 'Matomo Analytics', self::CAP_NOT_EXISTS, 'matomo', null, 'dashicons-analytics' ); |
| 76 | |
| 77 | if ( $this->settings->get_global_option( Settings::SHOW_GET_STARTED_PAGE ) && $get_started->can_user_manage() ) { |
| 78 | if (!is_multisite() || !is_network_admin()) { |
| 79 | add_submenu_page( |
| 80 | self::$parent_slug, |
| 81 | __( 'Get Started', 'matomo' ), |
| 82 | __( 'Get Started', 'matomo' ), |
| 83 | Capabilities::KEY_SUPERUSER, |
| 84 | self::SLUG_GET_STARTED, |
| 85 | array( |
| 86 | $get_started, |
| 87 | 'show', |
| 88 | ) |
| 89 | ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if ( is_network_admin() ) { |
| 94 | add_submenu_page( |
| 95 | self::$parent_slug, |
| 96 | __( 'Multi Site', 'matomo' ), |
| 97 | __( 'Multi Site', 'matomo' ), |
| 98 | Capabilities::KEY_SUPERUSER, |
| 99 | 'matomo-multisite', |
| 100 | array( |
| 101 | $info, |
| 102 | 'show_multisite', |
| 103 | ) |
| 104 | ); |
| 105 | } else { |
| 106 | add_submenu_page( |
| 107 | self::$parent_slug, |
| 108 | __( 'Summary', 'matomo' ), |
| 109 | __( 'Summary', 'matomo' ), |
| 110 | Capabilities::KEY_VIEW, |
| 111 | self::SLUG_REPORT_SUMMARY, |
| 112 | array( |
| 113 | $summary, |
| 114 | 'show', |
| 115 | ) |
| 116 | ); |
| 117 | |
| 118 | // the network itself is not a blog |
| 119 | add_submenu_page( |
| 120 | self::$parent_slug, |
| 121 | __( 'Reporting', 'matomo' ), |
| 122 | __( 'Reporting', 'matomo' ), |
| 123 | Capabilities::KEY_VIEW, |
| 124 | self::SLUG_REPORTING, |
| 125 | array( |
| 126 | $this, |
| 127 | 'reporting', |
| 128 | ) |
| 129 | ); |
| 130 | // the network itself is not a blog |
| 131 | if ( matomo_has_tag_manager() ) { |
| 132 | add_submenu_page( |
| 133 | self::$parent_slug, |
| 134 | __( 'Tag Manager', 'matomo' ), |
| 135 | __( 'Tag Manager', 'matomo' ), |
| 136 | Capabilities::KEY_WRITE, |
| 137 | self::SLUG_TAGMANAGER, |
| 138 | array( |
| 139 | $this, |
| 140 | 'tagmanager', |
| 141 | ) |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | } |
| 146 | |
| 147 | // we always show settings except when multi site is used, plugin is not network enabled, and we are in network admin |
| 148 | $can_matomo_be_managed = ( !is_multisite() || $this->settings->is_network_enabled() || !is_network_admin() ); |
| 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 ( $this->settings->is_network_enabled() || ! is_network_admin() ) { |
| 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 | $site = new Site(); |
| 264 | $idsite = $site->get_current_matomo_site_id(); |
| 265 | |
| 266 | if ( $idsite ) { |
| 267 | $url = add_query_arg( array( 'idSite' => (int) $idsite ), $url ); |
| 268 | } |
| 269 | |
| 270 | if ( ! empty( $_GET['report_date'] ) ) { |
| 271 | $url = add_query_arg( |
| 272 | array( |
| 273 | 'module' => 'CoreHome', |
| 274 | 'action' => 'index', |
| 275 | ), |
| 276 | $url |
| 277 | ); |
| 278 | |
| 279 | |
| 280 | $date = new Dates(); |
| 281 | list( $period, $date ) = $date->detect_period_and_date( $_GET['report_date'] ); |
| 282 | $url = add_query_arg( |
| 283 | array( |
| 284 | 'period' => $period, |
| 285 | 'date' => $date, |
| 286 | ), |
| 287 | $url |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | wp_safe_redirect( $url ); |
| 292 | exit; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * @api |
| 297 | */ |
| 298 | public static function get_matomo_reporting_url( $category, $subcategory, $params = array() ) { |
| 299 | $site = new Site(); |
| 300 | $idsite = $site->get_current_matomo_site_id(); |
| 301 | |
| 302 | if ( ! $idsite ) { |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | $idsite = (int) $idsite; |
| 307 | $params['category'] = $category; |
| 308 | $params['subcategory'] = $subcategory; |
| 309 | $params['idSite'] = $idsite; |
| 310 | |
| 311 | if ( empty( $params['period'] ) ) { |
| 312 | $params['period'] = 'day'; |
| 313 | } |
| 314 | if ( empty( $params['date'] ) ) { |
| 315 | $params['date'] = 'today'; |
| 316 | } |
| 317 | |
| 318 | $url = self::make_matomo_app_base_url(); |
| 319 | $url .= '?module=CoreHome&action=index&idSite=' . (int) $idsite . '&period=' . rawurlencode( $params['period'] ) . '&date=' . rawurlencode( $params['date'] ) . '#?&' . http_build_query( $params ); |
| 320 | |
| 321 | return $url; |
| 322 | } |
| 323 | |
| 324 | private static function make_matomo_app_base_url() { |
| 325 | $url = plugins_url( 'app', MATOMO_ANALYTICS_FILE ); |
| 326 | |
| 327 | return $url . '/index.php'; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * @api |
| 332 | */ |
| 333 | public static function get_matomo_action_url( $module, $action, $params = array() ) { |
| 334 | $site = new Site(); |
| 335 | $idsite = $site->get_current_matomo_site_id(); |
| 336 | |
| 337 | if ( ! $idsite ) { |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | $idsite = (int) $idsite; |
| 342 | $params['module'] = $module; |
| 343 | $params['action'] = $action; |
| 344 | $params['idSite'] = $idsite; |
| 345 | |
| 346 | if ( empty( $params['period'] ) ) { |
| 347 | $params['period'] = 'day'; |
| 348 | } |
| 349 | if ( empty( $params['date'] ) ) { |
| 350 | $params['date'] = 'today'; |
| 351 | } |
| 352 | |
| 353 | $url = self::make_matomo_app_base_url() . '?' . http_build_query( $params ); |
| 354 | |
| 355 | return $url; |
| 356 | } |
| 357 | |
| 358 | public function go_to_matomo_page( $module, $action, $cap ) { |
| 359 | if ( ! current_user_can( $cap ) ) { |
| 360 | return; |
| 361 | } |
| 362 | Bootstrap::do_bootstrap(); |
| 363 | |
| 364 | $user_preferences = new UserPreferences(); |
| 365 | $website_id = $user_preferences->getDefaultWebsiteId(); |
| 366 | $default_date = $user_preferences->getDefaultDate(); |
| 367 | $default_period = $user_preferences->getDefaultPeriod( false ); |
| 368 | |
| 369 | $url = self::make_matomo_app_base_url(); |
| 370 | $url .= '?idSite=' . (int) $website_id . '&period=' . rawurlencode( $default_period ) . '&date=' . rawurlencode( $default_date ); |
| 371 | $url .= '&module=' . rawurlencode( $module ) . '&action=' . rawurlencode( $action ); |
| 372 | wp_safe_redirect( $url ); |
| 373 | exit; |
| 374 | } |
| 375 | |
| 376 | } |
| 377 |