Render fixed bar form in page
[MAILPOET-2740]
This commit is contained in:
@ -12,6 +12,21 @@ class DisplayFormInWPContent {
|
|||||||
|
|
||||||
const NO_FORM_TRANSIENT_KEY = 'no_forms_displayed_bellow_content';
|
const NO_FORM_TRANSIENT_KEY = 'no_forms_displayed_bellow_content';
|
||||||
|
|
||||||
|
const SETUP = [
|
||||||
|
'below_post' => [
|
||||||
|
'post' => 'place_form_bellow_all_posts',
|
||||||
|
'page' => 'place_form_bellow_all_pages',
|
||||||
|
],
|
||||||
|
'popup' => [
|
||||||
|
'post' => 'place_popup_form_on_all_posts',
|
||||||
|
'page' => 'place_popup_form_on_all_pages',
|
||||||
|
],
|
||||||
|
'fixed_bar' => [
|
||||||
|
'post' => 'place_fixed_bar_form_on_all_posts',
|
||||||
|
'page' => 'place_fixed_bar_form_on_all_pages',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
/** @var WPFunctions */
|
/** @var WPFunctions */
|
||||||
private $wp;
|
private $wp;
|
||||||
|
|
||||||
@ -61,6 +76,7 @@ class DisplayFormInWPContent {
|
|||||||
foreach ($forms as $form) {
|
foreach ($forms as $form) {
|
||||||
$result .= $this->getContentBellow($form, 'popup');
|
$result .= $this->getContentBellow($form, 'popup');
|
||||||
$result .= $this->getContentBellow($form, 'below_post');
|
$result .= $this->getContentBellow($form, 'below_post');
|
||||||
|
$result .= $this->getContentBellow($form, 'fixed_bar');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
@ -88,15 +104,13 @@ class DisplayFormInWPContent {
|
|||||||
$forms = $this->formsRepository->findBy(['deletedAt' => null]);
|
$forms = $this->formsRepository->findBy(['deletedAt' => null]);
|
||||||
return array_filter($forms, function($form) {
|
return array_filter($forms, function($form) {
|
||||||
return (
|
return (
|
||||||
$this->shouldDisplayFormBellowContent($form)
|
$this->shouldDisplayForm($form)
|
||||||
|| $this->shouldDisplayPopupForm($form)
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getContentBellow(FormEntity $form, string $displayType): string {
|
private function getContentBellow(FormEntity $form, string $displayType): string {
|
||||||
if ($displayType === 'below_post' && !$this->shouldDisplayFormBellowContent($form)) return '';
|
if (!$this->shouldDisplayFormType($form, $displayType)) return '';
|
||||||
if ($displayType === 'popup' && !$this->shouldDisplayPopupForm($form)) return '';
|
|
||||||
$formData = [
|
$formData = [
|
||||||
'body' => $form->getBody(),
|
'body' => $form->getBody(),
|
||||||
'styles' => $form->getStyles(),
|
'styles' => $form->getStyles(),
|
||||||
@ -137,33 +151,29 @@ class DisplayFormInWPContent {
|
|||||||
return $this->templateRenderer->render('form/front_end_form.html', $templateData);
|
return $this->templateRenderer->render('form/front_end_form.html', $templateData);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function shouldDisplayFormBellowContent(FormEntity $form): bool {
|
private function shouldDisplayForm(FormEntity $form): bool {
|
||||||
$settings = $form->getSettings();
|
foreach (self::SETUP as $formType => $formSetup) {
|
||||||
if (!is_array($settings)) return false;
|
if ($this->shouldDisplayFormType($form, $formType)) {
|
||||||
if (!isset($settings['place_form_bellow_all_posts'])) return false;
|
return true;
|
||||||
if (
|
}
|
||||||
($settings['place_form_bellow_all_posts'] === '1')
|
}
|
||||||
&& $this->wp->isSingular('post')
|
|
||||||
) return true;
|
|
||||||
if (
|
|
||||||
($settings['place_form_bellow_all_pages'] === '1')
|
|
||||||
&& $this->wp->isPage()
|
|
||||||
) return true;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function shouldDisplayPopupForm(FormEntity $form): bool {
|
private function shouldDisplayFormType(FormEntity $form, string $formType): bool {
|
||||||
$settings = $form->getSettings();
|
$settings = $form->getSettings();
|
||||||
if (!is_array($settings)) return false;
|
if (!is_array($settings)) return false;
|
||||||
if (!isset($settings['place_popup_form_on_all_pages'])) return false;
|
|
||||||
if (
|
$keys = self::SETUP[$formType];
|
||||||
($settings['place_popup_form_on_all_posts'] === '1')
|
$key = '';
|
||||||
&& $this->wp->isSingular('post')
|
if ($this->wp->isSingular('post')) {
|
||||||
) return true;
|
$key = $keys['post'];
|
||||||
if (
|
|
||||||
($settings['place_popup_form_on_all_pages'] === '1')
|
|
||||||
&& $this->wp->isPage()
|
|
||||||
) return true;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
if ($this->wp->isPage()) {
|
||||||
|
$key = $keys['page'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return (isset($settings[$key]) && ($settings[$key] === '1'));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -255,6 +255,39 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
|||||||
expect($result)->endsWith($formHtml);
|
expect($result)->endsWith($formHtml);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAppendsRenderedFixedBarForm() {
|
||||||
|
$formHtml = '<form id="test-form"></form>';
|
||||||
|
$this->wp->expects($this->once())->method('isSingle')->willReturn(false);
|
||||||
|
$this->wp->expects($this->any())->method('isPage')->willReturn(true);
|
||||||
|
$this->assetsController->expects($this->once())->method('setupFrontEndDependencies');
|
||||||
|
$this->templateRenderer->expects($this->once())->method('render')->willReturn($formHtml);
|
||||||
|
$this->wp
|
||||||
|
->expects($this->never())
|
||||||
|
->method('setTransient');
|
||||||
|
$form = new FormEntity('My Form');
|
||||||
|
$form->setSettings([
|
||||||
|
'segments' => ['3'],
|
||||||
|
'place_form_bellow_all_pages' => '',
|
||||||
|
'place_form_bellow_all_posts' => '',
|
||||||
|
'place_popup_form_on_all_pages' => '',
|
||||||
|
'place_popup_form_on_all_posts' => '',
|
||||||
|
'place_fixed_bar_form_on_all_pages' => '1',
|
||||||
|
'place_fixed_bar_form_on_all_posts' => '1',
|
||||||
|
'success_message' => 'Hello',
|
||||||
|
]);
|
||||||
|
$form->setBody([[
|
||||||
|
'type' => 'submit',
|
||||||
|
'params' => ['label' => 'Subscribe!'],
|
||||||
|
'id' => 'submit',
|
||||||
|
'name' => 'Submit',
|
||||||
|
]]);
|
||||||
|
$this->repository->expects($this->once())->method('findBy')->willReturn([$form]);
|
||||||
|
|
||||||
|
$result = $this->hook->display('content');
|
||||||
|
expect($result)->notEquals('content');
|
||||||
|
expect($result)->endsWith($formHtml);
|
||||||
|
}
|
||||||
|
|
||||||
public function _after() {
|
public function _after() {
|
||||||
parent::_after();
|
parent::_after();
|
||||||
WPFunctions::set(new WPFunctions());
|
WPFunctions::set(new WPFunctions());
|
||||||
|
Reference in New Issue
Block a user