Validator
1 month ago
BehavioralSignals.php
1 month ago
CaptchaConstants.php
2 months ago
CaptchaFormRenderer.php
2 weeks ago
CaptchaHooks.php
2 months ago
CaptchaPhrase.php
1 year ago
CaptchaRenderer.php
4 months ago
CaptchaSession.php
1 year ago
CaptchaUrlFactory.php
4 months ago
PageRenderer.php
5 months ago
ReCaptchaHooks.php
2 months ago
ReCaptchaRenderer.php
1 year ago
ReCaptchaValidator.php
1 year ago
TurnstileHooks.php
2 months ago
TurnstileRenderer.php
2 months ago
TurnstileValidator.php
2 months ago
index.php
1 year ago
TurnstileHooks.php
114 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Captcha; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Env; |
| 9 | use MailPoet\Config\Renderer as BasicRenderer; |
| 10 | use MailPoet\Settings\SettingsController; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | class TurnstileHooks { |
| 14 | |
| 15 | const TURNSTILE_LIB_URL = 'https://challenges.cloudflare.com/turnstile/v0/api.js'; |
| 16 | |
| 17 | /** @var WPFunctions */ |
| 18 | private $wp; |
| 19 | |
| 20 | /** @var BasicRenderer */ |
| 21 | private $renderer; |
| 22 | |
| 23 | /** @var SettingsController */ |
| 24 | private $settings; |
| 25 | |
| 26 | /** @var TurnstileValidator */ |
| 27 | private $turnstileValidator; |
| 28 | |
| 29 | /** @var TurnstileRenderer */ |
| 30 | private $turnstileRenderer; |
| 31 | |
| 32 | public function __construct( |
| 33 | WPFunctions $wp, |
| 34 | BasicRenderer $renderer, |
| 35 | SettingsController $settings, |
| 36 | TurnstileValidator $turnstileValidator, |
| 37 | TurnstileRenderer $turnstileRenderer |
| 38 | ) { |
| 39 | $this->wp = $wp; |
| 40 | $this->renderer = $renderer; |
| 41 | $this->settings = $settings; |
| 42 | $this->turnstileValidator = $turnstileValidator; |
| 43 | $this->turnstileRenderer = $turnstileRenderer; |
| 44 | } |
| 45 | |
| 46 | public function isEnabled(): bool { |
| 47 | try { |
| 48 | if (!$this->settings->get(CaptchaConstants::ON_REGISTER_FORMS_SETTING_NAME, false)) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | return CaptchaConstants::isTurnstile( |
| 53 | $this->settings->get('captcha.type') |
| 54 | ); |
| 55 | } catch (\Throwable $e) { |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public function enqueueScripts() { |
| 61 | $this->wp->wpEnqueueScript( |
| 62 | 'mailpoet_turnstile', |
| 63 | self::TURNSTILE_LIB_URL, |
| 64 | [], |
| 65 | false, |
| 66 | [ |
| 67 | 'in_footer' => true, |
| 68 | 'strategy' => 'defer', |
| 69 | ] |
| 70 | ); |
| 71 | |
| 72 | $this->wp->wpEnqueueStyle( |
| 73 | 'mailpoet_public', |
| 74 | Env::$assetsUrl . '/dist/css/' . $this->renderer->getCssAsset('mailpoet-public.css') |
| 75 | ); |
| 76 | |
| 77 | $this->wp->wpEnqueueScript( |
| 78 | 'mailpoet_public', |
| 79 | Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('public.js'), |
| 80 | ['jquery'], |
| 81 | Env::$version, |
| 82 | [ |
| 83 | 'in_footer' => true, |
| 84 | 'strategy' => 'defer', |
| 85 | ] |
| 86 | ); |
| 87 | |
| 88 | $ajaxFailedErrorMessage = __('An error has happened while performing a request, please try again later.', 'mailpoet'); |
| 89 | $this->wp->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [ |
| 90 | 'ajax_url' => $this->wp->adminUrl('admin-ajax.php'), |
| 91 | 'is_rtl' => $this->wp->isRtl(), |
| 92 | 'ajax_common_error_message' => $ajaxFailedErrorMessage, |
| 93 | 'collect_subscriber_timezones' => $this->settings->isSettingEnabled('collect_subscriber_timezones.enabled'), |
| 94 | ]); |
| 95 | } |
| 96 | |
| 97 | public function render() { |
| 98 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 99 | echo $this->turnstileRenderer->render(); |
| 100 | } |
| 101 | |
| 102 | public function validate(\WP_Error $errors) { |
| 103 | try { |
| 104 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 105 | $responseToken = $_POST['cf-turnstile-response'] ?? ''; |
| 106 | $this->turnstileValidator->validate(is_string($responseToken) ? $responseToken : ''); |
| 107 | } catch (\Throwable $e) { |
| 108 | $errors->add('turnstile_failed', $e->getMessage()); |
| 109 | } |
| 110 | |
| 111 | return $errors; |
| 112 | } |
| 113 | } |
| 114 |