aam.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Plugin Name: Advanced Access Manager |
| 5 | * Description: Powerfully robust WordPress plugin designed to help you control every aspect of your website, your way. |
| 6 | * Version: 6.9.26 |
| 7 | * Author: AAM <support@aamplugin.com> |
| 8 | * Author URI: https://aamportal.com |
| 9 | * Text Domain: advanced-access-manager |
| 10 | * Domain Path: /lang/ |
| 11 | * |
| 12 | * ------- |
| 13 | * LICENSE: This file is subject to the terms and conditions defined in |
| 14 | * file 'license.txt', which is part of Advanced Access Manager source package. |
| 15 | * |
| 16 | **/ |
| 17 | |
| 18 | /** |
| 19 | * Main plugin's class |
| 20 | * |
| 21 | * @since 6.9.17 https://github.com/aamplugin/advanced-access-manager/issues/325 |
| 22 | * @since 6.9.13 https://github.com/aamplugin/advanced-access-manager/issues/300 |
| 23 | * @since 6.9.12 https://github.com/aamplugin/advanced-access-manager/issues/286 |
| 24 | * @since 6.9.11 https://github.com/aamplugin/advanced-access-manager/issues/282 |
| 25 | * @since 6.9.4 https://github.com/aamplugin/advanced-access-manager/issues/238 |
| 26 | * @since 6.0.0 Initial implementation of the class |
| 27 | * |
| 28 | * @package AAM |
| 29 | * @author AAM <support@aamplugin.com> |
| 30 | * |
| 31 | * @version 6.9.17 |
| 32 | */ |
| 33 | class AAM |
| 34 | { |
| 35 | |
| 36 | /** |
| 37 | * Single instance of itself |
| 38 | * |
| 39 | * @var AAM |
| 40 | * |
| 41 | * @access private |
| 42 | * @version 6.0.0 |
| 43 | */ |
| 44 | private static $_instance = null; |
| 45 | |
| 46 | /** |
| 47 | * User Subject |
| 48 | * |
| 49 | * @var AAM_Core_Subject_User|AAM_Core_Subject_Visitor |
| 50 | * |
| 51 | * @access private |
| 52 | * @version 6.0.0 |
| 53 | */ |
| 54 | private $_user = null; |
| 55 | |
| 56 | /** |
| 57 | * Initialize the AAM Object |
| 58 | * |
| 59 | * @return void |
| 60 | * |
| 61 | * @since 6.9.12 https://github.com/aamplugin/advanced-access-manager/issues/286 |
| 62 | * @since 6.0.0 Initial implementation of the method |
| 63 | * |
| 64 | * @access protected |
| 65 | * @version 6.9.12 |
| 66 | */ |
| 67 | protected function __construct() |
| 68 | { |
| 69 | // Initialize current user |
| 70 | $this->initializeUser(); |
| 71 | |
| 72 | // Make sure if user is changed dynamically, AAM adjusts accordingly |
| 73 | add_action('set_current_user', function() { |
| 74 | $this->initializeUser(); |
| 75 | }); |
| 76 | |
| 77 | // The same with with after user login. WordPress core has bug with this |
| 78 | add_action('wp_login', function($_, $user) { |
| 79 | $this->initializeUser($user); |
| 80 | }, 10, 2); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set Current User |
| 85 | * |
| 86 | * @param AAM_Core_Subject $user |
| 87 | * |
| 88 | * @return void |
| 89 | * |
| 90 | * @access public |
| 91 | * @version 6.0.0 |
| 92 | */ |
| 93 | public function setUser(AAM_Core_Subject $user) |
| 94 | { |
| 95 | $this->_user = $user; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get AAM API manager |
| 100 | * |
| 101 | * @return AAM_Core_Gateway |
| 102 | * |
| 103 | * @access public |
| 104 | * @version 6.0.0 |
| 105 | */ |
| 106 | public static function api() |
| 107 | { |
| 108 | return AAM_Core_Gateway::getInstance(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get current user |
| 113 | * |
| 114 | * @return AAM_Core_Subject |
| 115 | * |
| 116 | * @access public |
| 117 | * @version 6.0.0 |
| 118 | */ |
| 119 | public static function getUser() |
| 120 | { |
| 121 | return self::getInstance()->_user; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Change current user |
| 126 | * |
| 127 | * This method is triggered if some process updates current user |
| 128 | * |
| 129 | * @return AAM_Core_Subject |
| 130 | * |
| 131 | * @since 6.9.13 https://github.com/aamplugin/advanced-access-manager/issues/300 |
| 132 | * @since 6.9.12 https://github.com/aamplugin/advanced-access-manager/issues/286 |
| 133 | * @since 6.0.0 Initial implementation of the method |
| 134 | * |
| 135 | * @access public |
| 136 | * @version 6.9.13 |
| 137 | */ |
| 138 | public function initializeUser($user = null) |
| 139 | { |
| 140 | global $current_user; |
| 141 | |
| 142 | // Important! Do not use WP core function to avoid loop |
| 143 | if (is_a($user, 'WP_User')) { |
| 144 | $id = $user->ID; |
| 145 | } else { |
| 146 | $id = (is_a($current_user, 'WP_User') ? $current_user->ID : null); |
| 147 | } |
| 148 | |
| 149 | // Change current user |
| 150 | if ($id) { |
| 151 | $user = new AAM_Core_Subject_User($id); |
| 152 | } else { |
| 153 | $user = new AAM_Core_Subject_Visitor(); |
| 154 | } |
| 155 | |
| 156 | $this->setUser($user); |
| 157 | |
| 158 | $user->initialize(); |
| 159 | |
| 160 | return $user; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Make sure that AAM UI Page is used |
| 165 | * |
| 166 | * @return boolean |
| 167 | * |
| 168 | * @access public |
| 169 | * @version 6.0.0 |
| 170 | */ |
| 171 | public static function isAAM() |
| 172 | { |
| 173 | $page = filter_input(INPUT_GET, 'page'); |
| 174 | $action = filter_input(INPUT_POST, 'action'); |
| 175 | |
| 176 | $intersect = array_intersect(array('aam', 'aamc'), array($page, $action)); |
| 177 | |
| 178 | return (is_admin() && count($intersect)); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Bootstrap AAM when all plugins are loaded |
| 183 | * |
| 184 | * @return void |
| 185 | * |
| 186 | * @since 6.9.4 https://github.com/aamplugin/advanced-access-manager/issues/238 |
| 187 | * @since 6.0.0 Initial implementation of the method |
| 188 | * |
| 189 | * @access public |
| 190 | * @version 6.9.4 |
| 191 | */ |
| 192 | public static function onPluginsLoaded() |
| 193 | { |
| 194 | // Load AAM core config |
| 195 | AAM_Core_Config::bootstrap(); |
| 196 | |
| 197 | // Load the core service first |
| 198 | require_once __DIR__ . '/application/Service/Core.php'; |
| 199 | |
| 200 | // Load all the defined AAM services |
| 201 | foreach (new DirectoryIterator(__DIR__ . '/application/Service') as $service) { |
| 202 | if ($service->isFile()) { |
| 203 | require_once $service->getPathname(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | do_action('aam_services_loaded'); |
| 208 | |
| 209 | // Load AAM |
| 210 | AAM::getInstance(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Hook on WP core init |
| 215 | * |
| 216 | * @return void |
| 217 | * |
| 218 | * @access public |
| 219 | * @version 6.0.0 |
| 220 | */ |
| 221 | public static function onInit() |
| 222 | { |
| 223 | if (is_admin()) { |
| 224 | AAM_Backend_Manager::bootstrap(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Initialize the AAM plugin |
| 230 | * |
| 231 | * @return AAM |
| 232 | * |
| 233 | * @access public |
| 234 | * @version 6.0.0 |
| 235 | */ |
| 236 | public static function getInstance() |
| 237 | { |
| 238 | if (is_null(self::$_instance)) { |
| 239 | self::$_instance = new self; |
| 240 | |
| 241 | // Load AAM internationalization |
| 242 | load_plugin_textdomain(AAM_KEY, false, 'advanced-access-manager/lang'); |
| 243 | |
| 244 | // Validate logged in user status |
| 245 | if (is_user_logged_in()) { |
| 246 | AAM::getUser()->validateStatus(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return self::$_instance; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Activation hook |
| 255 | * |
| 256 | * @return void |
| 257 | * |
| 258 | * @since 6.9.17 https://github.com/aamplugin/advanced-access-manager/issues/325 |
| 259 | * @since 6.0.0 Initial implementation of the method |
| 260 | * |
| 261 | * @access public |
| 262 | * @version 6.9.17 |
| 263 | */ |
| 264 | public static function activate() |
| 265 | { |
| 266 | global $wp_version; |
| 267 | |
| 268 | //check PHP Version |
| 269 | if (version_compare(PHP_VERSION, '5.6.40') === -1) { |
| 270 | exit(__('PHP 5.6.40 or higher is required.', AAM_KEY)); |
| 271 | } elseif (version_compare($wp_version, '5.0.0') === -1) { |
| 272 | exit(__('WP 5.0.0 or higher is required.', AAM_KEY)); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Redirect user to AAM page after plugin activation |
| 278 | * |
| 279 | * @param string $plugin |
| 280 | * |
| 281 | * @return void |
| 282 | * |
| 283 | * @access public |
| 284 | * @static |
| 285 | * @version 6.9.11 |
| 286 | */ |
| 287 | public static function afterActivation($plugin) |
| 288 | { |
| 289 | if ( |
| 290 | $plugin === "advanced-access-manager/aam.php" |
| 291 | && !is_network_admin() |
| 292 | && AAM_Core_Request::server('REQUEST_METHOD') === 'GET' |
| 293 | && AAM_Core_Request::get('action') === 'activate' |
| 294 | && AAM_Core_Request::server('SCRIPT_NAME') === '/wp-admin/plugins.php' |
| 295 | ) { |
| 296 | wp_redirect(admin_url('admin.php?page=aam')); exit; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Deactivate hook |
| 302 | * |
| 303 | * Remove all leftovers from AAM execution |
| 304 | * |
| 305 | * @return void |
| 306 | * |
| 307 | * @access public |
| 308 | * @version 6.0.0 |
| 309 | */ |
| 310 | public static function uninstall() |
| 311 | { |
| 312 | //trigger any uninstall hook that is registered by any extension |
| 313 | do_action('aam-uninstall-action'); |
| 314 | |
| 315 | //clear all AAM settings |
| 316 | AAM_Core_API::clearSettings(); |
| 317 | } |
| 318 | |
| 319 | } |
| 320 | |
| 321 | if (defined('ABSPATH')) { |
| 322 | // Define few common constants |
| 323 | define('AAM_MEDIA', plugins_url('/media', __FILE__)); |
| 324 | define('AAM_KEY', 'advanced-access-manager'); |
| 325 | define('AAM_VERSION', '6.9.26'); |
| 326 | define('AAM_BASEDIR', __DIR__); |
| 327 | |
| 328 | // Load vendor |
| 329 | require __DIR__ . '/vendor/autoload.php'; |
| 330 | |
| 331 | // Register autoloader |
| 332 | require(__DIR__ . '/autoloader.php'); |
| 333 | AAM_Autoloader::register(); |
| 334 | |
| 335 | // Keep this as the lowest priority |
| 336 | add_action('plugins_loaded', 'AAM::onPluginsLoaded', -999); |
| 337 | |
| 338 | // The highest priority (higher the core) |
| 339 | // this is important to have to catch events like register core post types |
| 340 | add_action('init', 'AAM::onInit', -1); |
| 341 | |
| 342 | // Activation & deactivation hooks |
| 343 | register_activation_hook(__FILE__, array('AAM', 'activate')); |
| 344 | register_uninstall_hook(__FILE__, array('AAM', 'uninstall')); |
| 345 | |
| 346 | // Improve user experience by redirecting user to the AAM page after it is |
| 347 | // activated |
| 348 | add_action('activated_plugin', array('AAM', 'afterActivation')); |
| 349 | } |