Нет описания

Renderer.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace MailPoet\Form;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Entities\FormEntity;
  5. use MailPoet\Form\Templates\FormTemplate;
  6. use MailPoet\Form\Util\CustomFonts;
  7. use MailPoet\Form\Util\Styles;
  8. use MailPoet\Settings\SettingsController;
  9. use MailPoet\Subscription\Captcha;
  10. class Renderer {
  11. /** @var Styles */
  12. private $styleUtils;
  13. /** @var SettingsController */
  14. private $settings;
  15. /** @var BlocksRenderer */
  16. private $blocksRenderer;
  17. /** @var CustomFonts */
  18. private $customFonts;
  19. public function __construct(
  20. Styles $styleUtils,
  21. SettingsController $settings,
  22. CustomFonts $customFonts,
  23. BlocksRenderer $blocksRenderer
  24. ) {
  25. $this->styleUtils = $styleUtils;
  26. $this->settings = $settings;
  27. $this->blocksRenderer = $blocksRenderer;
  28. $this->customFonts = $customFonts;
  29. }
  30. public function renderStyles(FormEntity $form, string $prefix, string $displayType): string {
  31. $this->customFonts->enqueueStyle();
  32. $html = '.mailpoet_hp_email_label{display:none!important;}'; // move honeypot field out of sight
  33. $html .= $this->styleUtils->prefixStyles($this->getCustomStyles($form), $prefix);
  34. $html .= strip_tags($this->styleUtils->renderFormSettingsStyles($form, $prefix, $displayType));
  35. return $html;
  36. }
  37. public function renderHTML(FormEntity $form = null): string {
  38. if (($form instanceof FormEntity) && !empty($form->getBody()) && is_array($form->getSettings())) {
  39. return $this->renderBlocks($form->getBody(), $form->getSettings() ?? []);
  40. }
  41. return '';
  42. }
  43. public function getCustomStyles(FormEntity $form = null): string {
  44. if (($form instanceof FormEntity) && (strlen(trim($form->getStyles() ?? '')) > 0)) {
  45. return strip_tags($form->getStyles() ?? '');
  46. } else {
  47. return FormTemplate::DEFAULT_STYLES;
  48. }
  49. }
  50. public function renderBlocks(array $blocks = [], array $formSettings = [], bool $honeypotEnabled = true, bool $captchaEnabled = true): string {
  51. // add honeypot for spambots
  52. $html = ($honeypotEnabled) ? $this->renderHoneypot() : '';
  53. foreach ($blocks as $key => $block) {
  54. if ($captchaEnabled
  55. && $block['type'] === FormEntity::SUBMIT_BLOCK_TYPE
  56. && $this->settings->get('captcha.type') === Captcha::TYPE_RECAPTCHA
  57. ) {
  58. $html .= $this->renderReCaptcha();
  59. }
  60. if (in_array($block['type'], [FormEntity::COLUMN_BLOCK_TYPE, FormEntity::COLUMNS_BLOCK_TYPE])) {
  61. $blocks = $block['body'] ?? [];
  62. $html .= $this->blocksRenderer->renderContainerBlock($block, $this->renderBlocks($blocks, $formSettings, false)) . PHP_EOL;
  63. } else {
  64. $html .= $this->blocksRenderer->renderBlock($block, $formSettings) . PHP_EOL;
  65. }
  66. }
  67. return $html;
  68. }
  69. private function renderHoneypot(): string {
  70. return '<label class="mailpoet_hp_email_label">' . __('Please leave this field empty', 'mailpoet') . '<input type="email" name="data[email]"/></label>';
  71. }
  72. private function renderReCaptcha(): string {
  73. $siteKey = $this->settings->get('captcha.recaptcha_site_token');
  74. return '<div class="mailpoet_recaptcha" data-sitekey="' . $siteKey . '">
  75. <div class="mailpoet_recaptcha_container"></div>
  76. <noscript>
  77. <div>
  78. <div style="width: 302px; height: 422px; position: relative;">
  79. <div style="width: 302px; height: 422px; position: absolute;">
  80. <iframe src="https://www.google.com/recaptcha/api/fallback?k=' . $siteKey . '" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;">
  81. </iframe>
  82. </div>
  83. </div>
  84. <div style="width: 300px; height: 60px; border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
  85. <textarea id="g-recaptcha-response" name="data[recaptcha]" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none;" >
  86. </textarea>
  87. </div>
  88. </div>
  89. </noscript>
  90. <input class="mailpoet_recaptcha_field" type="hidden" name="recaptcha">
  91. </div>';
  92. }
  93. }