Feature
1 year ago
View
1 year ago
Widget
1 year ago
tmpl
3 months ago
AccessLevel.php
1 year ago
Feature.php
1 year ago
Manager.php
3 months ago
View.php
1 year ago
Manager.php
422 lines
| 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 | * Backend manager |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @version 7.0.0 |
| 15 | */ |
| 16 | class AAM_Backend_Manager |
| 17 | { |
| 18 | |
| 19 | /** |
| 20 | * Single instance of itself |
| 21 | * |
| 22 | * @var object |
| 23 | * @access private |
| 24 | * |
| 25 | * @version 7.0.0 |
| 26 | */ |
| 27 | private static $_instance = null; |
| 28 | |
| 29 | /** |
| 30 | * Initialize the AAM backend manager |
| 31 | * |
| 32 | * @return void |
| 33 | * @access protected |
| 34 | * |
| 35 | * @version 7.0.0 |
| 36 | */ |
| 37 | protected function __construct() |
| 38 | { |
| 39 | // Print required JS & CSS |
| 40 | add_action('aam_iframe_footer_action', function() { |
| 41 | $this->_print_js(); |
| 42 | }); |
| 43 | |
| 44 | // Alter user edit screen with support for multiple roles |
| 45 | if (AAM::api()->config->get('core.settings.multi_access_levels')) { |
| 46 | add_action('edit_user_profile', function($user) { |
| 47 | $this->_update_user_profile_form($user); |
| 48 | }); |
| 49 | add_action('user_new_form', array($this, 'user_new_form')); |
| 50 | |
| 51 | // User profile update action |
| 52 | add_action('profile_update', array($this, 'profileUpdate')); |
| 53 | add_action('user_register', array($this, 'profileUpdate')); |
| 54 | add_action('added_existing_user', array($this, 'profileUpdate')); |
| 55 | add_action('wpmu_activate_user', array($this, 'profileUpdate')); |
| 56 | } |
| 57 | |
| 58 | // Manager Admin Menu |
| 59 | if (!is_network_admin()) { |
| 60 | add_action('_user_admin_menu', array($this, 'adminMenu')); |
| 61 | add_action('_admin_menu', array($this, 'adminMenu')); |
| 62 | } |
| 63 | |
| 64 | // Manager AAM Ajax Requests |
| 65 | add_action('wp_ajax_aam', array($this, 'ajax')); |
| 66 | |
| 67 | // Manager user search on the AAM page |
| 68 | add_filter('user_search_columns', function($columns) { |
| 69 | $columns[] = 'display_name'; |
| 70 | return $columns; |
| 71 | }); |
| 72 | |
| 73 | // Footer thank you |
| 74 | add_filter('admin_footer_text', array($this, 'thankYou'), 999); |
| 75 | |
| 76 | // Control admin area |
| 77 | add_action('admin_init', array($this, 'adminInit')); |
| 78 | |
| 79 | // Check for pending migration scripts |
| 80 | if (current_user_can('update_plugins')) { |
| 81 | // Checking for the new update availability |
| 82 | $this->_check_for_premium_addon_update(); |
| 83 | } |
| 84 | |
| 85 | add_action( 'admin_enqueue_scripts', function() { |
| 86 | global $post; |
| 87 | |
| 88 | if (is_a($post, 'WP_Post') && ($post->post_type === 'aam_policy')) { |
| 89 | $settings = wp_enqueue_code_editor( |
| 90 | array('type' => 'application/json') |
| 91 | ); |
| 92 | |
| 93 | if ( false !== $settings ) { // In case Codemirror is disabled |
| 94 | wp_add_inline_script( |
| 95 | 'code-editor', |
| 96 | sprintf( |
| 97 | 'jQuery(() => wp.codeEditor.initialize("%s", %s));', |
| 98 | 'aam-policy-editor', |
| 99 | wp_json_encode($settings) |
| 100 | ) |
| 101 | ); |
| 102 | } |
| 103 | } |
| 104 | }); |
| 105 | |
| 106 | add_filter( |
| 107 | 'network_admin_plugin_action_links_advanced-access-manager/aam.php', |
| 108 | array($this, 'add_premium_link') |
| 109 | ); |
| 110 | add_filter( |
| 111 | 'plugin_action_links_advanced-access-manager/aam.php', |
| 112 | array($this, 'add_premium_link') |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Add the premium link |
| 118 | * |
| 119 | * @param array $actions |
| 120 | * |
| 121 | * @return array |
| 122 | * @access public |
| 123 | * |
| 124 | * @version 7.0.0 |
| 125 | */ |
| 126 | public function add_premium_link($actions) |
| 127 | { |
| 128 | if (!defined('AAM_COMPLETE_PACKAGE_LICENSE')) { |
| 129 | $actions['premium'] = sprintf( |
| 130 | '<a href="%s" target="_blank">%s</a>', |
| 131 | 'https://aamportal.com/premium', |
| 132 | __('Get Premium', 'advanced-access-manager') |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | return $actions; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Check if there is a new premium version available |
| 141 | * |
| 142 | * @return void |
| 143 | * @access private |
| 144 | * |
| 145 | * @version 7.0.0 |
| 146 | */ |
| 147 | private function _check_for_premium_addon_update() |
| 148 | { |
| 149 | $premium = AAM_Addon_Repository::get_instance()->get_premium_data(); |
| 150 | |
| 151 | if (!is_null($premium['version']) && $premium['hasUpdate']) { |
| 152 | AAM_Core_Console::add(__( |
| 153 | 'The new version of premium add-on is available. Go to your license page to download the latest release.', |
| 154 | 'advanced-access-manager' |
| 155 | )); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Print all the necessary JS assets for the AAM UI |
| 161 | * |
| 162 | * @return void |
| 163 | * @access public |
| 164 | * |
| 165 | * @version 7.0.0 |
| 166 | */ |
| 167 | private function _print_js() |
| 168 | { |
| 169 | if ((is_admin() && filter_input(INPUT_GET, 'page') === 'aam')) { |
| 170 | $access_level = AAM_Backend_AccessLevel::get_instance()->get_access_level(); |
| 171 | $ui = filter_input(INPUT_GET, 'aamframe'); |
| 172 | |
| 173 | // Prepare the JS locals |
| 174 | $locals = apply_filters('aam_js_localization_filter', array( |
| 175 | 'nonce' => wp_create_nonce('aam_ajax'), |
| 176 | 'rest_nonce' => wp_create_nonce('wp_rest'), |
| 177 | 'rest_base' => esc_url_raw(rest_url()), |
| 178 | 'ajaxurl' => esc_url(admin_url('admin-ajax.php')), |
| 179 | 'ui' => empty($ui) ? 'main' : $ui, |
| 180 | 'url' => array( |
| 181 | 'editUser' => esc_url(admin_url('user-edit.php')), |
| 182 | 'addUser' => esc_url(admin_url('user-new.php')), |
| 183 | 'editPost' => esc_url(admin_url('post.php')), |
| 184 | 'editTerm' => esc_url(admin_url('term.php')), |
| 185 | 'addPolicy' => esc_url(admin_url('post-new.php?post_type=aam_policy')) |
| 186 | ), |
| 187 | 'subject' => array( |
| 188 | 'type' => $access_level->type, |
| 189 | 'id' => $access_level->get_id(), |
| 190 | 'name' => $access_level->get_display_name() |
| 191 | ), |
| 192 | 'translation' => AAM_Backend_View_Localization::get(), |
| 193 | 'caps' => array( |
| 194 | 'create_roles' => current_user_can('aam_create_roles'), |
| 195 | 'create_users' => current_user_can('create_users'), |
| 196 | 'manage_policies' => is_main_site() |
| 197 | ) |
| 198 | )); |
| 199 | |
| 200 | echo '<script type="text/javascript">'; |
| 201 | echo 'var aamLocal = ' . wp_json_encode($locals) . "\n"; |
| 202 | echo file_get_contents(AAM_BASEDIR . '/media/js/vendor.js') . "\n"; |
| 203 | echo file_get_contents(AAM_BASEDIR . '/media/js/aam.js'); |
| 204 | echo '</script>'; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Edit existing user page |
| 210 | * |
| 211 | * Adding support for the multi-role if this feature is enabled |
| 212 | * |
| 213 | * @param WP_User $user |
| 214 | * |
| 215 | * @return void |
| 216 | * @access public |
| 217 | * |
| 218 | * @version 7.0.0 |
| 219 | */ |
| 220 | public function _update_user_profile_form($user) |
| 221 | { |
| 222 | if (current_user_can('promote_user', $user->ID)) { |
| 223 | require dirname(__FILE__) . '/tmpl/user/multiple-roles.php'; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Adjust user edit/add screen to support multiple roles |
| 229 | * |
| 230 | * @param string $param |
| 231 | * |
| 232 | * @return void |
| 233 | * @access public |
| 234 | * |
| 235 | * @version 7.1.2 |
| 236 | */ |
| 237 | public function user_new_form($param) |
| 238 | { |
| 239 | require dirname(__FILE__) . '/tmpl/user/multiple-roles.php'; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Profile updated hook |
| 244 | * |
| 245 | * @param int $id |
| 246 | * |
| 247 | * @return void |
| 248 | * |
| 249 | * @since 6.6.2 https://github.com/aamplugin/advanced-access-manager/issues/138 |
| 250 | * @since 6.0.0 Initial implementation of the method |
| 251 | * |
| 252 | * @access public |
| 253 | * @version 6.6.2 |
| 254 | */ |
| 255 | public function profileUpdate($id) |
| 256 | { |
| 257 | $user = get_user_by('ID', $id); |
| 258 | |
| 259 | $is_multirole = AAM::api()->config->get( |
| 260 | 'core.settings.multi_access_levels' |
| 261 | ); |
| 262 | |
| 263 | if ($is_multirole && current_user_can('promote_user', $id)) { |
| 264 | $roles = filter_input( |
| 265 | INPUT_POST, |
| 266 | 'aam_user_roles', |
| 267 | FILTER_DEFAULT, |
| 268 | FILTER_REQUIRE_ARRAY |
| 269 | ); |
| 270 | |
| 271 | // let's make sure that the list of roles is array |
| 272 | $roles = (is_array($roles) ? $roles : array()); |
| 273 | |
| 274 | // prepare the final list of roles that needs to be set |
| 275 | $newRoles = array_intersect($roles, array_keys(get_editable_roles())); |
| 276 | |
| 277 | if (!empty($newRoles)) { |
| 278 | // Remove all current roles and then set new |
| 279 | $user->set_role(''); |
| 280 | |
| 281 | foreach ($newRoles as $role) { |
| 282 | $user->add_role($role); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Render AAM iframe content if specified |
| 290 | * |
| 291 | * @return void |
| 292 | * @access public |
| 293 | * |
| 294 | * @version 7.0.0 |
| 295 | */ |
| 296 | public function adminInit() |
| 297 | { |
| 298 | $frame = filter_input(INPUT_GET, 'aamframe'); |
| 299 | |
| 300 | if ($frame) { |
| 301 | AAM_Backend_View::get_instance()->renderIFrame($frame); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Render "Thank You" note on the AAM page |
| 307 | * |
| 308 | * @param string $text |
| 309 | * |
| 310 | * @return string |
| 311 | * @access public |
| 312 | * |
| 313 | * @version 7.0.0 |
| 314 | */ |
| 315 | public function thankYou($text) |
| 316 | { |
| 317 | if ((is_admin() && filter_input(INPUT_GET, 'page') === 'aam')) { |
| 318 | $text = '<span id="footer-thankyou">'; |
| 319 | $text .= AAM_Backend_View_Helper::preparePhrase('[Help us] to be more noticeable and submit your review', 'b'); |
| 320 | $text .= ' <a href="https://wordpress.org/support/plugin/advanced-access-manager/reviews/"'; |
| 321 | $text .= 'target="_blank">here</a>'; |
| 322 | $text .= '</span>'; |
| 323 | } |
| 324 | |
| 325 | return $text; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Register AAM Admin Menu |
| 330 | * |
| 331 | * @return void |
| 332 | * |
| 333 | * @access public |
| 334 | * @version 6.0.0 |
| 335 | */ |
| 336 | public function adminMenu() |
| 337 | { |
| 338 | $bubble = null; // Notification "bubble" for the AAM menu item |
| 339 | |
| 340 | if (current_user_can('aam_show_notifications')) { |
| 341 | $count = AAM_Core_Console::count(); |
| 342 | |
| 343 | if ($count) { |
| 344 | $bubble = ' <span class="update-plugins">' |
| 345 | . '<span class="plugin-count">' . $count |
| 346 | . '</span></span>'; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | $cap_exists = AAM::api()->caps->exists('aam_manager'); |
| 351 | |
| 352 | // Register the menu |
| 353 | add_menu_page( |
| 354 | 'AAM', |
| 355 | 'AAM' . $bubble, |
| 356 | ($cap_exists ? 'aam_manager' : 'administrator'), |
| 357 | 'aam', |
| 358 | function() { |
| 359 | echo AAM_Backend_View::get_instance()->renderPage(); |
| 360 | }, |
| 361 | file_get_contents(AAM_BASEDIR . '/media/active-menu.data') |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Handle Ajax calls to AAM |
| 367 | * |
| 368 | * @return void |
| 369 | * @access public |
| 370 | * |
| 371 | * @version 7.0.2 |
| 372 | */ |
| 373 | public function ajax() |
| 374 | { |
| 375 | check_ajax_referer('aam_ajax'); |
| 376 | |
| 377 | // Clean buffer to make sure that nothing messing around with system |
| 378 | while (ob_get_level() > 0) { |
| 379 | ob_end_clean(); |
| 380 | } |
| 381 | |
| 382 | // Process ajax request |
| 383 | if (current_user_can('aam_manager')) { |
| 384 | echo AAM_Backend_View::get_instance()->processAjax(); |
| 385 | } else { |
| 386 | echo -1; |
| 387 | } |
| 388 | |
| 389 | exit; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Bootstrap the object |
| 394 | * |
| 395 | * @return AAM_Backend_Manager |
| 396 | * @access public |
| 397 | * |
| 398 | * @version 7.0.0 |
| 399 | */ |
| 400 | public static function bootstrap() |
| 401 | { |
| 402 | if (is_null(self::$_instance)) { |
| 403 | self::$_instance = new self; |
| 404 | } |
| 405 | |
| 406 | return self::$_instance; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Get single instance of itself |
| 411 | * |
| 412 | * @return AAM_Backend_Manager |
| 413 | * @access public |
| 414 | * |
| 415 | * @version 7.0.0 |
| 416 | */ |
| 417 | public static function get_instance() |
| 418 | { |
| 419 | return self::bootstrap(); |
| 420 | } |
| 421 | |
| 422 | } |