Skip to:
Content

BuddyPress.org

Changeset 6336


Ignore:
Timestamp:
09/16/2012 03:29:15 PM (12 years ago)
Author:
boonebgorges
Message:

Use bp_core_validate_email_address() during registration

This centralizes email validation between registration and the Settings
component.

Also introduces bp_core_add_validation_errors(), a utility function for
converting the return value of validation functions into WP_Error-style
messages. This can be used in the future, if better username validation
functions are introduced.

Fixes #4486

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-members/bp-members-functions.php

    r6314 r6336  
    10391039
    10401040/**
     1041 * Add the appropriate errors to a WP_Error object, given results of a validation test
     1042 *
     1043 * Functions like bp_core_validate_email_address() return a structured array
     1044 * of error codes. bp_core_add_validation_error_messages() takes this array and
     1045 * parses, adding the appropriate error messages to the WP_Error object.
     1046 *
     1047 * @since BuddyPress (1.7)
     1048 * @see bp_core_validate_email_address()
     1049 *
     1050 * @param obj $errors WP_Error object
     1051 * @param array $validation_results The return value of a validation function
     1052 *   like bp_core_validate_email_address()
     1053 */
     1054function bp_core_add_validation_error_messages( WP_Error $errors, $validation_results ) {
     1055    if ( ! empty( $validation_results['invalid'] ) )
     1056        $errors->add( 'user_email', __( 'Please check your email address.', 'buddypress' ) );
     1057
     1058    if ( ! empty( $validation_results['domain_banned'] ) )
     1059        $errors->add( 'user_email',  __( 'Sorry, that email address is not allowed!', 'buddypress' ) );
     1060
     1061    if ( ! empty( $validation_results['domain_not_allowed'] ) )
     1062        $errors->add( 'user_email', __( 'Sorry, that email address is not allowed!', 'buddypress' ) );
     1063
     1064    if ( ! empty( $validation_results['in_use'] ) )
     1065        $errors->add( 'user_email', __( 'Sorry, that email address is already used!', 'buddypress' ) );
     1066}
     1067
     1068/**
    10411069 * Validate a user name and email address when creating a new user.
    1042  *
    1043  * @todo Refactor to use bp_core_validate_email_address()
    10441070 *
    10451071 * @param string $user_name Username to validate
     
    10501076
    10511077    $errors = new WP_Error();
    1052     $user_email = sanitize_email( $user_email );
    10531078
    10541079    // Apply any user_login filters added by BP or other plugins before validating
     
    10791104        $errors->add( 'user_name', __( 'Sorry, usernames must have letters too!', 'buddypress' ) );
    10801105
    1081     if ( !is_email( $user_email ) )
    1082         $errors->add( 'user_email', __( 'Please check your email address.', 'buddypress' ) );
    1083 
    1084     if ( function_exists( 'is_email_address_unsafe' ) && is_email_address_unsafe( $user_email ) )
    1085         $errors->add( 'user_email',  __( 'Sorry, that email address is not allowed!', 'buddypress' ) );
    1086 
    1087     $limited_email_domains = get_site_option( 'limited_email_domains', 'buddypress' );
    1088 
    1089     if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
    1090         $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
    1091 
    1092         if ( in_array( $emaildomain, (array) $limited_email_domains ) == false )
    1093             $errors->add( 'user_email', __( 'Sorry, that email address is not allowed!', 'buddypress' ) );
    1094     }
    1095 
    10961106    // Check if the username has been used already.
    10971107    if ( username_exists( $user_name ) )
    10981108        $errors->add( 'user_name', __( 'Sorry, that username already exists!', 'buddypress' ) );
    10991109
    1100     // Check if the email address has been used already.
    1101     if ( email_exists( $user_email ) )
    1102         $errors->add( 'user_email', __( 'Sorry, that email address is already used!', 'buddypress' ) );
    1103 
     1110    // Validate the email address and process the validation results into
     1111    // error messages
     1112    $validate_email = bp_core_validate_email_address( $user_email );
     1113    bp_core_add_validation_error_messages( $errors, $validate_email );
     1114
     1115    // Assemble the return array
    11041116    $result = array( 'user_name' => $user_name, 'user_email' => $user_email, 'errors' => $errors );
    11051117
Note: See TracChangeset for help on using the changeset viewer.