|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Adds support to for site owners to edit user accounts for users on their site. |
| 4 | + * |
| 5 | + * WordPress, even in multisite mode, has only one User database table. |
| 6 | + * This can cause problems in a WaaS environment. |
| 7 | + * |
| 8 | + * A site owner with administrator role wants to edit the display name of a subscriber. |
| 9 | + * In the default Multisite only super admins can edit user accounts. |
| 10 | + * This makes it possible for Admins to edit users in their own sites. |
| 11 | + * |
| 12 | + * @package WP_Ultimo |
| 13 | + * @subpackage Compat/Edit_Users_Compat |
| 14 | + * @since 2.4.4 |
| 15 | + */ |
| 16 | + |
| 17 | +namespace WP_Ultimo\Compat; |
| 18 | + |
| 19 | +class Edit_Users_Compat { |
| 20 | + |
| 21 | + use \WP_Ultimo\Traits\Singleton; |
| 22 | + |
| 23 | + public function init(): void { |
| 24 | + // Add the settings to enable or disable this feature. |
| 25 | + add_action('wu_settings_login', [$this, 'add_settings'], 10); |
| 26 | + |
| 27 | + if ($this->should_load()) { |
| 28 | + // Apply the update_users_caps function to the 'map_meta_cap' filter. |
| 29 | + add_filter('map_meta_cap', [$this, 'update_users_caps'], 1, 4); |
| 30 | + |
| 31 | + // Add a filter to enable editing any user configuration. |
| 32 | + add_filter('enable_edit_any_user_configuration', '__return_true', 15); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Allows subsite administrators to edit users in a WordPress Multisite network. |
| 38 | + * |
| 39 | + * In a WordPress 3.x Network, the Super Admin role is the only role allowed to edit users. |
| 40 | + * |
| 41 | + * @param array $caps The user's capabilities. |
| 42 | + * @param string $cap The capability being checked. |
| 43 | + * @param int $user_id The user ID. |
| 44 | + * @param mixed $args Additional arguments. |
| 45 | + * |
| 46 | + * @return array Modified user capabilities. |
| 47 | + */ |
| 48 | + function update_users_caps($caps, $cap, $user_id, $args) { |
| 49 | + foreach ($caps as $key => $capability) { |
| 50 | + if ('do_not_allow' !== $capability) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + switch ( $cap ) { |
| 55 | + case 'edit_user': |
| 56 | + // Editing a user profile. |
| 57 | + if (empty($args[0]) || is_super_admin($args[0])) { |
| 58 | + // Trying to edit a super admin while not being a super admin. |
| 59 | + $caps[] = 'do_not_allow'; |
| 60 | + } elseif ( ! is_user_member_of_blog($args[0], get_current_blog_id()) || ! is_user_member_of_blog($user_id, get_current_blog_id())) { |
| 61 | + // Editing user and edited user aren't members of the same blog. |
| 62 | + $caps[] = 'do_not_allow'; |
| 63 | + } else { |
| 64 | + $caps[ $key ] = 'edit_users'; |
| 65 | + } |
| 66 | + |
| 67 | + break; |
| 68 | + case 'edit_users': |
| 69 | + $caps[ $key ] = 'edit_users'; |
| 70 | + break; |
| 71 | + case 'delete_user': |
| 72 | + case 'delete_users': |
| 73 | + $caps[ $key ] = 'delete_users'; |
| 74 | + break; |
| 75 | + case 'create_users': |
| 76 | + $caps[ $key ] = $cap; |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return $caps; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Allow plugin developers to disable this functionality to prevent compatibility issues. |
| 86 | + * |
| 87 | + * @since 2.0.0 |
| 88 | + * |
| 89 | + * @return boolean |
| 90 | + */ |
| 91 | + public function should_load() { |
| 92 | + |
| 93 | + return apply_filters('wu_should_load_edit_user_support', wu_get_setting('enable_edit_users', false)); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Add edit user setting to enable or disable this feature. |
| 98 | + * |
| 99 | + * @since 2.0.0 |
| 100 | + * |
| 101 | + * @return void. |
| 102 | + */ |
| 103 | + public function add_settings(): void { |
| 104 | + |
| 105 | + wu_register_settings_field( |
| 106 | + 'login-and-registration', |
| 107 | + 'enable_edit_users', |
| 108 | + [ |
| 109 | + 'title' => __('Enable Edit User Capability', 'multisite-ultimate'), |
| 110 | + 'desc' => __('Allow site owners to edit the user accounts of users on their own site.', 'multisite-ultimate'), |
| 111 | + 'type' => 'toggle', |
| 112 | + 'default' => 0, |
| 113 | + ] |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
0 commit comments