PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 5.3.5
Advanced Access Manager – Access Governance for WordPress v5.3.5
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 8 years ago View 8 years ago Widget 8 years ago phtml 8 years ago Authorization.php 8 years ago Feature.php 8 years ago Filter.php 8 years ago Manager.php 8 years ago Subject.php 8 years ago View.php 8 years ago
Manager.php
713 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_styles', array($this, 'printStylesheet'));
47
48 //map AAM UI specific capabilities
49 add_filter('map_meta_cap', array($this, 'mapMetaCap'), 10, 4);
50
51 //user profile update action
52 add_action('profile_update', array($this, 'profileUpdate'), 10, 2);
53
54 //post title decorator
55 add_filter('the_title', array($this, 'theTitle'), 999, 2);
56
57 //permalink manager
58 add_filter('get_sample_permalink_html', array($this, 'getPermalinkHtml'), 10, 5);
59
60 //screen options & contextual help hooks
61 add_filter('screen_options_show_screen', array($this, 'screenOptions'));
62 add_filter('contextual_help', array($this, 'helpOptions'), 10, 3);
63
64 //manager Admin Menu
65 if (is_multisite() && is_network_admin()) {
66 //register AAM in the network admin panel
67 add_action('network_admin_menu', array($this, 'adminMenu'));
68 } else {
69 add_action('admin_menu', array($this, 'adminMenu'));
70 add_action('all_admin_notices', array($this, 'notification'));
71 }
72
73 if (AAM_Core_Config::get('ui.settings.renderAccessMetabox', true)) {
74 add_action('edit_category_form_fields', array($this, 'renderTermMetabox'), 1);
75 add_action('edit_link_category_form_fields', array($this, 'renderTermMetabox'), 1);
76 add_action('edit_tag_form_fields', array($this, 'renderTermMetabox'), 1);
77
78 //register custom access control metabox
79 add_action('add_meta_boxes', array($this, 'metabox'));
80 }
81
82 //manager AAM Ajax Requests
83 add_action('wp_ajax_aam', array($this, 'ajax'));
84 //manager AAM Features Content rendering
85 add_action('admin_action_aamc', array($this, 'renderContent'));
86 //manager user search and authentication control
87 add_filter('user_search_columns', array($this, 'searchColumns'));
88
89 //manager WordPress metaboxes
90 add_action("in_admin_header", array($this, 'initMetaboxes'), 999);
91
92 if (AAM_Core_Config::get('ui.settings.renderAccessActionLink', true)) {
93 //extend post inline actions
94 add_filter('page_row_actions', array($this, 'postRowActions'), 10, 2);
95 add_filter('post_row_actions', array($this, 'postRowActions'), 10, 2);
96
97 //extend term inline actions
98 add_filter('tag_row_actions', array($this, 'tagRowActions'), 10, 2);
99
100 //manage access action to the user list
101 add_filter('user_row_actions', array($this, 'userActions'), 10, 2);
102 }
103
104 //footer thank you
105 add_filter('admin_footer_text', array($this, 'thankYou'), 999);
106
107 //control admin area
108 add_action('admin_init', array($this, 'adminInit'));
109
110 //register login widget
111 if (AAM_Core_Config::get('core.settings.secureLogin', true)) {
112 add_action('widgets_init', array($this, 'registerLoginWidget'));
113 add_action('wp_ajax_nopriv_aamlogin', array($this, 'handleLogin'));
114 }
115
116 //register backend hooks and filters
117 if (AAM_Core_Config::get('core.settings.backendAccessControl', true)) {
118 AAM_Backend_Filter::register();
119 }
120
121 AAM_Extension_Repository::getInstance()->hasUpdates();
122
123 if (version_compare(PHP_VERSION, '5.3.0') == -1) {
124 AAM_Core_Console::add(
125 'AAM requires PHP version 5.3.0 or higher to function properly'
126 );
127 }
128 }
129
130 /**
131 *
132 * @param type $caps
133 * @param type $cap
134 * @return type
135 */
136 public function mapMetaCap($caps, $cap) {
137 if (in_array($cap, AAM_Backend_Feature_Main_Capability::$groups['aam'])) {
138 if (!AAM_Core_API::capabilityExists($cap)) {
139 $caps = array(AAM_Core_Config::get('page.capability', 'administrator'));
140 }
141 }
142
143 return $caps;
144 }
145
146 /**
147 *
148 * @param string $html
149 * @return string
150 */
151 public function getPermalinkHtml($html) {
152 if (AAM_Core_API::capabilityExists('edit_permalink')
153 && !AAM::getUser()->hasCapability('edit_permalink')) {
154 $html = '';
155 }
156
157 return $html;
158 }
159
160 /**
161 * Profile updated hook
162 *
163 * Adjust expiration time and user cache if profile updated
164 *
165 * @param int $id
166 * @param WP_User $old
167 *
168 * @return void
169 *
170 * @access public
171 */
172 public function profileUpdate($id, $old) {
173 $user = get_user_by('ID', $id);
174
175 //role changed?
176 if (implode('', $user->roles) != implode('', $old->roles)) {
177 AAM_Core_API::clearCache(new AAM_Core_Subject_User($id));
178
179 //check if role has expiration data set
180 $role = (is_array($user->roles) ? $user->roles[0] : '');
181 $expire = AAM_Core_API::getOption("aam-role-{$role}-expiration", '');
182
183 if ($expire) {
184 update_user_option($id, "aam-original-roles", $old->roles);
185 update_user_option($id, "aam-role-expires", strtotime($expire));
186 }
187 }
188 }
189
190 /**
191 * Filter post title
192 *
193 * @param string $title
194 * @param int $id
195 *
196 * @return string
197 *
198 * @access public
199 */
200 public function theTitle($title, $id = null) {
201 if (empty($title) && AAM::isAAM()) { //apply filter only for AAM page
202 $title = '[No Title]: ID ' . ($id ? $id : '[No ID]');
203 }
204
205 return $title;
206 }
207
208 /**
209 *
210 * @param type $flag
211 * @return type
212 */
213 public function screenOptions($flag) {
214 if (AAM_Core_API::capabilityExists('show_screen_options')) {
215 $flag = AAM::getUser()->hasCapability('show_screen_options');
216 }
217
218 if (AAM::isAAM()) {
219 $flag = false;
220 }
221
222 return $flag;
223 }
224
225 /**
226 *
227 * @param array $help
228 * @param type $id
229 * @param type $screen
230 * @return array
231 */
232 public function helpOptions($help, $id, $screen) {
233 if (AAM_Core_API::capabilityExists('show_help_tabs')) {
234 if (!AAM::getUser()->hasCapability('show_help_tabs')) {
235 $screen->remove_help_tabs();
236 $help = array();
237 }
238 }
239
240 if (AAM::isAAM()) {
241 $screen->remove_help_tabs();
242 }
243
244 return $help;
245 }
246
247 /**
248 *
249 * @return type
250 */
251 public function handleLogin() {
252 $login = AAM_Core_Login::getInstance();
253
254 echo json_encode($login->execute());
255 exit;
256 }
257
258 /**
259 *
260 */
261 public function adminInit() {
262 $user = AAM::getUser();
263 $frame = AAM_Core_Request::get('aamframe');
264
265 if ($frame && $user->hasCapability('aam_manage_posts')) {
266 echo AAM_Backend_View::getInstance()->renderAccessFrame();
267 exit;
268 }
269 }
270
271 /**
272 *
273 * @param type $text
274 * @return string
275 */
276 public function thankYou($text) {
277 if (AAM::isAAM()) {
278 $text = '<span id="footer-thankyou">';
279 $text .= '<b>Please help us</b> and submit your review <a href="';
280 $text .= 'https://wordpress.org/support/plugin/advanced-access-manager/reviews/"';
281 $text .= 'target="_blank"><i class="icon-star"></i>';
282 $text .= '<i class="icon-star"></i><i class="icon-star"></i>';
283 $text .= '<i class="icon-star"></i><i class="icon-star"></i></a>';
284 $text .= '</span>';
285 }
286
287 return $text;
288 }
289
290 /**
291 *
292 */
293 public function registerLoginWidget() {
294 register_widget('AAM_Backend_Widget_Login');
295 }
296
297 /**
298 *
299 */
300 protected function checkUserAccess() {
301 $uid = get_current_user_id();
302
303 if ($uid && AAM_Core_API::capabilityExists('access_dashboard')) {
304 $caps = AAM::getUser()->allcaps;
305 if (empty($caps['access_dashboard'])) {
306 //also additionally check for AJAX calls
307 if (defined('DOING_AJAX') && empty($caps['allow_ajax_calls'])) {
308 AAM_Core_API::reject(
309 'backend', array('hook' => 'access_dashboard')
310 );
311 } elseif (!defined('DOING_AJAX')) {
312 AAM_Core_API::reject(
313 'backend', array('hook' => 'access_dashboard')
314 );
315 }
316 }
317 }
318 }
319
320 /**
321 *
322 */
323 protected function checkUserSwitch() {
324 if (AAM_Core_Request::get('action') == 'aam-switch-back') {
325 $current = get_current_user_id();
326 $uid = AAM_Core_API::getOption('aam-user-switch-' . $current);
327 $redirect = admin_url('admin.php?page=aam&user=' . $current);
328
329 check_admin_referer('aam-switch-' . $uid);
330
331 wp_clear_auth_cookie();
332 wp_set_auth_cookie( $uid, true );
333 wp_set_current_user( $uid );
334
335 AAM_Core_API::deleteOption('aam-user-switch-' . $current);
336
337 wp_redirect($redirect);
338 exit;
339 }
340 }
341
342 /**
343 *
344 */
345 public function notification() {
346 $uid = AAM_Core_API::getOption('aam-user-switch-' . get_current_user_id());
347
348 if ($uid) {
349 //get user's name
350 $user = new WP_User($uid);
351 $name = $user->display_name ? $user->display_name : $user->user_nicename;
352
353 //generate switch back URL
354 $url = wp_nonce_url(
355 'index.php?action=aam-switch-back', 'aam-switch-' . $uid
356 );
357
358 $style = 'padding: 10px; font-weight: 700; letter-spacing:0.5px;';
359
360 echo '<div class="updated notice">';
361 echo '<p style="' . $style . '">';
362 echo sprintf('Switch back to <a href="%s">%s</a>.', $url, $name);
363 echo '</p></div>';
364 }
365 }
366
367 /**
368 *
369 */
370 public function metabox() {
371 $frontend = AAM_Core_Config::get('core.settings.frontendAccessControl', true);
372 $backend = AAM_Core_Config::get('core.settings.backendAccessControl', true);
373 $api = AAM_Core_Config::get('core.settings.apiAccessControl', true);
374
375 if (($frontend || $backend || $api) && AAM::getUser()->hasCapability('aam_manage_posts')) {
376 add_meta_box(
377 'aam-acceess-manager',
378 __('Access Manager', AAM_KEY) . ' <small style="color:#999999;">by AAM plugin</small>',
379 array($this, 'renderPostMetabox'),
380 null,
381 'advanced',
382 'high'
383 );
384 }
385 }
386
387 /**
388 *
389 * @global type $post
390 */
391 public function renderPostMetabox() {
392 global $post;
393
394 if (is_a($post, 'WP_Post')) {
395 echo AAM_Backend_View::getInstance()->renderPostMetabox($post);
396 }
397 }
398
399 /**
400 *
401 * @param type $term
402 */
403 public function renderTermMetabox($term) {
404 if (is_a($term, 'WP_Term') && is_taxonomy_hierarchical($term->taxonomy)) {
405 $frontend = AAM_Core_Config::get('core.settings.frontendAccessControl', true);
406 $backend = AAM_Core_Config::get('core.settings.backendAccessControl', true);
407 $api = AAM_Core_Config::get('core.settings.apiAccessControl', true);
408
409 if (($frontend || $backend || $api) && AAM::getUser()->hasCapability('aam_manage_posts')) {
410 echo AAM_Backend_View::getInstance()->renderTermMetabox($term);
411 }
412 }
413 }
414
415 /**
416 * Hanlde Metabox initialization process
417 *
418 * @return void
419 *
420 * @access public
421 */
422 public function initMetaboxes() {
423 global $post;
424
425 if (AAM_Core_Request::get('init') == 'metabox') {
426 //make sure that nobody is playing with screen options
427 if (is_a($post, 'WP_Post')) {
428 $screen = $post->post_type;
429 } elseif ($screen_object = get_current_screen()) {
430 $screen = $screen_object->id;
431 } else {
432 $screen = '';
433 }
434
435 $model = new AAM_Backend_Feature_Main_Metabox;
436 $model->initialize($screen);
437 }
438 }
439
440 /**
441 * Add extra column to search in for User search
442 *
443 * @param array $columns
444 *
445 * @return array
446 *
447 * @access public
448 */
449 public function searchColumns($columns) {
450 $columns[] = 'display_name';
451
452 return $columns;
453 }
454
455 /**
456 *
457 * @param type $actions
458 * @param type $post
459 * @return string
460 */
461 public function postRowActions($actions, $post) {
462 if ($this->renderExternalUIFeature('aam_manage_posts')) {
463 $url = admin_url('admin.php?page=aam&oid=' . $post->ID . '&otype=post#post');
464
465 $actions['aam'] = '<a href="' . $url . '" target="_blank">';
466 $actions['aam'] .= __('Access', AAM_KEY) . '</a>';
467 }
468
469 return $actions;
470 }
471
472 /**
473 *
474 * @param type $actions
475 * @param type $term
476 * @return string
477 */
478 public function tagRowActions($actions, $term) {
479 if ($this->renderExternalUIFeature('aam_manage_posts')) {
480 $oid = $term->term_id . '|' . $term->taxonomy;
481 $url = admin_url('admin.php?page=aam&oid=' . $oid . '&otype=term#post');
482
483 $actions['aam'] = '<a href="' . $url . '" target="_blank">';
484 $actions['aam'] .= __('Access', AAM_KEY) . '</a>';
485 }
486
487 return $actions;
488 }
489
490 /**
491 * Add "Manage Access" action
492 *
493 * Add additional action to the user list table.
494 *
495 * @param array $actions
496 * @param WP_User $user
497 *
498 * @return array
499 *
500 * @access public
501 */
502 public function userActions($actions, $user) {
503 if ($this->renderExternalUIFeature('list_users')) {
504 $url = admin_url('admin.php?page=aam&user=' . $user->ID);
505
506 $actions['aam'] = '<a href="' . $url . '" target="_blank">';
507 $actions['aam'] .= __('Access', AAM_KEY) . '</a>';
508 }
509
510 return $actions;
511 }
512
513 /**
514 *
515 * @param type $cap
516 * @return type
517 */
518 protected function renderExternalUIFeature($cap) {
519 $frontend = AAM_Core_Config::get('core.settings.frontendAccessControl', true);
520 $backend = AAM_Core_Config::get('core.settings.backendAccessControl', true);
521 $api = AAM_Core_Config::get('core.settings.apiAccessControl', true);
522 $aamManager = AAM::getUser()->hasCapability('aam_manager');
523 $featureManager = AAM::getUser()->hasCapability($cap);
524
525 return ($frontend || $backend || $api) && $aamManager && $featureManager;
526 }
527
528 /**
529 * Print javascript libraries
530 *
531 * @return void
532 *
533 * @access public
534 */
535 public function printJavascript() {
536 if (AAM::isAAM()) {
537 wp_enqueue_script('aam-vendor', AAM_MEDIA . '/js/vendor.js');
538 wp_enqueue_script('aam-main', AAM_MEDIA . '/js/aam.js');
539
540 //add plugin localization
541 $this->printLocalization('aam-main');
542 }
543 }
544
545 /**
546 * Print plugin localization
547 *
548 * @param string $localKey
549 *
550 * @return void
551 *
552 * @access protected
553 */
554 protected function printLocalization($localKey) {
555 $subject = AAM_Backend_Subject::getInstance();
556
557 $locals = array(
558 'nonce' => wp_create_nonce('aam_ajax'),
559 'ajaxurl' => admin_url('admin-ajax.php'),
560 'url' => array(
561 'site' => admin_url('index.php'),
562 'editUser' => admin_url('user-edit.php'),
563 'addUser' => admin_url('user-new.php')
564 ),
565 'level' => AAM_Core_API::maxLevel(wp_get_current_user()->allcaps),
566 'subject' => array(
567 'type' => $subject->getUID(),
568 'id' => $subject->getId(),
569 'name' => $subject->getName(),
570 'level' => $subject->getMaxLevel(),
571 'blog' => get_current_blog_id()
572 ),
573 'translation' => AAM_Backend_View_Localization::get(),
574 'caps' => array(
575 'create_roles' => current_user_can('aam_create_roles'),
576 'create_users' => current_user_can('create_users')
577 )
578 );
579
580 if (AAM_Core_Request::get('aamframe')) {
581 $locals['ui'] = 'post';
582 }
583
584 wp_localize_script($localKey, 'aamLocal', $locals);
585 }
586
587 /**
588 * Print necessary styles
589 *
590 * @return void
591 *
592 * @access public
593 */
594 public function printStylesheet() {
595 if (AAM::isAAM()) {
596 wp_enqueue_style('aam-bt', AAM_MEDIA . '/css/bootstrap.min.css');
597 wp_enqueue_style('aam-db', AAM_MEDIA . '/css/datatables.min.css');
598 wp_enqueue_style('aam-main', AAM_MEDIA . '/css/aam.css');
599 }
600 }
601
602 /**
603 * Register Admin Menu
604 *
605 * @return void
606 *
607 * @access public
608 */
609 public function adminMenu() {
610 if (AAM_Core_Console::count()) {
611 $counter = '&nbsp;<span class="update-plugins">'
612 . '<span class="plugin-count">' . AAM_Core_Console::count()
613 . '</span></span>';
614 } else {
615 $counter = '';
616 }
617
618 //register the menu
619 add_menu_page(
620 'AAM',
621 'AAM' . $counter,
622 'aam_manager',
623 'aam',
624 array($this, 'renderPage'),
625 AAM_MEDIA . '/active-menu.svg'
626 );
627 }
628
629 /**
630 * Render Main Content page
631 *
632 * @return void
633 *
634 * @access public
635 */
636 public function renderPage() {
637 echo AAM_Backend_View::getInstance()->renderPage();
638 }
639
640 /**
641 * Render list of AAM Features
642 *
643 * Must be separate from Ajax call because WordPress ajax does not load
644 * a lot of UI stuff like admin menu
645 *
646 * @return void
647 *
648 * @access public
649 */
650 public function renderContent() {
651 check_ajax_referer('aam_ajax');
652
653 if (AAM::getUser()->hasCapability('aam_manager')) {
654 echo AAM_Backend_View::getInstance()->renderContent(
655 AAM_Core_Request::post('uiType', 'main')
656 );
657 } else {
658 echo __('Access Denied', AAM_KEY);
659 }
660
661 exit();
662 }
663
664 /**
665 * Handle Ajax calls to AAM
666 *
667 * @return void
668 *
669 * @access public
670 */
671 public function ajax() {
672 check_ajax_referer('aam_ajax');
673
674 //clean buffer to make sure that nothing messing around with system
675 while (@ob_end_clean()){}
676
677 //process ajax request
678 if (AAM::getUser()->hasCapability('aam_manager')) {
679 echo AAM_Backend_View::getInstance()->processAjax();
680 } else {
681 echo __('Access Denied', AAM_KEY);
682 }
683
684 exit();
685 }
686
687 /**
688 * Bootstrap the manager
689 *
690 * @return AAM_Backend_View
691 *
692 * @access public
693 */
694 public static function bootstrap() {
695 if (is_null(self::$_instance)) {
696 self::$_instance = new self;
697 }
698
699 return self::$_instance;
700 }
701
702 /**
703 * Get instance of itself
704 *
705 * @return AAM_Backend_View
706 *
707 * @access public
708 */
709 public static function getInstance() {
710 return self::bootstrap();
711 }
712
713 }