From fd1e8ee915c62bd5218ce3ec8fa4fe8098801d0a Mon Sep 17 00:00:00 2001 From: Pavel Dohnal Date: Wed, 29 Jan 2020 16:04:18 +0100 Subject: [PATCH] Display forms only on default post types [MAILPOET-2639] --- lib/Form/DisplayFormInWPContent.php | 14 +++++++++++--- lib/WP/Functions.php | 12 ++++++++++++ tests/unit/Form/DisplayFormInWPContentTest.php | 13 ++++++++----- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/Form/DisplayFormInWPContent.php b/lib/Form/DisplayFormInWPContent.php index 7baf9fbf4c..1c0d27c0ba 100644 --- a/lib/Form/DisplayFormInWPContent.php +++ b/lib/Form/DisplayFormInWPContent.php @@ -25,7 +25,7 @@ class DisplayFormInWPContent { $forms = $this->getForms(); if (count($forms) === 0) { - $this->wp->setTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY, true); + $this->saveNoPosts(); return $content; } @@ -39,10 +39,18 @@ class DisplayFormInWPContent { private function shouldDisplay():bool { if (!$this->wp->isSingle()) return false; - if ($this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY)) return false; + $cache = $this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY); + if (isset($cache[$this->wp->getPostType()])) return false; return true; } + private function saveNoPosts() { + $stored = $this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY); + if (!is_array($stored)) $stored = []; + $stored[$this->wp->getPostType()] = true; + $this->wp->setTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY, $stored); + } + /** * @return FormEntity[] */ @@ -66,7 +74,7 @@ class DisplayFormInWPContent { if (!isset($settings['placeFormBellowAllPosts'])) return false; if ( ($settings['placeFormBellowAllPosts'] === '1') - && !$this->wp->isPage() + && $this->wp->isSingular('post') ) return true; if ( ($settings['placeFormBellowAllPages'] === '1') diff --git a/lib/WP/Functions.php b/lib/WP/Functions.php index f2ba651174..824ee4d1ac 100644 --- a/lib/WP/Functions.php +++ b/lib/WP/Functions.php @@ -232,6 +232,10 @@ class Functions { return get_post_types($args, $output, $operator); } + public function getPostType($post = null) { + return get_post_type($post); + } + public function getPosts(array $args = null) { return get_posts($args); } @@ -590,6 +594,14 @@ class Functions { return is_page($page); } + /** + * @param string|array $postTypes Optional. Post type or array of post types. Default empty. + * @return bool Whether the query is for an existing single post of any of the given post types. + */ + public function isSingular($postTypes = ''): bool { + return is_singular($postTypes); + } + /** * @param string $package * @param array $args diff --git a/tests/unit/Form/DisplayFormInWPContentTest.php b/tests/unit/Form/DisplayFormInWPContentTest.php index 55e5dde24d..727f1f7335 100644 --- a/tests/unit/Form/DisplayFormInWPContentTest.php +++ b/tests/unit/Form/DisplayFormInWPContentTest.php @@ -30,7 +30,7 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest { public function testAppendsRenderedFormAfterPostContent() { $this->wp->expects($this->once())->method('isSingle')->willReturn(true); - $this->wp->expects($this->any())->method('isPage')->willReturn(false); + $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], @@ -53,6 +53,7 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest { public function testDoesNotAppendFormIfDisabled() { $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isPage')->willReturn(false); + $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], @@ -73,7 +74,7 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest { public function testAppendsMultipleRenderedFormAfterPostContent() { $this->wp->expects($this->once())->method('isSingle')->willReturn(true); - $this->wp->expects($this->any())->method('isPage')->willReturn(false); + $this->wp->expects($this->any())->method('isSingular')->willReturn(true); $form1 = new FormEntity('My Form'); $form1->setSettings([ 'segments' => ['3'], @@ -114,8 +115,9 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest { } public function testDoesNotAppendFormIfNotOnPost() { - $this->wp->expects($this->once())->method('isSingle')->willReturn(true); - $this->wp->expects($this->once())->method('isPage')->willReturn(true); + $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); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], @@ -176,10 +178,11 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest { public function testDoesNotQueryDatabaseIfTransientIsSet() { $this->wp->expects($this->any())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isPage')->willReturn(false); + $this->wp->expects($this->any())->method('getPostType')->willReturn('post'); $this->wp ->expects($this->once()) ->method('getTransient') - ->willReturn('true'); + ->willReturn(['post' => true]); $this->repository->expects($this->never())->method('findAll'); $this->hook->display('content');