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
Segment.php
189 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\BlockWrapperRenderer; |
| 9 | use MailPoet\Segments\SegmentsRepository; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | |
| 12 | class Segment { |
| 13 | |
| 14 | /** @var BlockRendererHelper */ |
| 15 | private $rendererHelper; |
| 16 | |
| 17 | /** @var WPFunctions */ |
| 18 | private $wp; |
| 19 | |
| 20 | /** @var BlockWrapperRenderer */ |
| 21 | private $wrapper; |
| 22 | |
| 23 | /** @var SegmentsRepository */ |
| 24 | private $segmentsRepository; |
| 25 | |
| 26 | public function __construct( |
| 27 | BlockRendererHelper $rendererHelper, |
| 28 | BlockWrapperRenderer $wrapper, |
| 29 | WPFunctions $wp, |
| 30 | SegmentsRepository $segmentsRepository |
| 31 | ) { |
| 32 | $this->rendererHelper = $rendererHelper; |
| 33 | $this->wrapper = $wrapper; |
| 34 | $this->wp = $wp; |
| 35 | $this->segmentsRepository = $segmentsRepository; |
| 36 | } |
| 37 | |
| 38 | public function render(array $block, array $formSettings, ?int $formId = null): string { |
| 39 | if (($block['params']['display_mode'] ?? null) === 'manage_subscription_choices') { |
| 40 | return $this->renderManageSubscriptionChoices($block, $formSettings, $formId); |
| 41 | } |
| 42 | |
| 43 | $html = ''; |
| 44 | |
| 45 | $fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']'; |
| 46 | $fieldValidation = $this->rendererHelper->getInputValidation($block, [], $formId); |
| 47 | |
| 48 | // Add fieldset around the checkboxes |
| 49 | $html .= '<fieldset>'; |
| 50 | $html .= $this->rendererHelper->renderLegend($block, $formSettings); |
| 51 | |
| 52 | $options = (!empty($block['params']['values']) |
| 53 | ? $block['params']['values'] |
| 54 | : [] |
| 55 | ); |
| 56 | |
| 57 | $options = array_map(function ($option) { |
| 58 | $option['id'] = intval($option['id']); |
| 59 | return $option; |
| 60 | }, $options); |
| 61 | $segmentsNamesMap = $this->getSegmentsNames($options); |
| 62 | |
| 63 | foreach ($options as $option) { |
| 64 | if (!isset($option['id']) || !isset($segmentsNamesMap[$option['id']])) continue; |
| 65 | |
| 66 | $id = $this->wp->wpUniqueId('mailpoet_segment_'); |
| 67 | $isChecked = (isset($option['is_checked']) && $option['is_checked']) ? 'checked="checked"' : ''; |
| 68 | |
| 69 | $html .= '<label class="mailpoet_checkbox_label" for="' . $id . '" ' |
| 70 | . $this->rendererHelper->renderFontStyle($formSettings) |
| 71 | . '>'; |
| 72 | $html .= '<input type="checkbox" class="mailpoet_checkbox" '; |
| 73 | $html .= 'id="' . $id . '" '; |
| 74 | $html .= 'name="' . $fieldName . '[]" '; |
| 75 | $html .= 'value="' . $option['id'] . '" ' . $isChecked . ' '; |
| 76 | $html .= $fieldValidation; |
| 77 | $html .= ' /> ' . $this->wp->escAttr($segmentsNamesMap[$option['id']]); |
| 78 | $html .= '</label>'; |
| 79 | } |
| 80 | |
| 81 | $html .= $this->rendererHelper->renderErrorsContainer($block, $formId); |
| 82 | |
| 83 | // End fieldset around checkboxes |
| 84 | $html .= '</fieldset>'; |
| 85 | |
| 86 | return $this->wrapper->render($block, $html); |
| 87 | } |
| 88 | |
| 89 | private function renderManageSubscriptionChoices(array $block, array $formSettings, ?int $formId = null): string { |
| 90 | $options = $this->getManageSubscriptionOptions($block['params']['values'] ?? []); |
| 91 | if (!$options) { |
| 92 | return ''; |
| 93 | } |
| 94 | |
| 95 | $html = '<fieldset class="mailpoet-manage-subscription-lists" data-automation-id="manage_subscription_lists">'; |
| 96 | $html .= $this->rendererHelper->renderLegend($block, $formSettings); |
| 97 | if (!empty($block['params']['description'])) { |
| 98 | $html .= '<p class="mailpoet-manage-subscription-lists-description">' . $this->wp->escHtml($block['params']['description']) . '</p>'; |
| 99 | } |
| 100 | |
| 101 | foreach ($options as $option) { |
| 102 | $segmentId = $option['id']; |
| 103 | $name = $option['name']; |
| 104 | $description = $option['public_description']; |
| 105 | $yesId = $this->wp->wpUniqueId('mailpoet_segment_choice_'); |
| 106 | $noId = $this->wp->wpUniqueId('mailpoet_segment_choice_'); |
| 107 | $fieldsetLabelId = 'mailpoet_segment_choice_' . $segmentId . '_label'; |
| 108 | $fieldsetDescriptionId = 'mailpoet_segment_choice_' . $segmentId . '_description'; |
| 109 | $describedBy = $description !== '' ? ' aria-describedby="' . $this->wp->escAttr($fieldsetDescriptionId) . '"' : ''; |
| 110 | $yesChecked = $option['is_checked'] ? ' checked="checked"' : ''; |
| 111 | $noChecked = $option['is_checked'] ? '' : ' checked="checked"'; |
| 112 | |
| 113 | $html .= '<div class="mailpoet-manage-subscription-list-row" data-automation-id="manage_subscription_list_' . $this->wp->escAttr($segmentId) . '">'; |
| 114 | $html .= '<div class="mailpoet-manage-subscription-list-copy">'; |
| 115 | $html .= '<div class="mailpoet-manage-subscription-list-name" id="' . $this->wp->escAttr($fieldsetLabelId) . '">' . $this->wp->escHtml($name) . '</div>'; |
| 116 | if ($description !== '') { |
| 117 | $html .= '<div class="mailpoet-manage-subscription-list-description" id="' . $this->wp->escAttr($fieldsetDescriptionId) . '">' . $this->wp->escHtml($description) . '</div>'; |
| 118 | } |
| 119 | $html .= '</div>'; |
| 120 | |
| 121 | $html .= '<fieldset class="mailpoet-manage-subscription-list-choice" aria-labelledby="' . $this->wp->escAttr($fieldsetLabelId) . '"' . $describedBy . '>'; |
| 122 | $html .= '<legend class="mailpoet-manage-subscription-choice-legend">' . $this->wp->escHtml(sprintf( |
| 123 | // translators: %s is the name of a mailing list. |
| 124 | __('Receive %s?', 'mailpoet'), |
| 125 | $name |
| 126 | )) . '</legend>'; |
| 127 | |
| 128 | $html .= '<label class="mailpoet-manage-subscription-choice-label" for="' . $this->wp->escAttr($yesId) . '">'; |
| 129 | $html .= '<input type="radio" class="mailpoet_radio" id="' . $this->wp->escAttr($yesId) . '" name="data[segment_choices][' . $this->wp->escAttr($segmentId) . ']" value="subscribed"' . $yesChecked . ' data-automation-id="manage_subscription_list_' . $this->wp->escAttr($segmentId) . '_yes" />'; |
| 130 | $html .= '<span>' . $this->wp->escHtml(__('Yes', 'mailpoet')) . '</span>'; |
| 131 | $html .= '</label>'; |
| 132 | |
| 133 | $html .= '<label class="mailpoet-manage-subscription-choice-label" for="' . $this->wp->escAttr($noId) . '">'; |
| 134 | $html .= '<input type="radio" class="mailpoet_radio" id="' . $this->wp->escAttr($noId) . '" name="data[segment_choices][' . $this->wp->escAttr($segmentId) . ']" value="unsubscribed"' . $noChecked . ' data-automation-id="manage_subscription_list_' . $this->wp->escAttr($segmentId) . '_no" />'; |
| 135 | $html .= '<span>' . $this->wp->escHtml(__('No', 'mailpoet')) . '</span>'; |
| 136 | $html .= '</label>'; |
| 137 | $html .= '</fieldset>'; |
| 138 | $html .= '</div>'; |
| 139 | } |
| 140 | |
| 141 | $html .= $this->rendererHelper->renderErrorsContainer($block, $formId); |
| 142 | $html .= '</fieldset>'; |
| 143 | |
| 144 | return $this->wrapper->render($block, $html); |
| 145 | } |
| 146 | |
| 147 | private function getManageSubscriptionOptions($values): array { |
| 148 | if (!is_array($values)) { |
| 149 | return []; |
| 150 | } |
| 151 | |
| 152 | $options = []; |
| 153 | foreach ($values as $value) { |
| 154 | if (!is_array($value) || empty($value['id']) || !isset($value['name'])) { |
| 155 | continue; |
| 156 | } |
| 157 | $idValue = $value['id']; |
| 158 | $nameValue = $value['name']; |
| 159 | $descriptionValue = $value['public_description'] ?? ''; |
| 160 | |
| 161 | if ((!is_int($idValue) && !is_string($idValue)) || !is_scalar($nameValue)) { |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | $id = (int)$idValue; |
| 166 | if ($id <= 0) { |
| 167 | continue; |
| 168 | } |
| 169 | $options[] = [ |
| 170 | 'id' => $id, |
| 171 | 'name' => (string)$nameValue, |
| 172 | 'public_description' => is_scalar($descriptionValue) ? trim((string)$descriptionValue) : '', |
| 173 | 'is_checked' => !empty($value['is_checked']), |
| 174 | ]; |
| 175 | } |
| 176 | return $options; |
| 177 | } |
| 178 | |
| 179 | private function getSegmentsNames($values): array { |
| 180 | $ids = array_column($values, 'id'); |
| 181 | $segments = $this->segmentsRepository->findByIds($ids); |
| 182 | $namesMap = []; |
| 183 | foreach ($segments as $segment) { |
| 184 | $namesMap[$segment->getId()] = $segment->getName(); |
| 185 | } |
| 186 | return $namesMap; |
| 187 | } |
| 188 | } |
| 189 |