PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 5.8.2
Advanced Access Manager – Access Governance for WordPress v5.8.2
6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 6.9.27 6.9.28 6.9.29 6.9.3 6.9.30 6.9.31 6.9.32 6.9.33 6.9.34 6.9.35 6.9.36 6.9.37 6.9.38 6.9.39 6.9.4 6.9.41 6.9.42 6.9.43 6.9.44 6.9.45 6.9.46 6.9.47 6.9.48 6.9.49 6.9.5 6.9.51 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.0-alpha.6 7.0.0-alpha.7 7.0.0-beta.1 7.0.0-rc1 7.0.0-rc2 7.0.0-rc3 7.0.1 7.0.10 7.0.11 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 7.1.0 7.1.1 trunk 3.0 4.0 4.0.1 4.1 4.2 4.3 4.4 4.4.1 4.5 4.6 4.6.1 4.6.2 4.7 4.7.1 4.7.2 4.7.5 4.7.6 4.8 4.8.1 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.5.1 4.9.5.2 5.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 5.1.1 5.10 5.11 5.2 5.2.1 5.2.5 5.2.6 5.2.7 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.4 5.4.1 5.4.2 5.4.3 5.4.3.1 5.4.3.2 5.5 5.5.1 5.5.2 5.6 5.6.1 5.6.1.1 5.7 5.7.1 5.7.2 5.7.3 5.8 5.8.1 5.8.2 5.8.3 5.9 5.9.1 5.9.1.1 5.9.2 5.9.2.1 5.9.3 5.9.4 5.9.5 5.9.6 5.9.6.1 5.9.6.2 5.9.6.3 5.9.7 5.9.7.1 5.9.7.2 5.9.7.3 5.9.8 5.9.8.1 5.9.9 5.9.9.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.2.0 6.2.1 6.2.2 6.3.0 6.3.1 6.3.2 6.3.3 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.6.0 6.6.1 6.6.2 6.6.3 6.6.4 6.7.0 6.7.1 6.7.2 6.7.3 6.7.4 6.7.5 6.7.6 6.7.7 6.7.8 6.7.9 6.8.0 6.8.1 6.8.2 6.8.3
advanced-access-manager / Application / Backend / Manager.php
advanced-access-manager / Application / Backend Last commit date
Feature 7 years ago View 7 years ago Widget 7 years ago phtml 7 years ago Authorization.php 7 years ago Feature.php 7 years ago Filter.php 7 years ago Manager.php 7 years ago Subject.php 7 years ago View.php 7 years ago
Manager.php
1017 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 * @author Vasyl Martyniuk <vasyl@vasyltech.com>
15 */
16 class AAM_Backend_Manager {
17
18 /**
19 * Single instance of itself
20 *
21 * @var AAM_Backend_Manager
22 *
23 * @access private
24 */
25 private static $_instance = null;
26
27 /**
28 * Initialize the object
29 *
30 * @return void
31 *
32 * @access protected
33 */
34 protected function __construct() {
35 //check if user is allowed to see backend
36 $this->checkUserAccess();
37
38 //check if user switch is required
39 $this->checkUserSwitch();
40
41 //cache clearing hook
42 add_action('aam-clear-cache-action', 'AAM_Core_API::clearCache');
43
44 //print required JS & CSS
45 add_action('admin_print_scripts', array($this, 'printJavascript'));
46 add_action('admin_print_footer_scripts', array($this, 'printFooterJavascript'));
47 add_action('admin_print_styles', array($this, 'printStylesheet'));
48
49 //map AAM UI specific capabilities
50 add_filter('map_meta_cap', array($this, 'mapMetaCap'), 10, 4);
51
52 //user profile update action
53 add_action('profile_update', array($this, 'profileUpdate'), 10, 2);
54
55 //post title decorator
56 add_filter('the_title', array($this, 'theTitle'), 999, 2);
57
58 //permalink manager
59 add_filter('get_sample_permalink_html', array($this, 'getPermalinkHtml'), 10, 5);
60
61 //access policy save
62 add_filter('wp_insert_post_data', array($this, 'filterPostData'), 10, 2);
63
64 //screen options & contextual help hooks
65 add_filter('screen_options_show_screen', array($this, 'screenOptions'));
66 add_filter('contextual_help', array($this, 'helpOptions'), 10, 3);
67
68 //manager Admin Menu
69 if (is_multisite() && is_network_admin()) {
70 //register AAM in the network admin panel
71 add_action('network_admin_menu', array($this, 'adminMenu'));
72 } else {
73 add_action('admin_menu', array($this, 'adminMenu'));
74 add_action('all_admin_notices', array($this, 'notification'));
75 }
76
77 if (AAM_Core_Config::get('ui.settings.renderAccessMetabox', true)) {
78 add_action('edit_category_form_fields', array($this, 'renderTermMetabox'), 1);
79 add_action('edit_link_category_form_fields', array($this, 'renderTermMetabox'), 1);
80 add_action('edit_tag_form_fields', array($this, 'renderTermMetabox'), 1);
81
82 //register custom access control metabox
83 add_action('add_meta_boxes', array($this, 'metabox'));
84 }
85
86 //register custom access control metabox
87 add_action('add_meta_boxes', array($this, 'registerPolicyDocMetabox'));
88
89 //manager AAM Ajax Requests
90 add_action('wp_ajax_aam', array($this, 'ajax'));
91 //manager AAM Features Content rendering
92 add_action('admin_action_aamc', array($this, 'renderContent'));
93 //manager user search and authentication control
94 add_filter('user_search_columns', array($this, 'searchColumns'));
95
96 //manager WordPress metaboxes
97 add_action("in_admin_header", array($this, 'initMetaboxes'), 999);
98
99 if (AAM_Core_Config::get('ui.settings.renderAccessActionLink', true)) {
100 //extend post inline actions
101 add_filter('page_row_actions', array($this, 'postRowActions'), 10, 2);
102 add_filter('post_row_actions', array($this, 'postRowActions'), 10, 2);
103
104 //extend term inline actions
105 add_filter('tag_row_actions', array($this, 'tagRowActions'), 10, 2);
106
107 //manage access action to the user list
108 add_filter('user_row_actions', array($this, 'userActions'), 10, 2);
109 }
110
111 //footer thank you
112 add_filter('admin_footer_text', array($this, 'thankYou'), 999);
113
114 //control admin area
115 add_action('admin_init', array($this, 'adminInit'));
116
117 //password reset feature
118 add_filter('show_password_fields', array($this, 'canChangePassword'), 10, 2);
119 add_action('check_passwords', array($this, 'canUpdatePassword'), 10, 3);
120
121 //admin toolbar
122 if (AAM::isAAM()) {
123 add_action('wp_after_admin_bar_render', array($this, 'cacheAdminBar'));
124 }
125
126 //register login widget
127 if (AAM_Core_Config::get('core.settings.secureLogin', true)) {
128 add_action('widgets_init', array($this, 'registerLoginWidget'));
129 add_action('wp_ajax_nopriv_aamlogin', array($this, 'handleLogin'));
130 }
131
132 //register backend hooks and filters
133 if (AAM_Core_Config::get('core.settings.backendAccessControl', true)) {
134 AAM_Backend_Filter::register();
135 }
136
137 AAM_Extension_Repository::getInstance()->hasUpdates();
138
139 if (version_compare(PHP_VERSION, '5.3.0') === -1) {
140 AAM_Core_Console::add(
141 'AAM requires PHP version 5.3.0 or higher to function properly'
142 );
143 }
144 }
145
146 /**
147 *
148 * @param boolean $result
149 * @param type $user
150 * @return boolean
151 */
152 public function canChangePassword($result, $user) {
153 $isProfile = $user->ID === get_current_user_id();
154 if ($isProfile) {
155 if (AAM_Core_API::capabilityExists('change_own_password')
156 && !AAM::getUser()->hasCapability('change_own_password')) {
157 $result = false;
158 }
159 } elseif (AAM_Core_API::capabilityExists('change_passwords')
160 && !AAM::getUser()->hasCapability('change_passwords')) {
161 $result = false;
162 }
163
164 return $result;
165 }
166
167 /**
168 *
169 * @param type $login
170 * @param type $password
171 */
172 public function canUpdatePassword($login, &$password, &$password2) {
173 $userId = AAM_Core_Request::post('user_id');
174 $isProfile = $userId === get_current_user_id();
175
176 if ($isProfile) {
177 if (AAM_Core_API::capabilityExists('change_own_password')
178 && !AAM::getUser()->hasCapability('change_own_password')) {
179 $password = $password2 = null;
180 }
181 } elseif (AAM_Core_API::capabilityExists('change_passwords')
182 && !AAM::getUser()->hasCapability('change_passwords')) {
183 $password = $password2 = null;
184 }
185 }
186
187 /**
188 *
189 * @param type $data
190 * @return type
191 */
192 public function filterPostData($data, $postarr) {
193 if (isset($data['post_type']) && ($data['post_type'] === 'aam_policy')) {
194 $content = trim(filter_input(INPUT_POST, 'aam-policy'));
195
196 if (!empty($content)) { // Edit form was submitted
197 $data['post_content'] = $content;
198 }
199
200 if (empty($data['post_content'])) {
201 $data['post_content'] = AAM_Backend_View_Helper::getDefaultPolicy();
202 }
203
204 AAM_Core_API::clearCache();
205 }
206
207 return $data;
208 }
209
210 /**
211 *
212 */
213 public function renderExportFields() {
214 ob_start();
215 require_once dirname(__FILE__) . '/phtml/system/export.phtml';
216 $content = ob_get_contents();
217 ob_end_clean();
218
219 echo $content;
220 }
221
222 /**
223 *
224 * @param type $args
225 * @return type
226 */
227 public function prepareExportArgs($args) {
228 if ($args['content'] === 'aam') {
229 $export = array();
230
231 foreach(AAM_Core_Request::get('export', array()) as $group => $settings) {
232 $export[$group] = implode(',', $settings);
233 }
234
235 if (empty($export)) {
236 $export = array('system' => 'roles,utilities,configpress');
237 }
238
239 $args['export'] = $export;
240 }
241
242 return $args;
243 }
244
245 /**
246 *
247 * @param type $args
248 */
249 public function exportSettings($args) {
250 if ($args['content'] === 'aam') {
251 $filename = 'aam.export.' . date('Y-m-d') . '.json';
252 header('Content-Description: File Transfer');
253 header('Content-Disposition: attachment; filename=' . $filename);
254 header('Content-Type: application/json; charset=' . get_option('blog_charset'), true);
255 $exporter = new AAM_Core_Exporter($args['export']);
256 echo wp_json_encode($exporter->run());
257 die();
258 }
259 }
260
261 /**
262 *
263 */
264 protected function registerAAMImporter() {
265 register_importer(
266 'aam',
267 'AAM Access Settings',
268 'Advanced Access Manager access settings and configurations',
269 array($this, 'renderImporer')
270 );
271 }
272
273 /**
274 *
275 */
276 public function renderImporer() {
277 $importer = new AAM_Core_Importer();
278 $importer->dispatch();
279 }
280
281 /**
282 *
283 * @param type $caps
284 * @param type $cap
285 * @return type
286 */
287 public function mapMetaCap($caps, $cap) {
288 if (in_array($cap, AAM_Backend_Feature_Main_Capability::$groups['aam'], true)) {
289 if (!AAM_Core_API::capabilityExists($cap)) {
290 $caps = array(AAM_Core_Config::get('page.capability', 'administrator'));
291 }
292 }
293
294 return $caps;
295 }
296
297 /**
298 *
299 * @param string $html
300 * @return string
301 */
302 public function getPermalinkHtml($html) {
303 if (AAM_Core_API::capabilityExists('edit_permalink')
304 && !AAM::getUser()->hasCapability('edit_permalink')) {
305 $html = '';
306 }
307
308 return $html;
309 }
310
311 /**
312 * Profile updated hook
313 *
314 * Adjust expiration time and user cache if profile updated
315 *
316 * @param int $id
317 * @param WP_User $old
318 *
319 * @return void
320 *
321 * @access public
322 */
323 public function profileUpdate($id, $old) {
324 $user = get_user_by('ID', $id);
325
326 //role changed?
327 if (implode('', $user->roles) !== implode('', $old->roles)) {
328 AAM_Core_API::clearCache(new AAM_Core_Subject_User($id));
329
330 //check if role has expiration data set
331 // TODO: This supports only the first role and NOT the multi-roles
332 if (is_array($user->roles)) {
333 $roles = array_values($user->roles);
334 $role = array_shift($roles);
335 $expire = AAM_Core_API::getOption("aam-role-{$role}-expiration", '');
336
337 if ($expire) {
338 update_user_option($id, "aam-original-roles", $old->roles);
339 update_user_option($id, "aam-role-expires", strtotime($expire));
340 }
341 }
342 }
343 }
344
345 /**
346 * Filter post title
347 *
348 * @param string $title
349 * @param int $id
350 *
351 * @return string
352 *
353 * @access public
354 */
355 public function theTitle($title, $id = null) {
356 if (empty($title) && AAM::isAAM()) { //apply filter only for AAM page
357 $title = '[No Title]: ID ' . ($id ? $id : '[No ID]');
358 }
359
360 return $title;
361 }
362
363 /**
364 *
365 * @param type $flag
366 * @return type
367 */
368 public function screenOptions($flag) {
369 if (AAM_Core_API::capabilityExists('show_screen_options')) {
370 $flag = AAM::getUser()->hasCapability('show_screen_options');
371 }
372
373 if (AAM::isAAM()) {
374 $flag = false;
375 }
376
377 return $flag;
378 }
379
380 /**
381 *
382 * @param array $help
383 * @param type $id
384 * @param type $screen
385 * @return array
386 */
387 public function helpOptions($help, $id, $screen) {
388 if (AAM_Core_API::capabilityExists('show_help_tabs')) {
389 if (!AAM::getUser()->hasCapability('show_help_tabs')) {
390 $screen->remove_help_tabs();
391 $help = array();
392 }
393 }
394
395 if (AAM::isAAM()) {
396 $screen->remove_help_tabs();
397 }
398
399 return $help;
400 }
401
402 /**
403 *
404 * @return type
405 */
406 public function handleLogin() {
407 $login = AAM_Core_Login::getInstance();
408
409 echo wp_json_encode($login->execute());
410 exit;
411 }
412
413 /**
414 *
415 */
416 public function adminInit() {
417 $user = AAM::getUser();
418 $frame = AAM_Core_Request::get('aamframe');
419
420 if ($frame && $user->hasCapability('aam_manage_posts')) {
421 echo AAM_Backend_View::getInstance()->renderAccessFrame();
422 exit;
423 }
424
425 // Import/Export feature
426 add_action('export_filters', array($this, 'renderExportFields'));
427 add_filter('export_args', array($this, 'prepareExportArgs'));
428 add_action('export_wp', array($this, 'exportSettings'));
429 $this->registerAAMImporter();
430 }
431
432 /**
433 *
434 * @param type $text
435 * @return string
436 */
437 public function thankYou($text) {
438 if (AAM::isAAM()) {
439 $text = '<span id="footer-thankyou">';
440 $text .= '<b>Please help us</b> and submit your review <a href="';
441 $text .= 'https://wordpress.org/support/plugin/advanced-access-manager/reviews/"';
442 $text .= 'target="_blank"><i class="icon-star"></i>';
443 $text .= '<i class="icon-star"></i><i class="icon-star"></i>';
444 $text .= '<i class="icon-star"></i><i class="icon-star"></i></a>';
445 $text .= '</span>';
446 }
447
448 return $text;
449 }
450
451 /**
452 *
453 */
454 public function registerLoginWidget() {
455 register_widget('AAM_Backend_Widget_Login');
456 }
457
458 /**
459 *
460 */
461 protected function checkUserAccess() {
462 $uid = get_current_user_id();
463
464 if ($uid && AAM_Core_API::capabilityExists('access_dashboard')) {
465 $caps = AAM::getUser()->allcaps;
466 if (empty($caps['access_dashboard'])) {
467 //also additionally check for AJAX calls
468 if (defined('DOING_AJAX') && empty($caps['allow_ajax_calls'])) {
469 AAM_Core_API::reject(
470 'backend', array('hook' => 'access_dashboard')
471 );
472 } elseif (!defined('DOING_AJAX')) {
473 AAM_Core_API::reject(
474 'backend', array('hook' => 'access_dashboard')
475 );
476 }
477 }
478 }
479 }
480
481 /**
482 *
483 */
484 protected function checkUserSwitch() {
485 if (AAM_Core_Request::get('action') === 'aam-switch-back') {
486 $current = get_current_user_id();
487 $uid = AAM_Core_API::getOption('aam-user-switch-' . $current);
488 $redirect = admin_url('admin.php?page=aam&user=' . $current);
489
490 check_admin_referer('aam-switch-' . $uid);
491
492 wp_clear_auth_cookie();
493 wp_set_auth_cookie( $uid, true );
494 wp_set_current_user( $uid );
495
496 AAM_Core_API::deleteOption('aam-user-switch-' . $current);
497
498 wp_redirect($redirect);
499 exit;
500 }
501 }
502
503 /**
504 *
505 */
506 public function notification() {
507 $uid = AAM_Core_API::getOption('aam-user-switch-' . get_current_user_id());
508
509 if ($uid) {
510 //get user's name
511 $user = new WP_User($uid);
512 $name = $user->display_name ? $user->display_name : $user->user_nicename;
513
514 //generate switch back URL
515 $url = wp_nonce_url(
516 'index.php?action=aam-switch-back', 'aam-switch-' . $uid
517 );
518
519 echo '<div class="updated notice">';
520 echo '<p style="padding: 10px; font-weight: 700; letter-spacing:0.5px;">';
521 echo sprintf('Switch back to <a href="%s">%s</a>.', $url, esc_js($name));
522 echo '</p></div>';
523 }
524 }
525
526 /**
527 *
528 */
529 public function metabox() {
530 global $post;
531
532 $frontend = AAM_Core_Config::get('core.settings.frontendAccessControl', true);
533 $backend = AAM_Core_Config::get('core.settings.backendAccessControl', true);
534 $api = AAM_Core_Config::get('core.settings.apiAccessControl', true);
535
536 $needAC = ($frontend || $backend || $api);
537 $allowed = AAM::getUser()->hasCapability('aam_manage_posts');
538 $notASP = (!is_a($post, 'WP_Post') || ($post->post_type !== 'aam_policy'));
539
540 if ($needAC && $allowed && $notASP) {
541 add_meta_box(
542 'aam-acceess-manager',
543 __('Access Manager', AAM_KEY) . ' <small style="color:#999999;">by AAM plugin</small>',
544 array($this, 'renderPostMetabox'),
545 null,
546 'advanced',
547 'high'
548 );
549 }
550 }
551
552 /**
553 *
554 * @global WP_Post $post
555 */
556 public function registerPolicyDocMetabox() {
557 global $post;
558
559 if (is_a($post, 'WP_Post') && ($post->post_type === 'aam_policy')) {
560 add_meta_box(
561 'aam-policy',
562 __('Policy Document', AAM_KEY),
563 array($this, 'renderPolicyMetabox'),
564 null,
565 'normal',
566 'high'
567 );
568 add_meta_box(
569 'aam-policy-attached',
570 __('Policy Principals', AAM_KEY),
571 array($this, 'renderPolicyPrincipalMetabox'),
572 null,
573 'side'
574 );
575 }
576 }
577
578 /**
579 *
580 * @global WP_Post $post
581 */
582 public function renderPolicyMetabox() {
583 global $post;
584
585 if (is_a($post, 'WP_Post')) {
586 echo AAM_Backend_View::getInstance()->renderPolicyMetabox($post);
587 }
588 }
589
590 /**
591 *
592 * @global WP_Post $post
593 */
594 public function renderPolicyPrincipalMetabox() {
595 global $post;
596
597 if (is_a($post, 'WP_Post')) {
598 echo AAM_Backend_View::getInstance()->renderPolicyPrincipalMetabox($post);
599 }
600 }
601
602 /**
603 *
604 * @global type $wp_admin_bar
605 */
606 public function cacheAdminBar() {
607 global $wp_admin_bar;
608 static $cache = null;
609
610 $reflection = new ReflectionClass(get_class($wp_admin_bar));
611
612 $prop = $reflection->getProperty('nodes');
613 $prop->setAccessible(true);
614
615 $nodes = $prop->getValue($wp_admin_bar);
616
617 if (isset($nodes['root']) && is_null($cache)) {
618 $cache = array();
619 foreach($nodes['root']->children as $node) {
620 $cache = array_merge($cache, $node->children);
621 }
622
623 // do some cleanup
624 foreach($cache as $i => $node) {
625 if ($node->id === 'menu-toggle') {
626 unset($cache[$i]);
627 }
628 }
629 }
630
631 return $cache;
632 }
633
634 /**
635 *
636 * @global type $post
637 */
638 public function renderPostMetabox() {
639 global $post;
640
641 if (is_a($post, 'WP_Post')) {
642 echo AAM_Backend_View::getInstance()->renderPostMetabox($post);
643 }
644 }
645
646 /**
647 *
648 * @param type $term
649 */
650 public function renderTermMetabox($term) {
651 if (is_a($term, 'WP_Term') && is_taxonomy_hierarchical($term->taxonomy)) {
652 $frontend = AAM_Core_Config::get('core.settings.frontendAccessControl', true);
653 $backend = AAM_Core_Config::get('core.settings.backendAccessControl', true);
654 $api = AAM_Core_Config::get('core.settings.apiAccessControl', true);
655
656 if (($frontend || $backend || $api) && AAM::getUser()->hasCapability('aam_manage_posts')) {
657 echo AAM_Backend_View::getInstance()->renderTermMetabox($term);
658 }
659 }
660 }
661
662 /**
663 * Hanlde Metabox initialization process
664 *
665 * @return void
666 *
667 * @access public
668 */
669 public function initMetaboxes() {
670 global $post;
671
672 if (AAM_Core_Request::get('init') === 'metabox') {
673 //make sure that nobody is playing with screen options
674 if (is_a($post, 'WP_Post')) {
675 $screen = $post->post_type;
676 } else {
677 $screen_object = get_current_screen();
678 $screen = ($screen_object ? $screen_object->id : '');
679 }
680
681 $model = new AAM_Backend_Feature_Main_Metabox;
682 $model->initialize($screen);
683 }
684 }
685
686 /**
687 * Add extra column to search in for User search
688 *
689 * @param array $columns
690 *
691 * @return array
692 *
693 * @access public
694 */
695 public function searchColumns($columns) {
696 $columns[] = 'display_name';
697
698 return $columns;
699 }
700
701 /**
702 *
703 * @param type $actions
704 * @param type $post
705 * @return string
706 */
707 public function postRowActions($actions, $post) {
708 if ($this->renderExternalUIFeature('aam_manage_posts')) {
709 $url = admin_url('admin.php?page=aam&oid=' . $post->ID . '&otype=post#post');
710
711 $actions['aam'] = '<a href="' . $url . '" target="_blank">';
712 $actions['aam'] .= __('Access', AAM_KEY) . '</a>';
713 }
714
715 return $actions;
716 }
717
718 /**
719 *
720 * @param type $actions
721 * @param type $term
722 * @return string
723 */
724 public function tagRowActions($actions, $term) {
725 if ($this->renderExternalUIFeature('aam_manage_posts')) {
726 $oid = $term->term_id . '|' . $term->taxonomy;
727 $url = admin_url('admin.php?page=aam&oid=' . $oid . '&otype=term#post');
728
729 $actions['aam'] = '<a href="' . $url . '" target="_blank">';
730 $actions['aam'] .= __('Access', AAM_KEY) . '</a>';
731 }
732
733 return $actions;
734 }
735
736 /**
737 * Add "Manage Access" action
738 *
739 * Add additional action to the user list table.
740 *
741 * @param array $actions
742 * @param WP_User $user
743 *
744 * @return array
745 *
746 * @access public
747 */
748 public function userActions($actions, $user) {
749 if ($this->renderExternalUIFeature('aam_manage_users')
750 || $this->renderExternalUIFeature('list_users')) {
751 $url = admin_url('admin.php?page=aam&user=' . $user->ID);
752
753 $actions['aam'] = '<a href="' . $url . '" target="_blank">';
754 $actions['aam'] .= __('Access', AAM_KEY) . '</a>';
755 }
756
757 return $actions;
758 }
759
760 /**
761 *
762 * @param type $cap
763 * @return type
764 */
765 protected function renderExternalUIFeature($cap) {
766 $frontend = AAM_Core_Config::get('core.settings.frontendAccessControl', true);
767 $backend = AAM_Core_Config::get('core.settings.backendAccessControl', true);
768 $api = AAM_Core_Config::get('core.settings.apiAccessControl', true);
769 $aamManager = AAM::getUser()->hasCapability('aam_manager');
770 $featureManager = AAM::getUser()->hasCapability($cap);
771
772 return ($frontend || $backend || $api) && $aamManager && $featureManager;
773 }
774
775 /**
776 * Print javascript libraries
777 *
778 * @return void
779 *
780 * @access public
781 */
782 public function printJavascript() {
783 if (AAM::isAAM()) {
784 wp_enqueue_script('aam-vendor', AAM_MEDIA . '/js/vendor.js');
785 wp_enqueue_script('aam-main', AAM_MEDIA . '/js/aam-5.8.js');
786
787 //add plugin localization
788 $this->printLocalization('aam-main');
789 }
790 }
791
792 /**
793 *
794 * @global type $menu
795 * @global type $submenu
796 */
797 public function printFooterJavascript() {
798 global $menu, $submenu;
799
800 if (AAM::isAAM()) {
801 $script = '<script type="text/javascript">';
802 $script .= 'var aamEnvData = ' . wp_json_encode(array(
803 'menu' => base64_encode(json_encode($menu)),
804 'submenu' => base64_encode(json_encode($submenu)),
805 'toolbar' => base64_encode(json_encode($this->cacheAdminBar()))
806 )) ;
807 $script .= '</script>';
808
809 echo $script;
810 }
811 }
812
813 /**
814 * Print plugin localization
815 *
816 * @param string $localKey
817 *
818 * @return void
819 *
820 * @access protected
821 */
822 protected function printLocalization($localKey) {
823 $subject = AAM_Backend_Subject::getInstance();
824 $endpoint = getenv('AAM_ENDPOINT');
825
826 $locals = array(
827 'nonce' => wp_create_nonce('aam_ajax'),
828 'ajaxurl' => esc_url(admin_url('admin-ajax.php')),
829 'ui' => AAM_Core_Request::get('aamframe', 'main'),
830 'url' => array(
831 'site' => esc_url(admin_url('index.php')),
832 'editUser' => esc_url(admin_url('user-edit.php')),
833 'addUser' => esc_url(admin_url('user-new.php')),
834 'addPolicy' => esc_url(admin_url('post-new.php?post_type=aam_policy'))
835 ),
836 'level' => AAM::getUser()->getMaxLevel(),
837 'subject' => array(
838 'type' => $subject->getUID(),
839 'id' => $subject->getId(),
840 'name' => $subject->getName(),
841 'level' => $subject->getMaxLevel(),
842 'blog' => get_current_blog_id()
843 ),
844 'system' => array(
845 'domain' => wp_parse_url(site_url(), PHP_URL_HOST),
846 'uid' => AAM_Core_API::getOption('aam-uid', null, 'site'),
847 'apiEndpoint' => ($endpoint ? $endpoint : AAM_Core_Server::SERVER_URL)
848 ),
849 'translation' => AAM_Backend_View_Localization::get(),
850 'caps' => array(
851 'create_roles' => current_user_can('aam_create_roles'),
852 'create_users' => current_user_can('create_users')
853 )
854 );
855
856 wp_localize_script($localKey, 'aamLocal', $locals);
857 }
858
859 /**
860 * Print necessary styles
861 *
862 * @return void
863 *
864 * @access public
865 */
866 public function printStylesheet() {
867 if (AAM::isAAM()) {
868 wp_enqueue_style('aam-bt', AAM_MEDIA . '/css/bootstrap.min.css');
869 wp_enqueue_style('aam-db', AAM_MEDIA . '/css/datatables.min.css');
870 wp_enqueue_style('aam-main', AAM_MEDIA . '/css/aam.css');
871 }
872 }
873
874 /**
875 * Register Admin Menu
876 *
877 * @return void
878 *
879 * @access public
880 */
881 public function adminMenu() {
882 if (AAM_Core_Console::count() && current_user_can('aam_show_notifications')) {
883 $counter = '&nbsp;<span class="update-plugins">'
884 . '<span class="plugin-count">' . AAM_Core_Console::count()
885 . '</span></span>';
886 } else {
887 $counter = '';
888 }
889
890 //register the menu
891 add_menu_page(
892 'AAM',
893 'AAM' . $counter,
894 'aam_manager',
895 'aam',
896 array($this, 'renderPage'),
897 AAM_MEDIA . '/active-menu.svg'
898 );
899
900 // Access policy page
901 add_submenu_page(
902 'aam',
903 'Access Policies',
904 'Access Policies',
905 AAM_Core_Config::get('policy.capability', 'aam_manage_policy'),
906 'edit.php?post_type=aam_policy'
907 );
908 }
909
910 /**
911 * Render Main Content page
912 *
913 * @return void
914 *
915 * @access public
916 */
917 public function renderPage() {
918 echo AAM_Backend_View::getInstance()->renderPage();
919 }
920
921 /**
922 * Render list of AAM Features
923 *
924 * Must be separate from Ajax call because WordPress ajax does not load
925 * a lot of UI stuff like admin menu
926 *
927 * @return void
928 *
929 * @access public
930 */
931 public function renderContent() {
932 check_ajax_referer('aam_ajax');
933
934 // flush any output buffer
935 @ob_clean();
936
937 if (AAM::getUser()->hasCapability('aam_manager')) {
938 $response = AAM_Backend_View::getInstance()->renderContent(
939 AAM_Core_Request::post('uiType', 'main')
940 );
941
942 $accept = AAM_Core_Request::server('HTTP_ACCEPT_ENCODING');
943 header('Content-Type: text/html; charset=UTF-8');
944
945 $zlib = strtolower(ini_get('zlib.output_compression'));
946 $compressed = count(array_intersect(
947 array('zlib output compression', 'ob_gzhandler'),
948 ob_list_handlers()
949 )) > 0;
950
951 if (in_array($zlib, array('1', 'on'), true) && !empty($accept)) {
952 header('Vary: Accept-Encoding'); // Handle proxies
953
954 if ( false !== stripos($accept, 'gzip') && function_exists('gzencode') ) {
955 header('Content-Encoding: gzip');
956 $response = ($compressed ? $response : gzencode($response, 3));
957 }
958 }
959
960 echo $response;
961 } else {
962 echo __('Access Denied', AAM_KEY);
963 }
964
965 exit();
966 }
967
968 /**
969 * Handle Ajax calls to AAM
970 *
971 * @return void
972 *
973 * @access public
974 */
975 public function ajax() {
976 check_ajax_referer('aam_ajax');
977
978 //clean buffer to make sure that nothing messing around with system
979 while (@ob_end_clean()){}
980
981 //process ajax request
982 if (AAM::getUser()->hasCapability('aam_manager')) {
983 echo AAM_Backend_View::getInstance()->processAjax();
984 } else {
985 echo __('Access Denied', AAM_KEY);
986 }
987
988 exit();
989 }
990
991 /**
992 * Bootstrap the manager
993 *
994 * @return AAM_Backend_View
995 *
996 * @access public
997 */
998 public static function bootstrap() {
999 if (is_null(self::$_instance)) {
1000 self::$_instance = new self;
1001 }
1002
1003 return self::$_instance;
1004 }
1005
1006 /**
1007 * Get instance of itself
1008 *
1009 * @return AAM_Backend_View
1010 *
1011 * @access public
1012 */
1013 public static function getInstance() {
1014 return self::bootstrap();
1015 }
1016
1017 }