| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* HTMLSelectOption converts an given array to a HTML Option List. |
| 10 |
* |
| 11 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 12 |
*/ |
| 13 |
class HTMLSelectOption |
| 14 |
{ |
| 15 |
/** |
| 16 |
* The key of the option field |
| 17 |
* @var string|int |
| 18 |
*/ |
| 19 |
private $key = ''; |
| 20 |
/** |
| 21 |
* The value of the option field |
| 22 |
* @var string|int |
| 23 |
*/ |
| 24 |
private $value = ''; |
| 25 |
|
| 26 |
/** |
| 27 |
* Flag if selected or not |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
private $selected = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* Create a new option list |
| 34 |
*/ |
| 35 |
public function __construct($key, $value, $selected = false) |
| 36 |
{ |
| 37 |
$this->setKey($key); |
| 38 |
$this->setValue($value); |
| 39 |
$this->setSelected($selected); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Return the HTML String for the option value |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
public function get() |
| 47 |
{ |
| 48 |
if($this->isSelected()){ |
| 49 |
return '<option value="' . esc_attr($this->getKey()) . '" selected="selected">' . esc_html($this->getValue()) . '</option>'; |
| 50 |
}else{ |
| 51 |
return '<option value="' . esc_attr($this->getKey()) . '">' . esc_html($this->getValue()) . '</option>'; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function getValue(): string |
| 59 |
{ |
| 60 |
return $this->value; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @param string $value |
| 65 |
*/ |
| 66 |
private function setValue(string $value) |
| 67 |
{ |
| 68 |
$this->value = $value; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public function getKey(): string |
| 75 |
{ |
| 76 |
return $this->key; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param string $key |
| 81 |
*/ |
| 82 |
private function setKey(string $key) |
| 83 |
{ |
| 84 |
$this->key = $key; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
public function isSelected(): bool |
| 91 |
{ |
| 92 |
return $this->selected; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @param bool $selected |
| 97 |
*/ |
| 98 |
public function setSelected(bool $selected) |
| 99 |
{ |
| 100 |
$this->selected = $selected; |
| 101 |
} |
| 102 |
} |
| 103 |
} |