Block
1 week ago
Listing
1 month ago
RestApi
1 month ago
Templates
2 years ago
Util
2 months ago
ApiDataSanitizer.php
2 weeks ago
AssetsController.php
2 months ago
BlockStylesRenderer.php
3 years ago
BlockWrapperRenderer.php
3 years ago
BlocksRenderer.php
1 week ago
DisplayFormInWPContent.php
2 months ago
FormHtmlSanitizer.php
1 month ago
FormMessageController.php
4 years ago
FormSaveController.php
1 year ago
FormsRepository.php
1 year ago
PreviewPage.php
2 months ago
PreviewWidget.php
1 year ago
Renderer.php
2 months ago
Widget.php
2 months ago
index.php
3 years ago
Renderer.php
148 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Form; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Captcha\CaptchaConstants; |
| 9 | use MailPoet\Entities\FormEntity; |
| 10 | use MailPoet\Form\Templates\FormTemplate; |
| 11 | use MailPoet\Form\Util\CustomFonts; |
| 12 | use MailPoet\Form\Util\Styles; |
| 13 | use MailPoet\Settings\SettingsController; |
| 14 | |
| 15 | class Renderer { |
| 16 | /** @var Styles */ |
| 17 | private $styleUtils; |
| 18 | |
| 19 | /** @var SettingsController */ |
| 20 | private $settings; |
| 21 | |
| 22 | /** @var BlocksRenderer */ |
| 23 | private $blocksRenderer; |
| 24 | |
| 25 | /** @var CustomFonts */ |
| 26 | private $customFonts; |
| 27 | |
| 28 | public function __construct( |
| 29 | Styles $styleUtils, |
| 30 | SettingsController $settings, |
| 31 | CustomFonts $customFonts, |
| 32 | BlocksRenderer $blocksRenderer |
| 33 | ) { |
| 34 | $this->styleUtils = $styleUtils; |
| 35 | $this->settings = $settings; |
| 36 | $this->blocksRenderer = $blocksRenderer; |
| 37 | $this->customFonts = $customFonts; |
| 38 | } |
| 39 | |
| 40 | public function renderStyles(FormEntity $form, string $prefix, string $displayType): string { |
| 41 | $this->customFonts->enqueueStyle(); |
| 42 | $html = $this->styleUtils->prefixStyles($this->getCustomStyles($form), $prefix); |
| 43 | $html .= strip_tags($this->styleUtils->renderFormSettingsStyles($form, $prefix, $displayType)); |
| 44 | return $html; |
| 45 | } |
| 46 | |
| 47 | public function renderHTML(?FormEntity $form = null): string { |
| 48 | if (($form instanceof FormEntity) && !empty($form->getBody()) && is_array($form->getSettings())) { |
| 49 | return $this->renderBlocks($form->getBody(), $form->getSettings(), $form->getId()); |
| 50 | } |
| 51 | return ''; |
| 52 | } |
| 53 | |
| 54 | public function getCustomStyles(?FormEntity $form = null): string { |
| 55 | if (($form instanceof FormEntity) && (strlen(trim($form->getStyles() ?? '')) > 0)) { |
| 56 | return strip_tags($form->getStyles() ?? ''); |
| 57 | } else { |
| 58 | return FormTemplate::DEFAULT_STYLES; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public function renderBlocks( |
| 63 | array $blocks = [], |
| 64 | array $formSettings = [], |
| 65 | ?int $formId = null, |
| 66 | bool $honeypotEnabled = true, |
| 67 | bool $captchaEnabled = true |
| 68 | ): string { |
| 69 | // add honeypot for spambots |
| 70 | $html = ($honeypotEnabled) ? $this->renderHoneypot() : ''; |
| 71 | foreach ($blocks as $key => $block) { |
| 72 | if ( |
| 73 | $captchaEnabled |
| 74 | && $block['type'] === FormEntity::SUBMIT_BLOCK_TYPE |
| 75 | ) { |
| 76 | $html .= $this->renderCaptcha(); |
| 77 | } |
| 78 | if (in_array($block['type'], [FormEntity::COLUMN_BLOCK_TYPE, FormEntity::COLUMNS_BLOCK_TYPE])) { |
| 79 | $blocks = $block['body'] ?? []; |
| 80 | $html .= $this->blocksRenderer->renderContainerBlock($block, $this->renderBlocks($blocks, $formSettings, $formId, false)) . PHP_EOL; |
| 81 | } else { |
| 82 | $html .= $this->blocksRenderer->renderBlock($block, $formSettings, $formId) . PHP_EOL; |
| 83 | } |
| 84 | } |
| 85 | return $html; |
| 86 | } |
| 87 | |
| 88 | private function renderHoneypot(): string { |
| 89 | return '<label class="mailpoet_hp_email_label" style="display: none !important;">' . __('Please leave this field empty', 'mailpoet') . '<input type="email" name="data[email]"/></label>'; |
| 90 | } |
| 91 | |
| 92 | private function renderCaptcha(): string { |
| 93 | $type = $this->settings->get('captcha.type'); |
| 94 | if (CaptchaConstants::isReCaptcha($type)) { |
| 95 | return $this->renderReCaptcha(); |
| 96 | } |
| 97 | if (CaptchaConstants::isTurnstile($type)) { |
| 98 | return $this->renderTurnstile(); |
| 99 | } |
| 100 | return ''; |
| 101 | } |
| 102 | |
| 103 | private function renderReCaptcha(): string { |
| 104 | if ($this->settings->get('captcha.type') === CaptchaConstants::TYPE_RECAPTCHA) { |
| 105 | $siteKey = (string)$this->settings->get('captcha.recaptcha_site_token'); |
| 106 | $size = ''; |
| 107 | } else { |
| 108 | $siteKey = (string)$this->settings->get('captcha.recaptcha_invisible_site_token'); |
| 109 | $size = 'invisible'; |
| 110 | } |
| 111 | $siteKeyAttr = esc_attr($siteKey); |
| 112 | $fallbackUrl = esc_url('https://www.google.com/recaptcha/api/fallback?k=' . $siteKey); |
| 113 | |
| 114 | $html = '<div class="mailpoet_recaptcha" data-sitekey="' . $siteKeyAttr . '" ' . ($size === 'invisible' ? 'data-size="invisible"' : '') . '> |
| 115 | <div class="mailpoet_recaptcha_container"></div> |
| 116 | <noscript> |
| 117 | <div> |
| 118 | <div class="mailpoet_recaptcha_noscript_container"> |
| 119 | <div> |
| 120 | <iframe src="' . $fallbackUrl . '" frameborder="0" scrolling="no"> |
| 121 | </iframe> |
| 122 | </div> |
| 123 | </div> |
| 124 | <div class="mailpoet_recaptcha_noscript_input"> |
| 125 | <textarea id="g-recaptcha-response" name="data[recaptcha]" class="g-recaptcha-response"> |
| 126 | </textarea> |
| 127 | </div> |
| 128 | </div> |
| 129 | </noscript> |
| 130 | <input class="mailpoet_recaptcha_field" type="hidden" name="recaptchaWidgetId"> |
| 131 | </div>'; |
| 132 | if ($size !== 'invisible') { |
| 133 | $html .= '<div class="parsley-errors-list parsley-required mailpoet_error_recaptcha">' . __('This field is required.', 'mailpoet') . '</div>'; |
| 134 | } |
| 135 | |
| 136 | return $html; |
| 137 | } |
| 138 | |
| 139 | private function renderTurnstile(): string { |
| 140 | $siteKey = esc_attr((string)$this->settings->get('captcha.turnstile_site_token')); |
| 141 | |
| 142 | return '<div class="mailpoet_turnstile" data-sitekey="' . $siteKey . '"> |
| 143 | <div class="mailpoet_turnstile_container"></div> |
| 144 | <input class="mailpoet_turnstile_field" type="hidden" name="turnstileWidgetId"> |
| 145 | </div>'; |
| 146 | } |
| 147 | } |
| 148 |