Input.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class AC_Form_Element_Input extends AC_Form_Element { |
| 8 | |
| 9 | protected function is_valid_type( $type ) { |
| 10 | $valid_types = array( |
| 11 | 'hidden', |
| 12 | 'text', |
| 13 | 'number', |
| 14 | 'email', |
| 15 | 'radio', |
| 16 | 'checkbox', |
| 17 | ); |
| 18 | |
| 19 | return in_array( $type, $valid_types ); |
| 20 | } |
| 21 | |
| 22 | public function render() { |
| 23 | $template = '<input %s>%s'; |
| 24 | |
| 25 | $attributes = $this->get_attributes(); |
| 26 | $attributes['name'] = $this->get_name(); |
| 27 | $attributes['id'] = $this->get_id(); |
| 28 | $attributes['value'] = $this->get_value(); |
| 29 | $attributes['type'] = $this->get_type(); |
| 30 | |
| 31 | return sprintf( $template, $this->get_attributes_as_string( $attributes ), $this->render_description() ); |
| 32 | } |
| 33 | |
| 34 | public function get_type() { |
| 35 | $type = $this->get_attribute( 'type' ); |
| 36 | |
| 37 | if ( ! $type ) { |
| 38 | return 'text'; |
| 39 | } |
| 40 | |
| 41 | return strtolower( $type ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param string $type |
| 46 | * |
| 47 | * @return $this |
| 48 | */ |
| 49 | public function set_type( $type ) { |
| 50 | if ( $this->is_valid_type( $type ) ) { |
| 51 | $this->set_attribute( 'type', $type ); |
| 52 | } |
| 53 | |
| 54 | return $this; |
| 55 | } |
| 56 | |
| 57 | } |
| 58 |