class-activation.php
3 years ago
class-admin-interface.php
3 years ago
class-content-management.php
3 years ago
class-deactivation.php
3 years ago
class-disable-components.php
3 years ago
class-security.php
3 years ago
class-settings-fields-render.php
3 years ago
class-settings-sanitization.php
3 years ago
class-settings-sections-fields.php
3 years ago
class-utilities.php
3 years ago
class-utilities.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class related to Utilities features |
| 7 | * |
| 8 | * @since 1.5.0 |
| 9 | */ |
| 10 | class Utilities { |
| 11 | |
| 12 | /** |
| 13 | * Redirect to custom internal URL after login for user roles |
| 14 | * |
| 15 | * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL. |
| 16 | * @param string $origin_url URL the user is coming from. |
| 17 | * @param object $user logged-in user's data. |
| 18 | * @since 1.5.0 |
| 19 | */ |
| 20 | public function redirect_for_roles_after_login( $redirect_to_url, $origin_url, $user ) { |
| 21 | |
| 22 | $options = get_option( ASENHA_SLUG_U ); |
| 23 | $redirect_after_login_to_slug = $options['redirect_after_login_to_slug']; |
| 24 | $redirect_after_login_for = $options['redirect_after_login_for']; |
| 25 | |
| 26 | if ( isset( $redirect_after_login_for ) && ( count( $redirect_after_login_for ) > 0 ) ) { |
| 27 | |
| 28 | // Assemble single-dimensional array of roles for which custom URL redirection should happen |
| 29 | $roles_for_custom_redirect = array(); |
| 30 | |
| 31 | foreach( $redirect_after_login_for as $role_slug => $custom_redirect ) { |
| 32 | if ( $custom_redirect ) { |
| 33 | $roles_for_custom_redirect[] = $role_slug; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Does the user have roles data in array form? |
| 38 | if ( isset( $user->roles ) && is_array( $user->roles ) ) { |
| 39 | |
| 40 | $current_user_roles = $user->roles; |
| 41 | |
| 42 | } |
| 43 | |
| 44 | // Set custom redirect URL for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard. |
| 45 | foreach ( $current_user_roles as $role ) { |
| 46 | if ( in_array( $role, $roles_for_custom_redirect ) ) { |
| 47 | |
| 48 | $redirect_to_url = home_url( $redirect_after_login_to_slug . '/' ); |
| 49 | |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | } |
| 54 | |
| 55 | return $redirect_to_url; |
| 56 | |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Redirect to custom internal URL after login for user roles |
| 61 | * |
| 62 | * @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL. |
| 63 | * @param string $origin_url URL the user is coming from. |
| 64 | * @param object $user logged-in user's data. |
| 65 | * @since 1.6.0 |
| 66 | */ |
| 67 | public function redirect_after_logout( $user_id ) { |
| 68 | |
| 69 | $options = get_option( ASENHA_SLUG_U ); |
| 70 | $redirect_after_logout_to_slug = $options['redirect_after_logout_to_slug']; |
| 71 | $redirect_after_logout_for = $options['redirect_after_logout_for']; |
| 72 | |
| 73 | $user = get_userdata( $user_id ); |
| 74 | |
| 75 | if ( isset( $redirect_after_logout_for ) && ( count( $redirect_after_logout_for ) > 0 ) ) { |
| 76 | |
| 77 | // Assemble single-dimensional array of roles for which custom URL redirection should happen |
| 78 | $roles_for_custom_redirect = array(); |
| 79 | |
| 80 | foreach( $redirect_after_logout_for as $role_slug => $custom_redirect ) { |
| 81 | if ( $custom_redirect ) { |
| 82 | $roles_for_custom_redirect[] = $role_slug; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Does the user have roles data in array form? |
| 87 | if ( isset( $user->roles ) && is_array( $user->roles ) ) { |
| 88 | |
| 89 | $current_user_roles = $user->roles; |
| 90 | |
| 91 | } |
| 92 | |
| 93 | // Redirect for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard. |
| 94 | foreach ( $current_user_roles as $role ) { |
| 95 | if ( in_array( $role, $roles_for_custom_redirect ) ) { |
| 96 | |
| 97 | wp_safe_redirect( home_url( $redirect_after_logout_to_slug . '/' ) ); |
| 98 | exit(); |
| 99 | |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Redirect 404 to homepage |
| 109 | * |
| 110 | * @since 1.7.0 |
| 111 | */ |
| 112 | public function redirect_404_to_homepage() { |
| 113 | |
| 114 | if ( ! is_404() || is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ) { |
| 115 | |
| 116 | return; |
| 117 | |
| 118 | } else { |
| 119 | |
| 120 | // wp_safe_redirect( home_url(), 301 ); |
| 121 | |
| 122 | header( 'HTTP/1.1 301 Moved Permanently'); |
| 123 | header( 'Location: ' . home_url() ); |
| 124 | exit(); |
| 125 | |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | } |