Prevent rendering forms multiple times

[MAILPOET-2902]
This commit is contained in:
Pavel Dohnal
2020-06-11 14:42:57 +02:00
committed by Veljko V
parent 3f6bfaa875
commit f3370a62d0
2 changed files with 41 additions and 0 deletions

View File

@@ -46,6 +46,12 @@ class DisplayFormInWPContent {
/** @var TemplateRenderer */ /** @var TemplateRenderer */
private $templateRenderer; private $templateRenderer;
/**
* We want to display forms only once per page, once we render we need to stop
* @var bool
*/
private $alreadyDisplayed;
public function __construct( public function __construct(
WPFunctions $wp, WPFunctions $wp,
FormsRepository $formsRepository, FormsRepository $formsRepository,
@@ -58,6 +64,7 @@ class DisplayFormInWPContent {
$this->formRenderer = $formRenderer; $this->formRenderer = $formRenderer;
$this->assetsController = $assetsController; $this->assetsController = $assetsController;
$this->templateRenderer = $templateRenderer; $this->templateRenderer = $templateRenderer;
$this->alreadyDisplayed = false;
} }
/** /**
@@ -81,6 +88,7 @@ class DisplayFormInWPContent {
$result .= $this->getContentBellow($form, $displayType); $result .= $this->getContentBellow($form, $displayType);
} }
$this->alreadyDisplayed = true;
return $result; return $result;
} }
@@ -89,6 +97,7 @@ class DisplayFormInWPContent {
if (!$this->wp->isSingle() && !$this->wp->isPage()) return false; if (!$this->wp->isSingle() && !$this->wp->isPage()) return false;
$cache = $this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY); $cache = $this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY);
if (isset($cache[$this->wp->getPostType()])) return false; if (isset($cache[$this->wp->getPostType()])) return false;
if ($this->alreadyDisplayed) return false;
return true; return true;
} }

View File

@@ -248,6 +248,38 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
expect($result)->endsWith($formHtml); expect($result)->endsWith($formHtml);
} }
public function testAppendsOnlyOnce() {
$formHtml = '<form id="test-form"></form>';
$this->wp->expects($this->any())->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_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);
$result = $this->hook->display('content');
expect($result)->equals('content');
}
public function testOnlyOneFormInEachCategory() { public function testOnlyOneFormInEachCategory() {
$formHtml = '<form id="test-form"></form>'; $formHtml = '<form id="test-form"></form>';
$this->wp->expects($this->once())->method('isSingle')->willReturn(false); $this->wp->expects($this->once())->method('isSingle')->willReturn(false);