Display form always in the_content, not just once

Use magical functions that seems to work

[MAILPOET-3009]
This commit is contained in:
Pavel Dohnal
2020-06-23 15:59:07 +02:00
committed by Veljko V
parent 9e3dcedb39
commit cee7416fdc
3 changed files with 17 additions and 42 deletions

View File

@@ -46,12 +46,6 @@ 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,
@@ -64,7 +58,6 @@ class DisplayFormInWPContent {
$this->formRenderer = $formRenderer;
$this->assetsController = $assetsController;
$this->templateRenderer = $templateRenderer;
$this->alreadyDisplayed = false;
}
/**
@@ -87,17 +80,21 @@ class DisplayFormInWPContent {
foreach ($forms as $displayType => $form) {
$result .= $this->getContentBellow($form, $displayType);
}
$this->alreadyDisplayed = true;
return $result;
}
private function shouldDisplay(): bool {
// This is a fix Yoast plugin and Shapely theme compatibility
// This is to make sure we only display once for each page
// Yast plugin calls `get_the_excerpt` which also triggers hook `the_content` we don't want to include our form in that
// Shapely calls the hook `the_content` multiple times on the page as well and we would display popup multiple times - not ideal
if (!$this->wp->inTheLoop() || !$this->wp->isMainQuery()) {
return false;
}
// this code ensures that we display the form only on a page which is related to single post
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;
}

View File

@@ -592,6 +592,14 @@ class Functions {
return wpautop($pee, $br);
}
public function inTheLoop(): bool {
return in_the_loop();
}
public function isMainQuery(): bool {
return is_main_query();
}
/**
* @param string $action
* @param array|object $args

View File

@@ -31,6 +31,8 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
parent::_before();
$this->repository = $this->createMock(FormsRepository::class);
$this->wp = $this->createMock(WPFunctions::class);
$this->wp->expects($this->any())->method('inTheLoop')->willReturn(true);
$this->wp->expects($this->any())->method('isMainQuery')->willReturn(true);
$this->wp->expects($this->any())->method('wpCreateNonce')->willReturn('asdfgh');
WPFunctions::set($this->wp);
$this->assetsController = $this->createMock(AssetsController::class);
@@ -248,38 +250,6 @@ 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);