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

RecaptchaV2InvisibleValidator::data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
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 RecaptchaV2InvisibleValidator extends CaptchaValidatorAbstract
9
{
10
    /**
11
     * @see https://developers.google.com/recaptcha/docs/invisible
12
     */
13
    public function config(): array
14
    {
15
        $language = $this->getLocale();
16
        $urlParameters = array_filter([
17
            'hl' => $language,
18
            'render' => 'explicit',
19
        ]);
20
        return glsr(CaptchaConfigDefaults::class)->merge([
21
            'badge' => glsr_get_option('forms.captcha.position'),
22
            'class' => 'glsr-g-recaptcha',
23
            'language' => $language,
24
            'sitekey' => $this->siteKey(),
25
            'size' => 'invisible',
26
            'theme' => glsr_get_option('forms.captcha.theme'),
27
            'type' => 'recaptcha_v2_invisible',
28
            'urls' => [
29
                'nomodule' => add_query_arg($urlParameters, 'https://www.google.com/recaptcha/api.js'),
30
            ],
31
        ]);
32
    }
33
34
    public function isEnabled(): bool
35
    {
36
        return glsr(Captcha::class)->isEnabled('recaptcha_v2_invisible');
37
    }
38
39
    protected function data(): array
40
    {
41
        $token = $this->token();
42
        if (array_key_exists($token, $this->errorCodes())) {
43
            $token = '';
44
        }
45
        return [
46
            'remoteip' => $this->request->ip_address,
47
            'response' => $token,
48
            'secret' => $this->siteSecret(),
49
            'sitekey' => $this->siteKey(),
50
        ];
51
    }
52
53
    protected function errorCodes(): array
54
    {
55
        return [
56
            'bad-request' => 'The request is invalid or malformed.',
57
            'invalid-input-response' => 'The response parameter is invalid or malformed.',
58
            'invalid-input-secret' => 'The secret key is invalid or malformed.',
59
            'missing-input-response' => 'The response parameter is missing.',
60
            'missing-input-secret' => 'Your secret key is missing.',
61
            'sitekey_invalid' => 'Your site key is invalid.',
62
            'sitekey_missing' => 'Your site key is missing.',
63
            'timeout-or-duplicate' => 'The response is no longer valid: either is too old or has been used previously.',
64
        ];
65
    }
66
67
    protected function errors(array $errors): array
68
    {
69
        if (empty($this->siteKey())) {
70
            $errors[] = 'sitekey_missing';
71
        } elseif ('sitekey_invalid' === $this->token()) {
72
            $errors[] = 'sitekey_invalid';
73
        }
74
        return parent::errors($errors);
75
    }
76
77
    protected function siteKey(): string
78
    {
79
        return glsr_get_option('forms.recaptcha.key');
80
    }
81
82
    protected function siteSecret(): string
83
    {
84
        return glsr_get_option('forms.recaptcha.secret');
85
    }
86
87
    protected function siteVerifyUrl(): string
88
    {
89
        return 'https://www.google.com/recaptcha/api/siteverify';
90
    }
91
92
    protected function token(): string
93
    {
94
        return $this->request['_recaptcha'] ?? '';
95
    }
96
}
97