LoginRedirect.php
| 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 | * Login Redirect service |
| 12 | * |
| 13 | * @package AAM |
| 14 | * |
| 15 | * @since 6.6.2 https://github.com/aamplugin/advanced-access-manager/issues/139 |
| 16 | * @since 6.5.0 Fixed https://github.com/aamplugin/advanced-access-manager/issues/98 |
| 17 | * @since 6.4.0 Fixed https://github.com/aamplugin/advanced-access-manager/issues/76 |
| 18 | * @since 6.0.0 Initial implementation of the class |
| 19 | * |
| 20 | * @version 6.6.2 |
| 21 | */ |
| 22 | class AAM_Service_LoginRedirect |
| 23 | { |
| 24 | use AAM_Core_Contract_ServiceTrait; |
| 25 | |
| 26 | /** |
| 27 | * AAM configuration setting that is associated with the service |
| 28 | * |
| 29 | * @version 6.0.0 |
| 30 | */ |
| 31 | const FEATURE_FLAG = 'core.service.login-redirect.enabled'; |
| 32 | |
| 33 | /** |
| 34 | * Constructor |
| 35 | * |
| 36 | * @return void |
| 37 | * |
| 38 | * @access protected |
| 39 | * @version 6.0.0 |
| 40 | */ |
| 41 | protected function __construct() |
| 42 | { |
| 43 | if (is_admin()) { |
| 44 | // Hook that initialize the AAM UI part of the service |
| 45 | if (AAM_Core_Config::get(self::FEATURE_FLAG, true)) { |
| 46 | add_action('aam_init_ui_action', function () { |
| 47 | AAM_Backend_Feature_Main_LoginRedirect::register(); |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | // Hook that returns the detailed information about the nature of the |
| 52 | // service. This is used to display information about service on the |
| 53 | // Settings->Services tab |
| 54 | add_filter('aam_service_list_filter', function ($services) { |
| 55 | $services[] = array( |
| 56 | 'title' => __('Login Redirect', AAM_KEY), |
| 57 | 'description' => __('Manage login redirect for any group of users or individual user when authentication is completed successfully.', AAM_KEY), |
| 58 | 'setting' => self::FEATURE_FLAG |
| 59 | ); |
| 60 | |
| 61 | return $services; |
| 62 | }, 30); |
| 63 | } |
| 64 | |
| 65 | // Hook into the WP core processes |
| 66 | if (AAM_Core_Config::get(self::FEATURE_FLAG, true)) { |
| 67 | $this->initializeHooks(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Initialize service hooks |
| 73 | * |
| 74 | * @return void |
| 75 | * |
| 76 | * @since 6.6.2 https://github.com/aamplugin/advanced-access-manager/issues/139 |
| 77 | * @since 6.4.0 Fixed https://github.com/aamplugin/advanced-access-manager/issues/76 |
| 78 | * @since 6.0.0 Initial implementation of the method |
| 79 | * |
| 80 | * @access protected |
| 81 | * @version 6.6.2 |
| 82 | */ |
| 83 | protected function initializeHooks() |
| 84 | { |
| 85 | // AAM Secure Login hooking |
| 86 | add_filter('aam_auth_response_filter', array($this, 'prepareLoginResponse'), 10, 3); |
| 87 | |
| 88 | // WP Core login redirect hook |
| 89 | add_filter('login_redirect', array($this, 'getLoginRedirect'), 10, 3); |
| 90 | |
| 91 | // Policy generation hook |
| 92 | add_filter( |
| 93 | 'aam_generated_policy_filter', array($this, 'generatePolicy'), 10, 4 |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Generate Login Redirect policy params |
| 99 | * |
| 100 | * @param array $policy |
| 101 | * @param string $resource_type |
| 102 | * @param array $options |
| 103 | * @param AAM_Core_Policy_Generator $generator |
| 104 | * |
| 105 | * @return array |
| 106 | * |
| 107 | * @access public |
| 108 | * @version 6.4.0 |
| 109 | */ |
| 110 | public function generatePolicy($policy, $resource_type, $options, $generator) |
| 111 | { |
| 112 | if ($resource_type === AAM_Core_Object_LoginRedirect::OBJECT_TYPE) { |
| 113 | if (!empty($options)) { |
| 114 | $policy['Param'] = array_merge( |
| 115 | $policy['Param'], |
| 116 | $generator->generateRedirectParam($options, 'login') |
| 117 | ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return $policy; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Prepare login redirect response |
| 126 | * |
| 127 | * This method hooks into the Secure Login redirect service and override the |
| 128 | * response for the Ajax login request |
| 129 | * |
| 130 | * @param array $response |
| 131 | * @param WP_REST_Request $request |
| 132 | * @param WP_User $user |
| 133 | * |
| 134 | * @return array |
| 135 | * |
| 136 | * @since 6.6.2 https://github.com/aamplugin/advanced-access-manager/issues/139 |
| 137 | * @since 6.0.0 Initial implementation of the method |
| 138 | * |
| 139 | * @access public |
| 140 | * @see AAM_Service_SecureLogin::authenticate |
| 141 | * @version 6.6.2 |
| 142 | */ |
| 143 | public function prepareLoginResponse($response, $request, $user) |
| 144 | { |
| 145 | if (empty($response['redirect']) || ($response['redirect'] === admin_url())) { |
| 146 | $response['redirect'] = $this->getUserRedirect($user); |
| 147 | } |
| 148 | |
| 149 | return $response; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get login redirect URL |
| 154 | * |
| 155 | * @param string $redirect |
| 156 | * @param string $requested |
| 157 | * @param WP_User $user |
| 158 | * |
| 159 | * @return string |
| 160 | * |
| 161 | * @since 6.5.0 Fixed the way login redirect is computed |
| 162 | * @since 6.0.0 Initial implementation of the method |
| 163 | * |
| 164 | * @access public |
| 165 | * @version 6.5.0 |
| 166 | */ |
| 167 | public function getLoginRedirect($redirect, $requested, $user) |
| 168 | { |
| 169 | if (is_a($user, 'WP_User') |
| 170 | && in_array($requested, array('', admin_url()), true) |
| 171 | ) { |
| 172 | $requested = $this->getUserRedirect($user); |
| 173 | $redirect = (!empty($requested) ? $requested : $redirect); |
| 174 | } |
| 175 | |
| 176 | return $redirect; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Prepare user redirect URL |
| 181 | * |
| 182 | * @param WP_User $user |
| 183 | * |
| 184 | * @return string|null |
| 185 | * |
| 186 | * @access protected |
| 187 | * @version 6.0.0 |
| 188 | */ |
| 189 | protected function getUserRedirect(WP_User $user) |
| 190 | { |
| 191 | $redirect = null; |
| 192 | |
| 193 | $settings = AAM::api()->getUser($user->ID)->getObject( |
| 194 | AAM_Core_Object_LoginRedirect::OBJECT_TYPE |
| 195 | )->getOption(); |
| 196 | |
| 197 | if (!empty($settings)) { |
| 198 | switch ($settings['login.redirect.type']) { |
| 199 | case 'page': |
| 200 | $redirect = get_page_link($settings['login.redirect.page']); |
| 201 | break; |
| 202 | |
| 203 | case 'url': |
| 204 | $redirect = $settings['login.redirect.url']; |
| 205 | break; |
| 206 | |
| 207 | case 'callback': |
| 208 | if (is_callable($settings['login.redirect.callback'])) { |
| 209 | $redirect = call_user_func($settings['login.redirect.callback']); |
| 210 | } |
| 211 | break; |
| 212 | |
| 213 | default: |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return $redirect; |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | if (defined('AAM_KEY')) { |
| 224 | AAM_Service_LoginRedirect::bootstrap(); |
| 225 | } |