• Resolved moein

    (@moeinkd)


    Hi
    thank you
    I just want the domains of gamil and yahoo to be allowed
    Otherwise, any domain will be blocked

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Hall

    (@hallme)

    Hey @moeinkd,
    Thanks for your support request.

    In the case of this it sounds like what you are looking for is a and email whitelist.

    We have created a feature request to be reviewed for future releases of the plugin. https://github.com/hallme/gravityforms-emailblacklist/issues/19

    With out knowing the exact details of what you are looking to do you can look into using to only send notifications or confirmation using conditional login for emails that end with gmail and yahoo.

    https://docs.gravityforms.com/enable-conditional-logic-for-notifications/
    https://docs.gravityforms.com/conditional-logic-confirmations/

    Thread Starter moein

    (@moeinkd)

    Hey @hallme
    thank you for your attention
    I found a code that allows (or ban) only the domains entered in it
    I hope it is useful for others and may help to your good plugin feature
    Regards

    /* Gravity Forms Email Domain Validator
    * @link      https://gravitywiz.com/banlimit-email-domains-for-gravity-form-email-fields/
    */
    
    class GW_Email_Domain_Validator {
        private $_args;
    	
        function __construct($args) {
            $this->_args = wp_parse_args( $args, array(
                //'form_id' => false,
                //'field_id' => false,
    			'domains' => false,
    			'validation_message' => __( '<strong>Error</strong>: Please enter a valid email address' ),
                'mode' => 'ban' // also accepts "limit"
            ) );
    
            // convert field ID to an array for consistency, it can be passed as an array or a single ID
            if($this->_args['field_id'] && !is_array($this->_args['field_id']))
                $this->_args['field_id'] = array($this->_args['field_id']);
    
            $form_filter = $this->_args['form_id'] ? "_{$this->_args['form_id']}" : '';
    
            add_filter("gform_validation{$form_filter}", array($this, 'validate'));
    
        }
    
        function validate($validation_result) {
    
            $form = $validation_result['form'];
    
            foreach($form['fields'] as &$field) {
    
                // if this is not an email field, skip
                if(RGFormsModel::get_input_type($field) != 'email')
                    continue;
    
                // if field ID was passed and current field is not in that array, skip
                if($this->_args['field_id'] && !in_array($field['id'], $this->_args['field_id']))
                    continue;
    
                $page_number = GFFormDisplay::get_source_page( $form['id'] );
                if( $page_number > 0 && $field->pageNumber != $page_number ) {
                    continue;
                }
    
                if( GFFormsModel::is_field_hidden( $form, $field, array() ) ) {
                	continue;
                }
    
                $domain = $this->get_email_domain($field);
    
                // if domain is valid OR if the email field is empty, skip
                if($this->is_domain_valid($domain) || empty($domain))
                    continue;
    
                $validation_result['is_valid'] = false;
                $field['failed_validation'] = true;
                $field['validation_message'] = sprintf($this->_args['validation_message'], $domain);
    
            }
    
            $validation_result['form'] = $form;
            return $validation_result;
        }
    
        function get_email_domain( $field ) {
            $email = explode( '@', rgpost( "input_{$field['id']}" ) );
            return trim( rgar( $email, 1 ) );
        }
    
        function is_domain_valid( $domain ) {
    
            $mode   = $this->_args['mode'];
    	    $domain = strtolower( $domain );
    
            foreach( $this->_args['domains'] as $_domain ) {
    
    	        $_domain = strtolower( $_domain );
    
                $full_match   = $domain == $_domain;
                $suffix_match = strpos( $_domain, '.' ) === 0 && $this->str_ends_with( $domain, $_domain );
                $has_match    = $full_match || $suffix_match;
    
                if( $mode == 'ban' && $has_match ) {
                    return false;
                } else if( $mode == 'limit' && $has_match ) {
                    return true;
                }
    
            }
    
            return $mode == 'limit' ? false : true;
        }
    
        function str_ends_with( $string, $text ) {
    
            $length      = strlen( $string );
            $text_length = strlen( $text );
    
            if( $text_length > $length ) {
                return false;
            }
    
            return substr_compare( $string, $text, $length - $text_length, $text_length ) === 0;
        }
    
    }
    
    class GWEmailDomainControl extends GW_Email_Domain_Validator { }
    
    # Configuration
    
    new GW_Email_Domain_Validator( array(
        //'form_id' => 326,
        //'field_id' => 1,
    	'domains' => array( 'gmail.com', 'hotmail.com', 'yahoo.com', 'domain.com' ),
    	'validation_message' => __( '<strong>Error</strong>: Please enter a valid email address' ),
        'mode' => 'limit'
    ) );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘just allow gmail and yahoo’ is closed to new replies.