BlockRendererHelper.php
3 weeks ago
Checkbox.php
1 year ago
Close.php
2 weeks ago
Column.php
2 months ago
Columns.php
2 months ago
Date.php
1 year ago
Divider.php
3 years ago
Heading.php
2 months ago
Html.php
1 year ago
Image.php
10 months ago
Paragraph.php
3 weeks ago
Radio.php
1 year ago
Segment.php
2 months ago
Select.php
2 months ago
Submit.php
1 year ago
Text.php
1 year ago
Textarea.php
1 year ago
index.php
3 years ago
Select.php
143 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Form\Block; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Form\BlockStylesRenderer; |
| 9 | use MailPoet\Form\BlockWrapperRenderer; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | |
| 12 | class Select { |
| 13 | |
| 14 | /** @var BlockRendererHelper */ |
| 15 | private $rendererHelper; |
| 16 | |
| 17 | /** @var WPFunctions */ |
| 18 | private $wp; |
| 19 | |
| 20 | /** @var BlockWrapperRenderer */ |
| 21 | private $wrapper; |
| 22 | |
| 23 | /** @var BlockStylesRenderer */ |
| 24 | private $blockStylesRenderer; |
| 25 | |
| 26 | public function __construct( |
| 27 | BlockRendererHelper $rendererHelper, |
| 28 | BlockWrapperRenderer $wrapper, |
| 29 | BlockStylesRenderer $blockStylesRenderer, |
| 30 | WPFunctions $wp |
| 31 | ) { |
| 32 | $this->rendererHelper = $rendererHelper; |
| 33 | $this->wrapper = $wrapper; |
| 34 | $this->wp = $wp; |
| 35 | $this->blockStylesRenderer = $blockStylesRenderer; |
| 36 | } |
| 37 | |
| 38 | public function render(array $block, array $formSettings, ?int $formId = null): string { |
| 39 | $html = ''; |
| 40 | |
| 41 | $fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']'; |
| 42 | $automationId = ($block['id'] == 'status') ? ' data-automation-id="form_status"' : ''; |
| 43 | $inputId = $this->getInputId($block, $formSettings); |
| 44 | $descriptionId = $this->getDescriptionId($block, $inputId); |
| 45 | |
| 46 | if ($inputId !== '') { |
| 47 | $block['params']['input_id'] = $inputId; |
| 48 | } |
| 49 | |
| 50 | $html .= $this->rendererHelper->renderLabel($block, $formSettings); |
| 51 | if (!empty($block['params']['description'])) { |
| 52 | $html .= '<p class="mailpoet_field_description"'; |
| 53 | if ($descriptionId !== '') { |
| 54 | $html .= ' id="' . $this->wp->escAttr($descriptionId) . '"'; |
| 55 | } |
| 56 | $html .= '>' . $this->wp->escHtml($block['params']['description']) . '</p>'; |
| 57 | } |
| 58 | $html .= '<select |
| 59 | class="mailpoet_select" |
| 60 | ' . ($inputId !== '' ? 'id="' . $this->wp->escAttr($inputId) . '"' : '') . ' |
| 61 | name="' . $fieldName . '"' |
| 62 | . $automationId |
| 63 | . ($descriptionId !== '' ? ' aria-describedby="' . $this->wp->escAttr($descriptionId) . '"' : '') |
| 64 | . ' style="' . $this->wp->escAttr($this->blockStylesRenderer->renderForSelect([], $formSettings)) . '"' |
| 65 | . '>'; |
| 66 | |
| 67 | if (isset($block['params']['label_within']) && $block['params']['label_within']) { |
| 68 | $label = $this->rendererHelper->getFieldLabel($block); |
| 69 | if (!empty($block['params']['required'])) { |
| 70 | $label .= ' *'; |
| 71 | } |
| 72 | $html .= '<option value="" disabled selected hidden>' . $this->wp->escHtml($label) . '</option>'; |
| 73 | } else { |
| 74 | if (empty($block['params']['required'])) { |
| 75 | $html .= '<option value="">-</option>'; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | $options = (!empty($block['params']['values']) |
| 80 | ? $block['params']['values'] |
| 81 | : [] |
| 82 | ); |
| 83 | |
| 84 | foreach ($options as $option) { |
| 85 | if (!empty($option['is_hidden'])) { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | $isSelected = ''; |
| 90 | |
| 91 | if ($this->rendererHelper->getFieldValue($block) === $option['value']) { |
| 92 | // use selected value if it exist |
| 93 | $isSelected = ' selected="selected"'; |
| 94 | } elseif ((isset($option['is_checked']) && $option['is_checked']) && !($this->rendererHelper->getFieldValue($block))) { |
| 95 | // use default value otherwise |
| 96 | $isSelected = ' selected="selected"'; |
| 97 | } |
| 98 | |
| 99 | $isDisabled = (!empty($option['is_disabled'])) ? ' disabled="disabled"' : ''; |
| 100 | |
| 101 | if (is_array($option['value'])) { |
| 102 | $value = key($option['value']); |
| 103 | $label = reset($option['value']); |
| 104 | } else { |
| 105 | $value = $option['value']; |
| 106 | $label = $option['value']; |
| 107 | } |
| 108 | |
| 109 | $html .= '<option value="' . $this->wp->escAttr($value) . '"' . $isSelected . $isDisabled . '>'; |
| 110 | $html .= $this->wp->escAttr($label); |
| 111 | $html .= '</option>'; |
| 112 | } |
| 113 | $html .= '</select>'; |
| 114 | |
| 115 | $html .= $this->rendererHelper->renderErrorsContainer($block, $formId); |
| 116 | |
| 117 | return $this->wrapper->render($block, $html); |
| 118 | } |
| 119 | |
| 120 | private function getInputId(array $block, array $formSettings): string { |
| 121 | if (!empty($block['params']['input_id']) && is_scalar($block['params']['input_id'])) { |
| 122 | return (string)$block['params']['input_id']; |
| 123 | } |
| 124 | if (isset($formSettings['id'])) { |
| 125 | return 'form_' . (string)$block['id'] . '_' . (string)$formSettings['id']; |
| 126 | } |
| 127 | return ''; |
| 128 | } |
| 129 | |
| 130 | private function getDescriptionId(array $block, string $inputId): string { |
| 131 | if (empty($block['params']['description'])) { |
| 132 | return ''; |
| 133 | } |
| 134 | if (!empty($block['params']['description_id']) && is_scalar($block['params']['description_id'])) { |
| 135 | return (string)$block['params']['description_id']; |
| 136 | } |
| 137 | if ($inputId !== '') { |
| 138 | return $inputId . '_description'; |
| 139 | } |
| 140 | return ''; |
| 141 | } |
| 142 | } |
| 143 |