Нет описания

BlockStylesRenderer.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace MailPoet\Form;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\WP\Functions as WPFunctions;
  5. class BlockStylesRenderer {
  6. /** @var WPFunctions */
  7. private $wp;
  8. public function __construct(
  9. WPFunctions $wp
  10. ) {
  11. $this->wp = $wp;
  12. }
  13. public function renderForTextInput(array $styles, array $formSettings = []): string {
  14. $rules = [];
  15. if (isset($styles['full_width']) && intval($styles['full_width'])) {
  16. $rules[] = 'width:100%;';
  17. }
  18. if (isset($styles['background_color']) && empty($styles['gradient'])) {
  19. $rules[] = "background-color:{$styles['background_color']};";
  20. }
  21. if (isset($styles['border_size']) || isset($styles['border_radius']) || isset($styles['border_color'])) {
  22. $rules[] = "border-style:solid;";
  23. }
  24. if (isset($styles['border_radius'])) {
  25. $rules[] = "border-radius:" . intval($styles['border_radius']) . "px !important;";
  26. }
  27. if (isset($styles['border_size'])) {
  28. $rules[] = "border-width:" . intval($styles['border_size']) . "px;";
  29. }
  30. if (isset($styles['border_color'])) {
  31. $rules[] = "border-color:{$styles['border_color']};";
  32. }
  33. if (isset($styles['padding'])) {
  34. $rules[] = "padding:{$styles['padding']}px;";
  35. } elseif (isset($formSettings['input_padding'])) {
  36. $rules[] = "padding:{$formSettings['input_padding']}px;";
  37. }
  38. if (isset($formSettings['alignment'])) {
  39. $rules[] = $this->convertAlignmentToMargin($formSettings['alignment']);
  40. }
  41. if (isset($styles['font_family'])) {
  42. $rules[] = "font-family:'{$styles['font_family']}';" ;
  43. } elseif (isset($formSettings['font_family'])) {
  44. $rules[] = "font-family:'{$formSettings['font_family']}';" ;
  45. }
  46. if (isset($styles['font_size'])) {
  47. $rules[] = "font-size:" . intval($styles['font_size']) . "px;";
  48. }
  49. if (isset($formSettings['fontSize']) && !isset($styles['font_size'])) {
  50. $rules[] = "font-size:" . intval($formSettings['fontSize']) . "px;";
  51. }
  52. if (isset($formSettings['fontSize']) || isset($styles['font_size'])) {
  53. $rules[] = "line-height:1.5;";
  54. $rules[] = "height:auto;";
  55. }
  56. if (isset($styles['font_color'])) {
  57. $rules[] = "color:{$styles['font_color']};";
  58. }
  59. return implode('', $rules);
  60. }
  61. public function renderForButton(array $styles, array $formSettings = []): string {
  62. $rules = [];
  63. if (!isset($styles['border_color'])) {
  64. $rules[] = "border-color:transparent;";
  65. }
  66. if (!empty($styles['gradient'])) {
  67. $rules[] = "background: {$styles['gradient']};";
  68. }
  69. if (isset($styles['bold']) && $styles['bold'] === '1') {
  70. $rules[] = "font-weight:bold;";
  71. }
  72. return $this->renderForTextInput($styles, $formSettings) . implode('', $rules);
  73. }
  74. public function renderForSelect(array $styles, array $formSettings = []): string {
  75. $rules = [];
  76. if (isset($formSettings['input_padding'])) {
  77. $rules[] = "padding:{$formSettings['input_padding']}px;";
  78. }
  79. if (isset($formSettings['alignment'])) {
  80. $rules[] = $this->convertAlignmentToMargin($formSettings['alignment']);
  81. }
  82. return implode('', $rules);
  83. }
  84. private function convertAlignmentToMargin(string $alignment): string {
  85. if ($alignment === 'right') {
  86. return 'margin: 0 0 0 auto;';
  87. }
  88. if ($alignment === 'center') {
  89. return 'margin: 0 auto;';
  90. }
  91. return 'margin: 0 auto 0 0;';
  92. }
  93. public function renderPlaceholderStyles(array $block, string $selector): string {
  94. if (
  95. isset($block['params']['label_within'])
  96. && $block['params']['label_within']
  97. && isset($block['styles']['font_color'])
  98. ) {
  99. return '<style>'
  100. . $selector . '::placeholder{'
  101. . 'color:' . $this->wp->escAttr($block['styles']['font_color']) . ';'
  102. . 'opacity: 1;'
  103. . '}'
  104. . '</style>';
  105. }
  106. return '';
  107. }
  108. }