Skip to content

Commit 911ee0b

Browse files
deal with Prosopo connection errors gracefully
1 parent a83a5d9 commit 911ee0b

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

integrations/prosopo-procaptcha/class-procaptcha.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ private function __construct()
6464
protected function read_settings()
6565
{
6666
$integrations = get_option('mc4wp_integrations', []);
67-
if (
68-
false === is_array($integrations) ||
67+
if (false === is_array($integrations) ||
6968
false === key_exists('prosopo-procaptcha', $integrations) ||
7069
false === is_array($integrations['prosopo-procaptcha'])
7170
) {
@@ -212,20 +211,29 @@ protected function is_human_made_request()
212211
/** @var MC4WP_Debug_Log */
213212
$logger = mc4wp('log');
214213
$logger->error(sprintf('ProCaptcha request error: %d %s - %s', wp_remote_retrieve_response_code($response), wp_remote_retrieve_response_message($response), wp_remote_retrieve_body($response)));
215-
return false;
214+
215+
// the check failed, but we don't want to break the form in case of Prosopo having server issues
216+
// so we write to log and act as if this user is human...
217+
return true;
216218
}
217219

218220
$body = wp_remote_retrieve_body($response);
219-
$body = json_decode($body, true);
220-
$is_verified = is_array($body) && isset($body['verified']) && $body['verified'];
221+
$data = json_decode($body, true);
222+
223+
// check if Prosopo API returned a correct JSON response
224+
if ($data === null || !is_array($data)) {
225+
$logger = mc4wp('log');
226+
$logger->error(sprintf('ProCaptcha returned a non-JSON response: %s', $body));
227+
return true;
228+
}
221229

230+
$is_verified = isset($data['verified']) && $data['verified'];
222231
return true === $is_verified;
223232
}
224233

225234
public function maybe_add_type_module_attribute(string $tag, string $handle, string $src): string
226235
{
227-
if (
228-
'prosopo-procaptcha' !== $handle ||
236+
if ('prosopo-procaptcha' !== $handle ||
229237
// make sure we don't make it twice if other Procaptcha integrations are present.
230238
false !== strpos('type="module"', $tag)
231239
) {
@@ -254,8 +262,7 @@ public function is_enabled()
254262
*/
255263
public function print_captcha_element($is_without_validation_element = false, $is_forced_render = false)
256264
{
257-
if (
258-
false === $this->is_displayed_for_authorized &&
265+
if (false === $this->is_displayed_for_authorized &&
259266
true === is_user_logged_in() &&
260267
false === $is_forced_render
261268
) {
@@ -322,8 +329,7 @@ public function register_error_message(array $messages)
322329
*/
323330
public function validate_form($error_keys, $form)
324331
{
325-
if (
326-
false === strpos($form->content, $this->get_field_stub()) ||
332+
if (false === strpos($form->content, $this->get_field_stub()) ||
327333
(false === $this->is_displayed_for_authorized && true === is_user_logged_in()) ||
328334
true === $this->is_human_made_request()
329335
) {

0 commit comments

Comments
 (0)