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\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;
|
||||
|
@ -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(),
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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');
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user