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
|
|
|
*/ |
49
|
|
|
public function __construct(array $input_settings = array(), EE_Registration_Config $registration_config = null) |
50
|
|
|
{ |
51
|
|
|
$this->_set_display_strategy(new EE_Invisible_Recaptcha_Display_Strategy()); |
52
|
|
|
parent::__construct($input_settings); |
53
|
|
|
$registration_config = $registration_config instanceof EE_Registration_Config |
54
|
|
|
? $registration_config |
55
|
|
|
: EE_Registry::instance()->CFG->registration; |
56
|
|
|
$this->config = $registration_config; |
57
|
|
|
$this->recaptcha_id = isset($input_settings['recaptcha_id']) |
58
|
|
|
? $input_settings['recaptcha_id'] |
59
|
|
|
: substr(spl_object_hash($this), 8, 8); |
60
|
|
|
$this->submit_button_id = isset($input_settings['submit_button_id']) |
61
|
|
|
? $input_settings['submit_button_id'] |
62
|
|
|
: ''; |
63
|
|
|
$this->registerScripts(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function useCaptcha() |
71
|
|
|
{ |
72
|
|
|
return $this->config->use_captcha && $this->config->recaptcha_theme === 'invisible'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function language() |
80
|
|
|
{ |
81
|
|
|
return $this->config->recaptcha_language; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function siteKey() |
89
|
|
|
{ |
90
|
|
|
return $this->config->recaptcha_publickey; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function secretKey() |
98
|
|
|
{ |
99
|
|
|
return $this->config->recaptcha_privatekey; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function recaptchaId() |
107
|
|
|
{ |
108
|
|
|
return $this->recaptcha_id; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function submitButtonId() |
116
|
|
|
{ |
117
|
|
|
return $this->submit_button_id; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function registerScripts() |
125
|
|
|
{ |
126
|
|
|
if (! $this->useCaptcha()) { |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
add_filter('script_loader_tag', array($this, 'addScriptAttributes'), 10, 2); |
130
|
|
|
wp_register_script( |
131
|
|
|
EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA, |
132
|
|
|
EED_Recaptcha_Invisible::assetsUrl() . 'espresso_invisible_recaptcha.js', |
133
|
|
|
array('espresso_core'), |
134
|
|
|
EVENT_ESPRESSO_VERSION, |
135
|
|
|
true |
136
|
|
|
); |
137
|
|
|
wp_register_script( |
138
|
|
|
EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_GOOGLE_INVISIBLE_RECAPTCHA, |
139
|
|
|
add_query_arg( |
140
|
|
|
array( |
141
|
|
|
'onload' => 'espressoLoadRecaptcha', |
142
|
|
|
'render' => 'explicit', |
143
|
|
|
'hl' => $this->language(), |
144
|
|
|
), |
145
|
|
|
'https://www.google.com/recaptcha/api.js?' |
146
|
|
|
), |
147
|
|
|
array(EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_ESPRESSO_INVISIBLE_RECAPTCHA), |
148
|
|
|
false, |
149
|
|
|
true |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $tag |
156
|
|
|
* @param string $handle |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function addScriptAttributes($tag, $handle) |
160
|
|
|
{ |
161
|
|
|
if ($handle === EE_Invisible_Recaptcha_Input::SCRIPT_HANDLE_GOOGLE_INVISIBLE_RECAPTCHA) { |
162
|
|
|
$tag = str_replace('></script>', ' async="async" defer="defer"></script>', $tag); |
163
|
|
|
} |
164
|
|
|
return $tag; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Gets the HTML for displaying the label for this form input |
170
|
|
|
* according to the form section's layout strategy |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function get_html_for_label() |
175
|
|
|
{ |
176
|
|
|
return ''; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|