Completed
Pull Request — staging (#840)
by
unknown
18:46
created

Recaptcha::get_options()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 128
nop 1
dl 0
loc 28
rs 8.2111
c 0
b 0
f 0
1
<?php
2
3
namespace YIKES\EasyForms\Model;
4
5
use YIKES\EasyForms\Exception\InvalidRecaptcha;
6
7
final class Recaptcha {
8
9
    public $recaptcha_options;
10
    public $site_key;
11
    public $secret_key;
12
13
    const STATUS     = 'yikes-mc-recaptcha-status';
14
    const SITE_KEY   = 'yikes-mc-recaptcha-site-key';
15
    const SECRET_KEY = 'yikes-mc-recaptcha-secret-key';
16
17
    public function setup( $recaptcha_options = [] ) {
18
        if ( ! $this->has_recaptcha() ) {
19
            return false;
20
        }
21
        return $this->get_options( $recaptcha_options );
22
    }
23
24
    private function has_recaptcha() {
25
        if ( get_option( static::STATUS, '' ) == '1' ) {
26
            return true;
27
        }
28
        return false;
29
    }
30
31
    private function get_site_key() {
32
        $site_key = get_option( 'yikes-mc-recaptcha-secret-key' , '' );
33
        if ( ! $site_key ) {
34
            throw InvalidRecaptcha::from_site_key();
35
        }
36
        return $site_key;
37
    }
38
39
    private function get_secret_key() {
40
        $secret_key = get_option( 'yikes-mc-recaptcha-secret-key' , '' );
41
        if ( ! $secret_key ) {
42
            throw InvalidRecaptcha::from_secret_key();
43
        }
44
        return $secret_key;
45
    }
46
47
    private function get_options( $defaults ) {
48
        // Store the site language (to load recaptcha in a specific language).
49
        $locale       = get_locale();
50
        $locale_split = explode( '_', $locale );
51
52
        // Setup reCAPTCHA parameters.
53
        $lang       = ! empty( $locale_split ) ? $locale_split[0] : $locale;
54
        $lang       = ! empty( $defaults['recaptcha_lang'] ) ? $defaults['recaptcha_lang'] : $lang;
55
        $type       = ! empty( $defaults['recaptcha_type'] ) ? strtolower( $defaults['recaptcha_type'] ) : 'image'; // setup recaptcha type
56
        $theme      = ! empty( $defaults['recaptcha_theme'] ) ? strtolower( $defaults['recaptcha_theme'] ) : 'light'; // setup recaptcha theme
57
        $size       = ! empty( $defaults['recaptcha_size'] ) ? strtolower( $defaults['recaptcha_size'] ) : 'normal'; // setup recaptcha size
58
        $data_cb    = ! empty( $defaults['recaptcha_data_callback'] ) ? $defaults['recaptcha_data_callback'] : false; // setup recaptcha size
59
        $expired_cb = ! empty( $defaults['recaptcha_expired_callback'] ) ? $defaults['recaptcha_expired_callback'] : false; // setup recaptcha size
60
61
        $script_params = '?hl=' . $lang . '&onload=renderReCaptchaCallback&render=explicit';
62
63
        return [
64
            'language'         => $lang,
65
            'theme'            => $theme,
66
            'type'             => $type,
67
            'size'             => $size,
68
            'success_callback' => $data_cb,
69
            'expired_callback' => $expired_cb,
70
            'script_params'    => $script_params,
71
            'site_key'         => $this->get_site_key(),
72
            'secret_key'       => $this->get_secret_key(),
73
        ];
74
    }
75
}