Display on tags/categories

[MAILPOET-3120]
This commit is contained in:
Pavel Dohnal
2020-09-08 15:27:22 +02:00
committed by Veljko V
parent 5a864f4f42
commit 3f65aaee8d
3 changed files with 70 additions and 3 deletions

View File

@@ -187,8 +187,11 @@ class DisplayFormInWPContent {
return false; return false;
} }
if ($this->wp->isSingular('post') && $this->shouldDisplayFormOnPost($setup, 'posts')) { if ($this->wp->isSingular('post')) {
return true; if ($this->shouldDisplayFormOnPost($setup, 'posts')) return true;
if (isset($setup['categories']) && $this->wp->hasCategory($setup['categories'])) return true;
if (isset($setup['tags']) && $this->wp->hasTag($setup['tags'])) return true;
return false;
} }
if ($this->wp->isPage() && $this->shouldDisplayFormOnPost($setup, 'pages')) { if ($this->wp->isPage() && $this->shouldDisplayFormOnPost($setup, 'pages')) {
return true; return true;
@@ -197,7 +200,7 @@ class DisplayFormInWPContent {
return false; return false;
} }
private function shouldDisplayFormOnPost($setup, $postsKey): bool { private function shouldDisplayFormOnPost(array $setup, string $postsKey): bool {
if (!isset($setup[$postsKey])) { if (!isset($setup[$postsKey])) {
return false; return false;
} }

View File

@@ -232,6 +232,14 @@ class Functions {
return get_post($post, $output, $filter); return get_post($post, $output, $filter);
} }
public function hasCategory($category = '', $post = null): bool {
return has_category($category, $post);
}
public function hasTag($tag = '', $post = null): bool {
return has_tag($tag, $post);
}
public function getPostThumbnailId($post = null) { public function getPostThumbnailId($post = null) {
return get_post_thumbnail_id($post); return get_post_thumbnail_id($post);
} }

View File

@@ -84,6 +84,62 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
expect($result)->endsWith($renderedForm); expect($result)->endsWith($renderedForm);
} }
public function testAppendsRenderedFormAfterOnASpecificCategory() {
$renderedForm = '<form class="form"></form>';
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
$this->wp->expects($this->any())->method('isSingular')->willReturn(true);
$this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]);
$this->wp->expects($this->any())->method('hasCategory')->willReturn(true);
$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' => ''],
'posts' => ['all' => '', 'selected' => ['2']],
'categories' => ['2'],
],
],
]);
$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 testAppendsRenderedFormAfterOnASpecificTag() {
$renderedForm = '<form class="form"></form>';
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
$this->wp->expects($this->any())->method('isSingular')->willReturn(true);
$this->wp->expects($this->any())->method('getPost')->willReturn(['ID' => 1]);
$this->wp->expects($this->any())->method('hasCategory')->willReturn(false);
$this->wp->expects($this->any())->method('hasTag')->willReturn(true);
$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' => ''],
'posts' => ['all' => '', 'selected' => ['2']],
'categories' => ['2'],
'tags' => ['3'],
],
],
]);
$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 testAppendsRenderedFormAfterOnASpecificPage() { public function testAppendsRenderedFormAfterOnASpecificPage() {
$renderedForm = '<form class="form"></form>'; $renderedForm = '<form class="form"></form>';
$this->wp->expects($this->any())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isSingle')->willReturn(true);