Installer
3 years ago
AdminNotices.php
1 month ago
AjaxHandler.php
1 year ago
Autologin.php
9 months ago
BlockRegistrations.php
10 months ago
BuddyPressBbPress.php
3 years ago
DisableConcurrentLogins.php
2 years ago
EditUserProfile.php
5 months ago
ExtensionManager.php
1 month ago
FileUploader.php
3 months ago
FormPreviewHandler.php
5 months ago
FormRepository.php
1 year ago
FormShortcodeDefaults.php
1 month ago
GDPR.php
3 years ago
Geolocation.php
3 years ago
GlobalSiteAccess.php
3 years ago
ImageUploader.php
1 year ago
LoginAuth.php
1 year ago
Miscellaneous.php
3 years ago
ModifyRedirectDefaultLinks.php
5 months ago
PPRESS_Session.php
1 year ago
PROFILEPRESS_sql.php
1 year ago
PasswordReset.php
1 year ago
ProfileUrlRewrite.php
4 years ago
RegistrationAuth.php
1 week ago
SendEmail.php
3 years ago
ShortcodeThemeFactory.php
5 years ago
UserAvatar.php
1 year ago
UserSignupLocationListingPage.php
3 years ago
UsernameEmailRestrictLogin.php
4 years ago
WPProfileFieldParserTrait.php
1 year ago
WelcomeEmailAfterSignup.php
1 year ago
default-email-template.php
1 year ago
index.php
3 years ago
ModifyRedirectDefaultLinks.php
398 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Alter default login, registration, password_reset login and logout url |
| 7 | */ |
| 8 | class ModifyRedirectDefaultLinks |
| 9 | { |
| 10 | public function __construct() |
| 11 | { |
| 12 | add_action('login_form_lostpassword', array($this, 'redirect_password_reset_page')); |
| 13 | add_filter('lostpassword_url', array($this, 'lost_password_url_func'), 999999999); |
| 14 | |
| 15 | add_action('login_form_login', array($this, 'redirect_login_page')); |
| 16 | add_filter('login_url', array($this, 'set_login_url_func'), 999999999, 3); |
| 17 | |
| 18 | add_action('login_form_register', array($this, 'redirect_reg_page')); |
| 19 | add_filter('register_url', array($this, 'register_url_func'), 999999999); |
| 20 | |
| 21 | add_filter('logout_url', array($this, 'logout_url_func'), 999999999, 2); |
| 22 | |
| 23 | add_filter('author_link', array($this, 'author_link_func'), 999999999, 2); |
| 24 | |
| 25 | add_filter('get_comment_author_url', array($this, 'comment_author_url_to_profile'), 999999999, 3); |
| 26 | |
| 27 | add_action('init', array($this, 'redirect_default_edit_profile_to_custom')); |
| 28 | |
| 29 | // redirect buddypress registration to PP custom registration page or default WP registration url if not set |
| 30 | add_action('template_redirect', array($this, 'redirect_bp_registration_page')); |
| 31 | add_filter('bp_get_signup_page', array($this, 'rewrite_bp_registration_url')); |
| 32 | |
| 33 | // redirect default logout page to blog homepage |
| 34 | add_action('init', array($this, 'redirect_logout_page')); |
| 35 | |
| 36 | add_action('template_redirect', [$this, 'redirect_author_page']); |
| 37 | } |
| 38 | |
| 39 | public function is_third_party_2fa_active() |
| 40 | { |
| 41 | global $pagenow; |
| 42 | |
| 43 | $is_active = false; |
| 44 | |
| 45 | if ('wp-login.php' == $pagenow) { |
| 46 | |
| 47 | if (class_exists('Jetpack') && \Jetpack::is_module_active('sso')) { |
| 48 | $is_active = true; |
| 49 | } |
| 50 | |
| 51 | if ( ! $is_active && function_exists('wd_di') && class_exists('\WP_Defender\Model\Setting\Two_Fa')) { |
| 52 | try { |
| 53 | if (wd_di()->get(\WP_Defender\Model\Setting\Two_Fa::class)->enabled) $is_active = true; |
| 54 | } catch (\Exception $e) { |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if ( ! $is_active && (defined('WORDFENCE_VERSION') || defined('WORDFENCE_LS_VERSION'))) { |
| 59 | $is_active = true; |
| 60 | } |
| 61 | |
| 62 | if ( ! $is_active && defined('WP_2FA_VERSION')) $is_active = true; |
| 63 | |
| 64 | if ( ! $is_active && defined('SIMBA_TFA_PLUGIN_DIR')) $is_active = true; |
| 65 | |
| 66 | if ( ! $is_active && class_exists('\SG_Security\Options_Service\Options_Service') && \SG_Security\Options_Service\Options_Service::is_enabled('sg2fa')) { |
| 67 | $is_active = true; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return apply_filters('ppress_is_third_party_2fa_active', $is_active); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Modify the lost password url returned by wp_lostpassword_url() function. |
| 76 | * |
| 77 | * @param $val |
| 78 | * |
| 79 | * @return string |
| 80 | */ |
| 81 | public function lost_password_url_func($val) |
| 82 | { |
| 83 | $page_id = ppress_get_setting('set_lost_password_url'); |
| 84 | |
| 85 | if ( ! empty($page_id) && 'publish' == get_post_status($page_id)) { |
| 86 | $val = get_permalink($page_id); |
| 87 | } |
| 88 | |
| 89 | return apply_filters('ppress_password_reset_url', $val); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Force redirection of default password reset to the page with custom one. |
| 94 | */ |
| 95 | public function redirect_password_reset_page() |
| 96 | { |
| 97 | if ( ! apply_filters('ppress_default_password_reset_redirect_enabled', true)) return; |
| 98 | |
| 99 | $page_id = ppress_get_setting('set_lost_password_url'); |
| 100 | |
| 101 | if (empty($page_id) || 'publish' != get_post_status($page_id)) return; |
| 102 | |
| 103 | wp_safe_redirect(get_permalink(absint($page_id))); |
| 104 | exit; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Modify the login url returned by wp_login_url() |
| 109 | * |
| 110 | * @param $url |
| 111 | * @param string $redirect |
| 112 | * @param bool $force_reauth |
| 113 | * |
| 114 | * @return string page with login shortcode |
| 115 | */ |
| 116 | public function set_login_url_func($url, $redirect, $force_reauth) |
| 117 | { |
| 118 | global $wp_rewrite; |
| 119 | |
| 120 | if (is_object($wp_rewrite)) { |
| 121 | |
| 122 | if ( ! $this->is_third_party_2fa_active()) { |
| 123 | |
| 124 | $login_page_id = ppress_get_setting('set_login_url'); |
| 125 | |
| 126 | if ( ! empty($login_page_id) && 'publish' == get_post_status($login_page_id)) { |
| 127 | |
| 128 | $url = get_permalink($login_page_id); |
| 129 | |
| 130 | if ( ! empty($redirect)) { |
| 131 | $url = add_query_arg('redirect_to', rawurlencode(wp_validate_redirect($redirect)), $url); |
| 132 | } |
| 133 | |
| 134 | if ($force_reauth) { |
| 135 | $url = add_query_arg('reauth', '1', $url); |
| 136 | } |
| 137 | |
| 138 | $url = esc_url_raw($url); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return apply_filters('ppress_login_url', $url, $redirect, $force_reauth); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Force redirect default login to page with login shortcode |
| 148 | */ |
| 149 | public function redirect_login_page() |
| 150 | { |
| 151 | if ( ! apply_filters('ppress_default_login_redirect_enabled', true)) return; |
| 152 | |
| 153 | if ($this->is_third_party_2fa_active()) return; |
| 154 | |
| 155 | $login_page_id = ppress_get_setting('set_login_url'); |
| 156 | |
| 157 | if (empty($login_page_id) || ('publish' != get_post_status($login_page_id))) return; |
| 158 | |
| 159 | $login_url = ppress_login_url(); |
| 160 | |
| 161 | // retrieve the query string if available. |
| 162 | $query_string = ! empty($_SERVER["QUERY_STRING"]) ? $_SERVER["QUERY_STRING"] : parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY); |
| 163 | |
| 164 | // if query string is available, append to login url. |
| 165 | if ( ! empty($query_string)) { |
| 166 | if (strpos($login_url, '?') !== false) { |
| 167 | $login_url .= "&$query_string"; |
| 168 | } else { |
| 169 | $login_url .= "?$query_string"; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | wp_safe_redirect(esc_url_raw($login_url)); |
| 174 | exit; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Modify the url returned by wp_registration_url(). |
| 179 | * |
| 180 | * @return string page url with registration shortcode. |
| 181 | */ |
| 182 | public function register_url_func($val) |
| 183 | { |
| 184 | $page_id = ppress_get_setting('set_registration_url'); |
| 185 | |
| 186 | if ( ! empty($page_id) && 'publish' == get_post_status($page_id)) { |
| 187 | $val = get_permalink($page_id); |
| 188 | } |
| 189 | |
| 190 | return apply_filters('ppress_registration_url', $val); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * force redirection of default registration to custom one |
| 195 | */ |
| 196 | public function redirect_reg_page() |
| 197 | { |
| 198 | if ( ! apply_filters('ppress_default_registration_redirect_enabled', true)) return; |
| 199 | |
| 200 | $page_id = ppress_get_setting('set_registration_url'); |
| 201 | |
| 202 | if (empty($page_id) || 'publish' != get_post_status($page_id)) return; |
| 203 | |
| 204 | $reg_url = ppress_registration_url(); |
| 205 | |
| 206 | wp_safe_redirect(esc_url_raw($reg_url)); |
| 207 | exit; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Add query string (url) to logout url which is url to redirect to after logout |
| 212 | * |
| 213 | * @param $logout_url string filter default login url to be modified |
| 214 | * @param $redirect string where to redirect to after logout |
| 215 | * |
| 216 | * @return string |
| 217 | */ |
| 218 | public function logout_url_func($logout_url, $redirect) |
| 219 | { |
| 220 | if (apply_filters('ppress_logout_url_enable_redirect_get_query', false) && ! empty($redirect)) { |
| 221 | return $logout_url; |
| 222 | } |
| 223 | |
| 224 | $set_redirect = false; |
| 225 | |
| 226 | $custom_logout_page_url = ppress_get_setting('custom_url_log_out'); |
| 227 | $logout_page_id = ppress_get_setting('set_log_out_url'); |
| 228 | |
| 229 | if (apply_filters('ppress_logout_url_enable_redirect_get_query', false) && ! empty($redirect)) { |
| 230 | $set_redirect = $redirect; |
| 231 | } elseif ( ! empty($custom_logout_page_url)) { |
| 232 | $set_redirect = $custom_logout_page_url; |
| 233 | } elseif ( ! empty($logout_page_id)) { |
| 234 | |
| 235 | if ($logout_page_id != 'default') { |
| 236 | |
| 237 | $db_logout_url = get_permalink(absint($logout_page_id)); |
| 238 | |
| 239 | $set_redirect = $db_logout_url; |
| 240 | |
| 241 | if (empty($db_logout_url) || $logout_page_id == 'current_view_page') { |
| 242 | // make redirect currently viewed page |
| 243 | $set_redirect = get_permalink(); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if ( ! empty($set_redirect)) { |
| 249 | $set_redirect = apply_filters('ppress_logout_redirect', $set_redirect); |
| 250 | $logout_url = esc_url_raw(add_query_arg('redirect_to', rawurlencode($set_redirect), $logout_url)); |
| 251 | } |
| 252 | |
| 253 | return $logout_url; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Redirect user edit profile (/wp-admin/profile.php) to "custom edit profile" page. |
| 258 | */ |
| 259 | public function redirect_default_edit_profile_to_custom() |
| 260 | { |
| 261 | if (ppress_settings_by_key('redirect_default_edit_profile_to_custom') == 'yes') { |
| 262 | |
| 263 | // Disable edit profile redirect for administrator. |
| 264 | $is_disable_for_admin = apply_filters('ppress_disable_admin_edit_profile_redirect', ppress_settings_by_key('disable_admin_edit_profile_redirect') === 'yes') && current_user_can('delete_users'); |
| 265 | |
| 266 | add_filter('edit_profile_url', function ($url) use ($is_disable_for_admin) { |
| 267 | |
| 268 | $page_id = ppress_settings_by_key('edit_user_profile_url'); |
| 269 | |
| 270 | if ( ! $is_disable_for_admin && ! empty($page_id)) { |
| 271 | $url = esc_url_raw(get_permalink($page_id)); |
| 272 | } |
| 273 | |
| 274 | return $url; |
| 275 | |
| 276 | }, 9999999999); |
| 277 | |
| 278 | if ($is_disable_for_admin) return; |
| 279 | |
| 280 | if ( ! empty(ppress_get_setting('edit_user_profile_url'))) { |
| 281 | |
| 282 | $edit_user_profile_url = ppress_edit_profile_url(); |
| 283 | |
| 284 | $page_viewed = esc_url_raw($_SERVER['REQUEST_URI']); |
| 285 | |
| 286 | if (isset($page_viewed) && strpos($page_viewed, 'wp-admin/profile.php') !== false) { |
| 287 | wp_safe_redirect(esc_url_raw($edit_user_profile_url)); |
| 288 | exit; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | public function author_link_func($url, $author_id) |
| 295 | { |
| 296 | if (ppress_settings_by_key('author_slug_to_profile') == 'on') { |
| 297 | $url = esc_url_raw(ppress_get_frontend_profile_url($author_id)); |
| 298 | } |
| 299 | |
| 300 | return $url; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * @param $url |
| 305 | * @param int $id comment ID |
| 306 | * @param \WP_Comment $comment |
| 307 | * |
| 308 | * @return string|void |
| 309 | */ |
| 310 | public function comment_author_url_to_profile($url, $id, $comment) |
| 311 | { |
| 312 | if (ppress_settings_by_key('comment_author_url_to_profile') == 'on') { |
| 313 | if (is_object($comment) && isset($comment->comment_author_email)) { |
| 314 | $email = $comment->comment_author_email; |
| 315 | $user = get_user_by('email', $email); |
| 316 | if (isset($user->user_login)) { |
| 317 | $url = esc_url_raw(ppress_get_frontend_profile_url($user->user_login)); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return $url; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Redirect the default logout page (/wp-login.php?loggedout=true) to blog homepage |
| 327 | */ |
| 328 | public function redirect_logout_page() |
| 329 | { |
| 330 | $page_viewed = basename(esc_url_raw($_SERVER['REQUEST_URI'])); |
| 331 | |
| 332 | if ($page_viewed == "wp-login.php?loggedout=true" && $_SERVER['REQUEST_METHOD'] == 'GET') { |
| 333 | wp_safe_redirect(home_url()); |
| 334 | exit; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | public function redirect_bp_registration_page() |
| 339 | { |
| 340 | if ( ! class_exists('BuddyPress') && ! function_exists('bp_has_custom_signup_page')) { |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | if ( ! bp_has_custom_signup_page()) return; |
| 345 | |
| 346 | if (ppress_get_setting('redirect_bp_registration_page') == 'yes') { |
| 347 | // ! bp_has_custom_signup_page() |
| 348 | $page = bp_get_root_domain() . '/' . bp_get_signup_slug(); |
| 349 | |
| 350 | if ($page == ppress_get_current_url()) { |
| 351 | wp_safe_redirect(ppress_registration_url()); |
| 352 | exit; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Rewrite buddypress registration url to PP custom url or WP's if not set. |
| 359 | * |
| 360 | * @param string $page |
| 361 | * |
| 362 | * @return string |
| 363 | */ |
| 364 | public function rewrite_bp_registration_url($page) |
| 365 | { |
| 366 | if (ppress_get_setting('redirect_bp_registration_page') == 'yes') { |
| 367 | |
| 368 | if (apply_filters('ppress_bp_registration_url', true)) { |
| 369 | $page = ppress_registration_url(); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | return $page; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Redirect author page to user's profile |
| 378 | */ |
| 379 | public function redirect_author_page() |
| 380 | { |
| 381 | if (ppress_settings_by_key('author_slug_to_profile') == 'on' && is_author()) { |
| 382 | $id = get_query_var('author'); |
| 383 | wp_redirect(ppress_get_frontend_profile_url($id)); |
| 384 | exit; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | public static function get_instance() |
| 389 | { |
| 390 | static $instance = false; |
| 391 | |
| 392 | if ( ! $instance) { |
| 393 | $instance = new self; |
| 394 | } |
| 395 | |
| 396 | return $instance; |
| 397 | } |
| 398 | } |