Use cache for forms rendering to
[MAILPOET-2639]
This commit is contained in:
committed by
Jack Kitterhing
parent
d06fc2d00d
commit
e644675046
@ -5,6 +5,7 @@ namespace MailPoet\API\JSON\v1;
|
|||||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||||
use MailPoet\API\JSON\Error as APIError;
|
use MailPoet\API\JSON\Error as APIError;
|
||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Config\AccessControl;
|
||||||
|
use MailPoet\Form\DisplayFormInWPContent;
|
||||||
use MailPoet\Form\Renderer as FormRenderer;
|
use MailPoet\Form\Renderer as FormRenderer;
|
||||||
use MailPoet\Form\Util;
|
use MailPoet\Form\Util;
|
||||||
use MailPoet\Listing;
|
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
|
// check if the user gets to pick his own lists
|
||||||
// or if it's selected by the admin
|
// or if it's selected by the admin
|
||||||
$hasSegmentSelection = false;
|
$hasSegmentSelection = false;
|
||||||
|
@ -7,31 +7,40 @@ use MailPoet\WP\Functions as WPFunctions;
|
|||||||
|
|
||||||
class DisplayFormInWPContent {
|
class DisplayFormInWPContent {
|
||||||
|
|
||||||
|
const NO_FORM_TRANSIENT_KEY = 'no_forms_displayed_bellow_content';
|
||||||
|
|
||||||
/** @var WPFunctions */
|
/** @var WPFunctions */
|
||||||
private $wp;
|
private $wp;
|
||||||
|
|
||||||
/** @var FormsRepository */
|
/** @var FormsRepository */
|
||||||
private $formsRepository;
|
private $formsRepository;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
private $appendedForm = false;
|
||||||
|
|
||||||
public function __construct(WPFunctions $wp, FormsRepository $formsRepository) {
|
public function __construct(WPFunctions $wp, FormsRepository $formsRepository) {
|
||||||
$this->wp = $wp;
|
$this->wp = $wp;
|
||||||
$this->formsRepository = $formsRepository;
|
$this->formsRepository = $formsRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO remove transient in the api on form save
|
|
||||||
|
|
||||||
public function display(string $content): string {
|
public function display(string $content): string {
|
||||||
|
$this->appendedForm = false;
|
||||||
$result = $content;
|
$result = $content;
|
||||||
if (!$this->wp->isSingle()) return $result;
|
if (!$this->wp->isSingle()) return $result;
|
||||||
|
if ($this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY)) return $result;
|
||||||
$forms = $this->formsRepository->findAll();
|
$forms = $this->formsRepository->findAll();
|
||||||
foreach ($forms as $form) {
|
foreach ($forms as $form) {
|
||||||
$result .= $this->getContentBellow($form);
|
$result .= $this->getContentBellow($form);
|
||||||
}
|
}
|
||||||
|
if (!$this->appendedForm) {
|
||||||
|
$this->wp->setTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY, true);
|
||||||
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getContentBellow(FormEntity $form): string {
|
private function getContentBellow(FormEntity $form): string {
|
||||||
if (!$this->shouldDisplayFormBellowContent($form)) return '';
|
if (!$this->shouldDisplayFormBellowContent($form)) return '';
|
||||||
|
$this->appendedForm = true;
|
||||||
return Renderer::render([
|
return Renderer::render([
|
||||||
'body' => $form->getBody(),
|
'body' => $form->getBody(),
|
||||||
'styles' => $form->getStyles(),
|
'styles' => $form->getStyles(),
|
||||||
|
@ -392,7 +392,7 @@ class Functions {
|
|||||||
return self_admin_url($path, $scheme);
|
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);
|
return set_transient($transient, $value, $expiration);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,6 +137,9 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
|||||||
public function testAppendsRenderedFormAfterPageContent() {
|
public function testAppendsRenderedFormAfterPageContent() {
|
||||||
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
|
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
|
||||||
$this->wp->expects($this->any())->method('isPage')->willReturn(true);
|
$this->wp->expects($this->any())->method('isPage')->willReturn(true);
|
||||||
|
$this->wp
|
||||||
|
->expects($this->never())
|
||||||
|
->method('setTransient');
|
||||||
$form = new FormEntity('My Form');
|
$form = new FormEntity('My Form');
|
||||||
$form->setSettings([
|
$form->setSettings([
|
||||||
'segments' => ['3'],
|
'segments' => ['3'],
|
||||||
@ -157,11 +160,29 @@ class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testSetsTransientToImprovePerformance() {
|
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() {
|
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');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user