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 / Capabilities.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
Capabilities.php
188 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 use WP_Roles;
13 use WpMatomo\Admin\Menu;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // if accessed directly
17 }
18
19 class Capabilities extends Feature {
20
21 const KEY_NONE = 'none_matomo';
22
23 /**
24 * @api
25 */
26 const KEY_VIEW = 'view_matomo';
27
28 /**
29 * @api
30 */
31 const KEY_WRITE = 'write_matomo';
32
33 /**
34 * @api
35 */
36 const KEY_ADMIN = 'admin_matomo';
37
38 /**
39 * @api
40 */
41 const KEY_SUPERUSER = 'superuser_matomo';
42 const KEY_STEALTH = 'stealth_matomo';
43
44 /**
45 * @var Settings
46 */
47 private $settings;
48
49 public function __construct( $settings ) {
50 $this->settings = $settings;
51 }
52
53 public function register_hooks() {
54 add_action( 'wp_roles_init', [ $this, 'add_capabilities_to_roles' ] );
55 add_filter( 'user_has_cap', [ $this, 'add_capabilities_to_user' ], 10, 4 );
56 add_filter( 'map_meta_cap', [ $this, 'map_meta_cap' ], 10, 4 );
57 }
58
59 /**
60 * Tests only
61 *
62 * @internal
63 */
64 public function remove_hooks() {
65 remove_action( 'wp_roles_init', [ $this, 'add_capabilities_to_roles' ] );
66 remove_filter( 'user_has_cap', [ $this, 'add_capabilities_to_user' ], 10 );
67 remove_filter( 'map_meta_cap', [ $this, 'map_meta_cap' ], 10 );
68 }
69
70 public function map_meta_cap( $caps, $cap, $user_id, $args ) {
71 if ( self::KEY_STEALTH === $cap ) {
72 // in multisite prevent super admin from having their tracking being filtered
73 // a super admin is usually allowed all actions... unless we add do_not_allow
74 if ( is_multisite() && is_super_admin( $user_id ) ) {
75 $stealth = $this->settings->get_global_option( Settings::OPTION_KEY_STEALTH );
76 if ( ! empty( $stealth['administrator'] ) ) {
77 $caps[] = 'do_not_allow';
78 }
79 }
80 }
81
82 if ( Menu::CAP_NOT_EXISTS === $cap
83 && is_multisite()
84 && is_super_admin( $user_id ) ) {
85 $caps[] = 'do_not_allow'; // prevent matomo-analytics submenu to be shown
86 }
87
88 return $caps;
89 }
90
91 public function add_capabilities_to_user( $allcaps, $caps, $args, $user ) {
92 if ( isset( $caps[0] ) ) {
93 $cap_request = $caps[0];
94 switch ( $cap_request ) {
95 // ensure the Matomo capability inheritcance always works
96 case self::KEY_SUPERUSER:
97 if ( $this->has_super_user_capability( $allcaps, $user ) ) {
98 $allcaps[ $cap_request ] = true;
99 }
100 break;
101
102 case self::KEY_VIEW:
103 case self::KEY_WRITE:
104 case self::KEY_ADMIN:
105 if ( empty( $allcaps[ $cap_request ] ) ) {
106 // when user has the above permission we also make sure to add all capabilites below... eg
107 // when user has write... then we ensure the user also has the view capability
108 if ( $this->has_any_higher_permission( $cap_request, $allcaps )
109 || $this->has_super_user_capability( $allcaps, $user ) ) {
110 $allcaps[ $cap_request ] = true;
111 }
112 }
113
114 break;
115 }
116 }
117
118 return $allcaps;
119 }
120
121 private function has_super_user_capability( $allcaps, $user ) {
122 if ( is_multisite() && $this->settings->is_network_enabled() ) {
123 if ( is_super_admin( $user->ID ) ) {
124 // only network manager can be super user in this case
125 return true;
126 }
127 } elseif ( ! empty( $allcaps['administrator'] ) || ( is_multisite() && is_super_admin( $user->ID ) ) ) {
128 return true;
129 }
130
131 return false;
132 }
133
134 /**
135 * @param WP_Roles $roles
136 */
137 public function add_capabilities_to_roles( $roles ) {
138 $access = $this->settings->get_global_option( Settings::OPTION_KEY_CAPS_ACCESS );
139 $stealth = $this->settings->get_global_option( Settings::OPTION_KEY_STEALTH );
140
141 if ( ! empty( $access ) && is_array( $access ) ) {
142 foreach ( $access as $role_name => $cap ) {
143 $role = $roles->get_role( $role_name );
144 if ( $role ) {
145 $role->capabilities[ $cap ] = true;
146 }
147 }
148 }
149
150 if ( ! empty( $stealth ) && is_array( $stealth ) ) {
151 foreach ( $stealth as $role_name => $enabled ) {
152 $role = $roles->get_role( $role_name );
153 if ( $role && $enabled ) {
154 $role->capabilities[ self::KEY_STEALTH ] = true;
155 }
156 }
157 }
158 }
159
160 public function get_all_capabilities_sorted_by_highest_permission() {
161 return [
162 self::KEY_SUPERUSER,
163 self::KEY_ADMIN,
164 self::KEY_WRITE,
165 self::KEY_VIEW,
166 ];
167 }
168
169 protected function has_any_higher_permission( $cap_to_find, $allcaps ) {
170 $all_caps = $this->get_all_capabilities_sorted_by_highest_permission();
171 if ( ! in_array( $cap_to_find, $all_caps, true ) ) {
172 return false;
173 }
174
175 foreach ( $all_caps as $cap ) {
176 if ( array_key_exists( $cap, $allcaps ) && ! empty( $allcaps[ $cap ] ) ) {
177 // eg if user has super user... then we return right away...
178 return true;
179 }
180 if ( $cap === $cap_to_find ) {
181 return false;
182 }
183 }
184
185 return false;
186 }
187 }
188