Admin
4 hours ago
Commands
2 years ago
Db
4 hours ago
Ecommerce
1 month ago
Report
3 months ago
Site
3 months ago
TrackingCode
4 months ago
Updater
4 years ago
User
4 hours ago
Workarounds
2 years ago
WpStatistics
6 months ago
views
2 months ago
AIBotTracking.php
4 months ago
API.php
4 hours ago
Access.php
4 years ago
AjaxTracker.php
4 months ago
Annotations.php
3 months ago
Bootstrap.php
10 months ago
Capabilities.php
1 month ago
Compatibility.php
3 months ago
Email.php
2 years ago
ErrorNotice.php
2 months ago
Feature.php
3 months ago
Installer.php
2 months ago
Logger.php
1 year ago
MinimumRequirementsNotice.php
1 month ago
OptOut.php
1 month ago
Paths.php
6 months ago
PluginActionLinks.php
3 months ago
PluginAdminOverrides.php
3 months ago
PluginInit.php
3 months ago
PrivacyBadge.php
4 years ago
RedirectOnActivation.php
3 months ago
Referral.php
3 months ago
Roles.php
3 months ago
ScheduledTasks.php
3 months ago
Settings.php
4 months ago
Site.php
3 years ago
TrackingCode.php
3 months ago
Uninstaller.php
6 months ago
Updater.php
10 months ago
User.php
4 hours ago
User.php
150 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; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // if accessed directly |
| 14 | } |
| 15 | |
| 16 | class User { |
| 17 | const USER_MAPPING_PREFIX = 'matomo-user-login-'; |
| 18 | |
| 19 | /** |
| 20 | * @var Settings |
| 21 | */ |
| 22 | private $settings; |
| 23 | |
| 24 | /** |
| 25 | * @api |
| 26 | */ |
| 27 | public function get_current_matomo_user_login() { |
| 28 | if ( get_current_user_id() ) { |
| 29 | return self::get_matomo_user_login( get_current_user_id() ); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public static function get_matomo_user_login( $wp_user_id ) { |
| 34 | return get_option( self::USER_MAPPING_PREFIX . $wp_user_id ); |
| 35 | } |
| 36 | |
| 37 | public static function map_matomo_user_login( $wp_user_id, $matomo_user_login ) { |
| 38 | if ( empty( $matomo_user_login ) ) { |
| 39 | delete_option( self::USER_MAPPING_PREFIX . $wp_user_id ); |
| 40 | } else { |
| 41 | update_option( self::USER_MAPPING_PREFIX . $wp_user_id, $matomo_user_login ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param string $matomo_user_login |
| 47 | * @return int[] |
| 48 | */ |
| 49 | public function get_wp_user_ids_for_matomo_login( $matomo_user_login ) { |
| 50 | global $wpdb; |
| 51 | |
| 52 | if ( empty( $matomo_user_login ) ) { |
| 53 | return []; |
| 54 | } |
| 55 | |
| 56 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching |
| 57 | $option_names = $wpdb->get_col( |
| 58 | $wpdb->prepare( |
| 59 | "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s AND option_value = %s", |
| 60 | $wpdb->esc_like( self::USER_MAPPING_PREFIX ) . '%', |
| 61 | $matomo_user_login |
| 62 | ) |
| 63 | ); |
| 64 | |
| 65 | $wp_user_ids = []; |
| 66 | foreach ( $option_names as $option_name ) { |
| 67 | $wp_user_id = (int) substr( $option_name, strlen( self::USER_MAPPING_PREFIX ) ); |
| 68 | if ( $wp_user_id ) { |
| 69 | $wp_user_ids[] = $wp_user_id; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return array_unique( $wp_user_ids ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param string $matomo_user_login |
| 78 | */ |
| 79 | public function delete_mappings_for_matomo_login( $matomo_user_login ) { |
| 80 | if ( empty( $matomo_user_login ) ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // on network-enabled multisite WP, there is one Matomo per network, so |
| 85 | // iterating over all blogs is not needed. |
| 86 | if ( ! $this->get_settings()->is_network_enabled() ) { |
| 87 | $this->delete_mappings_for_matomo_login_on_current_blog( $matomo_user_login ); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | global $wpdb; |
| 92 | |
| 93 | // the same Matomo login can be mapped from several blogs, so the mapping needs to be deleted |
| 94 | // for every blog it has been mapped within |
| 95 | $cache_key = 'blog_ids_for_mapping_cleanup'; |
| 96 | $cache_group = 'matomo'; |
| 97 | |
| 98 | $blogs = wp_cache_get( $cache_key, $cache_group ); |
| 99 | if ( false === $blogs ) { |
| 100 | // short lived cache so a sync deleting many users does not re-run this once per user; |
| 101 | // a slightly stale blog list is harmless here (deleted blogs are skipped below). |
| 102 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching |
| 103 | $blogs = $wpdb->get_results( 'SELECT blog_id, deleted FROM ' . $wpdb->blogs . ' ORDER BY blog_id', ARRAY_A ); |
| 104 | |
| 105 | if ( is_array( $blogs ) ) { |
| 106 | wp_cache_set( $cache_key, $blogs, $cache_group, 5 * MINUTE_IN_SECONDS ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if ( ! is_array( $blogs ) ) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | foreach ( $blogs as $blog ) { |
| 115 | if ( 1 === (int) $blog['deleted'] ) { |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | switch_to_blog( $blog['blog_id'] ); |
| 120 | try { |
| 121 | $this->delete_mappings_for_matomo_login_on_current_blog( $matomo_user_login ); |
| 122 | } finally { |
| 123 | restore_current_blog(); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param string $matomo_user_login |
| 130 | */ |
| 131 | private function delete_mappings_for_matomo_login_on_current_blog( $matomo_user_login ) { |
| 132 | foreach ( $this->get_wp_user_ids_for_matomo_login( $matomo_user_login ) as $wp_user_id ) { |
| 133 | delete_option( self::USER_MAPPING_PREFIX . $wp_user_id ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | public function uninstall() { |
| 138 | Uninstaller::uninstall_options( self::USER_MAPPING_PREFIX ); |
| 139 | } |
| 140 | |
| 141 | private function get_settings() { |
| 142 | if ( ! empty( $this->settings ) ) { |
| 143 | return $this->settings; |
| 144 | } |
| 145 | |
| 146 | $this->settings = \WpMatomo::$settings ? \WpMatomo::$settings : new Settings(); |
| 147 | return $this->settings; |
| 148 | } |
| 149 | } |
| 150 |