diff --git a/lib/Form/DisplayFormInWPContent.php b/lib/Form/DisplayFormInWPContent.php index 9a684892fd..cb6545fc50 100644 --- a/lib/Form/DisplayFormInWPContent.php +++ b/lib/Form/DisplayFormInWPContent.php @@ -187,8 +187,11 @@ class DisplayFormInWPContent { return false; } - if ($this->wp->isSingular('post') && $this->shouldDisplayFormOnPost($setup, 'posts')) { - return true; + if ($this->wp->isSingular('post')) { + 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')) { return true; @@ -197,7 +200,7 @@ class DisplayFormInWPContent { return false; } - private function shouldDisplayFormOnPost($setup, $postsKey): bool { + private function shouldDisplayFormOnPost(array $setup, string $postsKey): bool { if (!isset($setup[$postsKey])) { return false; } diff --git a/lib/WP/Functions.php b/lib/WP/Functions.php index 04df3c0ad2..fd5b1119e7 100644 --- a/lib/WP/Functions.php +++ b/lib/WP/Functions.php @@ -232,6 +232,14 @@ class Functions { 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) { return get_post_thumbnail_id($post); } diff --git a/tests/unit/Form/DisplayFormInWPContentTest.php b/tests/unit/Form/DisplayFormInWPContentTest.php index 458c6e3f7c..38bf18bc43 100644 --- a/tests/unit/Form/DisplayFormInWPContentTest.php +++ b/tests/unit/Form/DisplayFormInWPContentTest.php @@ -84,6 +84,62 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest { expect($result)->endsWith($renderedForm); } + public function testAppendsRenderedFormAfterOnASpecificCategory() { + $renderedForm = '
'; + $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 = '
'; + $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() { $renderedForm = '
'; $this->wp->expects($this->any())->method('isSingle')->willReturn(true);