Completed
Branch FET/9575/invisible-recaptcha (9a3ea6)
by
unknown
13:26
created

EED_Recaptcha_Invisible::setLocalizedVars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\caffeinated\modules\recaptcha_invisible\InvisibleRecaptcha;
4
use EventEspresso\caffeinated\modules\recaptcha_invisible\RecaptchaFactory;
5
use EventEspresso\core\exceptions\InvalidDataTypeException;
6
use EventEspresso\core\exceptions\InvalidInterfaceException;
7
use EventEspresso\core\services\loaders\LoaderFactory;
8
9
defined('EVENT_ESPRESSO_VERSION') || exit('NO direct script access allowed');
10
11
12
13
/**
14
 * EED_Recaptcha_Invisible
15
 * Integrates Google's Invisible reCAPTCHA into the form submission process
16
 * for both the Ticket Selector and Single Page Checkout
17
 *
18
 * @package        Event Espresso
19
 * @subpackage     /modules/recaptcha_invisible/
20
 * @author         Brent Christensen
21
 */
22
class EED_Recaptcha_Invisible extends EED_Module
23
{
24
25
    /**
26
     * @var EE_Registration_Config $config
27
     */
28
    private static $config;
29
30
    /**
31
     * @var array $localized_vars
32
     */
33
    private static $localized_vars;
34
35
36
    /**
37
     * @return EED_Module|EED_Recaptcha
38
     */
39
    public static function instance()
40
    {
41
        return parent::get_instance(__CLASS__);
42
    }
43
44
45
    /**
46
     * @return void
47
     * @throws InvalidInterfaceException
48
     * @throws InvalidDataTypeException
49
     * @throws InvalidArgumentException
50
     */
51
    public static function set_hooks()
52
    {
53
        EED_Recaptcha_Invisible::setProperties();
54
        if (EED_Recaptcha_Invisible::useInvisibleRecaptcha()) {
55
            // ticket selection
56
            add_filter(
57
                'FHEE__EE_Ticket_Selector__after_ticket_selector_submit',
58
                array('EED_Recaptcha_Invisible', 'ticketSelectorForm'),
59
                10, 2
60
            );
61
            add_action(
62
                'EED_Ticket_Selector__process_ticket_selections__before',
63
                array('EED_Recaptcha_Invisible', 'processTicketSelectorForm')
64
            );
65
            // checkout
66
            add_action(
67
                'AHEE__EE_SPCO_Reg_Step__display_reg_form__reg_form',
68
                array('EED_Recaptcha_Invisible', 'spcoRegStepForm')
69
            );
70
            add_filter(
71
                'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data',
72
                array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'),
73
                10, 2
74
            );
75
            add_action('loop_end', array('EED_Recaptcha_Invisible', 'setLocalizedVars'));
76
        }
77
    }
78
79
80
    /**
81
     * @return void
82
     * @throws InvalidInterfaceException
83
     * @throws InvalidDataTypeException
84
     * @throws InvalidArgumentException
85
     */
86
    public static function set_hooks_admin()
87
    {
88
        // admin settings
89
        add_action(
90
            'AHEE__Extend_Registration_Form_Admin_Page___reg_form_settings_template',
91
            array('EED_Recaptcha_Invisible', 'adminSettings')
92
        );
93
        add_filter(
94
            'FHEE__Extend_Registration_Form_Admin_Page___update_reg_form_settings__CFG_registration',
95
            array('EED_Recaptcha_Invisible', 'updateAdminSettings')
96
        );
97
    }
98
99
100
    /**
101
     * @return void
102
     * @throws InvalidInterfaceException
103
     * @throws InvalidDataTypeException
104
     * @throws InvalidArgumentException
105
     */
106
    public static function setProperties()
107
    {
108
109
        EED_Recaptcha_Invisible::$config = EE_Registry::instance()->CFG->registration;
110
        EED_Recaptcha_Invisible::$localized_vars = array(
111
            'siteKey'          => EED_Recaptcha_Invisible::$config->recaptcha_publickey,
112
            'recaptcha_passed' => EED_Recaptcha_Invisible::recaptchaPassed(),
113
            'wp_debug'         => WP_DEBUG,
114
            'mer_active'       => defined('EE_EVENT_QUEUE_BASE_URL'),
115
        );
116
    }
117
118
119
    /**
120
     * @return boolean
121
     */
122
    public static function useInvisibleRecaptcha()
123
    {
124
        return EED_Recaptcha_Invisible::$config->use_captcha
125
               && EED_Recaptcha_Invisible::$config->recaptcha_theme === 'invisible';
126
    }
127
128
129
130
    /**
131
     * @return void
132
     */
133
    public static function setLocalizedVars()
134
    {
135
        wp_localize_script(
136
            EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA,
137
            'eeRecaptcha',
138
            apply_filters(
139
                'FHEE__EED_Recaptcha_Invisible__setLocalizedVars__localized_vars',
140
                EED_Recaptcha_Invisible::$localized_vars
141
            )
142
        );
143
    }
144
145
146
    /**
147
     * @return string
148
     */
149
    public static function assetsUrl()
150
    {
151
        return plugin_dir_url(__FILE__) . 'assets' . DS;
152
    }
153
154
155
    /**
156
     * @param \WP $WP
157
     */
158
    public function run($WP)
159
    {
160
    }
161
162
163
    /**
164
     * @return boolean
165
     * @throws InvalidInterfaceException
166
     * @throws InvalidDataTypeException
167
     * @throws InvalidArgumentException
168
     */
169
    public static function recaptchaPassed()
170
    {
171
        static $recaptcha_passed = null;
172
        if($recaptcha_passed !== null) {
173
            return $recaptcha_passed;
174
        }
175
        // logged in means you have already passed a turing test of sorts
176
        if (EED_Recaptcha_Invisible::useInvisibleRecaptcha() === false || is_user_logged_in()) {
177
            $recaptcha_passed = true;
178
            return $recaptcha_passed;
179
        }
180
        // was test already passed?
181
        $recaptcha_passed = filter_var(
182
            EE_Registry::instance()->SSN->get_session_data(
183
                InvisibleRecaptcha::SESSION_DATA_KEY_RECAPTCHA_PASSED
184
            ),
185
            FILTER_VALIDATE_BOOLEAN
186
        );
187
        return $recaptcha_passed;
188
    }
189
190
191
    /**
192
     * @param EE_Request $request
193
     * @return bool
194
     * @throws InvalidArgumentException
195
     * @throws InvalidDataTypeException
196
     * @throws InvalidInterfaceException
197
     * @throws RuntimeException
198
     */
199
    public static function verifyToken(EE_Request $request)
200
    {
201
        $invisible_recaptcha = RecaptchaFactory::create();
202
        if ($invisible_recaptcha->verifyToken($request)) {
203
            add_action('shutdown', array('EED_Recaptcha_Invisible', 'setSessionData'));
204
            return true;
205
        }
206
        return false;
207
    }
208
209
210
    /**
211
     * @param EE_Form_Section_Proper $reg_form
212
     * @return void
213
     * @throws EE_Error
214
     * @throws InvalidArgumentException
215
     * @throws InvalidDataTypeException
216
     * @throws InvalidInterfaceException
217
     */
218
    public static function spcoRegStepForm(EE_Form_Section_Proper $reg_form)
219
    {
220
        // do nothing if form isn't for a reg step or test has already been passed
221
        if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) {
222
            return;
223
        }
224
        $default_hidden_inputs = $reg_form->get_subsection('default_hidden_inputs');
225
        if ($default_hidden_inputs instanceof EE_Form_Section_Proper) {
226
            $invisible_recaptcha = RecaptchaFactory::create();
227
            $invisible_recaptcha->addToFormSection($default_hidden_inputs);
228
        }
229
    }
