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

EED_Recaptcha_Invisible::updateAdminSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\caffeinated\modules\recaptcha_invisible\RecaptchaFactory;
4
use EventEspresso\core\exceptions\InvalidDataTypeException;
5
use EventEspresso\core\exceptions\InvalidInterfaceException;
6
use EventEspresso\core\services\loaders\LoaderFactory;
7
8
defined('EVENT_ESPRESSO_VERSION') || exit('NO direct script access allowed');
9
10
11
12
/**
13
 * EED_Recaptcha_Invisible
14
 * Integrates Google's Invisible reCAPTCHA into the form submission process
15
 * for both the Ticket Selector and Single Page Checkout
16
 *
17
 * @package        Event Espresso
18
 * @subpackage     /modules/recaptcha_invisible/
19
 * @author         Brent Christensen
20
 */
21
class EED_Recaptcha_Invisible extends EED_Module
22
{
23
24
    /**
25
     * @var EE_Registration_Config $config
26
     */
27
    private static $config;
28
29
30
    /**
31
     * @return EED_Module|EED_Recaptcha
32
     */
33
    public static function instance()
34
    {
35
        return parent::get_instance(__CLASS__);
36
    }
37
38
39
    /**
40
     * @return void
41
     * @throws InvalidInterfaceException
42
     * @throws InvalidDataTypeException
43
     * @throws InvalidArgumentException
44
     */
45
    public static function set_hooks()
46
    {
47
        EED_Recaptcha_Invisible::setProperties();
48
        if (EED_Recaptcha_Invisible::useInvisibleRecaptcha()) {
49
            if(EED_Recaptcha_Invisible::protectForm('ticket_selector')){
50
                // ticket selection
51
                add_filter(
52
                    'FHEE__EE_Ticket_Selector__after_ticket_selector_submit',
53
                    array('EED_Recaptcha_Invisible', 'ticketSelectorForm'),
54
                    10, 3
55
                );
56
                add_action(
57
                    'EED_Ticket_Selector__process_ticket_selections__before',
58
                    array('EED_Recaptcha_Invisible', 'processTicketSelectorForm')
59
                );
60
            }
61
            if (EED_Recaptcha_Invisible::protectForm('registration_form')) {
62
                // checkout
63
                add_action(
64
                    'AHEE__EE_SPCO_Reg_Step__display_reg_form__reg_form',
65
                    array('EED_Recaptcha_Invisible', 'spcoRegStepForm')
66
                );
67
                add_filter(
68
                    'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data',
69
                    array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'),
70
                    10, 2
71
                );
72
            }
73
            add_action('loop_end', array('EED_Recaptcha_Invisible', 'localizeScriptVars'));
74
        }
75
    }
76
77
78
    /**
79
     * @return void
80
     * @throws InvalidInterfaceException
81
     * @throws InvalidDataTypeException
82
     * @throws InvalidArgumentException
83
     */
84
    public static function set_hooks_admin()
85
    {
86
        EED_Recaptcha_Invisible::setProperties();
87
        if (EED_Recaptcha_Invisible::protectForm('ticket_selector')) {
88
            add_action(
89
                'EED_Ticket_Selector__process_ticket_selections__before',
90
                array('EED_Recaptcha_Invisible', 'processTicketSelectorForm')
91
            );
92
        }
93
        if (EED_Recaptcha_Invisible::protectForm('registration_form')) {
94
            add_filter(
95
                'FHEE__EE_Form_Section_Proper__receive_form_submission__req_data',
96
                array('EED_Recaptcha_Invisible', 'receiveSpcoRegStepForm'),
97
                10, 2
98
            );
99
        }
100
        // admin settings
101
        add_action(
102
            'AHEE__Extend_Registration_Form_Admin_Page___reg_form_settings_template',
103
            array('EED_Recaptcha_Invisible', 'adminSettings')
104
        );
105
        add_filter(
106
            'FHEE__Extend_Registration_Form_Admin_Page___update_reg_form_settings__CFG_registration',
107
            array('EED_Recaptcha_Invisible', 'updateAdminSettings')
108
        );
109
    }
110
111
112
    /**
113
     * @return void
114
     * @throws InvalidInterfaceException
115
     * @throws InvalidDataTypeException
116
     * @throws InvalidArgumentException
117
     */
118
    public static function setProperties()
119
    {
120
121
        EED_Recaptcha_Invisible::$config = EE_Registry::instance()->CFG->registration;
122
    }
123
124
125
126
    /**
127
     * @return boolean
128
     */
129
    public static function useInvisibleRecaptcha()
130
    {
131
        return EED_Recaptcha_Invisible::$config->use_captcha
132
               && EED_Recaptcha_Invisible::$config->recaptcha_theme === 'invisible';
133
    }
134
135
136
    /**
137
     * @param string $form
138
     * @return boolean
139
     */
140
    public static function protectForm($form)
141
    {
142
        return is_array(EED_Recaptcha_Invisible::$config->recaptcha_protected_forms)
143
            && in_array($form, EED_Recaptcha_Invisible::$config->recaptcha_protected_forms, true);
144
    }
145
146
147
148
149
    /**
150
     * @return void
151
     * @throws InvalidInterfaceException
152
     * @throws InvalidDataTypeException
153
     * @throws InvalidArgumentException
154
     */
155
    public static function localizeScriptVars()
156
    {
157
        wp_localize_script(
158
            EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA,
159
            'eeRecaptcha',
160
            RecaptchaFactory::create()->getLocalizedVars()
161
        );
162
    }
163
164
165
    /**
166
     * @return string
167
     */
168
    public static function assetsUrl()
169
    {
170
        return plugin_dir_url(__FILE__) . 'assets' . DS;
171
    }
172
173
174
    /**
175
     * @param \WP $WP
176
     */
177
    public function run($WP)
178
    {
179
    }
180
181
182
183
    /**
184
     * @param EE_Request $request
185
     * @return bool
186
     * @throws InvalidArgumentException
187
     * @throws InvalidDataTypeException
188
     * @throws InvalidInterfaceException
189
     * @throws RuntimeException
190
     */
191
    public static function verifyToken(EE_Request $request)
192
    {
193
        return RecaptchaFactory::create()->verifyToken($request);
194
    }
195
196
197
    /**
198
     * @param EE_Form_Section_Proper $reg_form
199
     * @return void
200
     * @throws EE_Error
201
     * @throws InvalidArgumentException
202
     * @throws InvalidDataTypeException
203
     * @throws InvalidInterfaceException
204
     * @throws DomainException
205
     */
206
    public static function spcoRegStepForm(EE_Form_Section_Proper $reg_form)
207
    {
208
        // do nothing if form isn't for a reg step or test has already been passed
209
        if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) {
210
            return;
211
        }
212
        $default_hidden_inputs = $reg_form->get_subsection('default_hidden_inputs');
213
        if ($default_hidden_inputs instanceof EE_Form_Section_Proper) {
214
            $invisible_recaptcha = RecaptchaFactory::create();
215
            $invisible_recaptcha->addToFormSection($default_hidden_inputs);
216
        }
217
    }
