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