Display form always in the_content, not just once
Use magical functions that seems to work [MAILPOET-3009]
This commit is contained in:
@@ -46,12 +46,6 @@ 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,
|
||||||
@@ -64,7 +58,6 @@ class DisplayFormInWPContent {
|
|||||||
$this->formRenderer = $formRenderer;
|
$this->formRenderer = $formRenderer;
|
||||||
$this->assetsController = $assetsController;
|
$this->assetsController = $assetsController;
|
||||||
$this->templateRenderer = $templateRenderer;
|
$this->templateRenderer = $templateRenderer;
|
||||||
$this->alreadyDisplayed = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,17 +80,21 @@ class DisplayFormInWPContent {
|
|||||||
foreach ($forms as $displayType => $form) {
|
foreach ($forms as $displayType => $form) {
|
||||||
$result .= $this->getContentBellow($form, $displayType);
|
$result .= $this->getContentBellow($form, $displayType);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->alreadyDisplayed = true;
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function shouldDisplay(): bool {
|
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
|
// 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;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -592,6 +592,14 @@ class Functions {
|
|||||||
return wpautop($pee, $br);
|
return wpautop($pee, $br);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function inTheLoop(): bool {
|
||||||
|
return in_the_loop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isMainQuery(): bool {
|
||||||
|
return is_main_query();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $action
|
* @param string $action
|
||||||
* @param array|object $args
|
* @param array|object $args
|
||||||
|
@@ -31,6 +31,8 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
|||||||
parent::_before();
|
parent::_before();
|
||||||
$this->repository = $this->createMock(FormsRepository::class);
|
$this->repository = $this->createMock(FormsRepository::class);
|
||||||
$this->wp = $this->createMock(WPFunctions::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');
|
$this->wp->expects($this->any())->method('wpCreateNonce')->willReturn('asdfgh');
|
||||||
WPFunctions::set($this->wp);
|
WPFunctions::set($this->wp);
|
||||||
$this->assetsController = $this->createMock(AssetsController::class);
|
$this->assetsController = $this->createMock(AssetsController::class);
|
||||||
@@ -248,38 +250,6 @@ 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);
|
||||||
|
Reference in New Issue
Block a user