218
219
220
    /**
221
     * @param EE_Form_Section_Proper $reg_form
222
     * @return bool
223
     * @throws InvalidDataTypeException
224
     * @throws InvalidInterfaceException
225
     * @throws EE_Error
226
     * @throws InvalidArgumentException
227
     */
228
    public static function processSpcoRegStepForm(EE_Form_Section_Proper $reg_form)
229
    {
230
        return strpos($reg_form->name(), 'reg-step-form') !== false
231
               && ! RecaptchaFactory::create()->recaptchaPassed();
232
    }
233
234
235
    /**
236
     * @param array|null             $req_data
237
     * @param EE_Form_Section_Proper $reg_form
238
     * @return array
239
     * @throws EE_Error
240
     * @throws InvalidArgumentException
241
     * @throws InvalidDataTypeException
242
     * @throws InvalidInterfaceException
243
     * @throws RuntimeException
244
     */
245
    public static function receiveSpcoRegStepForm($req_data = array(), EE_Form_Section_Proper $reg_form)
246
    {
247
        // do nothing if form isn't for a reg step or test has already been passed
248
        if (! EED_Recaptcha_Invisible::processSpcoRegStepForm($reg_form)) {
249
            return $req_data;
250
        }
251
        /** @var EE_Request $request */
252
        $request = LoaderFactory::getLoader()->getShared('EE_Request');
253
        if (! EED_Recaptcha_Invisible::verifyToken($request)) {
254
            if ($request->isAjax()) {
0 ignored issues
show
Deprecated Code introduced by
The method EE_Request::isAjax() has been deprecated with message: 4.9.53

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
255
                $json_response = new EE_SPCO_JSON_Response();
256
                $json_response->echoAndExit();
257
            }
258
            EEH_URL::safeRedirectAndExit(
259
                EE_Registry::instance()->CFG->core->reg_page_url()
260
            );
261
        }
262
        return $req_data;
263
    }
