Add form preview page form rendering
[MAILPOET-2743]
This commit is contained in:
committed by
Veljko V
parent
db618d7948
commit
0b512fe81b
@ -192,6 +192,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Form\Block\Text::class);
|
$container->autowire(\MailPoet\Form\Block\Text::class);
|
||||||
$container->autowire(\MailPoet\Form\Block\Textarea::class);
|
$container->autowire(\MailPoet\Form\Block\Textarea::class);
|
||||||
$container->autowire(\MailPoet\Form\FormFactory::class);
|
$container->autowire(\MailPoet\Form\FormFactory::class);
|
||||||
|
$container->autowire(\MailPoet\Form\PreviewPage::class);
|
||||||
$container->autowire(\MailPoet\Form\Util\Styles::class);
|
$container->autowire(\MailPoet\Form\Util\Styles::class);
|
||||||
// Helpscout
|
// Helpscout
|
||||||
$container->autowire(\MailPoet\Helpscout\Beacon::class);
|
$container->autowire(\MailPoet\Helpscout\Beacon::class);
|
||||||
|
107
lib/Form/PreviewPage.php
Normal file
107
lib/Form/PreviewPage.php
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Form;
|
||||||
|
|
||||||
|
use MailPoet\Config\Renderer as TemplateRenderer;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
|
class PreviewPage {
|
||||||
|
/** @var WPFunctions */
|
||||||
|
private $wp;
|
||||||
|
|
||||||
|
/** @var Renderer */
|
||||||
|
private $formRenderer;
|
||||||
|
|
||||||
|
/** @var TemplateRenderer */
|
||||||
|
private $templateRenderer;
|
||||||
|
|
||||||
|
/** @var FormsRepository */
|
||||||
|
private $formRepository;
|
||||||
|
|
||||||
|
/** @var AssetsController */
|
||||||
|
private $assetsController;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
WPFunctions $wp,
|
||||||
|
Renderer $formRenderer,
|
||||||
|
TemplateRenderer $templateRenderer,
|
||||||
|
FormsRepository $formRepository,
|
||||||
|
AssetsController $assetsController
|
||||||
|
) {
|
||||||
|
$this->wp = $wp;
|
||||||
|
$this->formRenderer = $formRenderer;
|
||||||
|
$this->templateRenderer = $templateRenderer;
|
||||||
|
$this->formRepository = $formRepository;
|
||||||
|
$this->assetsController = $assetsController;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renderPage(int $formId, string $formType): string {
|
||||||
|
$this->assetsController->setupFrontEndDependencies();
|
||||||
|
$formData = $this->fetchFormData($formId);
|
||||||
|
if (!is_array($formData)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return $this->getPostContent() . $this->getFormContent($formData, $formId, $formType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function renderTitle() {
|
||||||
|
return __('Sample page to preview your form', 'mailpoet');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array|null
|
||||||
|
*/
|
||||||
|
private function fetchFormData(int $id) {
|
||||||
|
$form = $this->formRepository->findOneById($id);
|
||||||
|
if ($form) {
|
||||||
|
return [
|
||||||
|
'body' => $form->getBody(),
|
||||||
|
'styles' => $form->getStyles(),
|
||||||
|
'settings' => $form->getSettings(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getFormContent(array $formData, int $formId, string $formType): string {
|
||||||
|
$htmlId = 'mailpoet_form_preview_' . $formId;
|
||||||
|
$templateData = [
|
||||||
|
'is_preview' => true,
|
||||||
|
'form_html_id' => $htmlId,
|
||||||
|
'form_id' => $formId,
|
||||||
|
'form_success_message' => $formData['settings']['success_message'] ?? null,
|
||||||
|
'form_type' => $formType,
|
||||||
|
'styles' => $this->formRenderer->renderStyles($formData, '#' . $htmlId),
|
||||||
|
'html' => $this->formRenderer->renderHTML($formData),
|
||||||
|
'form_element_styles' => $this->formRenderer->renderFormElementStyles($formData),
|
||||||
|
'success' => true,
|
||||||
|
'error' => true,
|
||||||
|
'delay' => 1,
|
||||||
|
'position' => $formData['settings'][$formType . '_form_position'] ?? '',
|
||||||
|
'backgroundColor' => $formData['settings']['backgroundColor'] ?? '',
|
||||||
|
];
|
||||||
|
$formPosition = $formData['settings'][$formType . '_form_position'] ?? '';
|
||||||
|
if (!$formPosition && $formType === 'fixed_bar') {
|
||||||
|
$formPosition = 'top';
|
||||||
|
}
|
||||||
|
if (!$formPosition && $formType === 'slide_in') {
|
||||||
|
$formPosition = 'left';
|
||||||
|
}
|
||||||
|
$templateData['position'] = $formPosition;
|
||||||
|
return $this->templateRenderer->render('form/front_end_form.html', $templateData);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getPostContent(): string {
|
||||||
|
$posts = $this->wp->getPosts([
|
||||||
|
'numberposts' => 1,
|
||||||
|
'orderby' => 'date',
|
||||||
|
'order' => 'DESC',
|
||||||
|
'post_status' => 'publish',
|
||||||
|
'post_type' => 'post',
|
||||||
|
]);
|
||||||
|
if (!isset($posts[0])) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return $posts[0]->post_content;
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
namespace MailPoet\Router\Endpoints;
|
namespace MailPoet\Router\Endpoints;
|
||||||
|
|
||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Config\AccessControl;
|
||||||
|
use MailPoet\Form\PreviewPage;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class FormPreview {
|
class FormPreview {
|
||||||
@ -15,31 +16,35 @@ class FormPreview {
|
|||||||
/** @var array|null */
|
/** @var array|null */
|
||||||
private $data;
|
private $data;
|
||||||
|
|
||||||
|
/** @var PreviewPage */
|
||||||
|
private $formPreviewPage;
|
||||||
|
|
||||||
public $allowedActions = [self::ACTION_VIEW];
|
public $allowedActions = [self::ACTION_VIEW];
|
||||||
public $permissions = [
|
public $permissions = [
|
||||||
'global' => AccessControl::NO_ACCESS_RESTRICTION,
|
'global' => AccessControl::NO_ACCESS_RESTRICTION,
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
WPFunctions $wp
|
WPFunctions $wp,
|
||||||
|
PreviewPage $formPreviewPage
|
||||||
) {
|
) {
|
||||||
$this->wp = $wp;
|
$this->wp = $wp;
|
||||||
|
$this->formPreviewPage = $formPreviewPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function view(array $data) {
|
public function view(array $data) {
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
$this->wp->addFilter('the_content', [$this,'renderContent'], 10);
|
$this->wp->addFilter('the_content', [$this,'renderContent'], 10);
|
||||||
$this->wp->addFilter('the_title', [$this,'renderTitle'], 10);
|
$this->wp->addFilter('the_title', [$this->formPreviewPage,'renderTitle'], 10);
|
||||||
$this->wp->addFilter('show_admin_bar', function () {
|
$this->wp->addFilter('show_admin_bar', function () {
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function renderContent(): string {
|
public function renderContent(): string {
|
||||||
return '<h1>Todo render form</h1>';
|
if (!isset($this->data['id']) || !isset($this->data['form_type'])) {
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
|
return $this->formPreviewPage->renderPage((int)$this->data['id'], (string)$this->data['form_type']);
|
||||||
public function renderTitle(): string {
|
|
||||||
return __('Sample page to preview your form', 'mailpoet');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user