230
231
232
    /**
233
     * @param EE_Form_Section_Proper $reg_form
234
     * @return bool
235
     * @throws InvalidDataTypeException
236
     * @throws InvalidInterfaceException
237
     * @throws EE_Error
238
     * @throws InvalidArgumentException
239
     */
240
    public static function processSpcoRegStepForm(EE_Form_Section_Proper $reg_form)
241
    {
242
        return strpos($reg_form->name(), 'reg-step-form') !== false
243
               || ! EED_Recaptcha_Invisible::recaptchaPassed();
244
    }
245
246
247
    /**
248
     * @param array|null             $req_data
249
     * @param EE_Form_Section_Proper $reg_form
250
     * @return array
251
     * @throws EE_Error
252
     * @throws InvalidArgumentException
253
     * @throws InvalidDataTypeException
254
     * @throws InvalidInterfaceException
255
     * @throws RuntimeException
256
     */
257
    public static function receiveSpcoRegStepForm($req_data = array(), EE_Form_Section_Proper $reg_form)
258
    {
259
        // do nothing if form isn't for a reg step or test has already been passed
260
        if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) {
261
            return $req_data;
262
        }
263
        /** @var EE_Request $request */
264
        $request = LoaderFactory::getLoader()->getShared('EE_Request');
265
        if (! EED_Recaptcha_Invisible::verifyToken($request)) {
266
            if ($request->isAjax()) {
267
                $json_response = new EE_SPCO_JSON_Response();
268
                $json_response->echoAndExit();
269
            }
270
            EEH_URL::safeRedirectAndExit(
271
                EE_Registry::instance()->CFG->core->reg_page_url()
272
            );
273
        }
