Completed
Branch BUG/11475/decode-site-title-fo... (bbd86e)
by
unknown
13:39 queued 25s
created

EE_Invisible_Recaptcha_Input::addIframeAssets()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 41
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 28
nc 2
nop 1
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\exceptions\InvalidDataTypeException;
4
use EventEspresso\core\exceptions\InvalidInterfaceException;
5
6
defined('EVENT_ESPRESSO_VERSION') || exit;
7
8
9
10
/**
11
 * Class EE_Invisible_Recaptcha_Input
12
 * Although technically not an actual form input,
13
 * this class allows Google's Invisible reCAPTCHA
14
 * to be added to Event Espresso's form system
15
 *
16
 * @author  Brent Christensen
17
 * @since   $VID:$
18
 */
19
class EE_Invisible_Recaptcha_Input extends EE_Form_Input_Base
20
{
21
22
    const SCRIPT_HANDLE_GOOGLE_INVISIBLE_RECAPTCHA   = 'google_invisible_recaptcha';
23
24
    const SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA = 'espresso_invisible_recaptcha';
25
26
    /**
27
     * @var EE_Registration_Config $config
28
     */
29
    private $config;
30
31
    /**
32
     * @var string $recaptcha_id
33
     */
34
    private $recaptcha_id;
35
36
    /**
37
     * @var string $submit_button_id
38
     */
39
    private $submit_button_id;
40
41
42
    /**
43
     * @param array                  $input_settings
44
     * @param EE_Registration_Config $registration_config
45
     * @throws InvalidArgumentException
46
     * @throws InvalidDataTypeException
47
     * @throws InvalidInterfaceException
48
     * @throws DomainException
49
     */
50
    public function __construct(array $input_settings = array(), EE_Registration_Config $registration_config = null)
51
    {
52
        $this->_set_display_strategy(new EE_Invisible_Recaptcha_Display_Strategy());
53
        parent::__construct($input_settings);
54
        $registration_config    = $registration_config instanceof EE_Registration_Config
55
            ? $registration_config
56
            : EE_Registry::instance()->CFG->registration;
57
        $this->config           = $registration_config;
58
        $this->recaptcha_id     = isset($input_settings['recaptcha_id'])
59
            ? $input_settings['recaptcha_id']
60
            : substr(spl_object_hash($this), 8, 8);
61
        $this->submit_button_id = isset($input_settings['submit_button_id'])
62
            ? $input_settings['submit_button_id']
63
            : '';
64
        if(
65
            isset($input_settings['localized_vars'])
66
            && filter_var($input_settings['iframe'], FILTER_VALIDATE_BOOLEAN)
67
        ) {
68
            $this->addIframeAssets($input_settings['localized_vars']);
69
        } else {
70
            $this->registerScripts();
71
        }
72
    }
73
74
75
    /**
76
     * @return bool
77
     */
78
    public function useCaptcha()
79
    {
80
        return $this->config->use_captcha && $this->config->recaptcha_theme === 'invisible';
81
    }
82
83
84
    /**
85
     * @return string
86
     */
87
    public function badge()
88
    {
89
        return $this->config->recaptcha_badge;
90
    }
91
92
93
    /**
94
     * @return string
95
     */
96
    public function language()
97
    {
98
        return $this->config->recaptcha_language;
99
    }
100
101
102
    /**
103
     * @return string
104
     */
105
    public function siteKey()
106
    {
107
        return $this->config->recaptcha_publickey;
108
    }
109
110
111
    /**
112
     * @return string
113
     */
114
    public function secretKey()
115
    {
116
        return $this->config->recaptcha_privatekey;
117
    }
118
119
120
    /**
121
     * @return string
122
     */
123
    public function recaptchaId()
124
    {
125
        return $this->recaptcha_id;
126
    }
127
128
129
    /**
130
     * @return string
131
     */
132
    public function submitButtonId()
133
    {
134
        return $this->submit_button_id;
135
    }
136
137
138
    /**
139
     * @param array $localized_vars
140
     * @throws DomainException
141
     */
142
    private function addIframeAssets(array $localized_vars)
143
    {
144
        if (! $this->useCaptcha()) {
145
            return;
146
        }
147
        add_filter(
148
            'FHEE__EED_Ticket_Selector__ticket_selector_iframe__js',
149
            function(array $iframe_assets) {
150
                $iframe_assets[ EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA ] =
151
                    EED_Recaptcha_Invisible::assetsUrl()
152
                    . 'espresso_invisible_recaptcha.js?ver='
153
                    . EVENT_ESPRESSO_VERSION;
154
                $iframe_assets[ EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_GOOGLE_INVISIBLE_RECAPTCHA ] =
155
                    add_query_arg(
156
                        array(
157
                            'onload' => 'espressoLoadRecaptcha',
158
                            'render' => 'explicit',
159
                            'hl'     => $this->language(),
160
                        ),
161
                        'https://www.google.com/recaptcha/api.js?'
162
                    );
163
                return $iframe_assets;
164
            }
165
        );
166
        add_filter(
167
            'FHEE__EventEspresso_modules_ticket_selector_TicketSelectorIframe__construct__js_attributes',
168
            function (array $iframe_asset_attributes)
169
            {
170
                $iframe_asset_attributes[ EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_GOOGLE_INVISIBLE_RECAPTCHA ]
171
                    = ' async="async" defer="defer"';
172
                return $iframe_asset_attributes;
173
            }
174
        );
175
        add_action(
176
            'AHEE__EventEspresso_modules_ticket_selector_TicketSelectorIframe__construct__complete',
177
            function (EventEspresso\modules\ticket_selector\TicketSelectorIframe $ticket_selector_iframe) use ($localized_vars)
178
            {
179
                $ticket_selector_iframe->addLocalizedVars($localized_vars, 'eeRecaptcha');
180
            }
181
        );
182
    }
183
184
185
    /**
186
     * @return void
187
     */
188
    private function registerScripts()
189
    {
190
        if (! $this->useCaptcha()) {
191
            return;
192
        }
193
        add_filter('script_loader_tag', array($this, 'addScriptAttributes'), 10, 2);
194
        wp_register_script(
195
            EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA,
196
            EED_Recaptcha_Invisible::assetsUrl() . 'espresso_invisible_recaptcha.js',
197
            array('espresso_core'),
198
            EVENT_ESPRESSO_VERSION,
199
            true
200
        );
201
        wp_register_script(
202
            EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_GOOGLE_INVISIBLE_RECAPTCHA,
203
            add_query_arg(
204
                array(
205
                    'onload' => 'espressoLoadRecaptcha',
206
                    'render' => 'explicit',
207
                    'hl'     => $this->language(),
208
                ),
209
                'https://www.google.com/recaptcha/api.js?'
210
            ),
211
            array(EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA),
212
            false,
213
            true
214
        );
215
    }
216
217
218
    /**
219
     * @param string $tag
220
     * @param string $handle
221
     * @return string
222
     */
223
    public function addScriptAttributes($tag, $handle)
224
    {
225
        if ($handle === EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_GOOGLE_INVISIBLE_RECAPTCHA) {
226
            $tag = str_replace('></script>', ' async="async" defer="defer"></script>', $tag);
227
        }
228
        return $tag;
229
    }
230
231
232
    /**
233
     * Gets the HTML for displaying the label for this form input
234
     * according to the form section's layout strategy
235
     *
236
     * @return string
237
     */
238
    public function get_html_for_label()
239
    {
240
        return '';
241
    }
242
}
243