<?php declare(strict_types = 1); namespace MailPoet\Config; if (!defined('ABSPATH')) exit; use MailPoet\Captcha\CaptchaConstants; use MailPoet\Captcha\ReCaptchaRenderer; use MailPoet\Captcha\ReCaptchaValidator; use MailPoet\Config\Renderer as BasicRenderer; use MailPoet\Settings\SettingsController; use MailPoet\WP\Functions as WPFunctions; class HooksReCaptcha { const RECAPTCHA_LIB_URL = 'https://www.google.com/recaptcha/api.js'; /** @var WPFunctions */ private $wp; /** @var BasicRenderer */ private $renderer; /** @var SettingsController */ private $settings; /** @var ReCaptchaValidator */ private $reCaptchaValidator; /** @var ReCaptchaRenderer */ private $reCaptchaRenderer; public function __construct( WPFunctions $wp, BasicRenderer $renderer, SettingsController $settings, ReCaptchaValidator $reCaptchaValidator, ReCaptchaRenderer $reCaptchaRenderer ) { $this->wp = $wp; $this->renderer = $renderer; $this->settings = $settings; $this->reCaptchaValidator = $reCaptchaValidator; $this->reCaptchaRenderer = $reCaptchaRenderer; } public function isEnabled(): bool { // A transient code to enable incremental development of the feature. // Later when a setting is introduced, this function will be adjusted. if (!in_array(getenv('MP_ENV'), ['development', 'test'])) { return false; } return CaptchaConstants::isReCaptcha( $this->settings->get('captcha.type') ); } public function enqueueScripts() { $this->wp->wpEnqueueScript('mailpoet_recaptcha', self::RECAPTCHA_LIB_URL); $this->wp->wpEnqueueStyle( 'mailpoet_public', Env::$assetsUrl . '/dist/css/' . $this->renderer->getCssAsset('mailpoet-public.css') ); $this->wp->wpEnqueueScript( 'mailpoet_public', Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('public.js'), ['jquery'], Env::$version, [ 'in_footer' => true, 'strategy' => 'defer', ] ); // necessary for public.js script $ajaxFailedErrorMessage = __('An error has happened while performing a request, please try again later.', 'mailpoet'); $this->wp->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [ 'ajax_url' => $this->wp->adminUrl('admin-ajax.php'), 'is_rtl' => (function_exists('is_rtl') && is_rtl()), 'ajax_common_error_message' => esc_js($ajaxFailedErrorMessage), ]); } public function render() { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $this->reCaptchaRenderer->render(); } public function validate(\WP_Error $errors) { try { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $responseToken = $_POST['g-recaptcha-response'] ?? ''; $this->reCaptchaValidator->validate($responseToken); } catch (\Throwable $e) { $errors->add('recaptcha_failed', $e->getMessage()); } return $errors; } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
PopulatorData | Folder | 0755 |
|
|
AccessControl.php | File | 3.89 KB | 0644 |
|
Activator.php | File | 4.94 KB | 0644 |
|
AssetsLoader.php | File | 1.68 KB | 0644 |
|
Capabilities.php | File | 2.87 KB | 0644 |
|
Changelog.php | File | 5.04 KB | 0644 |
|
DeactivationPoll.php | File | 1.43 KB | 0644 |
|
DeferredAdminNotices.php | File | 1.16 KB | 0644 |
|
Env.php | File | 4.16 KB | 0644 |
|
Hooks.php | File | 20.01 KB | 0644 |
|
HooksReCaptcha.php | File | 2.92 KB | 0644 |
|
HooksWooCommerce.php | File | 6.76 KB | 0644 |
|
Initializer.php | File | 16.88 KB | 0644 |
|
Installer.php | File | 3.12 KB | 0644 |
|
Localizer.php | File | 2.49 KB | 0644 |
|
Menu.php | File | 23.54 KB | 0644 |
|
PersonalDataErasers.php | File | 769 B | 0644 |
|
PersonalDataExporters.php | File | 3.22 KB | 0644 |
|
PluginActivatedHook.php | File | 776 B | 0644 |
|
Populator.php | File | 23.37 KB | 0644 |
|
PrivacyPolicy.php | File | 4.53 KB | 0644 |
|
Renderer.php | File | 4.34 KB | 0644 |
|
RendererFactory.php | File | 670 B | 0644 |
|
RequirementsChecker.php | File | 3.73 KB | 0644 |
|
Router.php | File | 1006 B | 0644 |
|
ServicesChecker.php | File | 7.04 KB | 0644 |
|
Shortcodes.php | File | 8.63 KB | 0644 |
|
SilentUpgraderSkin.php | File | 570 B | 0644 |
|
SubscriberChangesNotifier.php | File | 5 KB | 0644 |
|
TranslationUpdater.php | File | 7.92 KB | 0644 |
|
TwigEnvironment.php | File | 694 B | 0644 |
|
TwigFileSystemCache.php | File | 853 B | 0644 |
|
Updater.php | File | 1.97 KB | 0644 |
|
index.php | File | 6 B | 0644 |
|