274
        return $req_data;
275
    }
276
277
278
    /**
279
     * @param string   $html
280
     * @param EE_Event $event
281
     * @return string
282
     * @throws InvalidArgumentException
283
     * @throws InvalidDataTypeException
284
     * @throws InvalidInterfaceException
285
     * @throws EE_Error
286
     */
287
    public static function ticketSelectorForm($html = '', EE_Event $event)
288
    {
289
        // do nothing if test has  already  been passed
290
        if (EED_Recaptcha_Invisible::recaptchaPassed()) {
291
            return $html;
292
        }
293
        $html .= RecaptchaFactory::create()->getInputHtml(
294
            array('recaptcha_id' => $event->ID())
295
        );
296
        return $html;
297
    }
298
299
300
    /**
301
     * @return void
302
     * @throws InvalidArgumentException
303
     * @throws InvalidInterfaceException
304
     * @throws InvalidDataTypeException
305
     * @throws RuntimeException
306
     */
307
    public static function processTicketSelectorForm()
308
    {
309
        // do nothing if test has  already  been passed
310
        if (EED_Recaptcha_Invisible::recaptchaPassed()) {
311
            return;
312
        }
313
        /** @var EE_Request $request */
314
        $request = LoaderFactory::getLoader()->getShared('EE_Request');
315
        if (! EED_Recaptcha_Invisible::verifyToken($request)) {
316
            $event_id   = $request->get('tkt-slctr-event-id');
317
            $return_url = $request->is_set("tkt-slctr-return-url-{$event_id}")
318
                ? $request->get("tkt-slctr-return-url-{$event_id}")
319
                : get_permalink($event_id);
320
            EEH_URL::safeRedirectAndExit($return_url);
321
        }
322
    }
323
324
325
    /**
326
     * @throws InvalidArgumentException
327
     * @throws InvalidDataTypeException
328
     * @throws InvalidInterfaceException
329
     */
330
    public static function setSessionData()
331
    {
332
        EE_Registry::instance()->SSN->set_session_data(
333
            array(InvisibleRecaptcha::SESSION_DATA_KEY_RECAPTCHA_PASSED => true)
334
        );
335
    }
336
337
338
    /**
339
     * @throws EE_Error
340
     * @throws InvalidArgumentException
341
     * @throws InvalidDataTypeException
342
     * @throws InvalidInterfaceException
343
     */
344
    public static function adminSettings()
345
    {
346
        RecaptchaFactory::getAdminModule()->adminSettings();
347
    }
348
349
350
    /**
351
     * @param EE_Registration_Config $EE_Registration_Config
352
     * @return EE_Registration_Config
353
     * @throws EE_Error
354
     * @throws InvalidArgumentException
355
     * @throws InvalidDataTypeException
356
     * @throws InvalidInterfaceException
357
     * @throws ReflectionException
358
     */
359
    public static function updateAdminSettings(EE_Registration_Config $EE_Registration_Config)
360
    {
361
        return RecaptchaFactory::getAdminModule()->updateAdminSettings($EE_Registration_Config);
362
    }
363
}
364