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
ReCaptchaHooks.php
108 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 ReCaptchaHooks { |
| 14 | |
| 15 | const RECAPTCHA_LIB_URL = 'https://www.google.com/recaptcha/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 ReCaptchaValidator */ |
| 27 | private $reCaptchaValidator; |
| 28 | |
| 29 | /** @var ReCaptchaRenderer */ |
| 30 | private $reCaptchaRenderer; |
| 31 | |
| 32 | public function __construct( |
| 33 | WPFunctions $wp, |
| 34 | BasicRenderer $renderer, |
| 35 | SettingsController $settings, |
| 36 | ReCaptchaValidator $reCaptchaValidator, |
| 37 | ReCaptchaRenderer $reCaptchaRenderer |
| 38 | ) { |
| 39 | $this->wp = $wp; |
| 40 | $this->renderer = $renderer; |
| 41 | $this->settings = $settings; |
| 42 | $this->reCaptchaValidator = $reCaptchaValidator; |
| 43 | $this->reCaptchaRenderer = $reCaptchaRenderer; |
| 44 | } |
| 45 | |
| 46 | public function isEnabled(): bool { |
| 47 | // In some cases on multisite instance or during fresh install, this code may run |
| 48 | // before DB migrator and settings table is not ready at that time |
| 49 | try { |
| 50 | if (!$this->settings->get(CaptchaConstants::ON_REGISTER_FORMS_SETTING_NAME, false)) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | return CaptchaConstants::isReCaptcha( |
| 55 | $this->settings->get('captcha.type') |
| 56 | ); |
| 57 | } catch (\Exception $e) { |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public function enqueueScripts() { |
| 63 | $this->wp->wpEnqueueScript('mailpoet_recaptcha', self::RECAPTCHA_LIB_URL); |
| 64 | |
| 65 | $this->wp->wpEnqueueStyle( |
| 66 | 'mailpoet_public', |
| 67 | Env::$assetsUrl . '/dist/css/' . $this->renderer->getCssAsset('mailpoet-public.css') |
| 68 | ); |
| 69 | |
| 70 | $this->wp->wpEnqueueScript( |
| 71 | 'mailpoet_public', |
| 72 | Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('public.js'), |
| 73 | ['jquery'], |
| 74 | Env::$version, |
| 75 | [ |
| 76 | 'in_footer' => true, |
| 77 | 'strategy' => 'defer', |
| 78 | ] |
| 79 | ); |
| 80 | |
| 81 | // necessary for public.js script |
| 82 | $ajaxFailedErrorMessage = __('An error has happened while performing a request, please try again later.', 'mailpoet'); |
| 83 | $this->wp->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [ |
| 84 | 'ajax_url' => $this->wp->adminUrl('admin-ajax.php'), |
| 85 | 'is_rtl' => (function_exists('is_rtl') && is_rtl()), |
| 86 | 'ajax_common_error_message' => $ajaxFailedErrorMessage, |
| 87 | 'collect_subscriber_timezones' => $this->settings->isSettingEnabled('collect_subscriber_timezones.enabled'), |
| 88 | ]); |
| 89 | } |
| 90 | |
| 91 | public function render() { |
| 92 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 93 | echo $this->reCaptchaRenderer->render(); |
| 94 | } |
| 95 | |
| 96 | public function validate(\WP_Error $errors) { |
| 97 | try { |
| 98 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 99 | $responseToken = $_POST['g-recaptcha-response'] ?? ''; |
| 100 | $this->reCaptchaValidator->validate(is_string($responseToken) ? $responseToken : ''); |
| 101 | } catch (\Throwable $e) { |
| 102 | $errors->add('recaptcha_failed', $e->getMessage()); |
| 103 | } |
| 104 | |
| 105 | return $errors; |
| 106 | } |
| 107 | } |
| 108 |