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
Date.php
227 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 | use MailPoetVendor\Carbon\CarbonImmutable; |
| 12 | |
| 13 | class Date { |
| 14 | |
| 15 | /** @var BlockRendererHelper */ |
| 16 | private $rendererHelper; |
| 17 | |
| 18 | /** @var BlockWrapperRenderer */ |
| 19 | private $wrapper; |
| 20 | |
| 21 | /** @var BlockStylesRenderer */ |
| 22 | private $blockStylesRenderer; |
| 23 | |
| 24 | /** @var WPFunctions */ |
| 25 | private $wp; |
| 26 | |
| 27 | public function __construct( |
| 28 | BlockRendererHelper $rendererHelper, |
| 29 | BlockStylesRenderer $blockStylesRenderer, |
| 30 | BlockWrapperRenderer $wrapper, |
| 31 | WPFunctions $wp |
| 32 | ) { |
| 33 | $this->rendererHelper = $rendererHelper; |
| 34 | $this->wrapper = $wrapper; |
| 35 | $this->blockStylesRenderer = $blockStylesRenderer; |
| 36 | $this->wp = $wp; |
| 37 | } |
| 38 | |
| 39 | public function render(array $block, array $formSettings, ?int $formId = null): string { |
| 40 | $html = ''; |
| 41 | $html .= $this->rendererHelper->renderLabel($block, $formSettings); |
| 42 | $html .= $this->renderDateSelect($formId, $block, $formSettings); |
| 43 | return $this->wrapper->render($block, $html); |
| 44 | } |
| 45 | |
| 46 | private function renderDateSelect(?int $formId, array $block = [], $formSettings = []): string { |
| 47 | $html = ''; |
| 48 | |
| 49 | $fieldName = 'data[' . $this->rendererHelper->getFieldName($block) . ']'; |
| 50 | |
| 51 | $dateFormats = $this->getDateFormats(); |
| 52 | |
| 53 | // automatically select first date format |
| 54 | $dateFormat = $dateFormats[$block['params']['date_type']][0]; |
| 55 | |
| 56 | // set date format if specified |
| 57 | if ( |
| 58 | isset($block['params']['date_format']) |
| 59 | && strlen(trim($block['params']['date_format'])) > 0 |
| 60 | ) { |
| 61 | $dateFormat = $block['params']['date_format']; |
| 62 | } |
| 63 | |
| 64 | // generate an array of selectors based on date format |
| 65 | $dateSelectors = explode('/', $dateFormat); |
| 66 | |
| 67 | foreach ($dateSelectors as $dateSelector) { |
| 68 | if ($dateSelector === 'DD') { |
| 69 | $html .= '<select class="mailpoet_date_day" '; |
| 70 | $html .= ' style="' . $this->wp->escAttr($this->blockStylesRenderer->renderForSelect([], $formSettings)) . '"'; |
| 71 | $html .= $this->rendererHelper->getInputValidation($block, [ |
| 72 | 'required-message' => $this->wp->escAttr(__('Please select a day', 'mailpoet')), |
| 73 | ], $formId); |
| 74 | $html .= 'name="' . $fieldName . '[day]" placeholder="' . $this->wp->escAttr(__('Day', 'mailpoet')) . '">'; |
| 75 | $html .= $this->getDays($block); |
| 76 | $html .= '</select>'; |
| 77 | } else if ($dateSelector === 'MM') { |
| 78 | $html .= '<select class="mailpoet_select mailpoet_date_month" data-automation-id="form_date_month" '; |
| 79 | $html .= ' style="' . $this->wp->escAttr($this->blockStylesRenderer->renderForSelect([], $formSettings)) . '"'; |
| 80 | $html .= $this->rendererHelper->getInputValidation($block, [ |
| 81 | 'required-message' => $this->wp->escAttr(__('Please select a month', 'mailpoet')), |
| 82 | ], $formId); |
| 83 | $html .= 'name="' . $fieldName . '[month]" placeholder="' . $this->wp->escAttr(__('Month', 'mailpoet')) . '">'; |
| 84 | $html .= $this->getMonths($block); |
| 85 | $html .= '</select>'; |
| 86 | } else if ($dateSelector === 'YYYY') { |
| 87 | $html .= '<select class="mailpoet_date_year" data-automation-id="form_date_year" '; |
| 88 | $html .= ' style="' . $this->wp->escAttr($this->blockStylesRenderer->renderForSelect([], $formSettings)) . '"'; |
| 89 | $html .= $this->rendererHelper->getInputValidation($block, [ |
| 90 | 'required-message' => $this->wp->escAttr(__('Please select a year', 'mailpoet')), |
| 91 | ], $formId); |
| 92 | $html .= 'name="' . $fieldName . '[year]" placeholder="' . $this->wp->escAttr(__('Year', 'mailpoet')) . '">'; |
| 93 | $html .= $this->getYears($block); |
| 94 | $html .= '</select>'; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | $html .= $this->rendererHelper->renderErrorsContainer($block, $formId); |
| 99 | |
| 100 | return $html; |
| 101 | } |
| 102 | |
| 103 | public function getDateTypes(): array { |
| 104 | return [ |
| 105 | 'year_month_day' => $this->wp->escHtml(__('Year, month, day', 'mailpoet')), |
| 106 | 'year_month' => $this->wp->escHtml(__('Year, month', 'mailpoet')), |
| 107 | 'month' => $this->wp->escHtml(__('Month (January, February,...)', 'mailpoet')), |
| 108 | 'year' => $this->wp->escHtml(__('Year', 'mailpoet')), |
| 109 | ]; |
| 110 | } |
| 111 | |
| 112 | public function getDateFormats(): array { |
| 113 | return [ |
| 114 | 'year_month_day' => ['MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY/MM/DD'], |
| 115 | 'year_month' => ['MM/YYYY', 'YYYY/MM'], |
| 116 | 'year' => ['YYYY'], |
| 117 | 'month' => ['MM'], |
| 118 | ]; |
| 119 | } |
| 120 | |
| 121 | public function getMonthNames(): array { |
| 122 | return [__('January', 'mailpoet'), __('February', 'mailpoet'), __('March', 'mailpoet'), __('April', 'mailpoet'), |
| 123 | __('May', 'mailpoet'), __('June', 'mailpoet'), __('July', 'mailpoet'), __('August', 'mailpoet'), __('September', 'mailpoet'), |
| 124 | __('October', 'mailpoet'), __('November', 'mailpoet'), __('December', 'mailpoet'), |
| 125 | ]; |
| 126 | } |
| 127 | |
| 128 | private function getMonths(array $block = []): string { |
| 129 | $defaults = [ |
| 130 | 'selected' => null, |
| 131 | ]; |
| 132 | |
| 133 | if (!empty($block['params']['value'])) { |
| 134 | $date = CarbonImmutable::createFromFormat('Y-m-d H:i:s', $block['params']['value']); |
| 135 | if ($date instanceof CarbonImmutable) { |
| 136 | $defaults['selected'] = (int)date('m', $date->getTimestamp()); |
| 137 | } |
| 138 | } elseif (!empty($block['params']['is_default_today'])) { |
| 139 | // is default today |
| 140 | $defaults['selected'] = (int)date('m'); |
| 141 | } |
| 142 | // merge block with defaults |
| 143 | $block = array_merge($defaults, $block); |
| 144 | |
| 145 | $monthNames = $this->getMonthNames(); |
| 146 | |
| 147 | $html = ''; |
| 148 | |
| 149 | // empty value label |
| 150 | $html .= '<option value="">' . $this->wp->escHtml(__('Month', 'mailpoet')) . '</option>'; |
| 151 | |
| 152 | for ($i = 1; $i < 13; $i++) { |
| 153 | $isSelected = ($i === $block['selected']) ? 'selected="selected"' : ''; |
| 154 | $html .= '<option value="' . $i . '" ' . $isSelected . '>'; |
| 155 | $html .= $this->wp->escHtml($monthNames[$i - 1]); |
| 156 | $html .= '</option>'; |
| 157 | } |
| 158 | |
| 159 | return $html; |
| 160 | } |
| 161 | |
| 162 | private function getYears(array $block = []): string { |
| 163 | $defaults = [ |
| 164 | 'selected' => null, |
| 165 | 'from' => (int)date('Y') - 100, |
| 166 | 'to' => (int)date('Y'), |
| 167 | ]; |
| 168 | |
| 169 | if (!empty($block['params']['value'])) { |
| 170 | $date = CarbonImmutable::createFromFormat('Y-m-d H:i:s', $block['params']['value']); |
| 171 | if ($date instanceof CarbonImmutable) { |
| 172 | $defaults['selected'] = (int)date('Y', $date->getTimestamp()); |
| 173 | } |
| 174 | } elseif (!empty($block['params']['is_default_today'])) { |
| 175 | // is default today |
| 176 | $defaults['selected'] = (int)date('Y'); |
| 177 | } |
| 178 | |
| 179 | // merge block with defaults |
| 180 | $block = array_merge($defaults, $block); |
| 181 | |
| 182 | $html = ''; |
| 183 | |
| 184 | // empty value label |
| 185 | $html .= '<option value="">' . $this->wp->escHtml(__('Year', 'mailpoet')) . '</option>'; |
| 186 | |
| 187 | // return years as an array |
| 188 | for ($i = (int)$block['to']; $i > (int)($block['from'] - 1); $i--) { |
| 189 | $isSelected = ($i === $block['selected']) ? 'selected="selected"' : ''; |
| 190 | $html .= '<option value="' . $i . '" ' . $isSelected . '>' . $i . '</option>'; |
| 191 | } |
| 192 | |
| 193 | return $html; |
| 194 | } |
| 195 | |
| 196 | private function getDays(array $block = []): string { |
| 197 | $defaults = [ |
| 198 | 'selected' => null, |
| 199 | ]; |
| 200 | if (!empty($block['params']['value'])) { |
| 201 | $date = CarbonImmutable::createFromFormat('Y-m-d H:i:s', $block['params']['value']); |
| 202 | if ($date instanceof CarbonImmutable) { |
| 203 | $defaults['selected'] = (int)date('d', $date->getTimestamp()); |
| 204 | } |
| 205 | } elseif (!empty($block['params']['is_default_today'])) { |
| 206 | // is default today |
| 207 | $defaults['selected'] = (int)date('d'); |
| 208 | } |
| 209 | |
| 210 | // merge block with defaults |
| 211 | $block = array_merge($defaults, $block); |
| 212 | |
| 213 | $html = ''; |
| 214 | |
| 215 | // empty value label |
| 216 | $html .= '<option value="">' . $this->wp->escHtml(__('Day', 'mailpoet')) . '</option>'; |
| 217 | |
| 218 | // return days as an array |
| 219 | for ($i = 1; $i < 32; $i++) { |
| 220 | $isSelected = ($i === $block['selected']) ? 'selected="selected"' : ''; |
| 221 | $html .= '<option value="' . $i . '" ' . $isSelected . '>' . $i . '</option>'; |
| 222 | } |
| 223 | |
| 224 | return $html; |
| 225 | } |
| 226 | } |
| 227 |