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; return false;
} }
$key = ''; if ($this->wp->isSingular('post') && $this->shouldDisplayFormOnPost($setup, 'posts')) {
if ($this->wp->isSingular('post')) { return true;
return $this->shouldDisplayFormOnPost($setup);
} }
if ($this->wp->isPage()) { if ($this->wp->isPage() && $this->shouldDisplayFormOnPost($setup, 'pages')) {
$key = 'pages'; return true;
} }
// is enabled for this page? return false;
return (isset($setup[$key])
&& isset($setup[$key]['all'])
&& $setup[$key]['all'] === '1');
} }
private function shouldDisplayFormOnPost($setup): bool { private function shouldDisplayFormOnPost($setup, $postsKey): bool {
if (!isset($setup['posts'])) { if (!isset($setup[$postsKey])) {
return false; return false;
} }
if (isset($setup['posts']['all']) && $setup['posts']['all'] === '1') { if (isset($setup[$postsKey]['all']) && $setup[$postsKey]['all'] === '1') {
return true; return true;
} }
$post = $this->wp->getPost(null, ARRAY_A); $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 true;
} }
return false; return false;

View File

@@ -84,6 +84,28 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
expect($result)->endsWith($renderedForm); 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() { public function testItPassThruNonStringPostContent() {
$this->wp->expects($this->never())->method('isSingle'); $this->wp->expects($this->never())->method('isSingle');
$this->wp->expects($this->never())->method('isSingular'); $this->wp->expects($this->never())->method('isSingular');