ExtendedCapabilities.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Additional capabilities service |
| 12 | * |
| 13 | * Add custom capabilities support that enhance AAM functionality |
| 14 | * |
| 15 | * @since 6.4.3 Fixed https://github.com/aamplugin/advanced-access-manager/issues/93 |
| 16 | * @since 6.1.0 Fixed the bug where aam_show_toolbar was not taken in consideration |
| 17 | * due to incorrect placement |
| 18 | * @since 6.0.0 Initial implementation of the class |
| 19 | * |
| 20 | * @package AAM |
| 21 | * @version 6.4.3 |
| 22 | */ |
| 23 | class AAM_Service_ExtendedCapabilities |
| 24 | { |
| 25 | use AAM_Core_Contract_ServiceTrait, |
| 26 | AAM_Core_Contract_RequestTrait; |
| 27 | |
| 28 | /** |
| 29 | * AAM configuration setting that is associated with the service |
| 30 | * |
| 31 | * @version 6.0.0 |
| 32 | */ |
| 33 | const FEATURE_FLAG = 'core.service.additional-caps.enabled'; |
| 34 | |
| 35 | /** |
| 36 | * Constructor |
| 37 | * |
| 38 | * @access protected |
| 39 | * |
| 40 | * @return void |
| 41 | * @version 6.0.0 |
| 42 | */ |
| 43 | protected function __construct() |
| 44 | { |
| 45 | if (is_admin()) { |
| 46 | // Hook that returns the detailed information about the nature of the |
| 47 | // service. This is used to display information about service on the |
| 48 | // Settings->Services tab |
| 49 | add_filter('aam_service_list_filter', function ($services) { |
| 50 | $services[] = array( |
| 51 | 'title' => __('Additional Caps', AAM_KEY), |
| 52 | 'description' => __('Extend the WordPress core collection of capabilities that allow more granular access control to the backend core features.', AAM_KEY), |
| 53 | 'setting' => self::FEATURE_FLAG |
| 54 | ); |
| 55 | |
| 56 | return $services; |
| 57 | }, 1); |
| 58 | } |
| 59 | |
| 60 | // Hook that initialize the AAM UI part of the service |
| 61 | if (AAM_Core_Config::get(self::FEATURE_FLAG, true)) { |
| 62 | $this->initializeHooks(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Initialize service hooks |
| 68 | * |
| 69 | * @return void |
| 70 | * |
| 71 | * @since 6.4.2 Fixed https://github.com/aamplugin/advanced-access-manager/issues/93 |
| 72 | * @since 6.1.0 Fixed the bug where aam_show_toolbar was not taken in |
| 73 | * consideration due to incorrect placement |
| 74 | * @since 6.0.0 Initial implementation of the method |
| 75 | * |
| 76 | * @access public |
| 77 | * @version 6.4.3 |
| 78 | */ |
| 79 | protected function initializeHooks() |
| 80 | { |
| 81 | if (is_admin()) { |
| 82 | // Control admin area |
| 83 | add_action('admin_notices', array($this, 'controlAdminNotifications'), -1); |
| 84 | add_action('network_admin_notices', array($this, 'controlAdminNotifications'), -1); |
| 85 | add_action('user_admin_notices', array($this, 'controlAdminNotifications'), -1); |
| 86 | |
| 87 | // Screen options & contextual help hooks |
| 88 | add_filter('screen_options_show_screen', array($this, 'screenOptions')); |
| 89 | add_action('in_admin_header', function() { |
| 90 | if (!AAM_Core_API::isAAMCapabilityAllowed('aam_show_help_tabs')) { |
| 91 | get_current_screen()->remove_help_tabs(); |
| 92 | } |
| 93 | }); |
| 94 | |
| 95 | // Permalink manager |
| 96 | add_filter('get_sample_permalink_html', function ($html) { |
| 97 | if (!AAM_Core_API::isAAMCapabilityAllowed('aam_edit_permalink')) { |
| 98 | $html = ''; |
| 99 | } |
| 100 | |
| 101 | return $html; |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | add_action('init', function() { |
| 106 | if (is_user_logged_in()) { |
| 107 | // Check if user is allowed to see backend |
| 108 | if ( |
| 109 | is_admin() |
| 110 | && !AAM_Core_API::isAAMCapabilityAllowed('aam_access_dashboard') |
| 111 | ) { |
| 112 | // If this is the AJAX call, still allow it because it will break a lot |
| 113 | // of frontend stuff that depends on it |
| 114 | if (!defined('DOING_AJAX')) { |
| 115 | wp_die(__('Access Denied', AAM_KEY), 'aam_access_denied'); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Check if we need to show admin bar for the current user |
| 120 | if (AAM_Core_API::isAAMCapabilityAllowed('aam_show_toolbar') === false) { |
| 121 | add_filter('show_admin_bar', '__return_false', PHP_INT_MAX); |
| 122 | } |
| 123 | } |
| 124 | }, 1); |
| 125 | |
| 126 | // Password reset feature |
| 127 | add_filter('show_password_fields', array($this, 'canChangePassword'), 10, 2); |
| 128 | add_action('check_passwords', array($this, 'canUpdatePassword'), 10, 3); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Manage notifications visibility |
| 133 | * |
| 134 | * @return void |
| 135 | * |
| 136 | * @access public |
| 137 | * @version 6.0.0 |
| 138 | */ |
| 139 | public function controlAdminNotifications() |
| 140 | { |
| 141 | if (!AAM_Core_API::isAAMCapabilityAllowed('aam_show_admin_notices')) { |
| 142 | remove_all_actions('admin_notices'); |
| 143 | remove_all_actions('network_admin_notices'); |
| 144 | remove_all_actions('user_admin_notices'); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Control if user has access to the Screen Options |
| 150 | * |
| 151 | * @param boolean $flag |
| 152 | * |
| 153 | * @return boolean |
| 154 | * |
| 155 | * @access public |
| 156 | * @version 6.0.0 |
| 157 | */ |
| 158 | public function screenOptions($flag) |
| 159 | { |
| 160 | if (AAM_Core_API::capExists('aam_show_screen_options')) { |
| 161 | $flag = current_user_can('aam_show_screen_options'); |
| 162 | } |
| 163 | |
| 164 | return $flag; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Check if user can change his/her own password |
| 169 | * |
| 170 | * This method determines if password change fields are going to be dispalyed |
| 171 | * |
| 172 | * @param boolean $result |
| 173 | * @param WP_User $user |
| 174 | * |
| 175 | * @return boolean |
| 176 | * |
| 177 | * @access public |
| 178 | * @version 6.0.0 |
| 179 | */ |
| 180 | public function canChangePassword($result, $user) |
| 181 | { |
| 182 | $isProfile = $user->ID === get_current_user_id(); |
| 183 | |
| 184 | if ($isProfile) { |
| 185 | if (!AAM_Core_API::isAAMCapabilityAllowed('aam_change_own_password')) { |
| 186 | $result = false; |
| 187 | } |
| 188 | } elseif (!AAM_Core_API::isAAMCapabilityAllowed('aam_change_passwords')) { |
| 189 | $result = false; |
| 190 | } |
| 191 | |
| 192 | return $result; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Check if user can update others password |
| 197 | * |
| 198 | * @param mixed $login |
| 199 | * @param string $password |
| 200 | * @param string $password2 |
| 201 | * |
| 202 | * @return void |
| 203 | * |
| 204 | * @access public |
| 205 | * @version 6.0.0 |
| 206 | */ |
| 207 | public function canUpdatePassword($login, &$password, &$password2) |
| 208 | { |
| 209 | $userId = $this->getFromPost('user_id', FILTER_VALIDATE_INT); |
| 210 | $isProfile = $userId === get_current_user_id(); |
| 211 | |
| 212 | if ($isProfile) { |
| 213 | if (!AAM_Core_API::isAAMCapabilityAllowed('aam_change_own_password')) { |
| 214 | $password = $password2 = null; |
| 215 | } |
| 216 | } elseif (!AAM_Core_API::isAAMCapabilityAllowed('aam_change_passwords')) { |
| 217 | $password = $password2 = null; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | if (defined('AAM_KEY')) { |
| 224 | AAM_Service_ExtendedCapabilities::bootstrap(); |
| 225 | } |