PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
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 / User.php
matomo / classes / WpMatomo Last commit date
Admin 1 week ago Commands 2 years ago Db 3 weeks ago Ecommerce 1 week ago Report 1 week ago Site 1 week ago TrackingCode 1 week ago Updater 4 years ago User 1 week ago Workarounds 2 years ago WpStatistics 1 week ago views 1 week ago AIBotTracking.php 1 week ago API.php 3 weeks ago Access.php 1 week ago AjaxTracker.php 5 months ago Annotations.php 1 week ago Bootstrap.php 11 months ago Capabilities.php 1 week ago Compatibility.php 1 week ago Email.php 1 week ago ErrorNotice.php 1 week ago Feature.php 3 months ago Installer.php 1 week ago Logger.php 1 year ago MinimumRequirementsNotice.php 1 month ago OptOut.php 2 months ago Paths.php 1 week ago PluginActionLinks.php 3 months ago PluginAdminOverrides.php 1 week ago PluginInit.php 1 week ago PrivacyBadge.php 4 years ago RedirectOnActivation.php 3 months ago Referral.php 3 months ago Roles.php 3 months ago ScheduledTasks.php 1 week ago Settings.php 1 week ago Site.php 3 years ago TrackingCode.php 1 week ago Uninstaller.php 6 months ago Updater.php 1 week ago User.php 3 weeks 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