diff --git a/mailpoet/lib/Subscription/Captcha/CaptchaSession.php b/mailpoet/lib/Subscription/Captcha/CaptchaSession.php index ce16161d2d..bda4e8651b 100644 --- a/mailpoet/lib/Subscription/Captcha/CaptchaSession.php +++ b/mailpoet/lib/Subscription/Captcha/CaptchaSession.php @@ -15,8 +15,8 @@ class CaptchaSession { /** @var WPFunctions */ private $wp; - /** @var ?string */ - private $id = null; + /** @var string */ + private $id = ''; public function __construct( WPFunctions $wp @@ -24,11 +24,14 @@ class CaptchaSession { $this->wp = $wp; } - public function init($id = null) { + public function init(string $id = null) { $this->id = $id ?: Security::generateRandomString(self::ID_LENGTH); } - public function getId(): ?string { + public function getId(): string { + if (!$this->id) { + $this->init(); + } return $this->id; } diff --git a/mailpoet/lib/Subscription/Captcha/Validator/BuiltInCaptchaValidator.php b/mailpoet/lib/Subscription/Captcha/Validator/BuiltInCaptchaValidator.php index 5a82f5a370..25ccfa0487 100644 --- a/mailpoet/lib/Subscription/Captcha/Validator/BuiltInCaptchaValidator.php +++ b/mailpoet/lib/Subscription/Captcha/Validator/BuiltInCaptchaValidator.php @@ -56,7 +56,7 @@ class BuiltInCaptchaValidator implements CaptchaValidator { throw new ValidationError( __('Please fill in the CAPTCHA.', 'mailpoet'), [ - 'redirect_url' => $this->subscriptionUrlFactory->getCaptchaUrl($this->getSessionId()), + 'redirect_url' => $this->subscriptionUrlFactory->getCaptchaUrl($this->captchaSession->getId()), ] ); } @@ -65,7 +65,7 @@ class BuiltInCaptchaValidator implements CaptchaValidator { throw new ValidationError( __('Please regenerate the CAPTCHA.', 'mailpoet'), [ - 'redirect_url' => $this->subscriptionUrlFactory->getCaptchaUrl($this->getSessionId()), + 'redirect_url' => $this->subscriptionUrlFactory->getCaptchaUrl($this->captchaSession->getId()), ] ); } @@ -84,15 +84,6 @@ class BuiltInCaptchaValidator implements CaptchaValidator { } - private function getSessionId() { - $id = $this->captchaSession->getId(); - if ($id === null) { - $this->captchaSession->init(); - $id = $this->captchaSession->getId(); - } - return $id; - } - public function isRequired($subscriberEmail = null) { if ($this->isUserExemptFromCaptcha()) { return false;