Introduce captcha session service

[MAILPOET-2248]
This commit is contained in:
Rostislav Wolny
2019-08-05 08:53:53 +02:00
committed by Jack Kitterhing
parent ee1cee27dc
commit 6a494a2fae
3 changed files with 65 additions and 0 deletions

View File

@ -163,6 +163,7 @@ class ContainerConfigurator implements IContainerConfigurator {
// Subscription // Subscription
$container->autowire(\MailPoet\Subscription\Captcha::class)->setPublic(true); $container->autowire(\MailPoet\Subscription\Captcha::class)->setPublic(true);
$container->autowire(\MailPoet\Subscription\CaptchaRenderer::class)->setPublic(true); $container->autowire(\MailPoet\Subscription\CaptchaRenderer::class)->setPublic(true);
$container->autowire(\MailPoet\Subscription\CaptchaSession::class);
$container->autowire(\MailPoet\Subscription\Comment::class)->setPublic(true); $container->autowire(\MailPoet\Subscription\Comment::class)->setPublic(true);
$container->autowire(\MailPoet\Subscription\Form::class)->setPublic(true); $container->autowire(\MailPoet\Subscription\Form::class)->setPublic(true);
$container->autowire(\MailPoet\Subscription\Manage::class)->setPublic(true); $container->autowire(\MailPoet\Subscription\Manage::class)->setPublic(true);

View File

@ -0,0 +1,56 @@
<?php
namespace MailPoet\Subscription;
use MailPoet\Config\Session;
use MailPoet\WP\Functions as WPFunctions;
class CaptchaSession {
const EXPIRATION = 300; // 5 minutes
const SESSION_HASH_KEY = 'hash';
const SESSION_FORM_KEY = 'form';
/** @var WPFunctions */
private $wp;
/** @var Session */
private $session;
function __construct(WPFunctions $wp, Session $session) {
$this->wp = $wp;
$this->session = $session;
}
function isAvailable() {
return $this->session->getId() !== null;
}
function reset() {
$this->wp->deleteTransient($this->getKey(self::SESSION_FORM_KEY));
$this->wp->deleteTransient($this->getKey(self::SESSION_HASH_KEY));
}
function setFormData(array $data) {
$this->wp->setTransient($this->getKey(self::SESSION_FORM_KEY), $data, self::EXPIRATION);
}
function getFormData() {
return $this->wp->getTransient($this->getKey(self::SESSION_FORM_KEY));
}
function setCaptchaHash($hash) {
$this->wp->setTransient($this->getKey(self::SESSION_HASH_KEY), $hash, self::EXPIRATION);
}
function getCaptchaHash() {
return $this->wp->getTransient($this->getKey(self::SESSION_HASH_KEY));
}
private function getKey($type) {
if ($this->session->getId() === null) {
throw new \Exception("MailPoet session not initialized.");
}
return implode('_', ['MAILPOET', $this->session->getId(), $type]);
}
}

View File

@ -384,6 +384,14 @@ class Functions {
return set_transient($transient, $value, $expiration); return set_transient($transient, $value, $expiration);
} }
function getTransient($transient) {
return get_transient($transient);
}
function deleteTransient($transient) {
return delete_transient($transient);
}
function singlePostTitle($prefix = '', $display = true) { function singlePostTitle($prefix = '', $display = true) {
return single_post_title($prefix, $display); return single_post_title($prefix, $display);
} }