Element.php
290 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | abstract class AC_Form_Element { |
| 8 | |
| 9 | /** |
| 10 | * @var array |
| 11 | */ |
| 12 | protected $attributes = array(); |
| 13 | |
| 14 | /** |
| 15 | * Options for element like select |
| 16 | * |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $options = array(); |
| 20 | |
| 21 | /** |
| 22 | * The elements value |
| 23 | * |
| 24 | * @var mixed |
| 25 | */ |
| 26 | protected $value; |
| 27 | |
| 28 | /** |
| 29 | * Label |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $label; |
| 34 | |
| 35 | /** |
| 36 | * Extra description |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | protected $description; |
| 41 | |
| 42 | /** |
| 43 | * Setup element with base name and id |
| 44 | * |
| 45 | * @param string $name |
| 46 | * @param array $options |
| 47 | */ |
| 48 | public function __construct( $name, array $options = array() ) { |
| 49 | $this->set_name( $name ); |
| 50 | $this->set_id( $name ); |
| 51 | $this->set_options( $options ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @return string|false |
| 56 | */ |
| 57 | protected function render_description() { |
| 58 | if ( ! $this->get_description() ) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | $template = '<p class="help-msg">%s</p>'; |
| 63 | |
| 64 | return sprintf( $template, $this->get_description() ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Render this element |
| 69 | * |
| 70 | * @return string |
| 71 | */ |
| 72 | abstract public function render(); |
| 73 | |
| 74 | /** |
| 75 | * @param $key |
| 76 | * |
| 77 | * @return string|false |
| 78 | */ |
| 79 | public function get_attribute( $key ) { |
| 80 | if ( ! isset( $this->attributes[ $key ] ) ) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | return trim( $this->attributes[ $key ] ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param string $key |
| 89 | * @param string $value |
| 90 | * |
| 91 | * @return $this |
| 92 | */ |
| 93 | public function set_attribute( $key, $value ) { |
| 94 | if ( 'value' === $key ) { |
| 95 | $this->set_value( $value ); |
| 96 | |
| 97 | return $this; |
| 98 | } |
| 99 | |
| 100 | $this->attributes[ $key ] = $value; |
| 101 | |
| 102 | return $this; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @return array |
| 107 | */ |
| 108 | public function get_attributes() { |
| 109 | return $this->attributes; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param array $attributes |
| 114 | * |
| 115 | * @return $this |
| 116 | */ |
| 117 | public function set_attributes( array $attributes ) { |
| 118 | foreach ( $attributes as $key => $value ) { |
| 119 | $this->set_attribute( $key, $value ); |
| 120 | } |
| 121 | |
| 122 | return $this; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get attributes as string |
| 127 | * |
| 128 | * @param array $attributes |
| 129 | * |
| 130 | * @return string |
| 131 | */ |
| 132 | protected function get_attributes_as_string( array $attributes ) { |
| 133 | $output = array(); |
| 134 | |
| 135 | foreach ( $attributes as $key => $value ) { |
| 136 | $output[] = $this->get_attribute_as_string( $key, $value ); |
| 137 | } |
| 138 | |
| 139 | return implode( ' ', $output ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Render an attribute |
| 144 | * |
| 145 | * @param string $key |
| 146 | * @param string $value |
| 147 | * |
| 148 | * @return string |
| 149 | */ |
| 150 | protected function get_attribute_as_string( $key, $value = null ) { |
| 151 | if ( null === $value ) { |
| 152 | $value = $this->get_attribute( $key ); |
| 153 | } |
| 154 | |
| 155 | return ac_helper()->html->get_attribute_as_string( $key, $value ); |
| 156 | } |
| 157 | |
| 158 | public function get_name() { |
| 159 | return $this->get_attribute( 'name' ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @param string $name |
| 164 | * |
| 165 | * @return $this |
| 166 | */ |
| 167 | public function set_name( $name ) { |
| 168 | return $this->set_attribute( 'name', $name ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @return false|string |
| 173 | */ |
| 174 | public function get_id() { |
| 175 | return $this->get_attribute( 'id' ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @param string $id |
| 180 | * |
| 181 | * @return $this |
| 182 | */ |
| 183 | public function set_id( $id ) { |
| 184 | return $this->set_attribute( 'id', $id ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @return mixed |
| 189 | */ |
| 190 | public function get_value() { |
| 191 | return $this->value; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @param mixed $value |
| 196 | * |
| 197 | * @return $this |
| 198 | */ |
| 199 | public function set_value( $value ) { |
| 200 | $this->value = $value; |
| 201 | |
| 202 | return $this; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @param string $class |
| 207 | * |
| 208 | * @return $this |
| 209 | */ |
| 210 | public function set_class( $class ) { |
| 211 | $this->set_attribute( 'class', $class ); |
| 212 | |
| 213 | return $this; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @param string $class |
| 218 | * |
| 219 | * @return $this |
| 220 | */ |
| 221 | public function add_class( $class ) { |
| 222 | $parts = explode( ' ', (string) $this->get_attribute( 'class' ) ); |
| 223 | $parts[] = $class; |
| 224 | |
| 225 | $this->set_class( implode( ' ', $parts ) ); |
| 226 | |
| 227 | return $this; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @return string |
| 232 | */ |
| 233 | public function get_label() { |
| 234 | return $this->label; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @param string $label |
| 239 | * |
| 240 | * @return $this |
| 241 | */ |
| 242 | public function set_label( $label ) { |
| 243 | $this->label = $label; |
| 244 | |
| 245 | return $this; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * @param array $options |
| 250 | * |
| 251 | * @return $this |
| 252 | */ |
| 253 | public function set_options( array $options ) { |
| 254 | $this->options = $options; |
| 255 | |
| 256 | return $this; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * @return array |
| 261 | */ |
| 262 | public function get_options() { |
| 263 | return $this->options; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @return string |
| 268 | */ |
| 269 | public function get_description() { |
| 270 | return $this->description; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @return $this |
| 275 | */ |
| 276 | public function set_description( $description ) { |
| 277 | $this->description = $description; |
| 278 | |
| 279 | return $this; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * @return string |
| 284 | */ |
| 285 | public function __toString() { |
| 286 | return $this->render(); |
| 287 | } |
| 288 | |
| 289 | } |
| 290 |