| 1 |
<?php |
| 2 |
namespace MailPoet\Form\Block; |
| 3 |
|
| 4 |
if(!defined('ABSPATH')) exit; |
| 5 |
|
| 6 |
class Select extends Base { |
| 7 |
|
| 8 |
static function render($block) { |
| 9 |
$html = ''; |
| 10 |
|
| 11 |
$field_name = 'data[' . static::getFieldName($block) . ']'; |
| 12 |
$field_validation = static::getInputValidation($block); |
| 13 |
$automation_id = ($block['id'] == 'status') ? 'data-automation-id="form_status"' : ''; |
| 14 |
$html .= '<p class="mailpoet_paragraph">'; |
| 15 |
$html .= static::renderLabel($block); |
| 16 |
$html .= '<select class="mailpoet_select" name="'.$field_name.'" ' . $automation_id . '>'; |
| 17 |
|
| 18 |
if(isset($block['params']['label_within']) && $block['params']['label_within']) { |
| 19 |
$html .= '<option value="">'.static::getFieldLabel($block).'</option>'; |
| 20 |
} else { |
| 21 |
if(empty($block['params']['required']) || !$block['params']['required']) { |
| 22 |
$html .= '<option value="">-</option>'; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
$options = (!empty($block['params']['values']) |
| 27 |
? $block['params']['values'] |
| 28 |
: array() |
| 29 |
); |
| 30 |
|
| 31 |
foreach($options as $option) { |
| 32 |
if(!empty($option['is_hidden'])) { |
| 33 |
continue; |
| 34 |
} |
| 35 |
|
| 36 |
$is_selected = ( |
| 37 |
(isset($option['is_checked']) && $option['is_checked']) |
| 38 |
|| |
| 39 |
(self::getFieldValue($block) === $option['value']) |
| 40 |
) ? ' selected="selected"' : ''; |
| 41 |
|
| 42 |
$is_disabled = (!empty($option['is_disabled'])) ? ' disabled="disabled"' : ''; |
| 43 |
|
| 44 |
if(is_array($option['value'])) { |
| 45 |
$value = key($option['value']); |
| 46 |
$label = reset($option['value']); |
| 47 |
} else { |
| 48 |
$value = $option['value']; |
| 49 |
$label = $option['value']; |
| 50 |
} |
| 51 |
|
| 52 |
$html .= '<option value="'.$value.'"' . $is_selected . $is_disabled . '>'; |
| 53 |
$html .= esc_attr($label); |
| 54 |
$html .= '</option>'; |
| 55 |
} |
| 56 |
$html .= '</select>'; |
| 57 |
|
| 58 |
$html .= '</p>'; |
| 59 |
|
| 60 |
return $html; |
| 61 |
} |
| 62 |
} |