| 1 |
<?php |
| 2 |
|
| 3 |
namespace forge12\contactform7\CF7DoubleOptIn { |
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
require_once('HTMLSelectOption.class.php'); |
| 9 |
|
| 10 |
/** |
| 11 |
* HTMLSelect converts an given array to a HTML Option List. |
| 12 |
* |
| 13 |
* @package forge12\contactform7\CF7DoubleOptIn |
| 14 |
*/ |
| 15 |
class HTMLSelect |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Create a new Select |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
private $options = array(); |
| 22 |
|
| 23 |
/** |
| 24 |
* Stores the attributes like class, id ... for the select field |
| 25 |
*/ |
| 26 |
private $attributes = array(); |
| 27 |
|
| 28 |
/** |
| 29 |
* Stores the identifier name for the select field. |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
private $name = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* Create a new option list |
| 36 |
*/ |
| 37 |
public function __construct($name, $options = array(), $attributes = array()) |
| 38 |
{ |
| 39 |
$this->setName($name); |
| 40 |
$this->setAttributes($attributes); |
| 41 |
$this->setOptions($options); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Set the name for the select field |
| 46 |
* @param string $name |
| 47 |
*/ |
| 48 |
private function setName($name) |
| 49 |
{ |
| 50 |
$this->name = $name; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Set additional attributes like class, id, etc. |
| 55 |
* @param array $attributes |
| 56 |
*/ |
| 57 |
private function setAttributes(array $attributes) |
| 58 |
{ |
| 59 |
if (empty($attributes)) { |
| 60 |
return; |
| 61 |
} |
| 62 |
$this->attributes = $attributes; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get a list of attributes |
| 67 |
* @param $key |
| 68 |
* @return array|mixed |
| 69 |
*/ |
| 70 |
private function getAttributeByKey($key) |
| 71 |
{ |
| 72 |
if (isset($this->attributes[$key])) { |
| 73 |
return $this->attributes[$key]; |
| 74 |
} |
| 75 |
return array(); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Return a rendered HTML Select list |
| 80 |
* @return string |
| 81 |
*/ |
| 82 |
public function get() |
| 83 |
{ |
| 84 |
$attr = array(); |
| 85 |
|
| 86 |
foreach ($this->attributes as $key => $value) { |
| 87 |
$attr[] = esc_attr($key) . '="' . esc_attr(implode(' ', $value)) . '"'; |
| 88 |
} |
| 89 |
|
| 90 |
$options = ''; |
| 91 |
foreach ($this->options as $Option/* @var HTMLSelectOption $Option */) { |
| 92 |
$options .= $Option->get(); |
| 93 |
} |
| 94 |
|
| 95 |
return '<select name="' . esc_attr($this->name) . '" ' . implode(' ', $attr) . '>' . $options . '</select>'; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Check if an option exists. |
| 100 |
* @param $key |
| 101 |
* @return bool |
| 102 |
*/ |
| 103 |
private function isOption($key) |
| 104 |
{ |
| 105 |
if (isset($this->options[$key])) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the given option by the key |
| 113 |
* @param string|int |
| 114 |
* |
| 115 |
* @return HTMLSelectOption|null |
| 116 |
*/ |
| 117 |
public function getOptionByKey($key) |
| 118 |
{ |
| 119 |
if ($this->isOption($key)) { |
| 120 |
return $this->options[$key]; |
| 121 |
} |
| 122 |
return null; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Set an Option selected by the given key. |
| 127 |
* @param $key |
| 128 |
*/ |
| 129 |
public function setOptionSelectedByKey($key) |
| 130 |
{ |
| 131 |
if ($this->isOption($key)) { |
| 132 |
/** |
| 133 |
* Reset the select value for all options |
| 134 |
*/ |
| 135 |
foreach ($this->options as $optionKey => $Option/** @var HTMLSelectOption $Option */) { |
| 136 |
if ($key == $optionKey) { |
| 137 |
$Option->setSelected(true); |
| 138 |
} else { |
| 139 |
$Option->setSelected(false); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Add a new option to the select list |
| 147 |
* @param $key |
| 148 |
* @param $value |
| 149 |
* @param bool $selected |
| 150 |
* @throws \Exception |
| 151 |
*/ |
| 152 |
public function addOption($key, $value, bool $selected = false) |
| 153 |
{ |
| 154 |
if (empty($key)) { |
| 155 |
throw new \Exception('HTMLSelect:addOption - key must have a value'); |
| 156 |
} |
| 157 |
if (empty($value)) { |
| 158 |
throw new \Exception('HTMLSelect:addOption - value must have a value'); |
| 159 |
} |
| 160 |
|
| 161 |
$this->options[$key] = new HTMLSelectOption($key, $value, $selected); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* @param array $options |
| 166 |
*/ |
| 167 |
public function setOptions(array $options) |
| 168 |
{ |
| 169 |
foreach ($options as $key => $value) { |
| 170 |
try { |
| 171 |
$this->addOption($key, $value, false); |
| 172 |
} catch (\Exception $e) { |
| 173 |
echo $e->getMessage(); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |