| 1 |
<template> |
| 2 |
<div |
| 3 |
v-if="isHCaptchaConfigured" |
| 4 |
class="ivyforms-hcaptcha-container ivyforms-flex ivyforms-flex-direction-column ivyforms-width-100" |
| 5 |
:class="{ 'ivyforms-hcaptcha-container--has-error': error }" |
| 6 |
> |
| 7 |
<HCaptcha |
| 8 |
:site-key="hcaptchaConfig.siteKey" |
| 9 |
:type="hcaptchaConfig.type" |
| 10 |
:error="hcaptchaError" |
| 11 |
@verify="handleVerify" |
| 12 |
@error="handleError" |
| 13 |
@expire="handleExpire" |
| 14 |
/> |
| 15 |
|
| 16 |
<div |
| 17 |
v-if="hcaptchaError" |
| 18 |
class="ivyforms-security-field-error ivyforms-flex ivyforms-align-items-center regular-14 ivyforms-mt-6 ivyforms-gap-4" |
| 19 |
> |
| 20 |
<IvyIcon name="danger" size="s" type="fill-duo" color="var(--map-status-error-symbol-0)" /> |
| 21 |
{{ hcaptchaError }} |
| 22 |
</div> |
| 23 |
</div> |
| 24 |
<div v-else class="ivyforms-hcaptcha-not-configured ivyforms-width-100"> |
| 25 |
<IvyAlert :description="getLabel('hcaptcha_not_configured_public')" type="warning" /> |
| 26 |
</div> |
| 27 |
</template> |
| 28 |
|
| 29 |
<script setup lang="ts"> |
| 30 |
import { ref, computed, watch } from 'vue' |
| 31 |
import { useLabels } from '@/composables/useLabels' |
| 32 |
import HCaptcha from '@/views/_components/hcaptcha/HCaptcha.vue' |
| 33 |
import IvyAlert from '@/views/_components/alert/IvyAlert.vue' |
| 34 |
import IvyIcon from '@/views/_components/icon/IvyIcon.vue' |
| 35 |
import type { Field } from '@/types/field' |
| 36 |
import type { HCaptchaConfig, WindowHCaptchaConfig } from '@/types/hcaptcha/hcaptcha-interface' |
| 37 |
import type { HCaptchaType } from '@/types/hcaptcha/hcaptcha-type' |
| 38 |
|
| 39 |
declare global { |
| 40 |
interface Window { |
| 41 |
wpIvyHCaptchaConfig?: WindowHCaptchaConfig | unknown[] |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
interface Props { |
| 46 |
field: Field |
| 47 |
modelValue?: string |
| 48 |
error?: string |
| 49 |
} |
| 50 |
|
| 51 |
interface Emits { |
| 52 |
(event: 'update:modelValue', value: string): void |
| 53 |
} |
| 54 |
|
| 55 |
const props = defineProps<Props>() |
| 56 |
const emit = defineEmits<Emits>() |
| 57 |
|
| 58 |
const { getLabel } = useLabels() |
| 59 |
const hcaptchaToken = ref('') |
| 60 |
const hcaptchaError = ref('') |
| 61 |
|
| 62 |
// Check if hCaptcha is configured |
| 63 |
const isHCaptchaConfigured = computed(() => { |
| 64 |
// Check if global hCaptcha config exists and is configured |
| 65 |
if (!window.wpIvyHCaptchaConfig || Array.isArray(window.wpIvyHCaptchaConfig)) { |
| 66 |
return false |
| 67 |
} |
| 68 |
|
| 69 |
const config = window.wpIvyHCaptchaConfig as WindowHCaptchaConfig |
| 70 |
const hcaptchaData = config.hcaptcha |
| 71 |
|
| 72 |
if (!hcaptchaData) { |
| 73 |
return false |
| 74 |
} |
| 75 |
|
| 76 |
// Convert configured value to boolean (it might be string or number from PHP) |
| 77 |
const isConfigured = |
| 78 |
hcaptchaData.configured === true || |
| 79 |
hcaptchaData.configured === 1 || |
| 80 |
hcaptchaData.configured === '1' |
| 81 |
|
| 82 |
return isConfigured && !!hcaptchaData.siteKey |
| 83 |
}) |
| 84 |
|
| 85 |
// Get hCaptcha configuration from window object |
| 86 |
const hcaptchaConfig = computed<HCaptchaConfig>(() => { |
| 87 |
if (!window.wpIvyHCaptchaConfig || Array.isArray(window.wpIvyHCaptchaConfig)) { |
| 88 |
return { |
| 89 |
siteKey: '', |
| 90 |
type: 'checkbox', |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
const config = window.wpIvyHCaptchaConfig as WindowHCaptchaConfig |
| 95 |
const hcaptchaData = config.hcaptcha |
| 96 |
|
| 97 |
if (!hcaptchaData) { |
| 98 |
return { |
| 99 |
siteKey: '', |
| 100 |
type: 'checkbox', |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return { |
| 105 |
siteKey: hcaptchaData.siteKey || '', |
| 106 |
type: (hcaptchaData.type as HCaptchaType) || 'checkbox', |
| 107 |
} |
| 108 |
}) |
| 109 |
|
| 110 |
// Handle successful verification |
| 111 |
const handleVerify = (token: string) => { |
| 112 |
hcaptchaToken.value = token |
| 113 |
hcaptchaError.value = '' |
| 114 |
emit('update:modelValue', token) |
| 115 |
} |
| 116 |
|
| 117 |
// Handle error |
| 118 |
const handleError = (error: string) => { |
| 119 |
hcaptchaError.value = error || getLabel('hcaptcha_verification_failed') |
| 120 |
hcaptchaToken.value = '' |
| 121 |
emit('update:modelValue', '') |
| 122 |
} |
| 123 |
|
| 124 |
// Handle expiration |
| 125 |
const handleExpire = () => { |
| 126 |
hcaptchaError.value = getLabel('hcaptcha_expired') |
| 127 |
hcaptchaToken.value = '' |
| 128 |
emit('update:modelValue', '') |
| 129 |
} |
| 130 |
|
| 131 |
// Watch for external errors |
| 132 |
watch( |
| 133 |
() => props.error, |
| 134 |
(newError) => { |
| 135 |
if (newError) { |
| 136 |
hcaptchaError.value = newError |
| 137 |
} |
| 138 |
}, |
| 139 |
{ immediate: true }, |
| 140 |
) |
| 141 |
</script> |
| 142 |
|
| 143 |
<style lang="scss" scoped> |
| 144 |
.ivyforms-security-field-error { |
| 145 |
color: var(--map-status-error-symbol-0); |
| 146 |
} |
| 147 |
</style> |
| 148 |
|