| 1 |
<?php |
| 2 |
/** |
| 3 |
* LoginPress Allow Domain Class. |
| 4 |
* |
| 5 |
* This class handles allow/disallow domains for registration. |
| 6 |
* Purpose of this class is to restrict user registration based on email domains. |
| 7 |
* |
| 8 |
* @package LoginPress |
| 9 |
* @since 6.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'LoginPress_Domains' ) ) { |
| 18 |
|
| 19 |
/** |
| 20 |
* Add LoginPress Allow/Disallow Domains for registration. |
| 21 |
* |
| 22 |
* @since 6.0.0 |
| 23 |
*/ |
| 24 |
class LoginPress_Domains { |
| 25 |
|
| 26 |
/** |
| 27 |
* LoginPress settings array. |
| 28 |
* |
| 29 |
* @var array<string, mixed> $loginpress_setting |
| 30 |
* @since 6.0.0 |
| 31 |
*/ |
| 32 |
public $loginpress_setting; |
| 33 |
|
| 34 |
/** |
| 35 |
* Final validated domains list. |
| 36 |
* |
| 37 |
* @var array<string> $final_domains_list |
| 38 |
* @since 6.0.0 |
| 39 |
*/ |
| 40 |
public $final_domains_list; |
| 41 |
|
| 42 |
/** |
| 43 |
* Class Constructor. |
| 44 |
* |
| 45 |
* @since 6.0.0 |
| 46 |
*/ |
| 47 |
public function __construct() { |
| 48 |
|
| 49 |
$this->loginpress_setting = get_option( 'loginpress_setting' ); |
| 50 |
$for_validation = isset( $this->loginpress_setting['restrict_domains_textarea'] ) && ! empty( $this->loginpress_setting['restrict_domains_textarea'] ) ? $this->loginpress_setting['restrict_domains_textarea'] : array(); |
| 51 |
$this->final_domains_list = array_map( 'strtolower', $this->loginpress_validate_domain_list( $for_validation ) ); |
| 52 |
$this->hooks(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Add hooks. |
| 57 |
* |
| 58 |
* @since 6.0.0 |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function hooks() { |
| 62 |
|
| 63 |
if ( empty( $this->final_domains_list ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
add_filter( 'registration_errors', array( $this, 'loginpress_reg_allow_disallow' ), 10, 3 ); |
| 67 |
add_filter( 'loginpress_social_login_register_email', array( $this, 'loginpress_login_allow_disallow' ), 10, 1 ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Validate registration based on allowed/disallowed domains. |
| 72 |
* |
| 73 |
* If the option to restrict domains is enabled, this function checks if the user's email domain is in the list of allowed/disallowed domains. |
| 74 |
* If it is, the registration is blocked and an error message is added to the WP_Error object. |
| 75 |
* |
| 76 |
* @param WP_Error $errors WP_Error object. |
| 77 |
* @param string $sanitized_user_login Sanitized username. |
| 78 |
* @param string $user_email User email. |
| 79 |
* |
| 80 |
* @return mixed WP_Error WP_Error object with the error message if the user's email domain is blocked. |
| 81 |
* |
| 82 |
* @since 6.0.0 |
| 83 |
*/ |
| 84 |
public function loginpress_reg_allow_disallow( $errors, $sanitized_user_login, $user_email ) { |
| 85 |
|
| 86 |
// Add email format validation. |
| 87 |
$user_email = is_email( $user_email ) ? sanitize_email( $user_email ) : ''; |
| 88 |
$user_domain = ! empty( $user_email ) ? '@' . explode( '@', $user_email )[1] : false; |
| 89 |
/** |
| 90 |
* Check for valid domain format. |
| 91 |
* |
| 92 |
* @phpstan-ignore-next-line |
| 93 |
*/ |
| 94 |
if ( ! $user_domain || false === strpos( $user_domain, '.' ) || '' === $user_email ) { |
| 95 |
$errors->add( 'invalid_email_format', __( 'Please enter a valid email address.', 'loginpress' ) ); |
| 96 |
return $errors; |
| 97 |
} |
| 98 |
|
| 99 |
$restricted_domains = isset( $this->loginpress_setting['restrict_domains_radio'] ) ? $this->loginpress_setting['restrict_domains_radio'] : ''; |
| 100 |
|
| 101 |
if ( 'allow' === $restricted_domains ) { |
| 102 |
|
| 103 |
if ( ! in_array( strtolower( $user_domain ), $this->final_domains_list, true ) ) { |
| 104 |
$errors->add( 'restricted_domain', __( 'Registration from this domain is not allowed.', 'loginpress' ) ); |
| 105 |
return $errors; |
| 106 |
} |
| 107 |
} elseif ( 'disallow' === $restricted_domains ) { |
| 108 |
|
| 109 |
if ( in_array( strtolower( $user_domain ), $this->final_domains_list, true ) ) { |
| 110 |
$errors->add( 'restricted_domain', __( 'Registration from this domain is not allowed.', 'loginpress' ) ); |
| 111 |
return $errors; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return $errors; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Validates and formats a list of domains. |
| 120 |
* |
| 121 |
* This function takes a list of domains and ensures each domain is valid. |
| 122 |
* It trims any leading '@' character and validates the domain format. |
| 123 |
* If valid, it adds '@' back to the domain and includes it in the final list. |
| 124 |
* |
| 125 |
* @param array<string> $domain_list Array of domains to be validated. |
| 126 |
* @return array<string> $final_domain_list Array of validated and formatted domains. |
| 127 |
* |
| 128 |
* @since 6.0.0 |
| 129 |
*/ |
| 130 |
public function loginpress_validate_domain_list( $domain_list ) { |
| 131 |
|
| 132 |
$final_domain_list = array(); |
| 133 |
foreach ( $domain_list as $domain ) { |
| 134 |
$domain = trim( $domain, '@' ); |
| 135 |
if ( filter_var( $domain, FILTER_VALIDATE_DOMAIN ) ) { |
| 136 |
$final_domain_list[] = '@' . $domain; |
| 137 |
} |
| 138 |
} |
| 139 |
return $final_domain_list; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* Validates login based on allowed/disallowed domains for Social login. |
| 145 |
* |
| 146 |
* @param string $user_email User email address. |
| 147 |
* |
| 148 |
* @return string|void Returns the user email if the domain is allowed; otherwise, it terminates the process with an error message. |
| 149 |
* |
| 150 |
* @since 6.0.0 |
| 151 |
*/ |
| 152 |
public function loginpress_login_allow_disallow( $user_email ) { |
| 153 |
|
| 154 |
$restricted_domains = isset( $this->loginpress_setting['restrict_domains_radio'] ) ? $this->loginpress_setting['restrict_domains_radio'] : ''; |
| 155 |
$user_email = is_email( $user_email ) ? sanitize_email( $user_email ) : ''; |
| 156 |
$email_parts = explode( '@', $user_email ); |
| 157 |
$user_domain = ! empty( $user_email ) && isset( $email_parts[1] ) ? '@' . $email_parts[1] : false; |
| 158 |
if ( 'allow' === $restricted_domains ) { |
| 159 |
|
| 160 |
if ( ! in_array( strtolower( $user_domain ? $user_domain : '' ), $this->final_domains_list, true ) ) { |
| 161 |
$error = __( '<strong>ERROR:</strong> Registration from this domain is not allowed.', 'loginpress' ); |
| 162 |
wp_die( $error ); // phpcs:ignore |
| 163 |
} else { |
| 164 |
return $user_email; |
| 165 |
} |
| 166 |
} elseif ( 'disallow' === $restricted_domains ) { |
| 167 |
|
| 168 |
if ( in_array( strtolower( $user_domain ? $user_domain : '' ), $this->final_domains_list, true ) ) { |
| 169 |
$error = __( '<strong>ERROR:</strong> Registration from this domain is not allowed.', 'loginpress' ); |
| 170 |
wp_die( $error ); // phpcs:ignore |
| 171 |
} else { |
| 172 |
return $user_email; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return $user_email; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
new LoginPress_Domains(); |
| 181 |
|