wp = $wp; $this->formsRepository = $formsRepository; $this->formRenderer = $formRenderer; $this->assetsController = $assetsController; $this->templateRenderer = $templateRenderer; } /** * This takes input from an action and any plugin or theme can pass anything. * We return string for regular content otherwise we just pass thru what comes. * @param mixed $content * @return string|mixed */ public function display($content = null) { if (!is_string($content) || !$this->shouldDisplay()) return $content; $forms = $this->getForms(); if (count($forms) === 0) { $this->saveNoForms(); return $content; } $this->assetsController->setupFrontEndDependencies(); $result = $content; foreach ($forms as $displayType => $form) { $result .= $this->getContentBellow($form, $displayType); } return $result; } private function shouldDisplay(): bool { // This is a fix Yoast plugin and Shapely theme compatibility // This is to make sure we only display once for each page // Yast plugin calls `get_the_excerpt` which also triggers hook `the_content` we don't want to include our form in that // Shapely calls the hook `the_content` multiple times on the page as well and we would display popup multiple times - not ideal if (!$this->wp->inTheLoop() || !$this->wp->isMainQuery()) { return false; } // this code ensures that we display the form only on a page which is related to single post if (!$this->wp->isSingle() && !$this->wp->isPage()) return false; $cache = $this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY); if (isset($cache[$this->wp->getPostType()])) return false; return true; } private function saveNoForms() { $stored = $this->wp->getTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY); if (!is_array($stored)) $stored = []; $stored[$this->wp->getPostType()] = true; $this->wp->setTransient(DisplayFormInWPContent::NO_FORM_TRANSIENT_KEY, $stored); } /** * @return array */ private function getForms(): array { $forms = $this->formsRepository->findBy([ 'deletedAt' => null, 'status' => FormEntity::STATUS_ENABLED, ], ['updatedAt' => 'ASC']); $forms = $this->filterOneFormInEachDisplayType($forms); return $forms; } /** * @param FormEntity[] $forms * @return array */ private function filterOneFormInEachDisplayType($forms): array { $formsFiltered = []; foreach ($forms as $form) { foreach (self::TYPES as $displayType) { if ($this->shouldDisplayFormType($form, $displayType)) { $formsFiltered[$displayType] = $form; } } } return $formsFiltered; } private function getContentBellow(FormEntity $form, string $displayType): string { if (!$this->shouldDisplayFormType($form, $displayType)) return ''; $formData = [ 'body' => $form->getBody(), 'styles' => $form->getStyles(), 'settings' => $form->getSettings(), ]; $formSettings = $form->getSettings(); if (!is_array($formSettings)) return ''; $htmlId = 'mp_form_' . $displayType . $form->getId(); $templateData = [ 'form_html_id' => $htmlId, 'form_id' => $form->getId(), 'form_success_message' => $formSettings['success_message'] ?? null, 'form_type' => $displayType, 'styles' => $this->formRenderer->renderStyles($formData, '#' . $htmlId, $displayType), 'html' => $this->formRenderer->renderHTML($formData), 'close_button_icon' => $formSettings['close_button'] ?? 'round_white', ]; // (POST) non ajax success/error variables $templateData['success'] = ( (isset($_GET['mailpoet_success'])) && ((int)$_GET['mailpoet_success'] === $form->getId()) ); $templateData['error'] = ( (isset($_GET['mailpoet_error'])) && ((int)$_GET['mailpoet_error'] === $form->getId()) ); $templateData['delay'] = $formSettings['form_placement'][$displayType]['delay'] ?? 0; $templateData['position'] = $formSettings['form_placement'][$displayType]['position'] ?? ''; $templateData['backgroundColor'] = $formSettings['backgroundColor'] ?? ''; $templateData['fontFamily'] = $formSettings['font_family'] ?? ''; // generate security token $templateData['token'] = Security::generateToken(); // add API version $templateData['api_version'] = API::CURRENT_VERSION; return $this->templateRenderer->render('form/front_end_form.html', $templateData); } private function shouldDisplayFormType(FormEntity $form, string $formType): bool { $settings = $form->getSettings(); // check the structure just to be sure if (!is_array($settings) || !isset($settings['form_placement'][$formType]) || !is_array($settings['form_placement'][$formType]) ) return false; $setup = $settings['form_placement'][$formType]; if ($setup['enabled'] !== '1') { return false; } $key = ''; if ($this->wp->isSingular('post')) { $key = 'posts'; } if ($this->wp->isPage()) { $key = 'pages'; } // is enabled for this page? return (isset($setup[$key]) && isset($setup[$key]['all']) && $setup[$key]['all'] === '1'); } }