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

RecaptchaAdminSettings   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 337
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
dl 0
loc 337
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A adminSettings() 0 4 1
B settingsForm() 0 32 1
B appearanceSettings() 0 122 5
B mainSettings() 0 64 5
C updateAdminSettings() 0 59 10
1
<?php
2
3
namespace EventEspresso\caffeinated\modules\recaptcha_invisible;
4
5
use EE_Admin_Two_Column_Layout;
6
use EE_Checkbox_Multi_Input;
7
use EE_Div_Per_Section_Layout;
8
use EE_Error;
9
use EE_Form_Section_HTML;
10
use EE_Form_Section_Proper;
11
use EE_Radio_Button_Input;
12
use EE_Registration_Config;
13
use EE_Select_Input;
14
use EE_Text_Input;
15
use EE_Yes_No_Input;
16
use EEH_HTML;
17
use EEH_Template;
18
use EventEspresso\core\exceptions\InvalidDataTypeException;
19
use EventEspresso\core\exceptions\InvalidInterfaceException;
20
use InvalidArgumentException;
21
use ReflectionException;
22
23
defined('EVENT_ESPRESSO_VERSION') || exit;
24
25
26
27
/**
28
 * Class RecaptchaAdminSettings
29
 * Generates and processes forms for administrating Event Espresso's Google's reCAPTCHA settings
30
 *
31
 * @package EventEspresso\caffeinated\modules\recaptcha_invisible
32
 * @author  Brent Christensen
33
 * @since   $VID:$
34
 */
