diff --git a/lib/Form/DisplayFormInWPContent.php b/lib/Form/DisplayFormInWPContent.php index cb6545fc50..fad1eff29b 100644 --- a/lib/Form/DisplayFormInWPContent.php +++ b/lib/Form/DisplayFormInWPContent.php @@ -187,10 +187,10 @@ class DisplayFormInWPContent { return false; } - if ($this->wp->isSingular('post')) { + if ($this->wp->isSingular('post') || $this->wp->isSingular('product')) { 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; + if ($this->shouldDisplayFormOnCategory($setup)) return true; + if ($this->shouldDisplayFormOnTag($setup)) return true; return false; } if ($this->wp->isPage() && $this->shouldDisplayFormOnPost($setup, 'pages')) { @@ -213,4 +213,18 @@ class DisplayFormInWPContent { } return false; } + + private function shouldDisplayFormOnCategory(array $setup): bool { + if (!isset($setup['categories'])) return false; + if ($this->wp->hasCategory($setup['categories'])) return true; + if ($this->wp->hasTerm($setup['categories'], 'product_cat')) return true; + return false; + } + + private function shouldDisplayFormOnTag(array $setup): bool { + if (!isset($setup['tags'])) return false; + if ($this->wp->hasTag($setup['tags'])) return true; + if ($this->wp->hasTerm($setup['tags'], 'product_tag')) return true; + return false; + } } diff --git a/lib/WP/Functions.php b/lib/WP/Functions.php index fd5b1119e7..ddf12cca86 100644 --- a/lib/WP/Functions.php +++ b/lib/WP/Functions.php @@ -240,6 +240,10 @@ class Functions { return has_tag($tag, $post); } + public function hasTerm($term = '', $taxonomy = '', $post = null): bool { + return has_term($term, $taxonomy, $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 38bf18bc43..3022dd9e34 100644 --- a/tests/unit/Form/DisplayFormInWPContentTest.php +++ b/tests/unit/Form/DisplayFormInWPContentTest.php @@ -111,6 +111,34 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest { expect($result)->endsWith($renderedForm); } + public function testAppendsRenderedFormAfterOnASpecificWoocommerceCategory() { + $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('hasTerm')->with(['2'], 'product_cat')->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); @@ -140,6 +168,35 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest { expect($result)->endsWith($renderedForm); } + public function testAppendsRenderedFormAfterOnASpecificWooCommerceTag() { + $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(false); + $this->wp->expects($this->any())->method('hasTerm')->with(['3'], 'product_tag')->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']], + '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);