Display forms only on default post types
[MAILPOET-2639]
This commit is contained in:
committed by
Jack Kitterhing
parent
bdfd488ae4
commit
fd1e8ee915
@ -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')
|
||||
|
@ -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
|
||||
|
@ -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');
|
||||
|
Reference in New Issue
Block a user