Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option to block domains #2606

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion js/qsm-quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions php/admin/options-page-contact-tab.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ function qsm_options_contact_tab_template() {
<em><?php esc_html_e('Leave blank to allow all domains. ', 'quiz-master-next');?></em><br/>
<em><?php esc_html_e('Comma separated list of domains. (i.e. example.com,abc.com)', 'quiz-master-next');?></em>
</div>
<div class="qsm-contact-form-group qsm-email-option">
<label class="qsm-contact-form-label"><?php esc_html_e('Block Domains', 'quiz-master-next');?></label>
<textarea class="qsm-contact-form-control" name="blockdomains">{{data.blockdomains}}</textarea>
<em><?php esc_html_e('Comma separated list of domains. (i.e. example.com,abc.com)', 'quiz-master-next');?></em>
</div>
</div>
</div>
</script>
Expand Down
17 changes: 17 additions & 0 deletions php/classes/class-qsm-contact-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'] ) {
Expand Down