|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Elements: Select. |
|
4
|
|
|
* |
|
5
|
|
|
* An Select element with the list of options. |
|
6
|
|
|
* |
|
7
|
|
|
* @since 3.18.0 |
|
8
|
|
|
* @package Wordlift |
|
9
|
|
|
* @subpackage Wordlift/admin |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Define the {@link Wordlift_Admin_Select_Element} class. |
|
14
|
|
|
* |
|
15
|
|
|
* @since 3.18.0 |
|
16
|
|
|
* @package Wordlift |
|
17
|
|
|
* @subpackage Wordlift/admin |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class Wordlift_Admin_Select_Element implements Wordlift_Admin_Element { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritdoc |
|
23
|
|
|
*/ |
|
24
|
|
|
public function render( $args ) { |
|
25
|
|
|
// Some select fields may need custom script/styles to work. |
|
26
|
|
|
$this->enqueue_resources(); |
|
27
|
|
|
|
|
28
|
|
|
// Parse the arguments and merge with default values. |
|
29
|
|
|
$params = wp_parse_args( |
|
30
|
|
|
$args, |
|
31
|
|
|
array( |
|
32
|
|
|
'id' => uniqid( 'wl-input-' ), |
|
33
|
|
|
'name' => uniqid( 'wl-input-' ), |
|
34
|
|
|
'value' => '', |
|
35
|
|
|
'class' => '', |
|
36
|
|
|
'notice' => '', |
|
37
|
|
|
'description' => false, |
|
38
|
|
|
'data' => array(), |
|
39
|
|
|
'options' => array(), |
|
40
|
|
|
) |
|
41
|
|
|
); |
|
42
|
|
|
?> |
|
43
|
|
|
<select |
|
44
|
|
|
id="<?php echo esc_attr( $params['id'] ); ?>" |
|
45
|
|
|
name="<?php echo esc_attr( $params['name'] ); ?>" |
|
46
|
|
|
class="<?php echo esc_attr( $params['class'] ); ?>" |
|
47
|
|
|
<?php echo $this->get_data_attributes( $params['data'] ); ?> |
|
48
|
|
|
> |
|
49
|
|
|
<?php $this->render_options( $params ); ?> |
|
50
|
|
|
</select> |
|
51
|
|
|
|
|
52
|
|
|
<?php |
|
53
|
|
|
// Print the notice message if there is such. |
|
54
|
|
|
$this->print_notice( $params['notice'] ); |
|
55
|
|
|
|
|
56
|
|
|
// Print the field description. |
|
57
|
|
|
echo $this->get_description( $params['description'] ); |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns html escaped description string. |
|
64
|
|
|
* Note: Only `a` tags are allowed with only `href` attributes. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string|bool $description The field description or false if not set. |
|
67
|
|
|
* |
|
68
|
|
|
* @since 3.18.0 |
|
69
|
|
|
* |
|
70
|
|
|
* @return string|void The description or null if not set. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function get_description( $description ) { |
|
73
|
|
|
// Bail if the description is not set. |
|
74
|
|
|
if ( empty( $description ) ) { |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Remove all characters except links. |
|
79
|
|
|
$filtered_descrption = wp_kses( |
|
80
|
|
|
$description, |
|
81
|
|
|
array( |
|
82
|
|
|
'a' => array( |
|
83
|
|
|
'href' => array(), |
|
84
|
|
|
), |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
return wpautop( $filtered_descrption ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Prints the field notice that will be shown on error. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $notice The notice to add. |
|
95
|
|
|
* |
|
96
|
|
|
* @since 3.18.0 |
|
97
|
|
|
* |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
|
|
public function print_notice( $notice ) { |
|
101
|
|
|
// Bail if the notice is empty. |
|
102
|
|
|
if ( empty( $notice ) ) { |
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// Print the notice message. |
|
107
|
|
|
printf( |
|
108
|
|
|
'<small class="wl-select-notices">%s</small>', |
|
109
|
|
|
$notice |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Adds data attributes to select element. |
|
115
|
|
|
* |
|
116
|
|
|
* We need to use method here, because different select elements |
|
117
|
|
|
* may have different data attributes. |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $pre_attributes Array of all data attributes. |
|
120
|
|
|
* |
|
121
|
|
|
* @since 3.18.0 |
|
122
|
|
|
* |
|
123
|
|
|
* @return string $output The data attributes or empty string |
|
124
|
|
|
*/ |
|
125
|
|
|
private function get_data_attributes( $pre_attributes ) { |
|
126
|
|
|
// The output. |
|
127
|
|
|
$output = ''; |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Filter: 'wl_admin_select_element_data_attributes' - Allow third |
|
131
|
|
|
* parties to modify the field data attrbutes. |
|
132
|
|
|
* |
|
133
|
|
|
* @since 3.18.0 |
|
134
|
|
|
* |
|
135
|
|
|
* @param array $pre_attributes Array of the field data attributes. |
|
136
|
|
|
*/ |
|
137
|
|
|
$attributes = apply_filters( 'wl_admin_select_element_data_attributes', $pre_attributes ); |
|
138
|
|
|
|
|
139
|
|
|
// Bail is there are no data attributes. |
|
140
|
|
|
if ( empty( $attributes ) ) { |
|
141
|
|
|
return $output; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// Loop throught all data attributes and build the output string. |
|
145
|
|
|
foreach ( $attributes as $name => $value ) { |
|
146
|
|
|
$output .= sprintf( |
|
147
|
|
|
'data-%s="%s" ', |
|
148
|
|
|
$name, |
|
149
|
|
|
esc_attr( $value ) |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// Finally return the output escaped. |
|
154
|
|
|
return $output; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Prints specific resources(scripts/styles) if the select requires them. |
|
159
|
|
|
* |
|
160
|
|
|
* @since 3.18.0 |
|
161
|
|
|
* |
|
162
|
|
|
* @return void |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function enqueue_resources() { |
|
165
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Abstract method that renders the select options. |
|
170
|
|
|
* |
|
171
|
|
|
* @since 3.18.0 |
|
172
|
|
|
* |
|
173
|
|
|
* @param array $params Select params. |
|
174
|
|
|
* |
|
175
|
|
|
* @return Prints the select options. |
|
176
|
|
|
*/ |
|
177
|
|
|
abstract function render_options( $params ); |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.