Completed
Push — master ( a00b53...3dc4d8 )
by
unknown
21:18
created

Select::__construct()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 4.6666
cc 8
eloc 16
nc 128
nop 1
1
<?php
2
/**
3
 * Select Field
4
 *
5
 * @package SimpleCalendar/Admin
6
 */
7
namespace SimpleCalendar\Admin\Fields;
8
9
use SimpleCalendar\Abstracts\Field;
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Select input field.
17
 *
18
 * A standard dropdown or a multiselect field.
19
 *
20
 * @since 3.0.0
21
 */
22
class Select extends Field {
23
24
	/**
25
	 * Enhanced select.
26
	 *
27
	 * @access public
28
	 * @var bool
29
	 */
30
	public $enhanced = false;
31
32
	/**
33
	 * Multiselect.
34
	 *
35
	 * @var bool
36
	 */
37
	public $multiselect = false;
38
39
	/**
40
	 * Allow void option.
41
	 *
42
	 * @access private
43
	 * @var bool
44
	 */
45
	private $allow_void = false;
46
47
	/**
48
	 * Construct.
49
	 *
50
	 * @since 3.0.0
51
	 *
52
	 * @param array $field
53
	 */
54
	public function __construct( $field ) {
55
56
		$class = 'simcal-field-select';
57
58
		$enhanced = isset( $field['enhanced'] ) ? $field['enhanced'] : '';
59
		if ( 'enhanced' == $enhanced )  {
60
			$this->enhanced = true;
61
			$class .= ' simcal-field-select-enhanced';
62
		}
63
64
		$multiselect = isset( $field['multiselect'] ) ? $field['multiselect'] : '';
65
		if ( 'multiselect' == $multiselect ) {
66
			$this->multiselect = true;
67
			$class .= ' simcal-field-multiselect';
68
		}
69
70
		if ( isset( $field['default'] ) ) {
71
			$this->default = $field['default'];
72
		}
73
74
		$this->type_class = $class;
75
76
		$allow_void = isset( $field['allow_void'] ) ? $field['allow_void'] : '';
77
		$this->allow_void = 'allow_void' == $allow_void ? true : false;
78
79
		parent::__construct( $field );
80
	}
81
82
	/**
83
	 * Outputs the field markup.
84
	 *
85
	 * @since 3.0.0
86
	 */
87
	public function html() {
88
89
		if ( $this->multiselect === true && ! is_array( $this->value ) ) {
0 ignored issues
show
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
90
			$this->value = explode( ',', $this->value );
91
		}
92
93
		if ( $this->default ) {
94
			if ( empty( $this->value ) || $this->value == '' ) {
95
				$this->value = $this->default;
96
			}
97
		}
98
99
		?>
100
		<select name="<?php echo $this->name; ?><?php if ( $this->multiselect === true ) { echo '[]'; } ?>"
0 ignored issues
show
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
101
		        id="<?php echo $this->id; ?>"
102
		        style="<?php echo $this->style; ?>"
103
		        class="<?php echo $this->class; ?>"
104
				<?php echo $this->attributes; ?>
105
				<?php echo ( $this->multiselect === true ) ? ' multiple="multiple"' : ''; ?>>
106
			<?php
107
108
			if ( $this->allow_void === true ) {
0 ignored issues
show
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
109
				echo '<option value=""' . selected( '', $this->value, false ) . '></option>';
110
			}
111
112
			foreach ( $this->options as $option => $name ) {
113
				if ( is_array( $this->value ) ) {
114
					$selected =	selected( in_array( $option, $this->value ), true, false );
0 ignored issues
show
introduced by
Expected 1 space after "="; 5 found
Loading history...
115
				} else {
116
					$selected = selected( $this->value, trim( strval( $option ) ), false );
117
				}
118
				echo '<option value="' . $option . '" ' . $selected . '>' . esc_attr( $name ) . '</option>';
119
			}
120
121
			?>
122
		</select>
123
		<?php
124
125
		echo $this->tooltip;
126
127
		if ( ! empty( $this->description ) ) {
128
			echo '<p class="description">' . wp_kses_post( $this->description ) . '</p>';
129
		}
130
131
	}
132
133
}
134