264
265
266
    /**
267
     * @param string   $html
268
     * @param EE_Event $event
269
     * @param bool     $iframe
270
     * @return string
271
     * @throws EE_Error
272
     * @throws InvalidArgumentException
273
     * @throws InvalidDataTypeException
274
     * @throws InvalidInterfaceException
275
     * @throws ReflectionException
276
     * @throws DomainException
277
     */
278
    public static function ticketSelectorForm($html = '', EE_Event $event, $iframe = false)
279
    {
280
        $recaptcha = RecaptchaFactory::create();
281
        // do nothing if test has  already  been passed
282
        if ($recaptcha->recaptchaPassed()) {
283
            return $html;
284
        }
285
        $html .= $recaptcha->getInputHtml(
286
            array(
287
                'recaptcha_id'   => $event->ID(),
288
                'iframe'         => $iframe,
289
                'localized_vars' => $recaptcha->getLocalizedVars(),
290
            )
291
        );
292
        return $html;
293
    }
294
295
296
    /**
297
     * @return void
298
     * @throws InvalidArgumentException
299
     * @throws InvalidInterfaceException
300
     * @throws InvalidDataTypeException
301
     * @throws RuntimeException
302
     */
303
    public static function processTicketSelectorForm()
304
    {
305
        // do nothing if test has  already  been passed
306
        if (RecaptchaFactory::create()->recaptchaPassed()) {
307
            return;
308
        }
309
        /** @var EE_Request $request */
310
        $request = LoaderFactory::getLoader()->getShared('EE_Request');
311
        if (! EED_Recaptcha_Invisible::verifyToken($request)) {
312
            $event_id   = $request->get('tkt-slctr-event-id');
0 ignored issues
show
Deprecated Code introduced by
The method EE_Request::get() has been deprecated with message: 4.9.53

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
313
            $return_url = $request->is_set("tkt-slctr-return-url-{$event_id}")
0 ignored issues
show
Deprecated Code introduced by
The method EE_Request::is_set() has been deprecated with message: 4.9.53

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
314
                ? $request->get("tkt-slctr-return-url-{$event_id}")
0 ignored issues
show
Deprecated Code introduced by
The method EE_Request::get() has been deprecated with message: 4.9.53

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
315
                : get_permalink($event_id);
316
            EEH_URL::safeRedirectAndExit($return_url);
317
        }
318
    }
319
320
321
    /**
322
     * @throws EE_Error
323
     * @throws InvalidArgumentException
324
     * @throws InvalidDataTypeException
325
     * @throws InvalidInterfaceException
326
     */
327
    public static function adminSettings()
328
    {
329
        RecaptchaFactory::getAdminModule()->adminSettings();
330
    }
331
332
333
    /**
334
     * @param EE_Registration_Config $EE_Registration_Config
335
     * @return EE_Registration_Config
336
     * @throws EE_Error
337
     * @throws InvalidArgumentException
338
     * @throws InvalidDataTypeException
339
     * @throws InvalidInterfaceException
340
     * @throws ReflectionException
341
     */
342
    public static function updateAdminSettings(EE_Registration_Config $EE_Registration_Config)
343
    {
344
        return RecaptchaFactory::getAdminModule()->updateAdminSettings($EE_Registration_Config);
345
    }
346
}
347