Use cache for forms rendering to

[MAILPOET-2639]
This commit is contained in:
Pavel Dohnal
2020-01-28 16:52:27 +01:00
committed by Jack Kitterhing
parent d06fc2d00d
commit e644675046
4 changed files with 36 additions and 3 deletions

View File

@ -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(),