//Adicionando o JavaScript para Validação no Rodapé
function wp24horas_add_cpf_cnpj_validation_script() {
?>
document.addEventListener('DOMContentLoaded', function() {
document.addEventListener('wpcf7submit', function(event) {
var cpfCnpjField = document.querySelector('[name="cpf_cnpj"]');
if (cpfCnpjField && !isValidCpfCnpj(cpfCnpjField.value)) {
event.preventDefault();
alert('CPF/CNPJ inválido. Por favor, insira um valor válido.');
}
});
function isValidCpfCnpj(value) {
value = value.replace(/[\D]/g, ''); // Remove caracteres não numéricos
if (value.length === 11) {
return isValidCpf(value);
} else if (value.length === 14) {
return isValidCnpj(value);
}
return false;
}
function isValidCpf(cpf) {
if (cpf.length !== 11 || /^(\d)\1{10}$/.test(cpf)) return false;
var sum, rest;
sum = 0;
for (var i = 1; i <= 9; i++) sum += parseInt(cpf.substring(i - 1, i)) * (11 - i);
rest = (sum * 10) % 11;
if ((rest === 10) || (rest === 11)) rest = 0;
if (rest !== parseInt(cpf.substring(9, 10))) return false;
sum = 0;
for (var i = 1; i = 1; i--) {
sum += numbers.charAt(length - i) * pos--;
if (pos < 2) pos = 9;
}
var result = sum % 11 = 1; i--) {
sum += numbers.charAt(length - i) * pos--;
if (pos < 2) pos = 9;
}
result = sum % 11 < 2 ? 0 : 11 - sum % 11;
if (result !== parseInt(digits.charAt(1))) return false;
return true;
}
});
true));
}
function wp24horas_form_tag_cpf_cnpj($tag) {
$tag = new WPCF7_FormTag($tag);
$validation_error = wpcf7_get_validation_error($tag->name);
$class = wpcf7_form_controls_class($tag->type);
if ($validation_error) {
$class .= ' wpcf7-not-valid';
}
$atts = array();
$atts['type'] = 'text';
$atts['name'] = $tag->name;
$atts['value'] = $tag->get_default_option($tag->values);
$atts['class'] = $tag->get_class_option($class);
$atts['id'] = $tag->get_option('id', 'id', true);
$atts['tabindex'] = $tag->get_option('tabindex', 'int', true);
$atts['aria-required'] = $tag->is_required() ? 'true' : 'false';
$atts['aria-invalid'] = $validation_error ? 'true' : 'false';
return sprintf('%3$s', sanitize_html_class($tag->name), wpcf7_format_atts($atts), $validation_error);
}
//Adicionando a Validação Personalizada no PHP
add_filter('wpcf7_validate_cpf_cnpj*', 'wp24horas_validate_cpf_cnpj', 20, 2);
add_filter('wpcf7_validate_cpf_cnpj', 'wp24horas_validate_cpf_cnpj', 20, 2);
function wp24horas_validate_cpf_cnpj($result, $tag) {
$tag = new WPCF7_FormTag($tag);
$name = $tag->name;
$value = isset($_POST[$name]) ? trim($_POST[$name]) : '';
if (!wp24horas_is_valid_cpf_cnpj($value)) {
$result->invalidate($tag, 'CPF/CNPJ inválido.');
}
return $result;
}
function wp24horas_is_valid_cpf_cnpj($value) {
$value = preg_replace('/[^0-9]/', '', $value);
if (strlen($value) === 11) {
return wp24horas_is_valid_cpf($value);
} elseif (strlen($value) === 14) {
return wp24horas_is_valid_cnpj($value);
}
return false;
}
function wp24horas_is_valid_cpf($cpf) {
if (strlen($cpf) != 11 || preg_match('/(\d)\1{10}/', $cpf)) {
return false;
}
for ($t = 9; $t < 11; $t++) {
for ($d = 0, $c = 0; $c < $t; $c++) {
$d += $cpf[$c] * (($t + 1) - $c);
}
$d = ((10 * $d) % 11) % 10;
if ($cpf[$c] != $d) {
return false;
}
}
return true;
}
function wp24horas_is_valid_cnpj($cnpj) {
if (strlen($cnpj) != 14 || preg_match('/(\d)\1{13}/', $cnpj)) {
return false;
}
$t = 12;
for ($d = 0, $p = 5, $c = 0; $c < $t; $c++) {
$d += $cnpj[$c] * $p;
$p = ($p == 2) ? 9 : $p - 1;
}
$d = ((10 * $d) % 11) % 10;
if ($cnpj[$c] != $d) {
return false;
}
$t = 13;
for ($d = 0, $p = 6, $c = 0; $c < $t; $c++) {
$d += $cnpj[$c] * $p;
$p = ($p == 2) ? 9 : $p - 1;
}
$d = ((10 * $d) % 11) % 10;
if ($cnpj[$c] != $d) {
return false;
}
return true;
}