diff --git a/js/qsm-quiz.js b/js/qsm-quiz.js index 171674df1..d29d022d3 100644 --- a/js/qsm-quiz.js +++ b/js/qsm-quiz.js @@ -596,13 +596,28 @@ function isValidDomains(email, domains) { if (0 == domains.length) { return true; } - for (var i = 0; i < domains.length; i++) { + for (let i = 0; i < domains.length; i++) { if (email.indexOf(domains[i]) != -1) { return true; } } return false; } +function isBlockedDomain(email, blockdomains) { + if (typeof blockdomains === 'undefined') { + return false; + } + if (blockdomains.length === 0) { + return false; + } + for (let i = 0; i < blockdomains.length; i++) { + if (email.indexOf(blockdomains[i]) !== -1) { + return true; + } + } + return false; +} + /** * Validates a URL. * @@ -732,6 +747,16 @@ function qmnValidation(element, quiz_form_id) { show_result_validation = false; } } + /** + * Validate email from blocked domains. + */ + let blockdomains = jQuery(this).attr('data-blockdomains'); + if (typeof blockdomains !== 'undefined') { + if (isBlockedDomain(x, blockdomains.split(","))) { + qmnDisplayError(error_messages.email_error_text, jQuery(this), quiz_form_id); + show_result_validation = false; + } + } } if (jQuery(this).attr('class').indexOf('mlwUrl') !== -1 && this.value !== "") { // Remove any trailing and preceeding space. diff --git a/php/admin/options-page-contact-tab.php b/php/admin/options-page-contact-tab.php index 7e643a536..a6d8966e2 100644 --- a/php/admin/options-page-contact-tab.php +++ b/php/admin/options-page-contact-tab.php @@ -200,6 +200,11 @@ function qsm_options_contact_tab_template() {
+
+ + + +
diff --git a/php/classes/class-qsm-contact-manager.php b/php/classes/class-qsm-contact-manager.php index 758e9b5fa..4fd703457 100644 --- a/php/classes/class-qsm-contact-manager.php +++ b/php/classes/class-qsm-contact-manager.php @@ -372,6 +372,18 @@ public static function save_fields( $quiz_id, $fields ) { $fields[ $i ]['allowdomains'] = implode( ',', $allowdomains ); } + // Validate blocked domains + if ( ! empty( $fields[ $i ]['blockdomains'] ) ) { + $blockdomains = explode( ',', $fields[ $i ]['blockdomains'] ); + // Trim domains + $blockdomains = array_map( 'trim', $blockdomains ); + // Filter domain + $blockdomains = array_filter( $blockdomains, function( $blockdomain ) { + return preg_match( '/^([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,63}$/', $blockdomain ) && ( strlen( $blockdomain ) <= 253 ); + } ); + + $fields[ $i ]['blockdomains'] = implode( ',', $blockdomains ); + } if ( ! empty( $fields[ $i ]['options'] ) ) { $options = sanitize_text_field( wp_unslash( $fields[ $i ]['options'] ) ); $fields[ $i ]['options'] = $options; @@ -476,6 +488,11 @@ public static function generate_contact_field( $field, $index, $quiz_options, $d $allowdomains = array_map( 'trim', explode( ',', $field['allowdomains'] ) ); $fieldAttr .= " data-domains='" . implode( ',', array_filter( $allowdomains ) ) . "' "; } + // Add code to block specific domains + if ( isset( $field['blockdomains'] ) && ! empty( $field['blockdomains'] ) ) { + $blockdomains = array_map( 'trim', explode( ',', $field['blockdomains'] ) ); + $fieldAttr .= " data-blockdomains='" . implode( ',', array_filter( $blockdomains ) ) . "' "; + } $class = apply_filters( 'qsm_contact_email_field_class', $class, $field['use'] ); $fieldAttr .= " placeholder='" . esc_attr( wp_strip_all_tags( $field_placeholder ) ) . "' "; if ( ! isset( $field['hide_label'] ) || 'true' != $field['hide_label'] ) {