35
class RecaptchaAdminSettings
36
{
37
38
    /**
39
     * @var EE_Registration_Config $config
40
     */
41
    private $config;
42
43
44
    /**
45
     * RecaptchaAdminSettings constructor.
46
     *
47
     * @param EE_Registration_Config $registration_config
48
     */
49
    public function __construct(EE_Registration_Config $registration_config)
50
    {
51
        $this->config = $registration_config;
52
    }
53
54
55
    /**
56
     * @throws InvalidArgumentException
57
     * @throws InvalidInterfaceException
58
     * @throws InvalidDataTypeException
59
     * @throws EE_Error
60
     */
61
    public function adminSettings()
62
    {
63
        echo $this->settingsForm()->get_html_and_js();
64
    }
65
66
67
    /**
68
     * @return EE_Form_Section_Proper
69
     * @throws EE_Error
70
     */
71
    protected function settingsForm()
72
    {
73
        return new EE_Form_Section_Proper(
74
            array(
75
                'name'            => 'recaptcha_settings_form',
76
                'html_id'         => 'recaptcha_settings_form',
77
                'layout_strategy' => new EE_Div_Per_Section_Layout(),
78
                'subsections'     => apply_filters(
79
                    'FHEE__EED_Recaptcha___recaptcha_settings_form__form_subsections',
80
                    array(
81
                        'main_settings_hdr'       => new EE_Form_Section_HTML(
82
                            EEH_HTML::h2(
83
                                esc_html__('reCAPTCHA Anti-spam Settings', 'event_espresso')
84
                                . EEH_Template::get_help_tab_link('recaptcha_info')
85
                            )
86
                        ),
87
                        'main_settings'           => $this->mainSettings(),
88
                        'appearance_settings_hdr' => new EE_Form_Section_HTML(
89
                            EEH_HTML::h2(esc_html__('reCAPTCHA Appearance', 'event_espresso'))
90
                        ),
91
                        'appearance_settings'     => $this->appearanceSettings(),
92
                        'required_fields_note'    => new EE_Form_Section_HTML(
93
                            EEH_HTML::p(
94
                                esc_html__('All fields marked with a * are required fields', 'event_espresso'),
95
                                '', 'grey-text'
96
                            )
97
                        ),
98
                    )
99
                ),
100
            )
101
        );
102
    }
103
104
105
    /**
106
     * @return EE_Form_Section_Proper
107
     * @throws EE_Error
108
     */
109
    protected function mainSettings()
110
    {
111
        return new EE_Form_Section_Proper(
112
            array(
113
                'name'            => 'recaptcha_settings_tbl',
114
                'html_id'         => 'recaptcha_settings_tbl',
115
                'html_class'      => 'form-table',
116
                'layout_strategy' => new EE_Admin_Two_Column_Layout(),
117
                'subsections'     => apply_filters(
118
                    'FHEE__EED_Recaptcha___recaptcha_main_settings__form_subsections',
119
                    array(
120
                        'use_captcha'          => new EE_Yes_No_Input(
121
                            array(
122
                                'html_label_text'         => esc_html__('Use reCAPTCHA', 'event_espresso'),
123
                                'html_help_text'          => sprintf(
124
                                    esc_html__('reCAPTCHA is a free service that  protects your website from spam and abuse. It employs advanced risk analysis technology to separate humans from abusive actors. Sign up %1$shere%2$s to receive your Public and Private keys.',
125
                                        'event_espresso'),
126
                                    '<a href="https://www.google.com/recaptcha/intro/index.html">',
127
                                    '</a>'
128
                                ),
129
                                'default'                 => $this->config->use_captcha !== null
130
                                    ? $this->config->use_captcha : false,
131
                                'display_html_label_text' => false,
132
                            )
133
                        ),
134
                        'recaptcha_publickey'  => new EE_Text_Input(
135
                            array(
136
                                'html_label_text' => esc_html__('Site Key', 'event_espresso'),
137
                                'html_help_text'  => esc_html__('The site key is used to display the widget on your site.',
138
                                    'event_espresso'),
139
                                'default'         => $this->config->recaptcha_publickey !== null
140
                                    ? stripslashes($this->config->recaptcha_publickey) : '',
141
                            )
142
                        ),
143
                        'recaptcha_privatekey' => new EE_Text_Input(
144
                            array(
145
                                'html_label_text' => esc_html__('Secret Key', 'event_espresso'),
146
                                'html_help_text'  => esc_html__('The secret key authorizes communication between your application backend and the reCAPTCHA server to verify the user\'s response. The secret key needs to be kept safe for security purposes.',
147
                                    'event_espresso'),
148
                                'default'         => $this->config->recaptcha_privatekey !== null
149
                                    ? stripslashes($this->config->recaptcha_privatekey)
150
                                    : '',
151
                            )
152
                        ),
153
                        'recaptcha_protected_forms' => new EE_Checkbox_Multi_Input(
154
                            array(
155
                                'ticket_selector'   => esc_html__('Ticket Selector', 'event_espresso'),
156
                                'registration_form' => esc_html__('Registration Form', 'event_espresso'),
157
                            ),
158
                            array(
159
                                'html_label_text'         => esc_html__('Invisible reCAPTCHA Protection', 'event_espresso'),
160
                                'html_help_text'          => esc_html__('Select which Event Espresso forms you would like to enable Invisible reCAPTCHA on.',
161
                                    'event_espresso'),
162
                                'default'                 => is_array($this->config->recaptcha_protected_forms)
163
                                    ? $this->config->recaptcha_protected_forms
164
                                    : array(),
165
                                'display_html_label_text' => false,
166
                            )
167
                        ),
168
                    )
169
                ),
170
            )
171
        );
172
    }
173
174
175
    /**
176
     * @return EE_Form_Section_Proper
177
     * @throws EE_Error
178
     */
179
    protected function appearanceSettings()
180
    {
181
        return new EE_Form_Section_Proper(
182
            array(
183
                'name'            => 'recaptcha_appearance_settings_tbl',
184
                'html_id'         => 'recaptcha_appearance_settings_tbl',
185
                'html_class'      => 'form-table',
186
                'layout_strategy' => new EE_Admin_Two_Column_Layout(),
187
                'subsections'     => apply_filters(
188
                    'FHEE__EED_Recaptcha___recaptcha_appearance_settings__form_subsections',
189
                    array(
190
                        'recaptcha_theme'    => new EE_Radio_Button_Input(
191
                            array(
192
                                'invisible' => esc_html__('Invisible', 'event_espresso'),
193
                                'light'     => esc_html__('Light', 'event_espresso'),
194
                                'dark'      => esc_html__('Dark', 'event_espresso'),
195
                            ),
196
                            array(
197
                                'html_label_text'         => esc_html__('Theme', 'event_espresso'),
198
                                'html_help_text'          => esc_html__('The color theme of the widget.',
199
                                    'event_espresso'),
200
                                'default'                 => $this->config->recaptcha_theme !== null
201
                                    ? $this->config->recaptcha_theme
202
                                    : 'invisible',
203
                                'display_html_label_text' => false,
204
                            )
205
                        ),
206
                        'recaptcha_badge'     => new EE_Radio_Button_Input(
207
                            array(
208
                                'bottomleft' => esc_html__('Bottom Left', 'event_espresso'),
209
                                'bottomright' => esc_html__('Bottom Right', 'event_espresso'),
210
                                'inline' => esc_html__('Inline', 'event_espresso'),
211
                            ),
212
                            array(
213
                                'html_label_text'         => esc_html__('Invisible reCAPTCHA Badge Position', 'event_espresso'),
214
                                'html_help_text'          => esc_html__(
215
                                    'If using Invisible reCAPTCHA, then this determines the position of the reCAPTCHA badge. "Bottom Left" and "Bottom Right" both will float at the bottom of the screen. "Inline" appears beside the submit button but allows you to control the CSS.',
216
                                    'event_espresso'
217
                                ),
218
                                'default'                 => $this->config->recaptcha_badge !== null
219
                                    ? $this->config->recaptcha_badge
220
                                    : 'bottomleft',
221
                                'display_html_label_text' => false,
222
                            )
223
                        ),
224
                        'recaptcha_type'     => new EE_Radio_Button_Input(
225
                            array(
226
                                'image' => esc_html__('Image', 'event_espresso'),
227
                                'audio' => esc_html__('Audio', 'event_espresso'),
228
                            ),
229
                            array(
230
                                'html_label_text'         => esc_html__('Type', 'event_espresso'),
231
                                'html_help_text'          => esc_html__('The type of CAPTCHA to serve.',
232
                                    'event_espresso'),
233
                                'default'                 => $this->config->recaptcha_type !== null
234
                                    ? $this->config->recaptcha_type
235
                                    : 'image',
236
                                'display_html_label_text' => false,
237
                            )
238
                        ),
239
                        'recaptcha_language' => new EE_Select_Input(
240
                            array(
241
                                'ar'     => esc_html__('Arabic', 'event_espresso'),
242
                                'bg'     => esc_html__('Bulgarian', 'event_espresso'),
243
                                'ca'     => esc_html__('Catalan', 'event_espresso'),
244
                                'zh-CN'  => esc_html__('Chinese (Simplified)', 'event_espresso'),
245
                                'zh-TW'  => esc_html__('Chinese (Traditional)	', 'event_espresso'),
246
                                'hr'     => esc_html__('Croatian', 'event_espresso'),
247
                                'cs'     => esc_html__('Czech', 'event_espresso'),
248
                                'da'     => esc_html__('Danish', 'event_espresso'),
249
                                'nl'     => esc_html__('Dutch', 'event_espresso'),
250
                                'en-GB'  => esc_html__('English (UK)', 'event_espresso'),
251
                                'en'     => esc_html__('English (US)', 'event_espresso'),
252
                                'fil'    => esc_html__('Filipino', 'event_espresso'),
253
                                'fi'     => esc_html__('Finnish', 'event_espresso'),
254
                                'fr'     => esc_html__('French', 'event_espresso'),
255
                                'fr-CA'  => esc_html__('French (Canadian)', 'event_espresso'),
256
                                'de'     => esc_html__('German', 'event_espresso'),
257
                                'de-AT'  => esc_html__('German (Austria)', 'event_espresso'),
258
                                'de-CH'  => esc_html__('German (Switzerland)', 'event_espresso'),
259
                                'el'     => esc_html__('Greek', 'event_espresso'),
260
                                'iw'     => esc_html__('Hebrew', 'event_espresso'),
261
                                'hi'     => esc_html__('Hindi', 'event_espresso'),
262
                                'hu'     => esc_html__('Hungarian', 'event_espresso'),
263
                                'id'     => esc_html__('Indonesian', 'event_espresso'),
264
                                'it'     => esc_html__('Italian', 'event_espresso'),
265
                                'ja'     => esc_html__('Japanese', 'event_espresso'),
266
                                'ko'     => esc_html__('Korean', 'event_espresso'),
267
                                'lv'     => esc_html__('Latvian', 'event_espresso'),
268
                                'lt'     => esc_html__('Lithuanian', 'event_espresso'),
269
                                'no'     => esc_html__('Norwegian', 'event_espresso'),
270
                                'fa'     => esc_html__('Persian', 'event_espresso'),
271
                                'pl'     => esc_html__('Polish', 'event_espresso'),
272
                                'pt'     => esc_html__('Portuguese', 'event_espresso'),
273
                                'pt-BR'  => esc_html__('Portuguese (Brazil)', 'event_espresso'),
274
                                'pt-PT'  => esc_html__('Portuguese (Portugal)', 'event_espresso'),
275
                                'ro'     => esc_html__('Romanian', 'event_espresso'),
276
                                'ru'     => esc_html__('Russian', 'event_espresso'),
277
                                'sr'     => esc_html__('Serbian', 'event_espresso'),
278
                                'sk'     => esc_html__('Slovak', 'event_espresso'),
279
                                'sl'     => esc_html__('Slovenian', 'event_espresso'),
280
                                'es'     => esc_html__('Spanish', 'event_espresso'),
281
                                'es-419' => esc_html__('Spanish (Latin America)', 'event_espresso'),
282
                                'sv'     => esc_html__('Swedish', 'event_espresso'),
283
                                'th'     => esc_html__('Thai', 'event_espresso'),
284
                                'tr'     => esc_html__('Turkish', 'event_espresso'),
285
                                'uk'     => esc_html__('Ukrainian', 'event_espresso'),
286
                                'vi'     => esc_html__('Vietnamese', 'event_espresso'),
287
                            ),
288
                            array(
289
                                'html_label_text' => esc_html__('Language', 'event_espresso'),
290
                                'html_help_text'  => esc_html__('Forces the widget to render in a specific language.',
291
                                    'event_espresso'),
292
                                'default'         => $this->config->recaptcha_language !== null
293
                                    ? $this->config->recaptcha_language : 'en',
294
                            )
295
                        ),
296
                    )
297
                ),
298
            )
299
        );
300
    }
301
302
303
    /**
304
     * @param EE_Registration_Config $EE_Registration_Config
305
     * @return EE_Registration_Config
306
     * @throws InvalidArgumentException
307
     * @throws InvalidInterfaceException
308
     * @throws InvalidDataTypeException
309
     * @throws EE_Error
310
     * @throws ReflectionException
311
     */
312
    public function updateAdminSettings(EE_Registration_Config $EE_Registration_Config)
313
    {
314
        try {
315
            $recaptcha_settings_form = $this->settingsForm();
316
            // if not displaying a form, then check for form submission
317
            if ($recaptcha_settings_form->was_submitted()) {
318
                // capture form data
319
                $recaptcha_settings_form->receive_form_submission();
320
                // validate form data
321
                if ($recaptcha_settings_form->is_valid()) {
322
                    // grab validated data from form
323
                    $valid_data = $recaptcha_settings_form->valid_data();
324
                    // user proofing recaptcha:  If Use reCAPTCHA is set to yes but we dont' have site or secret keys then set Use reCAPTCHA to FALSE and give error message.
325
                    if (
326
                        $valid_data['main_settings']['use_captcha']
327
                        && (
328
                            ! $EE_Registration_Config->use_captcha
329
                            && (
330
                                empty($valid_data['main_settings']['recaptcha_publickey'])
331
                                || empty($valid_data['main_settings']['recaptcha_privatekey'])
332
                            )
333
                        )
334
                        && apply_filters(
335
                            'FHEE__Extend_Registration_Form_Admin_Page__check_for_recaptcha_keys',
336
                            true,
337
                            $EE_Registration_Config
338
                        )
339
                    ) {
340
                        $valid_data['main_settings']['use_captcha'] = false;
341
                        EE_Error::add_error(
342
                            esc_html__(
343
                                'The use reCAPTCHA setting has been reset to "no". In order to enable the reCAPTCHA service, you must enter a Site Key and Secret Key.',
344
                                'event_espresso'
345
                            ),
346
                            __FILE__, __FUNCTION__, __LINE__
347
                        );
348
                    }
349
                    $EE_Registration_Config->use_captcha          = $valid_data['main_settings']['use_captcha'];
350
                    $EE_Registration_Config->recaptcha_publickey  = $valid_data['main_settings']['recaptcha_publickey'];
351
                    $EE_Registration_Config->recaptcha_protected_forms = $valid_data['main_settings']['recaptcha_protected_forms'];
352
                    $EE_Registration_Config->recaptcha_privatekey = $valid_data['main_settings']['recaptcha_privatekey'];
353
                    $EE_Registration_Config->recaptcha_type       = $valid_data['appearance_settings']['recaptcha_type'];
354
                    $EE_Registration_Config->recaptcha_theme      = $valid_data['appearance_settings']['recaptcha_theme'];
355
                    $EE_Registration_Config->recaptcha_badge      = $valid_data['appearance_settings']['recaptcha_badge'];
356
                    $EE_Registration_Config->recaptcha_language   = $valid_data['appearance_settings']['recaptcha_language'];
357
                } else {
358
                    if ($recaptcha_settings_form->submission_error_message() !== '') {
359
                        EE_Error::add_error(
360
                            $recaptcha_settings_form->submission_error_message(),
361
                            __FILE__, __FUNCTION__, __LINE__
362
                        );
363
                    }
364
                }
365
            }
366
        } catch (EE_Error $e) {
367
            $e->get_error();
368
        }
369
        return $EE_Registration_Config;
370
    }
371
}
372