Display form on product pages

[MAILPOET-3210]
This commit is contained in:
Pavel Dohnal
2020-09-14 13:52:48 +02:00
committed by Veljko V
parent 72ab5343c0
commit 55a895f2c4
3 changed files with 78 additions and 3 deletions

View File

@@ -187,10 +187,10 @@ class DisplayFormInWPContent {
return false; return false;
} }
if ($this->wp->isSingular('post')) { if ($this->wp->isSingular('post') || $this->wp->isSingular('product')) {
if ($this->shouldDisplayFormOnPost($setup, 'posts')) return true; if ($this->shouldDisplayFormOnPost($setup, 'posts')) return true;
if (isset($setup['categories']) && $this->wp->hasCategory($setup['categories'])) return true; if ($this->shouldDisplayFormOnCategory($setup)) return true;
if (isset($setup['tags']) && $this->wp->hasTag($setup['tags'])) return true; if ($this->shouldDisplayFormOnTag($setup)) return true;
return false; return false;
} }
if ($this->wp->isPage() && $this->shouldDisplayFormOnPost($setup, 'pages')) { if ($this->wp->isPage() && $this->shouldDisplayFormOnPost($setup, 'pages')) {
@@ -213,4 +213,18 @@ class DisplayFormInWPContent {
} }
return false; 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;
}
} }

View File

@@ -240,6 +240,10 @@ class Functions {
return has_tag($tag, $post); return has_tag($tag, $post);
} }
public function hasTerm($term = '', $taxonomy = '', $post = null): bool {
return has_term($term, $taxonomy, $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

@@ -111,6 +111,34 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
expect($result)->endsWith($renderedForm); expect($result)->endsWith($renderedForm);
} }
public function testAppendsRenderedFormAfterOnASpecificWoocommerceCategory() {
$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('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() { public function testAppendsRenderedFormAfterOnASpecificTag() {
$renderedForm = '<form class="form"></form>'; $renderedForm = '<form class="form"></form>';
$this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->once())->method('isSingle')->willReturn(true);
@@ -140,6 +168,35 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
expect($result)->endsWith($renderedForm); expect($result)->endsWith($renderedForm);
} }
public function testAppendsRenderedFormAfterOnASpecificWooCommerceTag() {
$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(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() { 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);