parser = $parser; } /** * Check whether an email passes an allow-list of rules. * * Returns true when: * - $rules is empty (open registration), or * - at least one rule in $rules matches $user_email. * * @param string $user_email Email address to test. * @param array $rules Array of rule strings (domain / TLD / email / wildcard). * @return bool * @since 1.4.0 */ public function is_email_allowed( $user_email, array $rules ) { $rules = array_filter( $rules ); if ( empty( $rules ) ) { return true; } return null !== $this->find_matching_rule( $user_email, $rules ); } /** * Return the first rule that matches the given email, or null if none match. * * @param string $user_email Email address to test. * @param array $rules Array of rule strings. * @return string|null The matching rule, or null. * @since 1.4.0 */ public function find_matching_rule( $user_email, array $rules ) { $email = strtolower( trim( $user_email ) ); $parts = explode( '@', $email ); if ( count( $parts ) !== 2 || '' === $parts[1] ) { return null; } $email_domain = $parts[1]; foreach ( $rules as $rule ) { if ( $this->matches( $email, $email_domain, $rule ) ) { return $rule; } } return null; } /** * Test a single normalised email + domain pair against one rule string. * * @param string $email Full lowercase email address. * @param string $email_domain Domain part of the email (lowercase). * @param string $rule Raw rule string. * @return bool */ private function matches( $email, $email_domain, $rule ) { $parsed = $this->parser->parse( $rule ); $value = $parsed['value']; switch ( $parsed['type'] ) { case AEDR_Rule_Parser::TYPE_EMAIL: return $email === $value; case AEDR_Rule_Parser::TYPE_TLD: return substr( $email_domain, -strlen( $value ) ) === $value; case AEDR_Rule_Parser::TYPE_DOMAIN: return $email_domain === $value; case AEDR_Rule_Parser::TYPE_WILDCARD: // *.example.com matches both example.com and any subdomain of it. $base = substr( $value, 2 ); // Strip '*.', leaving the base domain. return $email_domain === $base || substr( $email_domain, -( strlen( $base ) + 1 ) ) === '.' . $base; default: return false; } } } }