$mode, $key => array_values( array_unique( $domains ) ) ), $issues ); } private static function valid( $domain ) { return strlen( $domain ) <= 253 && (bool) preg_match( '/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/D', $domain ); } /** A visible instruction, also used as the error when a valid address is refused. */ public static function instruction( array $field ) { switch ( $field['email_domain_mode'] ?? 'any' ) { case 'work': return __( 'Enter a work email address, such as you@your-company.com.', 'accessible-forms-by-accessible-org' ); case 'allow': $domains = (array) ( $field['email_domains'] ?? array() ); if ( 1 === count( $domains ) ) { return sprintf( /* translators: %s: an allowed email domain, including the @. */ __( 'Enter an email address ending in %s.', 'accessible-forms-by-accessible-org' ), '@' . reset( $domains ) ); } if ( $domains ) { $examples = implode( ', ', array_map( static function ( $domain ) { return '@' . $domain; }, array_slice( $domains, 0, 3 ) ) ); return sprintf( /* translators: %s: examples of allowed email domains. */ __( 'Use an approved email domain such as %s.', 'accessible-forms-by-accessible-org' ), $examples ); } return __( 'Enter an email address from an organization approved for this form.', 'accessible-forms-by-accessible-org' ); case 'deny': return __( 'Use an email address from a different domain.', 'accessible-forms-by-accessible-org' ); } return ''; } public static function allows( array $field, $address ) { $mode = $field['email_domain_mode'] ?? 'any'; if ( 'any' === $mode ) { return true; } $list = 'work' === $mode ? ( $field['free_email_domains'] ?? self::FREE_PROVIDERS ) : ( $field['email_domains'] ?? array() ); $match = in_array( strtolower( substr( strrchr( $address, '@' ), 1 ) ), (array) $list, true ); return 'allow' === $mode ? $match : ! $match; } }