Keine Beschreibung

BlockRendererHelper.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace MailPoet\Form\Block;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Form\Util\FieldNameObfuscator;
  5. use MailPoet\Models\ModelValidator;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. /**
  8. * This class still covers several responsibilities and could be further refactored
  9. * @package MailPoet\Form\Block
  10. */
  11. class BlockRendererHelper {
  12. /** @var FieldNameObfuscator */
  13. private $fieldNameObfuscator;
  14. /** @var WPFunctions */
  15. protected $wp;
  16. public function __construct(
  17. FieldNameObfuscator $fieldNameObfuscator,
  18. WPFunctions $wp
  19. ) {
  20. $this->fieldNameObfuscator = $fieldNameObfuscator;
  21. $this->wp = $wp;
  22. }
  23. public function getInputValidation(array $block, array $extraRules = []): string {
  24. $rules = [];
  25. $blockId = $this->wp->escAttr($block['id']);
  26. if ($blockId === 'email') {
  27. $rules['required'] = true;
  28. $rules['minlength'] = ModelValidator::EMAIL_MIN_LENGTH;
  29. $rules['maxlength'] = ModelValidator::EMAIL_MAX_LENGTH;
  30. $rules['error-message'] = __('Please specify a valid email address.', 'mailpoet');
  31. }
  32. if (($blockId === 'first_name') || ($blockId === 'last_name')) {
  33. $errorMessages = [
  34. __('Please specify a valid name', 'mailpoet'),
  35. __('Addresses in names are not permitted, please add your name instead.', 'mailpoet'),
  36. ];
  37. $rules['names'] = '[' . implode(',', array_map(function (string $errorMessage): string {
  38. return htmlspecialchars((string)json_encode($errorMessage), ENT_QUOTES);
  39. }, $errorMessages)) . ']';
  40. }
  41. if ($blockId === 'segments') {
  42. $rules['required'] = true;
  43. $rules['mincheck'] = 1;
  44. $rules['group'] = $blockId;
  45. $rules['errors-container'] = '.mailpoet_error_' . $blockId;
  46. $rules['required-message'] = __('Please select a list', 'mailpoet');
  47. }
  48. if (!empty($block['params']['required'])) {
  49. $rules['required'] = true;
  50. $rules['required-message'] = __('This field is required.', 'mailpoet');
  51. }
  52. if (!empty($block['params']['validate'])) {
  53. if ($block['params']['validate'] === 'phone') {
  54. $rules['pattern'] = "^[\d\+\-\.\(\)\/\s]*$";
  55. $rules['error-message'] = __('Please specify a valid phone number', 'mailpoet');
  56. } else {
  57. $rules['type'] = $this->wp->escAttr($block['params']['validate']);
  58. }
  59. }
  60. if (in_array($block['type'], ['radio', 'checkbox'])) {
  61. $rules['group'] = 'custom_field_' . $blockId;
  62. $rules['errors-container'] = '.mailpoet_error_' . $blockId;
  63. $rules['required-message'] = __('Please select at least one option', 'mailpoet');
  64. }
  65. if ($block['type'] === 'date') {
  66. $rules['group'] = 'custom_field_' . $blockId;
  67. $rules['errors-container'] = '.mailpoet_error_' . $blockId;
  68. }
  69. $validation = [];
  70. $rules = array_merge($rules, $extraRules);
  71. if (!empty($rules)) {
  72. $rules = array_unique($rules);
  73. foreach ($rules as $rule => $value) {
  74. if (is_bool($value)) {
  75. $value = ($value) ? 'true' : 'false';
  76. }
  77. // We need to use single quotes because we need to pass array of strings as a parameter for custom validation
  78. if ($rule === 'names') {
  79. $validation[] = 'data-parsley-' . $rule . '=\'' . $value . '\'';
  80. } else {
  81. $validation[] = 'data-parsley-' . $rule . '="' . $value . '"';
  82. }
  83. }
  84. }
  85. return join(' ', $validation);
  86. }
  87. public function renderLabel(array $block, array $formSettings): string {
  88. $html = '';
  89. if (
  90. isset($block['params']['hide_label'])
  91. && $block['params']['hide_label']
  92. ) {
  93. return $html;
  94. }
  95. if (
  96. isset($block['params']['label_within'])
  97. && $block['params']['label_within']
  98. ) {
  99. return $html;
  100. }
  101. $automationId = null;
  102. if (in_array($block['id'], ['email', 'last_name', 'first_name'], true)) {
  103. $automationId = 'data-automation-id="form_' . $block['id'] . '_label" ';
  104. }
  105. if (isset($block['params']['label'])
  106. && strlen(trim($block['params']['label'])) > 0) {
  107. $html .= '<label '
  108. . 'class="mailpoet_' . $block['type'] . '_label" '
  109. . $this->renderFontStyle($formSettings, $block['styles'] ?? [])
  110. . ($automationId ?? '')
  111. . '>';
  112. $html .= htmlspecialchars($block['params']['label']);
  113. if (isset($block['params']['required']) && $block['params']['required']) {
  114. $html .= ' <span class="mailpoet_required">*</span>';
  115. }
  116. $html .= '</label>';
  117. }
  118. return $html;
  119. }
  120. public function renderFontStyle(array $formSettings, array $styles = []) {
  121. $rules = [];
  122. if (isset($formSettings['fontSize'])) {
  123. $rules[] = 'font-size: ' . trim($formSettings['fontSize']) . 'px;';
  124. $rules[] = 'line-height: ' . trim($formSettings['fontSize']) * 1.2 . 'px";';
  125. }
  126. if (isset($styles['bold'])) {
  127. $rules[] = 'font-weight: bold;';
  128. }
  129. return $rules ? 'style="' . $this->wp->escAttr(implode("", $rules)) . '"' : '';
  130. }
  131. public function renderInputPlaceholder(array $block): string {
  132. $html = '';
  133. // if the label is displayed as a placeholder,
  134. if (
  135. isset($block['params']['label_within'])
  136. && $block['params']['label_within']
  137. ) {
  138. // display only label
  139. $html .= ' placeholder="';
  140. $html .= static::getFieldLabel($block);
  141. // add an asterisk if it's a required field
  142. if (isset($block['params']['required']) && $block['params']['required']) {
  143. $html .= ' *';
  144. }
  145. $html .= '" ';
  146. }
  147. return $html;
  148. }
  149. // return field name depending on block data
  150. public function getFieldName(array $block = []): string {
  151. if ((int)$block['id'] > 0) {
  152. return 'cf_' . $block['id'];
  153. } elseif (isset($block['params']['obfuscate']) && !$block['params']['obfuscate']) {
  154. return $block['id'];
  155. } else {
  156. return $this->fieldNameObfuscator->obfuscate($block['id']);//obfuscate field name for spambots
  157. }
  158. }
  159. public function getFieldLabel(array $block = []): string {
  160. return (isset($block['params']['label'])
  161. && strlen(trim($block['params']['label'])) > 0)
  162. ? $this->wp->escHtml(trim($block['params']['label'])) : '';
  163. }
  164. public function getFieldValue($block = []) {
  165. return (isset($block['params']['value'])
  166. && strlen(trim($block['params']['value'])) > 0)
  167. ? $this->wp->escAttr(trim($block['params']['value'])) : '';
  168. }
  169. public function getInputModifiers(array $block = []): string {
  170. $modifiers = [];
  171. if (isset($block['params']['readonly']) && $block['params']['readonly']) {
  172. $modifiers[] = 'readonly';
  173. }
  174. if (isset($block['params']['disabled']) && $block['params']['disabled']) {
  175. $modifiers[] = 'disabled';
  176. }
  177. return join(' ', $modifiers);
  178. }
  179. }