Completed
Branch FET/9575/invisible-recaptcha (a1de93)
by
unknown
80:39 queued 68:16
created

EED_Recaptcha_Invisible   B

Complexity

Total Complexity 32

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
dl 0
loc 305
rs 8.123
c 0
b 0
f 0
wmc 32
lcom 1
cbo 16

17 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 4 1
B set_hooks() 0 26 2
A set_hooks_admin() 0 12 1
A setProperties() 0 4 1
A useInvisibleRecaptcha() 0 5 2
A assetsUrl() 0 4 1
A run() 0 3 1
A recaptchaPassed() 0 14 3
A verifyToken() 0 9 2
A spcoRegStepForm() 0 12 3
A processSpcoRegStepForm() 0 4 2
A receiveSpcoRegStepForm() 0 19 4
A ticketSelectorForm() 0 11 2
A processTicketSelectorForm() 0 16 4
A setSessionData() 0 6 1
A adminSettings() 0 4 1
A updateAdminSettings() 0 4 1
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
    /**
32
     * @return EED_Module|EED_Recaptcha
33
     */
34
    public static function instance()
35
    {
36
        return parent::get_instance(__CLASS__);
37
    }
38
39
40
    /**
41
     * @return void
42
     * @throws InvalidInterfaceException
43
     * @throws InvalidDataTypeException
44
     * @throws InvalidArgumentException
45
     */
46
    public static function set_hooks()
47
    {
48
        EED_Recaptcha_Invisible::setProperties();
49
        if (EED_Recaptcha_Invisible::useInvisibleRecaptcha()) {
50
            // ticket selection
51
            add_filter(
52
                'FHEE__EE_Ticket_Selector__after_ticket_selector_submit',
53
                array('EED_Recaptcha_Invisible', 'ticketSelectorForm'),
54
                10, 2
55
            );
56
            add_action(
57
                'EED_Ticket_Selector__process_ticket_selections__before',
58
                array('EED_Recaptcha_Invisible', 'processTicketSelectorForm')
59
            );
60
            // checkout
61
            add_action(
62
                'AHEE__EE_SPCO_Reg_Step__display_reg_form__reg_form',
63
                array('EED_Recaptcha_Invisible', 'spcoRegStepForm')
64
            );
65
            add_filter(
66
                'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data',
67
                array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'),
68
                10, 2
69
            );
70
        }
71
    }
72
73
74
    /**
75
     * @return void
76
     * @throws InvalidInterfaceException
77
     * @throws InvalidDataTypeException
78
     * @throws InvalidArgumentException
79
     */
80
    public static function set_hooks_admin()
81
    {
82
        // admin settings
83
        add_action(
84
            'AHEE__Extend_Registration_Form_Admin_Page___reg_form_settings_template',
85
            array('EED_Recaptcha_Invisible', 'adminSettings')
86
        );
87
        add_filter(
88
            'FHEE__Extend_Registration_Form_Admin_Page___update_reg_form_settings__CFG_registration',
89
            array('EED_Recaptcha_Invisible', 'updateAdminSettings')
90
        );
91
    }
92
93
94
    /**
95
     * @return void
96
     * @throws InvalidInterfaceException
97
     * @throws InvalidDataTypeException
98
     * @throws InvalidArgumentException
99
     */
100
    public static function setProperties()
101
    {
102
        EED_Recaptcha_Invisible::$config = EE_Registry::instance()->CFG->registration;
103
    }
104
105
106
    /**
107
     * @return boolean
108
     */
109
    public static function useInvisibleRecaptcha()
110
    {
111
        return EED_Recaptcha_Invisible::$config->use_captcha
112
               && EED_Recaptcha_Invisible::$config->recaptcha_theme === 'invisible';
113
    }
114
115
116
    /**
117
     * @return string
118
     */
119
    public static function assetsUrl()
120
    {
121
        return plugin_dir_url(__FILE__) . 'assets' . DS;
122
    }
123
124
125
    /**
126
     * @param \WP $WP
127
     */
128
    public function run($WP)
129
    {
130
    }
131
132
133
    /**
134
     * @return boolean
135
     * @throws InvalidInterfaceException
136
     * @throws InvalidDataTypeException
137
     * @throws InvalidArgumentException
138
     */
139
    public static function recaptchaPassed()
140
    {
141
        // logged in means you have already passed a turing test of sorts
142
        if (EED_Recaptcha_Invisible::useInvisibleRecaptcha() === false || is_user_logged_in()) {
143
            return true;
144
        }
145
        // was test already passed?
146
        return filter_var(
147
            EE_Registry::instance()->SSN->get_session_data(
148
                InvisibleRecaptcha::SESSION_DATA_KEY_RECAPTCHA_PASSED
149
            ),
150
            FILTER_VALIDATE_BOOLEAN
151
        );
152
    }
153
154
155
    /**
156
     * @param EE_Request $request
157
     * @return bool
158
     * @throws InvalidArgumentException
159
     * @throws InvalidDataTypeException
160
     * @throws InvalidInterfaceException
161
     * @throws RuntimeException
162
     */
163
    public static function verifyToken(EE_Request $request)
164
    {
165
        $invisible_recaptcha = RecaptchaFactory::create();
166
        if ($invisible_recaptcha->verifyToken($request)) {
167
            add_action('shutdown', array('EED_Recaptcha_Invisible', 'setSessionData'));
168
            return true;
169
        }
170
        return false;
171
    }
