validator = $validator; $this->csv_processor = new AEDR_CSV_Processor(); $this->parser = new AEDR_Rule_Parser(); $this->matcher = new AEDR_Rule_Matcher( $this->parser ); add_action( 'admin_menu', array( $this, 'aedr_add_admin_menu' ) ); add_action( 'admin_init', array( $this, 'aedr_settings_init' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'aedr_enqueue_admin_scripts' ) ); add_action( 'admin_post_aedr_export_csv', array( $this, 'aedr_handle_export_csv' ) ); add_action( 'wp_ajax_aedr_test_email', array( $this, 'aedr_ajax_test_email' ) ); } /** * Add admin menu. * * Adds a menu item for the plugin settings page. * * @since 1.0.0 */ public function aedr_add_admin_menu() { add_options_page( 'Advanced Email Domain Restriction', 'Domain Restriction', 'manage_options', 'advanced-email-domain-restriction', array( $this, 'aedr_options_page' ) ); } /** * Run the main functionality. * * Hooks into various registration processes to enforce email domain restrictions. * * @since 1.0.0 * @since 1.1.0 woocommerce action added * @since 1.5.0 LearnDash, BuddyPress, MemberPress, Paid Memberships Pro, Gravity Forms, Fluent Forms support added */ public function run() { add_filter( 'registration_errors', array( $this, 'aedr_check_email_domain' ), 999, 3 ); add_filter( 'woocommerce_registration_errors', array( $this, 'aedr_check_woocommerce_email_domain' ), 999, 3 ); add_filter( 'wp_mu_validate_user_signup', array( $this, 'aedr_check_multisite_email_domain' ), 999 ); // Contact Form 7 Support. add_filter( 'wpcf7_validate_email', array( $this, 'aedr_wpcf7_validate_email' ), 10, 2 ); add_filter( 'wpcf7_validate_email*', array( $this, 'aedr_wpcf7_validate_email' ), 10, 2 ); // WPForms Support. add_action( 'wpforms_process_validate_email', array( $this, 'aedr_wpforms_validate_email' ), 10, 3 ); // Elementor Pro Forms Support. add_action( 'elementor_pro/forms/validation', array( $this, 'aedr_elementor_validate_email' ), 10, 2 ); // LearnDash Support. // The modal already blocks via registration_errors (it posts to wp-login.php?action=register), // but it only *displays* a message for error codes present in its own recognized-errors list — // this filter adds ours to that list so the modal actually shows the message. add_filter( 'learndash_registration_errors', array( $this, 'aedr_learndash_registration_errors' ) ); // BuddyPress / BuddyBoss Support. add_filter( 'bp_core_validate_user_signup', array( $this, 'aedr_check_buddypress_email_domain' ), 999 ); // MemberPress Support. add_filter( 'mepr-validate-signup', array( $this, 'aedr_check_memberpress_email_domain' ) ); // Paid Memberships Pro Support. add_filter( 'pmpro_registration_checks', array( $this, 'aedr_check_pmpro_email_domain' ) ); // Gravity Forms Support. add_filter( 'gform_field_validation', array( $this, 'aedr_check_gravity_forms_email_domain' ), 10, 5 ); // Fluent Forms Support. add_filter( 'fluentform/validate_input_item_input_email', array( $this, 'aedr_check_fluent_forms_email_domain' ), 10, 5 ); // Tutor LMS Support. add_filter( 'tutor_signup_validation_errors', array( $this, 'aedr_check_tutor_lms_email_domain' ) ); // ACF (Advanced Custom Fields) Support. add_filter( 'acf/validate_value/type=email', array( $this, 'aedr_check_acf_email_domain' ), 10, 4 ); } /** * Get plugin settings. * * @return array * @since 1.2.0 */ private function get_settings() { if ( null === $this->settings ) { $this->settings = get_option( 'aedr_settings', array() ); } return $this->settings; } /** * Handle CSV export. * * @since 1.2.0 */ public function aedr_handle_export_csv() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'advanced-email-domain-restriction' ) ); } check_admin_referer( 'aedr_export_nonce' ); $options = $this->get_settings(); $domains = isset( $options['aedr_allowed_domains'] ) ? $options['aedr_allowed_domains'] : array(); $this->csv_processor->export_domains_to_csv( $domains ); } /** * AJAX handler: test whether an email address would be allowed or blocked. * * @since 1.4.0 */ public function aedr_ajax_test_email() { check_ajax_referer( 'aedr_test_email_nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Unauthorized', 'advanced-email-domain-restriction' ) ), 403 ); } $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $options = $this->get_settings(); $rules = isset( $options['aedr_allowed_domains'] ) ? (array) $options['aedr_allowed_domains'] : array(); $rules = array_filter( array_map( 'strtolower', array_map( 'trim', $rules ) ) ); if ( empty( $rules ) ) { wp_send_json_success( array( 'allowed' => true, 'matched_rule' => null, 'note' => __( 'No rules configured — all emails are allowed.', 'advanced-email-domain-restriction' ), ) ); } $matched_rule = $this->matcher->find_matching_rule( $email, $rules ); wp_send_json_success( array( 'allowed' => null !== $matched_rule, 'matched_rule' => $matched_rule, ) ); } /** * Initialize settings. * * Registers the settings, sections, and fields for the plugin. * * @since 1.0.0 */ public function aedr_settings_init() { register_setting( 'aedr_options', 'aedr_settings', array( $this, 'aedr_validate_settings' ) ); add_settings_section( 'aedr_section', __( 'Domain Settings', 'advanced-email-domain-restriction' ), array( $this, 'aedr_settings_section_callback' ), 'aedr_options' ); // Add fields for allowed domains. add_settings_field( 'aedr_allowed_domains', __( 'Allowed Domains', 'advanced-email-domain-restriction' ), array( $this, 'aedr_render_entry_table' ), 'aedr_options', 'aedr_section', array( 'field' => 'aedr_allowed_domains', 'label' => __( 'Domain', 'advanced-email-domain-restriction' ), ) ); // Add field for custom error messages. add_settings_field( 'aedr_custom_error_message_allowed', __( 'Custom Error Message for Allowed Domains', 'advanced-email-domain-restriction' ), array( $this, 'aedr_render_text_field' ), 'aedr_options', 'aedr_section', array( 'field' => 'aedr_custom_error_message_allowed' ) ); } /** * Render entry table. * * Renders a table for entering allowed domains, with inline rule-type labels * and a test-email checker panel. * * @param array $args Arguments for rendering the table. * * @since 1.0.0 */ public function aedr_render_entry_table( $args ) { $options = $this->get_settings(); $entries = isset( $options[ $args['field'] ] ) ? $options[ $args['field'] ] : array(); ?>
example.com — .edu — john@example.com — *.example.com — =1` query flag (see `learndash_user_register_error()` in
* includes/payments/ld-login-registration-functions.php), and the modal only
* *displays* a message for codes present in its own recognized-errors list
* (built by `learndash_login_error_conditions()` in the same file). Without
* this filter our error code isn't in that list, so the user sees no message
* even though registration was correctly blocked.
*
* `learndash_registration_errors` (no hyphen) is the current, non-deprecated
* filter as of LearnDash 4.5.0 — it replaced the older hyphenated
* `learndash-registration-errors` filter, which is deprecated and no longer
* reliable.
*
* @param array $registration_errors Map of error code => message LearnDash recognizes.
* @return array
* @since 1.5.0
*/
public function aedr_learndash_registration_errors( $registration_errors ) {
$registration_errors[ AEDR_Validator::ERROR_CODE ] = $this->aedr_get_custom_error_message();
return $registration_errors;
}
/**
* Check email domain during BuddyPress / BuddyBoss registration.
*
* @param array $result The signup result, including user_name, user_email, and errors keys.
* @return array The modified signup result.
* @since 1.5.0
*/
public function aedr_check_buddypress_email_domain( $result ) {
if ( empty( $result['user_email'] ) || $this->validator->is_domain_allowed( $result['user_email'] ) ) {
return $result;
}
$result['errors']->add( AEDR_Validator::ERROR_CODE, $this->aedr_get_custom_error_message() );
return $result;
}
/**
* Check email domain during MemberPress signup.
*
* @param array $errors Existing MemberPress signup error strings.
* @return array
* @since 1.5.0
*/
public function aedr_check_memberpress_email_domain( $errors ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended -- MemberPress verifies its own nonce before calling validation filters.
$email = isset( $_POST['user_email'] ) ? sanitize_email( wp_unslash( $_POST['user_email'] ) ) : '';
if ( empty( $email ) || $this->validator->is_domain_allowed( $email ) ) {
return $errors;
}
$errors[] = $this->aedr_get_custom_error_message();
return $errors;
}
/**
* Check email domain during Paid Memberships Pro registration/checkout.
*
* @param bool $should_continue Whether to continue registration.
* @return bool
* @since 1.5.0
*/
public function aedr_check_pmpro_email_domain( $should_continue ) {
if ( ! $should_continue ) {
return $should_continue;
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended -- PMPro verifies its own nonce before calling registration checks.
$email = isset( $_REQUEST['bemail'] ) ? sanitize_email( wp_unslash( $_REQUEST['bemail'] ) ) : '';
if ( empty( $email ) || $this->validator->is_domain_allowed( $email ) ) {
return $should_continue;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- $pmpro_msg/$pmpro_msgt are PMPro's own globals, not ours; this is how PMPro itself expects registration messages to be set.
global $pmpro_msg, $pmpro_msgt;
$pmpro_msg = $this->aedr_get_custom_error_message();
$pmpro_msgt = 'pmpro_error';
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
return false;
}
/**
* Validate email fields on Gravity Forms submissions.
*
* @param array $result Validation result: { is_valid, message }.
* @param mixed $value Field value (string, or array for multi-input fields).
* @param array $form Current form.
* @param object $field Current field.
* @param string $context Submission context (form-submit, api-submit, api-validate).
* @return array
* @since 1.5.0
*/
public function aedr_check_gravity_forms_email_domain( $result, $value, $form, $field, $context = '' ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- $context is optional for backward compatibility with GF < 2.6.3.2.
if ( 'email' !== $field->type ) {
return $result;
}
$email = sanitize_email( is_array( $value ) ? reset( $value ) : $value );
if ( empty( $email ) || $this->validator->is_domain_allowed( $email ) ) {
return $result;
}
$result['is_valid'] = false;
$result['message'] = $this->aedr_get_custom_error_message();
return $result;
}
/**
* Validate the email input on Fluent Forms submissions.
*
* @param string|array $error Existing error/default value.
* @param array $field Field settings.
* @param array $form_data Submitted form data.
* @param array $fields All fields.
* @param object $form Form object.
* @return string|array
* @since 1.5.0
*/
public function aedr_check_fluent_forms_email_domain( $error, $field, $form_data, $fields = array(), $form = null ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- $fields and $form are optional for backward compatibility.
$field_name = isset( $field['name'] ) ? $field['name'] : '';
if ( empty( $field_name ) || empty( $form_data[ $field_name ] ) ) {
return $error;
}
$email = sanitize_email( $form_data[ $field_name ] );
if ( empty( $email ) || $this->validator->is_domain_allowed( $email ) ) {
return $error;
}
return array( $this->aedr_get_custom_error_message() );
}
/**
* Validate email domain on Tutor LMS student & instructor registration.
*
* @param array $errors Existing registration validation errors.
* @return array
* @since 1.5.0
*/
public function aedr_check_tutor_lms_email_domain( $errors ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended -- Tutor LMS verifies nonce before running registration validation filters.
$email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '';
if ( empty( $email ) || $this->validator->is_domain_allowed( $email ) ) {
return $errors;
}
if ( ! is_array( $errors ) ) {
$errors = array();
}
$errors['email'] = $this->aedr_get_custom_error_message();
return $errors;
}
/**
* Validate ACF email field inputs against allowed domain rules.
*
* @param bool|string $valid Validation result (true, or error message string).
* @param mixed $value Field value.
* @param array $field Field settings array.
* @param string $input_name Input element name attribute.
* @return bool|string
* @since 1.5.0
*/
public function aedr_check_acf_email_domain( $valid, $value, $field, $input_name ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- $field and $input_name are required by acf/validate_value/type=email signature.
if ( true !== $valid || empty( $value ) ) {
return $valid;
}
$email = sanitize_email( is_array( $value ) ? reset( $value ) : $value );
if ( empty( $email ) || $this->validator->is_domain_allowed( $email ) ) {
return $valid;
}
return $this->aedr_get_custom_error_message();
}
/**
* Validate settings.
*
* Validates the settings input before saving. Deduplicates rules and
* generates admin notices for CSV import summaries and duplicate removals.
*
* @param array $input The input settings.
*
* @return array The validated settings.
*
* @since 1.0.0
*/
public function aedr_validate_settings( $input ) {
if ( ! current_user_can( 'manage_options' ) ) {
return array();
}
$validated_input = array();
// Validate allowed domains from manual input.
if ( isset( $input['aedr_allowed_domains'] ) && is_array( $input['aedr_allowed_domains'] ) ) {
$domains = array_map( 'sanitize_text_field', $input['aedr_allowed_domains'] );
$domains = array_map( 'trim', $domains );
$domains = array_map( 'strtolower', $domains );
$domains = array_filter( $domains );
$validated_input['aedr_allowed_domains'] = array_values( $domains );
} else {
$validated_input['aedr_allowed_domains'] = array();
}
// Handle CSV Bulk Upload.
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- WordPress Settings API verifies nonce before calling sanitize callbacks.
if ( ! empty( $_FILES['aedr_csv_upload']['tmp_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$result = $this->csv_processor->parse_csv_domains( // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.NonceVerification.Missing
$_FILES['aedr_csv_upload'], // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$validated_input['aedr_allowed_domains']
);
if ( ! empty( $result['domains'] ) ) {
$validated_input['aedr_allowed_domains'] = array_values(
array_unique( array_merge( $validated_input['aedr_allowed_domains'], $result['domains'] ) )
);
add_settings_error(
'aedr_settings',
'aedr_csv_success',
sprintf(
/* translators: 1: imported count, 2: skipped count, 3: invalid count */
__( 'CSV import complete — Imported: %1$d / Skipped duplicates: %2$d / Invalid rows: %3$d', 'advanced-email-domain-restriction' ),
$result['imported'],
$result['skipped'],
$result['invalid']
),
'updated'
);
} else {
add_settings_error(
'aedr_settings',
'aedr_csv_error',
sprintf(
/* translators: 1: skipped count, 2: invalid count */
__( 'No new rules imported from CSV — Skipped duplicates: %1$d / Invalid rows: %2$d', 'advanced-email-domain-restriction' ),
$result['skipped'],
$result['invalid']
),
'error'
);
}
}
// Deduplicate manually-entered rules and report.
$before = count( $validated_input['aedr_allowed_domains'] );
$validated_input['aedr_allowed_domains'] = array_values( array_unique( $validated_input['aedr_allowed_domains'] ) );
$removed = $before - count( $validated_input['aedr_allowed_domains'] );
if ( $removed > 0 ) {
add_settings_error(
'aedr_settings',
'aedr_duplicates_removed',
sprintf(
/* translators: 1: total saved count, 2: duplicates removed count */
__( '%1$d rules saved, %2$d duplicate(s) removed.', 'advanced-email-domain-restriction' ),
count( $validated_input['aedr_allowed_domains'] ),
$removed
),
'updated'
);
}
// Validate custom error messages.
if ( isset( $input['aedr_custom_error_message_allowed'] ) ) {
$validated_input['aedr_custom_error_message_allowed'] = sanitize_textarea_field( $input['aedr_custom_error_message_allowed'] );
}
// Invalidate settings cache.
$this->settings = null;
return $validated_input;
}
}
}