Width.php
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace AC\Settings\Column;
use AC\Settings;
use AC\Type\ColumnWidth;
use AC\View;
use InvalidArgumentException;
class Width extends Settings\Column
implements Settings\Header {
/**
* @var integer
*/
private $width;
/**
* @var string
*/
private $width_unit;
protected function define_options() {
return [
'width',
'width_unit' => '%',
];
}
private function get_valid_width_units() {
return [
'%' => '%',
'px' => 'px',
];
}
private function is_valid_width_unit( $width_unit ) {
return array_key_exists( $width_unit, $this->get_valid_width_units() );
}
public function create_view() {
$width = $this->create_element( 'text' )
->set_attribute( 'placeholder', __( 'Auto', 'codepress-admin-columns' ) )
->set_attribute( 'data-width-input', '' );
$unit = $this->create_element( 'radio', 'width_unit' )
->set_attribute( 'data-unit-input', '' )
->set_options( $this->get_valid_width_units() );
$section = new View( [
'width' => $width,
'unit' => $unit,
] );
$section->set_template( 'settings/setting-width' );
$view = new View( [
'label' => __( 'Width', 'codepress-admin-columns' ),
'sections' => [ $section ],
] );
return $view;
}
public function create_header_view() {
$column_width = $this->get_column_width();
return new View( [
'title' => __( 'width', 'codepress-admin-columns' ),
'content' => $column_width ? $column_width->get_value() . $column_width->get_unit() : '',
] );
}
/**
* @return ColumnWidth|null
*/
public function get_column_width() {
try {
$column_width = new ColumnWidth(
$this->width_unit,
$this->width
);
} catch ( InvalidArgumentException $e ) {
return null;
}
return $column_width;
}
/**
* @return int
*/
public function get_width() {
return $this->width;
}
/**
* @param $value
*
* @return bool
*/
public function set_width( $value ) {
// Backwards compatible for AC 2.9
if ( '' === $value ) {
$this->width = $value;
return true;
}
$value = absint( $value );
if ( $value > 0 ) {
$this->width = $value;
return true;
}
return false;
}
/**
* @return string
*/
public function get_width_unit() {
return $this->width_unit;
}
/**
* @param string $width_unit
*
* @return bool
*/
public function set_width_unit( $width_unit ) {
if ( ! $this->is_valid_width_unit( $width_unit ) ) {
return false;
}
$this->width_unit = $width_unit;
return true;
}
}