Prevent rendering forms multiple times
[MAILPOET-2902]
This commit is contained in:
@@ -46,6 +46,12 @@ class DisplayFormInWPContent {
|
||||
/** @var 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(
|
||||
WPFunctions $wp,
|
||||
FormsRepository $formsRepository,
|
||||
@@ -58,6 +64,7 @@ class DisplayFormInWPContent {
|
||||
$this->formRenderer = $formRenderer;
|
||||
$this->assetsController = $assetsController;
|
||||
$this->templateRenderer = $templateRenderer;
|
||||
$this->alreadyDisplayed = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,6 +88,7 @@ class DisplayFormInWPContent {
|
||||
$result .= $this->getContentBellow($form, $displayType);
|
||||
}
|
||||
|
||||
$this->alreadyDisplayed = true;
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -89,6 +97,7 @@ class DisplayFormInWPContent {
|
||||
if (!$this->wp->isSingle() && !$this->wp->isPage()) return false;
|
||||
$cache = $this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY);
|
||||
if (isset($cache[$this->wp->getPostType()])) return false;
|
||||
if ($this->alreadyDisplayed) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -248,6 +248,38 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
||||
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() {
|
||||
$formHtml = '<form id="test-form"></form>';
|
||||
$this->wp->expects($this->once())->method('isSingle')->willReturn(false);
|
||||
|
Reference in New Issue
Block a user