[MAILPOET-2639]
This commit is contained in:
Pavel Dohnal
2020-01-29 09:25:25 +01:00
committed by Jack Kitterhing
parent e644675046
commit c6aa1311ad

View File

@ -15,32 +15,45 @@ class DisplayFormInWPContent {
/** @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;
} }
public function display(string $content): string { public function display(string $content): string {
$this->appendedForm = false; if(!$this->shouldDisplay()) return $content;
$forms = $this->getForms();
if (count($forms) === 0) {
$this->wp->setTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY, true);
return $content;
}
$result = $content; $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) { 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 shouldDisplay():bool {
if (!$this->wp->isSingle()) return false;
if ($this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY)) return false;
return true;
}
/**
* @return FormEntity[]
*/
private function getForms():array {
$forms = $this->formsRepository->findAll();
return array_filter($forms, function($form) {
return $this->shouldDisplayFormBellowContent($form);
});
}
private function getContentBellow(FormEntity $form): string { private function getContentBellow(FormEntity $form): string {
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(),