Test Failed
Push — main ( 6435b1...db043e )
by Paul
16:23 queued 07:02
created

ProcaptchaValidator::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Validator;
4
5
use GeminiLabs\SiteReviews\Defaults\CaptchaConfigDefaults;
6
use GeminiLabs\SiteReviews\Modules\Captcha;
7
 
8
class ProcaptchaValidator extends CaptchaValidatorAbstract
9
{
10
    /**
11
     * @see https://docs.prosopo.io/
12
     */
13
    public function config(): array
14
    {
15
        return glsr(CaptchaConfigDefaults::class)->merge([
16
            'class' => glsr_get_option('forms.captcha.theme').' procaptcha',
17
            'captcha_type' => glsr_get_option('forms.procaptcha.type', 'frictionless'),
18
            'language' => $this->getLocale(),
19
            'sitekey' => $this->siteKey(),
20
            'theme' => glsr_get_option('forms.captcha.theme'),
21
            'type' => 'procaptcha',
22
            'urls' => [ // order is intentional, the module always loads first
23
                'module' => 'https://js.prosopo.io/js/procaptcha.bundle.js',
24
            ],
25
        ]);
26
    }
27
28
    public function isEnabled(): bool
29
    {
30
        return glsr(Captcha::class)->isEnabled('procaptcha');
31
    }
32
33
    public function isTokenValid(array $response): bool
34
    {
35
        return $response['success'];
36
    }
37
38
    protected function data(): array
39
    {
40
        return [
41
            'token' => $this->token(),
42
            'secret' => $this->siteSecret(),
43
        ];
44
    }
45
46
    protected function errorCodes(): array
47
    {
48
        return [
49
            'sitekey_invalid' => 'Your site key is likely invalid.',
50
            'sitekey_missing' => 'Your site key is missing.',
51
        ];
52
    }
53
54
    protected function errors(array $errors): array
55
    {
56
        if (empty($this->siteKey())) {
57
            $errors[] = 'sitekey_missing';
58
        }
59
        return parent::errors($errors);
60
    }
61
62
    protected function response(array $body): array
63
    {
64
        $body = wp_parse_args($body, [
65
            'error' => '',
66
            'status' => '',
67
            'verified' => false,
68
        ]);
69
        return [
70
            'action' => '', // unused
71
            'errors' => [$body['error']],
72
            'score' => 0, // unused
73
            'success' => ('ok' === $body['status'] && wp_validate_boolean($body['verified'])),
74
        ];
75
    }
76
77
    protected function siteKey(): string
78
    {
79
        return glsr_get_option('forms.procaptcha.key');
80
    }
81
82
    protected function siteSecret(): string
83
    {
84
        return glsr_get_option('forms.procaptcha.secret');
85
    }
86
87
    protected function siteVerifyUrl(): string
88
    {
89
        return 'https://api.prosopo.io/siteverify';
90
    }
91
92
    protected function token(): string
93
    {
94
        return $this->request['_procaptcha'] ?? '';
95
    }
96
}
97