Introduce captcha session service
[MAILPOET-2248]
This commit is contained in:
committed by
Jack Kitterhing
parent
ee1cee27dc
commit
6a494a2fae
@ -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);
|
||||||
|
56
lib/Subscription/CaptchaSession.php
Normal file
56
lib/Subscription/CaptchaSession.php
Normal 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]);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user