Display form for selected pages

[MAILPOET-3120]
This commit is contained in:
Pavel Dohnal
2020-09-08 14:53:51 +02:00
committed by Veljko V
parent eb5ae12cd7
commit 5a864f4f42
2 changed files with 31 additions and 13 deletions

View File

@@ -187,29 +187,25 @@ class DisplayFormInWPContent {
return false;
}
$key = '';
if ($this->wp->isSingular('post')) {
return $this->shouldDisplayFormOnPost($setup);
if ($this->wp->isSingular('post') && $this->shouldDisplayFormOnPost($setup, 'posts')) {
return true;
}
if ($this->wp->isPage()) {
$key = 'pages';
if ($this->wp->isPage() && $this->shouldDisplayFormOnPost($setup, 'pages')) {
return true;
}
// is enabled for this page?
return (isset($setup[$key])
&& isset($setup[$key]['all'])
&& $setup[$key]['all'] === '1');
return false;
}
private function shouldDisplayFormOnPost($setup): bool {
if (!isset($setup['posts'])) {
private function shouldDisplayFormOnPost($setup, $postsKey): bool {
if (!isset($setup[$postsKey])) {
return false;
}
if (isset($setup['posts']['all']) && $setup['posts']['all'] === '1') {
if (isset($setup[$postsKey]['all']) && $setup[$postsKey]['all'] === '1') {
return true;
}
$post = $this->wp->getPost(null, ARRAY_A);
if (isset($setup['posts']['selected']) && in_array($post['ID'], $setup['posts']['selected'])) {
if (isset($setup[$postsKey]['selected']) && in_array($post['ID'], $setup[$postsKey]['selected'])) {
return true;
}
return false;

View File

@@ -84,6 +84,28 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
expect($result)->endsWith($renderedForm);
}
public function testAppendsRenderedFormAfterOnASpecificPage() {
$renderedForm = '<form class="form"></form>';
$this->wp->expects($this->any())->method('isSingle')->willReturn(true);
$this->wp->expects($this->any())->method('isPage')->willReturn(true);
$this->wp->expects($this->any())->method('isSingular')->willReturn(false);
$this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]);
$this->assetsController->expects($this->once())->method('setupFrontEndDependencies');
$this->templateRenderer->expects($this->once())->method('render')->willReturn($renderedForm);
$form = new FormEntity('My Form');
$form->setSettings([
'segments' => ['3'],
'form_placement' => [
'below_posts' => ['enabled' => '1', 'pages' => ['all' => '', 'selected' => ['1']], 'posts' => ['all' => '']],
],
]);
$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($renderedForm);
}
public function testItPassThruNonStringPostContent() {
$this->wp->expects($this->never())->method('isSingle');
$this->wp->expects($this->never())->method('isSingular');