File: /var/www/html/wp-content/mu-plugins/ethnic-email-domain-policy.php
<?php
/**
* Shared email domain policy helpers.
*
* @package EthnicInfotech
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( 'ethnic_email_domain_blocked_suffixes' ) ) {
function ethnic_email_domain_blocked_suffixes() {
$suffixes = array(
'ru',
);
$suffixes = array_map(
static function ( $suffix ) {
$suffix = is_scalar( $suffix ) ? strtolower( trim( (string) $suffix ) ) : '';
$suffix = ltrim( $suffix, '.' );
return preg_replace( '/[^a-z0-9.-]/', '', $suffix );
},
$suffixes
);
return array_values( array_filter( $suffixes ) );
}
}
if ( ! function_exists( 'ethnic_email_domain_suffix_is_blocked' ) ) {
function ethnic_email_domain_suffix_is_blocked( $domain, $suffix ) {
$domain = strtolower( trim( (string) $domain ) );
$suffix = strtolower( ltrim( trim( (string) $suffix ), '.' ) );
if ( '' === $domain || '' === $suffix ) {
return false;
}
if ( $domain === $suffix ) {
return true;
}
$needle = '.' . $suffix;
if ( strlen( $domain ) <= strlen( $needle ) ) {
return false;
}
return substr( $domain, -strlen( $needle ) ) === $needle;
}
}
if ( ! function_exists( 'ethnic_email_domain_is_blocked' ) ) {
function ethnic_email_domain_is_blocked( $email ) {
$email = sanitize_email( (string) $email );
if ( '' === $email || false === strpos( $email, '@' ) ) {
return false;
}
$domain = strtolower( trim( substr( strrchr( $email, '@' ), 1 ) ) );
if ( '' === $domain ) {
return false;
}
foreach ( ethnic_email_domain_blocked_suffixes() as $suffix ) {
if ( ethnic_email_domain_suffix_is_blocked( $domain, $suffix ) ) {
return true;
}
}
return false;
}
}
if ( ! function_exists( 'ethnic_email_domain_is_allowed' ) ) {
function ethnic_email_domain_is_allowed( $email ) {
return ! ethnic_email_domain_is_blocked( $email );
}
}
if ( ! function_exists( 'ethnic_email_domain_block_message' ) ) {
function ethnic_email_domain_block_message() {
return 'Email addresses ending in .ru are not allowed. Please use a different business email address.';
}
}