Display popup form on the page

[MAILPOET-2741]
This commit is contained in:
Pavel Dohnal
2020-03-25 09:19:57 +01:00
committed by Veljko V
parent b5ce2a97c9
commit de0f31d0ee
2 changed files with 64 additions and 2 deletions

View File

@ -86,7 +86,10 @@ class DisplayFormInWPContent {
private function getForms(): array {
$forms = $this->formsRepository->findBy(['deletedAt' => null]);
return array_filter($forms, function($form) {
return $this->shouldDisplayFormBellowContent($form);
return (
$this->shouldDisplayFormBellowContent($form)
|| $this->shouldDisplayPopupForm($form)
);
});
}
@ -102,7 +105,7 @@ class DisplayFormInWPContent {
'form_html_id' => $htmlId,
'form_id' => $form->getId(),
'form_success_message' => $formSettings['success_message'] ?? null,
'form_type' => 'below_post',
'form_type' => $this->getFormType($form),
'styles' => $this->formRenderer->renderStyles($formData, '#' . $htmlId),
'html' => $this->formRenderer->renderHTML($formData),
'form_element_styles' => $this->formRenderer->renderFormElementStyles($formData),
@ -128,6 +131,19 @@ class DisplayFormInWPContent {
return $this->templateRenderer->render('form/front_end_form.html', $templateData);
}
private function getFormType(FormEntity $form): string {
$settings = $form->getSettings();
if (
isset($settings['place_popup_form_on_all_pages'])
&& (
($settings['place_popup_form_on_all_posts'] === '1')
||
($settings['place_popup_form_on_all_pages'] === '1')
)
) return 'popup';
return 'below_post';
}
private function shouldDisplayFormBellowContent(FormEntity $form): bool {
$settings = $form->getSettings();
if (!is_array($settings)) return false;
@ -142,4 +158,19 @@ class DisplayFormInWPContent {
) return true;
return false;
}
private function shouldDisplayPopupForm(FormEntity $form): bool {
$settings = $form->getSettings();
if (!is_array($settings)) return false;
if (!isset($settings['place_popup_form_on_all_pages'])) return false;
if (
($settings['place_popup_form_on_all_posts'] === '1')
&& $this->wp->isSingular('post')
) return true;
if (
($settings['place_popup_form_on_all_pages'] === '1')
&& $this->wp->isPage()
) return true;
return false;
}
}

View File

@ -224,6 +224,37 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
$this->hook->display('content');
}
public function testAppendsRenderedPopupForm() {
$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' => '1',
'place_popup_form_on_all_posts' => '',
'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() {
parent::_after();
WPFunctions::set(new WPFunctions());