Move captcha page rendering to a separate class [MAILPOET-2015]
This commit is contained in:
85
lib/Subscription/CaptchaRenderer.php
Normal file
85
lib/Subscription/CaptchaRenderer.php
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Subscription;
|
||||||
|
|
||||||
|
use MailPoet\Models\Form as FormModel;
|
||||||
|
use MailPoet\Util\Url as UrlHelper;
|
||||||
|
use MailPoet\Form\Renderer as FormRenderer;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
|
class CaptchaRenderer {
|
||||||
|
/** @var UrlHelper */
|
||||||
|
private $url_helper;
|
||||||
|
|
||||||
|
function __construct() {
|
||||||
|
$this->url_helper = new UrlHelper(new WPFunctions());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCaptchaPageTitle() {
|
||||||
|
return WPFunctions::get()->__("Confirm you’re not a robot", 'mailpoet');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCaptchaPageContent() {
|
||||||
|
$fields = [
|
||||||
|
[
|
||||||
|
'id' => 'captcha',
|
||||||
|
'type' => 'text',
|
||||||
|
'params' => [
|
||||||
|
'label' => WPFunctions::get()->__('Type in the input the characters you see in the picture above:', 'mailpoet'),
|
||||||
|
'value' => '',
|
||||||
|
'obfuscate' => false,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$form = array_merge(
|
||||||
|
$fields,
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'id' => 'submit',
|
||||||
|
'type' => 'submit',
|
||||||
|
'params' => [
|
||||||
|
'label' => WPFunctions::get()->__('Subscribe', 'mailpoet'),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$form_id = isset($_SESSION[Captcha::SESSION_FORM_KEY]['form_id']) ? (int)$_SESSION[Captcha::SESSION_FORM_KEY]['form_id'] : 0;
|
||||||
|
$form_model = FormModel::findOne($form_id);
|
||||||
|
if (!$form_model instanceof FormModel) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$form_model = $form_model->asArray();
|
||||||
|
|
||||||
|
$form_html = '<form method="POST" ' .
|
||||||
|
'action="' . admin_url('admin-post.php?action=mailpoet_subscription_form') . '" ' .
|
||||||
|
'class="mailpoet_form mailpoet_captcha_form" ' .
|
||||||
|
'novalidate>';
|
||||||
|
$form_html .= '<input type="hidden" name="data[form_id]" value="' . $form_id . '" />';
|
||||||
|
$form_html .= '<input type="hidden" name="api_version" value="v1" />';
|
||||||
|
$form_html .= '<input type="hidden" name="endpoint" value="subscribers" />';
|
||||||
|
$form_html .= '<input type="hidden" name="mailpoet_method" value="subscribe" />';
|
||||||
|
$form_html .= '<input type="hidden" name="mailpoet_redirect" ' .
|
||||||
|
'value="' . htmlspecialchars($this->url_helper->getCurrentUrl(), ENT_QUOTES) . '" />';
|
||||||
|
|
||||||
|
$width = 220;
|
||||||
|
$height = 60;
|
||||||
|
$captcha_url = Url::getCaptchaImageUrl($width, $height);
|
||||||
|
|
||||||
|
$form_html .= '<div class="mailpoet_form_hide_on_success">';
|
||||||
|
$form_html .= '<p class="mailpoet_paragraph">';
|
||||||
|
$form_html .= '<img class="mailpoet_captcha mailpoet_captcha_update" src="' . $captcha_url . '" width="' . $width . '" height="' . $height . '" title="' . WPFunctions::get()->__('Click to refresh the captcha', 'mailpoet') . '" />';
|
||||||
|
$form_html .= '</p>';
|
||||||
|
|
||||||
|
// subscription form
|
||||||
|
$form_html .= FormRenderer::renderBlocks($form, $honeypot = false);
|
||||||
|
$form_html .= '</div>';
|
||||||
|
$form_html .= '<div class="mailpoet_message">';
|
||||||
|
$form_html .= '<p class="mailpoet_validate_success" style="display:none;">' . $form_model['settings']['success_message'] . '</p>';
|
||||||
|
$form_html .= '<p class="mailpoet_validate_error" style="display:none;"></p>';
|
||||||
|
$form_html .= '</div>';
|
||||||
|
$form_html .= '</form>';
|
||||||
|
return $form_html;
|
||||||
|
}
|
||||||
|
}
|
@@ -39,6 +39,9 @@ class Pages {
|
|||||||
/** @var WPFunctions */
|
/** @var WPFunctions */
|
||||||
private $wp;
|
private $wp;
|
||||||
|
|
||||||
|
/** @var CaptchaRenderer */
|
||||||
|
private $captcha_renderer;
|
||||||
|
|
||||||
function __construct(
|
function __construct(
|
||||||
NewSubscriberNotificationMailer $new_subscriber_notification_sender,
|
NewSubscriberNotificationMailer $new_subscriber_notification_sender,
|
||||||
WPFunctions $wp,
|
WPFunctions $wp,
|
||||||
@@ -49,6 +52,7 @@ class Pages {
|
|||||||
$this->new_subscriber_notification_sender = $new_subscriber_notification_sender;
|
$this->new_subscriber_notification_sender = $new_subscriber_notification_sender;
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
$this->url_helper = $url_helper;
|
$this->url_helper = $url_helper;
|
||||||
|
$this->captcha_renderer = new CaptchaRenderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
function init($action = false, $data = [], $init_shortcodes = false, $init_page_filters = false) {
|
function init($action = false, $data = [], $init_shortcodes = false, $init_page_filters = false) {
|
||||||
@@ -162,7 +166,7 @@ class Pages {
|
|||||||
// when it's our own page, generate page title based on requested action
|
// when it's our own page, generate page title based on requested action
|
||||||
switch ($this->action) {
|
switch ($this->action) {
|
||||||
case self::ACTION_CAPTCHA:
|
case self::ACTION_CAPTCHA:
|
||||||
return $this->getCaptchaTitle();
|
return $this->captcha_renderer->getCaptchaPageTitle();
|
||||||
|
|
||||||
case self::ACTION_CONFIRM:
|
case self::ACTION_CONFIRM:
|
||||||
return $this->getConfirmTitle();
|
return $this->getConfirmTitle();
|
||||||
@@ -189,7 +193,7 @@ class Pages {
|
|||||||
|
|
||||||
switch ($this->action) {
|
switch ($this->action) {
|
||||||
case self::ACTION_CAPTCHA:
|
case self::ACTION_CAPTCHA:
|
||||||
$content = $this->getCaptchaContent();
|
$content = $this->captcha_renderer->getCaptchaPageContent();
|
||||||
break;
|
break;
|
||||||
case self::ACTION_CONFIRM:
|
case self::ACTION_CONFIRM:
|
||||||
$content = $this->getConfirmContent();
|
$content = $this->getConfirmContent();
|
||||||
@@ -225,10 +229,6 @@ class Pages {
|
|||||||
return $meta;
|
return $meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getCaptchaTitle() {
|
|
||||||
return WPFunctions::get()->__("Confirm you’re not a robot", 'mailpoet');
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getConfirmTitle() {
|
private function getConfirmTitle() {
|
||||||
if ($this->isPreview()) {
|
if ($this->isPreview()) {
|
||||||
$title = sprintf(
|
$title = sprintf(
|
||||||
@@ -264,70 +264,6 @@ class Pages {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getCaptchaContent() {
|
|
||||||
$fields = [
|
|
||||||
[
|
|
||||||
'id' => 'captcha',
|
|
||||||
'type' => 'text',
|
|
||||||
'params' => [
|
|
||||||
'label' => WPFunctions::get()->__('Type in the input the characters you see in the picture above:', 'mailpoet'),
|
|
||||||
'value' => '',
|
|
||||||
'obfuscate' => false,
|
|
||||||
],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
$form = array_merge(
|
|
||||||
$fields,
|
|
||||||
[
|
|
||||||
[
|
|
||||||
'id' => 'submit',
|
|
||||||
'type' => 'submit',
|
|
||||||
'params' => [
|
|
||||||
'label' => WPFunctions::get()->__('Subscribe', 'mailpoet'),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
$form_id = isset($_SESSION[Captcha::SESSION_FORM_KEY]['form_id']) ? (int)$_SESSION[Captcha::SESSION_FORM_KEY]['form_id'] : 0;
|
|
||||||
$form_model = FormModel::findOne($form_id);
|
|
||||||
if (!$form_model instanceof FormModel) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$form_model = $form_model->asArray();
|
|
||||||
|
|
||||||
$form_html = '<form method="POST" ' .
|
|
||||||
'action="' . admin_url('admin-post.php?action=mailpoet_subscription_form') . '" ' .
|
|
||||||
'class="mailpoet_form mailpoet_captcha_form" ' .
|
|
||||||
'novalidate>';
|
|
||||||
$form_html .= '<input type="hidden" name="data[form_id]" value="' . $form_id . '" />';
|
|
||||||
$form_html .= '<input type="hidden" name="api_version" value="v1" />';
|
|
||||||
$form_html .= '<input type="hidden" name="endpoint" value="subscribers" />';
|
|
||||||
$form_html .= '<input type="hidden" name="mailpoet_method" value="subscribe" />';
|
|
||||||
$form_html .= '<input type="hidden" name="mailpoet_redirect" ' .
|
|
||||||
'value="' . htmlspecialchars($this->url_helper->getCurrentUrl(), ENT_QUOTES) . '" />';
|
|
||||||
|
|
||||||
$width = 220;
|
|
||||||
$height = 60;
|
|
||||||
$captcha_url = Url::getCaptchaImageUrl($width, $height);
|
|
||||||
|
|
||||||
$form_html .= '<div class="mailpoet_form_hide_on_success">';
|
|
||||||
$form_html .= '<p class="mailpoet_paragraph">';
|
|
||||||
$form_html .= '<img class="mailpoet_captcha mailpoet_captcha_update" src="' . $captcha_url . '" width="' . $width . '" height="' . $height . '" title="' . WPFunctions::get()->__('Click to refresh the captcha', 'mailpoet') . '" />';
|
|
||||||
$form_html .= '</p>';
|
|
||||||
|
|
||||||
// subscription form
|
|
||||||
$form_html .= FormRenderer::renderBlocks($form, $honeypot = false);
|
|
||||||
$form_html .= '</div>';
|
|
||||||
$form_html .= '<div class="mailpoet_message">';
|
|
||||||
$form_html .= '<p class="mailpoet_validate_success" style="display:none;">' . $form_model['settings']['success_message'] . '</p>';
|
|
||||||
$form_html .= '<p class="mailpoet_validate_error" style="display:none;"></p>';
|
|
||||||
$form_html .= '</div>';
|
|
||||||
$form_html .= '</form>';
|
|
||||||
return $form_html;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getConfirmContent() {
|
private function getConfirmContent() {
|
||||||
if ($this->isPreview() || $this->subscriber !== false) {
|
if ($this->isPreview() || $this->subscriber !== false) {
|
||||||
return $this->wp->__("Yup, we've added you to our email list. You'll hear from us shortly.", 'mailpoet');
|
return $this->wp->__("Yup, we've added you to our email list. You'll hear from us shortly.", 'mailpoet');
|
||||||
|
Reference in New Issue
Block a user