diff --git a/lib/API/JSON/v1/Forms.php b/lib/API/JSON/v1/Forms.php index 46aa9df38e..44dbbab572 100644 --- a/lib/API/JSON/v1/Forms.php +++ b/lib/API/JSON/v1/Forms.php @@ -5,6 +5,7 @@ namespace MailPoet\API\JSON\v1; use MailPoet\API\JSON\Endpoint as APIEndpoint; use MailPoet\API\JSON\Error as APIError; use MailPoet\Config\AccessControl; +use MailPoet\Form\DisplayFormInWPContent; use MailPoet\Form\Renderer as FormRenderer; use MailPoet\Form\Util; use MailPoet\Listing; @@ -174,6 +175,8 @@ class Forms extends APIEndpoint { } } + WPFunctions::get()->deleteTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY); + // check if the user gets to pick his own lists // or if it's selected by the admin $hasSegmentSelection = false; diff --git a/lib/Form/DisplayFormInWPContent.php b/lib/Form/DisplayFormInWPContent.php index 460de13365..8b811bfef8 100644 --- a/lib/Form/DisplayFormInWPContent.php +++ b/lib/Form/DisplayFormInWPContent.php @@ -7,31 +7,40 @@ use MailPoet\WP\Functions as WPFunctions; class DisplayFormInWPContent { + const NO_FORM_TRANSIENT_KEY = 'no_forms_displayed_bellow_content'; + /** @var WPFunctions */ private $wp; /** @var FormsRepository */ private $formsRepository; + /** @var bool */ + private $appendedForm = false; + public function __construct(WPFunctions $wp, FormsRepository $formsRepository) { $this->wp = $wp; $this->formsRepository = $formsRepository; } - // TODO remove transient in the api on form save - public function display(string $content): string { + $this->appendedForm = false; $result = $content; if (!$this->wp->isSingle()) return $result; + if ($this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY)) return $result; $forms = $this->formsRepository->findAll(); foreach ($forms as $form) { $result .= $this->getContentBellow($form); } + if (!$this->appendedForm) { + $this->wp->setTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY, true); + } return $result; } private function getContentBellow(FormEntity $form): string { if (!$this->shouldDisplayFormBellowContent($form)) return ''; + $this->appendedForm = true; return Renderer::render([ 'body' => $form->getBody(), 'styles' => $form->getStyles(), diff --git a/lib/WP/Functions.php b/lib/WP/Functions.php index 3e4be5759a..f2ba651174 100644 --- a/lib/WP/Functions.php +++ b/lib/WP/Functions.php @@ -392,7 +392,7 @@ class Functions { return self_admin_url($path, $scheme); } - public function setTransient($transient, $value, $expiration) { + public function setTransient($transient, $value, $expiration = 0) { return set_transient($transient, $value, $expiration); } diff --git a/tests/unit/Form/DisplayFormInWPContentTest.php b/tests/unit/Form/DisplayFormInWPContentTest.php index fd48a2cc38..55e5dde24d 100644 --- a/tests/unit/Form/DisplayFormInWPContentTest.php +++ b/tests/unit/Form/DisplayFormInWPContentTest.php @@ -137,6 +137,9 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest { public function testAppendsRenderedFormAfterPageContent() { $this->wp->expects($this->once())->method('isSingle')->willReturn(true); $this->wp->expects($this->any())->method('isPage')->willReturn(true); + $this->wp + ->expects($this->never()) + ->method('setTransient'); $form = new FormEntity('My Form'); $form->setSettings([ 'segments' => ['3'], @@ -157,11 +160,29 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest { } public function testSetsTransientToImprovePerformance() { + $this->wp->expects($this->once())->method('isSingle')->willReturn(true); + $this->wp->expects($this->any())->method('isPage')->willReturn(false); + $this->wp + ->expects($this->once()) + ->method('setTransient'); + $form1 = new FormEntity('My Form'); + $form2 = new FormEntity('My Form'); + $this->repository->expects($this->once())->method('findAll')->willReturn([$form1, $form2]); + + $this->hook->display('content'); } 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->once()) + ->method('getTransient') + ->willReturn('true'); + $this->repository->expects($this->never())->method('findAll'); + $this->hook->display('content'); } }