172
173
174
    /**
175
     * @param EE_Form_Section_Proper $reg_form
176
     * @return void
177
     * @throws EE_Error
178
     * @throws InvalidArgumentException
179
     * @throws InvalidDataTypeException
180
     * @throws InvalidInterfaceException
181
     */
182
    public static function spcoRegStepForm(EE_Form_Section_Proper $reg_form)
183
    {
184
        // do nothing if form isn't for a reg step or test has already been passed
185
        if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) {
186
            return;
187
        }
188
        $default_hidden_inputs = $reg_form->get_subsection('default_hidden_inputs');
189
        if ($default_hidden_inputs instanceof EE_Form_Section_Proper) {
190
            $invisible_recaptcha = RecaptchaFactory::create();
191
            $invisible_recaptcha->addToFormSection($default_hidden_inputs);
192
        }
193
    }
194
195
196
    /**
197
     * @param EE_Form_Section_Proper $reg_form
198
     * @return bool
199
     * @throws InvalidDataTypeException
200
     * @throws InvalidInterfaceException
201
     * @throws EE_Error
202
     * @throws InvalidArgumentException
203
     */
204
    public static function processSpcoRegStepForm(EE_Form_Section_Proper $reg_form)
205
    {
206
        return strpos($reg_form->name(), 'reg-step-form') === false || EED_Recaptcha_Invisible::recaptchaPassed();
207
    }
208
209
210
    /**
211
     * @param array|null             $req_data
212
     * @param EE_Form_Section_Proper $reg_form
213
     * @return array
214
     * @throws EE_Error
215
     * @throws InvalidArgumentException
216
     * @throws InvalidDataTypeException
217
     * @throws InvalidInterfaceException
218
     * @throws RuntimeException
219
     */
220
    public static function receiveSpcoRegStepForm($req_data = array(), EE_Form_Section_Proper $reg_form)
221
    {
222
        // do nothing if form isn't for a reg step or test has already been passed
223
        if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) {
224
            return $req_data;
225
        }
226
        /** @var EE_Request $request */
227
        $request = LoaderFactory::getLoader()->getShared('EE_Request');
228
        if (! EED_Recaptcha_Invisible::verifyToken($request)) {
229
            if ($request->isAjax()) {
230
                $json_response = new EE_SPCO_JSON_Response();
231
                $json_response->echoAndExit();
232
            }
233
            EEH_URL::safeRedirectAndExit(
234
                EE_Registry::instance()->CFG->core->reg_page_url()
235
            );
236
        }
237
        return $req_data;
238
    }
239
240
241
    /**
242
     * @param string   $html
243
     * @param EE_Event $event
244
     * @return string
245
     * @throws InvalidArgumentException
246
     * @throws InvalidDataTypeException
247
     * @throws InvalidInterfaceException
248
     * @throws EE_Error
249
     */
250
    public static function ticketSelectorForm($html = '', EE_Event $event)
251
    {
252
        // do nothing if test has  already  been passed
253
        if (EED_Recaptcha_Invisible::recaptchaPassed()) {
254
            return $html;
255
        }
256
        $html .= RecaptchaFactory::create()->getInputHtml(
257
            array('recaptcha_id' => $event->ID())
258
        );
259
        return $html;
260
    }
261
262
263
    /**
264
     * @return void
265
     * @throws InvalidArgumentException
266
     * @throws InvalidInterfaceException
267
     * @throws InvalidDataTypeException
268
     * @throws RuntimeException
269
     */
270
    public static function processTicketSelectorForm()
271
    {
272
        // do nothing if test has  already  been passed
273
        if (EED_Recaptcha_Invisible::recaptchaPassed()) {
274
            return;
275
        }
276
        /** @var EE_Request $request */
277
        $request = LoaderFactory::getLoader()->getShared('EE_Request');
278
        if (! EED_Recaptcha_Invisible::verifyToken($request)) {
279
            $event_id   = $request->get('tkt-slctr-event-id');
280
            $return_url = $request->is_set("tkt-slctr-return-url-{$event_id}")
281
                ? $request->get("tkt-slctr-return-url-{$event_id}")
282
                : get_permalink($event_id);
283
            EEH_URL::safeRedirectAndExit($return_url);
284
        }
285
    }
286
287
288
    /**
289
     * @throws InvalidArgumentException
290
     * @throws InvalidDataTypeException
291
     * @throws InvalidInterfaceException
292
     */
293
    public static function setSessionData()
294
    {
295
        EE_Registry::instance()->SSN->set_session_data(
296
            array(InvisibleRecaptcha::SESSION_DATA_KEY_RECAPTCHA_PASSED => true)
297
        );
298
    }
299
300
301
    /**
302
     * @throws EE_Error
303
     * @throws InvalidArgumentException
304
     * @throws InvalidDataTypeException
305
     * @throws InvalidInterfaceException
306
     */
307
    public static function adminSettings()
308
    {
309
        RecaptchaFactory::getAdminModule()->adminSettings();
310
    }
311
312
313
    /**
314
     * @param EE_Registration_Config $EE_Registration_Config
315
     * @return EE_Registration_Config
316
     * @throws EE_Error
317
     * @throws InvalidArgumentException
318
     * @throws InvalidDataTypeException
319
     * @throws InvalidInterfaceException
320
     * @throws ReflectionException
321
     */
322
    public static function updateAdminSettings(EE_Registration_Config $EE_Registration_Config)
323
    {
324
        return RecaptchaFactory::getAdminModule()->updateAdminSettings($EE_Registration_Config);
325
    }
326
}
327