| 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 |
* AAM core service |
| 12 |
* |
| 13 |
* @since 6.9.10 https://github.com/aamplugin/advanced-access-manager/issues/276 |
| 14 |
* @since 6.9.9 https://github.com/aamplugin/advanced-access-manager/issues/268 |
| 15 |
* @since 6.9.9 https://github.com/aamplugin/advanced-access-manager/issues/265 |
| 16 |
* @since 6.9.5 https://github.com/aamplugin/advanced-access-manager/issues/243 |
| 17 |
* @since 6.9.3 https://github.com/aamplugin/advanced-access-manager/issues/236 |
| 18 |
* @since 6.7.5 https://github.com/aamplugin/advanced-access-manager/issues/173 |
| 19 |
* @since 6.5.3 https://github.com/aamplugin/advanced-access-manager/issues/126 |
| 20 |
* @since 6.4.2 https://github.com/aamplugin/advanced-access-manager/issues/82 |
| 21 |
* @since 6.4.0 Added "Manage Access" toolbar item to single & multi-site network |
| 22 |
* @since 6.0.5 Making sure that only if user is allowed to manage other users |
| 23 |
* @since 6.0.4 Bug fixing. Unwanted "Access Denied" metabox on the Your Profile page |
| 24 |
* @since 6.0.0 Initial implementation of the class |
| 25 |
* |
| 26 |
* @package AAM |
| 27 |
* @version 6.9.10 |
| 28 |
*/ |
| 29 |
class AAM_Service_Core |
| 30 |
{ |
| 31 |
|
| 32 |
use AAM_Core_Contract_SingletonTrait; |
| 33 |
|
| 34 |
/** |
| 35 |
* URI that is used to check for plugin updates |
| 36 |
* |
| 37 |
* @version 6.0.0 |
| 38 |
*/ |
| 39 |
const PLUGIN_CHECK_URI = 'api.wordpress.org/plugins/update-check'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor |
| 43 |
* |
| 44 |
* @access protected |
| 45 |
* |
| 46 |
* @since 6.9.10 https://github.com/aamplugin/advanced-access-manager/issues/276 |
| 47 |
* @since 6.9.9 https://github.com/aamplugin/advanced-access-manager/issues/268 |
| 48 |
* @since 6.9.5 https://github.com/aamplugin/advanced-access-manager/issues/243 |
| 49 |
* @since 6.9.3 https://github.com/aamplugin/advanced-access-manager/issues/236 |
| 50 |
* @since 6.4.2 https://github.com/aamplugin/advanced-access-manager/issues/82 |
| 51 |
* @since 6.4.0 Added "Manage Access" toolbar item |
| 52 |
* @since 6.0.5 Fixed bug when Access Manager metabox is rendered for users that |
| 53 |
* have ability to manage other users |
| 54 |
* @since 6.0.4 Fixed bug when Access Manager metabox is rendered on profile edit |
| 55 |
* page |
| 56 |
* @since 6.0.0 Initial implementation of the method |
| 57 |
* |
| 58 |
* @return void |
| 59 |
* @version 6.9.10 |
| 60 |
*/ |
| 61 |
protected function __construct() |
| 62 |
{ |
| 63 |
if (is_admin()) { |
| 64 |
$metaboxEnabled = AAM_Core_Config::get( |
| 65 |
'ui.settings.renderAccessMetabox', false |
| 66 |
); |
| 67 |
|
| 68 |
if ($metaboxEnabled && current_user_can('aam_manager')) { |
| 69 |
add_action('edit_user_profile', array($this, 'renderAccessWidget')); |
| 70 |
} |
| 71 |
|
| 72 |
// Hook that initialize the AAM UI part of the service |
| 73 |
add_action('aam_init_ui_action', function () { |
| 74 |
AAM_Backend_Feature_Subject_User::register(); |
| 75 |
|
| 76 |
AAM_Backend_Feature_Settings_Service::register(); |
| 77 |
AAM_Backend_Feature_Settings_Core::register(); |
| 78 |
AAM_Backend_Feature_Settings_Content::register(); |
| 79 |
AAM_Backend_Feature_Settings_ConfigPress::register(); |
| 80 |
AAM_Backend_Feature_Settings_Manager::register(); |
| 81 |
}, 1); |
| 82 |
} |
| 83 |
|
| 84 |
// Add toolbar "Manage Access" item |
| 85 |
add_action('admin_bar_menu', function($wp_admin_bar) { |
| 86 |
if (current_user_can('aam_manager')) { |
| 87 |
$wp_admin_bar->add_menu( |
| 88 |
array( |
| 89 |
'parent' => 'site-name', |
| 90 |
'id' => 'aam', |
| 91 |
'title' => __('Manager Access', AAM_KEY), |
| 92 |
'href' => admin_url('admin.php?page=aam'), |
| 93 |
) |
| 94 |
); |
| 95 |
} |
| 96 |
}, 999); |
| 97 |
|
| 98 |
// Add "Manage Access" to all sites if multisite network |
| 99 |
if (is_multisite()) { |
| 100 |
add_action('admin_bar_menu', function($wp_admin_bar) { |
| 101 |
$blog_count = 0; |
| 102 |
|
| 103 |
if (is_a($wp_admin_bar->user, 'stdClass')) { |
| 104 |
if (is_iterable($wp_admin_bar->user->blogs)) { |
| 105 |
$blog_count = count($wp_admin_bar->user->blogs); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if ($blog_count > 0 || current_user_can('manage_network')) { |
| 110 |
foreach((array) $wp_admin_bar->user->blogs as $blog) { |
| 111 |
switch_to_blog($blog->userblog_id); |
| 112 |
|
| 113 |
$menu_id = 'blog-' . $blog->userblog_id; |
| 114 |
|
| 115 |
if (current_user_can('aam_manager')) { |
| 116 |
$wp_admin_bar->add_menu( |
| 117 |
array( |
| 118 |
'parent' => $menu_id, |
| 119 |
'id' => $menu_id . '-aam', |
| 120 |
'title' => __('Manage Access', AAM_KEY), |
| 121 |
'href' => admin_url('admin.php?page=aam'), |
| 122 |
) |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
restore_current_blog(); |
| 127 |
} |
| 128 |
} |
| 129 |
}, 999); |
| 130 |
} |
| 131 |
|
| 132 |
// Check if user has ability to perform certain task based on provided |
| 133 |
// capability and meta data |
| 134 |
add_filter('map_meta_cap', array($this, 'mapMetaCaps'), 999, 4); |
| 135 |
|
| 136 |
// User expiration hook |
| 137 |
add_action('aam_set_user_expiration_action', function($settings) { |
| 138 |
AAM::getUser()->setUserExpiration($settings); |
| 139 |
}); |
| 140 |
|
| 141 |
// Run upgrades if available |
| 142 |
AAM_Core_Migration::run(); |
| 143 |
|
| 144 |
// Bootstrap RESTful API |
| 145 |
AAM_Core_Restful::bootstrap(); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Render "Access Manager" widget on the user/profile edit screen |
| 150 |
* |
| 151 |
* @param WP_User $user |
| 152 |
* |
| 153 |
* @return void |
| 154 |
* |
| 155 |
* @since 6.0.5 Making sure that user metabox is rendered only if user is allowed |
| 156 |
* to manage other users |
| 157 |
* @since 6.0.0 Initial implementation of the method |
| 158 |
* |
| 159 |
* @access public |
| 160 |
* @version 6.0.5 |
| 161 |
*/ |
| 162 |
public function renderAccessWidget($user) |
| 163 |
{ |
| 164 |
if (current_user_can('aam_manage_users')) { |
| 165 |
echo AAM_Backend_View::getInstance()->renderUserMetabox($user); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Check user capability |
| 171 |
* |
| 172 |
* This is a hack function that add additional layout on top of WordPress |
| 173 |
* core functionality. Based on the capability passed in the $args array as |
| 174 |
* "0" element, it performs additional check on user's capability to manage |
| 175 |
* post, users etc. |
| 176 |
* |
| 177 |
* @param array $caps |
| 178 |
* @param string $cap |
| 179 |
* @param int $user_id |
| 180 |
* @param array $args |
| 181 |
* |
| 182 |
* @return array |
| 183 |
* |
| 184 |
* @since 6.9.9 https://github.com/aamplugin/advanced-access-manager/issues/265 |
| 185 |
* @since 6.5.3 https://github.com/aamplugin/advanced-access-manager/issues/126 |
| 186 |
* @since 6.0.0 Initial implementation of the method |
| 187 |
* |
| 188 |
* @access public |
| 189 |
* @version 6.9.9 |
| 190 |
*/ |
| 191 |
public function mapMetaCaps($caps, $cap, $user_id, $args) |
| 192 |
{ |
| 193 |
$objectId = (isset($args[0]) ? $args[0] : null); |
| 194 |
|
| 195 |
// Mutate any AAM specific capability if it does not exist |
| 196 |
foreach ((array) $caps as $i => $capability) { |
| 197 |
if ( |
| 198 |
is_string($capability) && (strpos($capability, 'aam_') === 0) |
| 199 |
&& !AAM_Core_API::capExists($capability) |
| 200 |
) { |
| 201 |
$caps[$i] = AAM_Core_Config::get( |
| 202 |
'page.capability', |
| 203 |
'administrator' |
| 204 |
); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
switch ($cap) { |
| 209 |
case 'install_plugins': |
| 210 |
case 'delete_plugins': |
| 211 |
case 'edit_plugins': |
| 212 |
case 'update_plugins': |
| 213 |
$action = explode('_', $cap); |
| 214 |
$caps = $this->checkPluginsAction($action[0], $caps, $cap); |
| 215 |
break; |
| 216 |
|
| 217 |
case 'activate_plugin': |
| 218 |
case 'deactivate_plugin': |
| 219 |
$action = explode('_', $cap); |
| 220 |
$caps = $this->checkPluginAction( |
| 221 |
$objectId, $action[0], $caps, $cap |
| 222 |
); |
| 223 |
break; |
| 224 |
|
| 225 |
default: |
| 226 |
break; |
| 227 |
} |
| 228 |
|
| 229 |
return $caps; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Check if specific action for plugins is allowed |
| 234 |
* |
| 235 |
* @param string $action |
| 236 |
* @param array $caps |
| 237 |
* @param string $cap |
| 238 |
* |
| 239 |
* @return array |
| 240 |
* |
| 241 |
* @access protected |
| 242 |
* @version 6.0.0 |
| 243 |
*/ |
| 244 |
protected function checkPluginsAction($action, $caps, $cap) |
| 245 |
{ |
| 246 |
$allow = apply_filters('aam_allowed_plugin_action_filter', null, $action); |
| 247 |
|
| 248 |
if ($allow !== null) { |
| 249 |
$caps[] = $allow ? $cap : 'do_not_allow'; |
| 250 |
} |
| 251 |
|
| 252 |
return $caps; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Check if specific action is allowed upon provided plugin |
| 257 |
* |
| 258 |
* @param string $plugin |
| 259 |
* @param string $action |
| 260 |
* @param array $caps |
| 261 |
* @param string $cap |
| 262 |
* |
| 263 |
* @return array |
| 264 |
* |
| 265 |
* @access protected |
| 266 |
* @version 6.0.0 |
| 267 |
*/ |
| 268 |
protected function checkPluginAction($plugin, $action, $caps, $cap) |
| 269 |
{ |
| 270 |
$parts = explode('/', $plugin); |
| 271 |
$slug = (!empty($parts[0]) ? $parts[0] : null); |
| 272 |
|
| 273 |
$allow = apply_filters( |
| 274 |
'aam_allowed_plugin_action_filter', null, $action, $slug |
| 275 |
); |
| 276 |
|
| 277 |
if ($allow !== null) { |
| 278 |
$caps[] = $allow ? $cap : 'do_not_allow'; |
| 279 |
} |
| 280 |
|
| 281 |
return $caps; |
| 282 |
} |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
if (defined('AAM_KEY')) { |
| 287 |
AAM_Service_Core::bootstrap(); |
| 288 |
} |