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