PluginProbe
Filter Everything — WordPress & WooCommerce Filters / trunk
Filter Everything — WordPress & WooCommerce Filters vtrunk
1.9.6 1.9.5 1.9.4 1.9.3 1.9.2.2 1.9.2.1 trunk 1.2.1 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.1 1.4.4 1.4.5 1.4.8 1.4.9 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 All 51 releases
filter-everything / src / FormFields / Input.php

Input.php in Filter Everything — WordPress & WooCommerce Filters trunk, at src/FormFields/Input.php

363 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FilterEverything\Filter;
4
5 if ( ! defined('ABSPATH') ) {
6 exit;
7 }
8
9 class Input {
10
11 public $validAttributes = array(
12 'id',
13 'class',
14 'name',
15 'value',
16 'onclick',
17 'disabled',
18 'selected',
19 'size',
20 'data-',
21 'placeholder',
22 'readonly',
23 'options',
24 'textarea_rows',
25 'multiple',
26 'title',
27 'button_name'
28 );
29
30 public $attributes;
31
32 public function __construct( $attributes = '' )
33 {
34 if ( $attributes ){
35 $this->setAttributes( $attributes );
36 }
37 }
38
39 public function setAttributes( $attributes )
40 {
41 if( ! is_array( $attributes ) ){
42 $attributes = array( $attributes );
43 }
44
45 $this->attributes = $this->sanitizeAttributes( $attributes );
46 }
47
48 protected function getAttribute( $key ){
49 if( isset( $this->attributes[ $key ] ) ){
50 return $this->attributes[ $key ];
51 }
52
53 return false;
54 }
55
56 public function getAttributes()
57 {
58 return $this->attributes;
59 }
60
61 protected function sanitizeAttributes( $attributes )
62 {
63 $sanitizedAttributes = [];
64 $attributes = array_change_key_case( $attributes, CASE_LOWER );
65
66 foreach ( $attributes as $key => $value ){
67 if( in_array( $key, $this->validAttributes ) ){
68 $sanitizedAttributes[ $key ] = $value;
69 }
70
71 if( mb_strpos( $key, 'data-' ) !== false ){
72 $sanitizedAttributes[ $key ] = $value;
73 }
74 }
75
76 return $sanitizedAttributes;
77
78 }
79
80 protected function renderAttributes( $skip = [] )
81 {
82 $html = '';
83
84 foreach ( (array) $this->attributes as $key => $value ) {
85 // do not sanitize attribute name because we already validated them
86 if( in_array( $key, $skip ) ){
87 continue;
88 }
89 $html .= ' ' . esc_attr($key) . '="' . esc_attr( $value ).'"';
90 }
91
92 return $html;
93 }
94
95 }
96
97 class Checkbox extends Input {
98 public function render(){
99
100 $html = '<input type="checkbox"';
101 $html .= $this->renderAttributes( array('placeholder') );
102 $html .= checked('yes', $this->getAttribute('value'), false );
103 $html .= ' />';
104
105 if( $this->getAttribute('placeholder') ){
106 $html .= '<span class="wpc-checkbox-placeholder">'.esc_html( $this->getAttribute('placeholder') ).'</span>';
107 }
108
109 return apply_filters( 'wpc_input_type_checkbox', $html, $this->getAttributes() );
110 }
111 }
112
113 class File extends Input {
114 public function render(){
115 $html = '<input type="file"';
116 $html .= $this->renderAttributes();
117 $html .= ' />';
118
119 return apply_filters( 'wpc_input_type_file', $html, $this->getAttributes() );
120 }
121 }
122
123 class Hidden extends Input {
124 public function render(){
125 $html = '<input type="hidden"';
126 $html .= $this->renderAttributes();
127 $html .= ' />'."\r\n";
128
129 return apply_filters( 'wpc_input_type_hidden', $html, $this->getAttributes() );
130 }
131 }
132
133 class Image extends Input {
134
135 }
136
137 class Password extends Input {
138 public function render(){
139 $html = '<input type="password"';
140 $html .= $this->renderAttributes();
141 $html .= ' />'."\r\n";
142
143 return apply_filters( 'wpc_input_type_password', $html, $this->getAttributes() );
144 }
145 }
146
147 class Submit extends Input {
148 public function __construct($options, $validation = []){
149 if(!is_array($options)){
150 $o = [];
151 $o['value'] = $options;
152 $o['name'] = 'submit';
153 $o['id'] = 'submit';
154 } else {
155 $o = $options;
156 }
157 parent::__construct($o, $validation);
158 }
159
160 public function render(){
161 $class = 'class';
162 if(!$this->$class){
163 $class = ' class="submit"';
164 }
165
166 $html = '<input '.$class.' type="submit"';
167 $html .= $this -> renderAttributes();
168 $html .= ' value="'.$this->value.'"';
169 $html .= ' />';
170
171 return apply_filters( 'wpc_input_type_submit', $html, $this->getAttributes() );
172 }
173 }
174
175 class Text extends Input {
176 public function render(){
177 $html = '<input type="text"';
178 $html .= $this->renderAttributes();
179 $html .= ' />'."\r\n";
180
181 return apply_filters( 'wpc_input_type_text', $html, $this->getAttributes() );
182 }
183 }
184
185 class Radio extends Input {
186 public function render(){
187
188 $html = '<ul class="wpc-radio-list" id="'.$this->getAttribute('id' ).'">';
189 $current_value = $this->getAttribute('value');
190
191 foreach ( $this->getAttribute('options' ) as $value => $label ) {
192 $html .= '<li>';
193 $html .= '<label>';
194 $html .= '<input type="radio"';
195 $html .= $this->renderAttributes( $skip = array( 'options', 'value', 'id' ) );
196 $html .= ' value="' . $value . '"';
197 if ( $value === $current_value ) {
198 $html .= ' checked="checked"';
199 }
200 $html .= ' />';
201 $html .= $label;
202 $html .= '</label>';
203 $html .= '</li>';
204 }
205
206 $html .= '</ul>';
207
208 return apply_filters( 'wpc_input_type_radio', $html, $this->getAttributes() );
209 }
210 }
211
212 class Select extends Input {
213
214 public function render(){
215 $html = '<select';
216 $html .= $this->renderAttributes( array( 'value', 'options', 'disabled' ) );
217 $html .= '>';
218
219 $html .= $this->renderDropdown( $this->getAttribute('options' ) , $this->getAttribute('value' ), $this->getAttribute('disabled' ) );
220
221 $html .= '</select>'."\r\n";
222
223 return apply_filters( 'wpc_input_type_select', $html, $this->getAttributes() );
224 }
225
226 private function renderDropdown( $options, $selected, $disabled = [] )
227 {
228 $html = '';
229 $option_group = false;
230
231 // For multidimensional array
232 foreach ( $options as $key => $option ){
233 if( isset( $option['group_label'] ) ){
234 $option_group = true;
235
236 $html .= '<optgroup label="'.$option['group_label'].'" class="'.sanitize_title( $option['group_label'] ).'">';
237 if( isset( $option['entities'] ) ){
238 $html .= $this->renderOptions( $option['entities'], $selected, $disabled );
239 }
240 $html .= '</optgroup>'."\r\n";
241 }
242 }
243
244 // In case for simple options array
245 if( ! $option_group ){
246 $html .= $this->renderOptions( $options, $selected, $disabled );
247 }
248
249 return $html;
250 }
251
252 private function renderOptions( $options, $selected = '', $disabled = [] ){
253 if( ! $options || ! is_array( $options ) ){
254 return false;
255 }
256 $html = '';
257 foreach ( $options as $value => $label ){
258 $html .= $this->renderOption( $label, $value, $selected, $disabled );
259 }
260
261 return $html;
262 }
263
264 private function renderOption( $label, $value, $selected = '', $disabled = [] ){
265 $attributes = '';
266 $attr = apply_filters( 'wpc_dropdown_option_attr', $label );
267 $label = flrt_extract_var( $attr, 'label' );
268
269 foreach ( (array) $attr as $key => $val ){
270 $attributes .= $key .'="'.$val.'" ';
271 }
272
273 $html = '<option value="'.$value.'" '.$attributes;
274
275 if( is_array( $selected ) ){
276 if( in_array( $value, $selected ) ){
277 $html .= ' selected="selected"';
278 }
279 }else{
280 if( (string) $value === $selected ){
281 $html .= ' selected="selected"';
282 }
283 }
284
285 if ( is_array( $disabled ) && ! empty( $disabled ) ) {
286
287 if ( in_array( $value, $disabled ) ) {
288 $html .= ' disabled="disabled"';
289 }
290 }
291
292 $html .= apply_filters('flrt_before_render_admin_select_option', $value, $this->getAttributes(), $label, $disabled);
293
294 $html .= '>' . $label . '</option>'."\r\n";
295
296 return $html;
297 }
298 }
299
300 class Textarea extends Input {
301 public function render(){
302 $html = '<textarea';
303 $html .= $this->renderAttributes( array( 'value' ) );
304 $html .= '>';
305 $html .= esc_textarea( $this->getAttribute('value' ) );
306 $html .= '</textarea>'."\r\n";
307
308 return apply_filters( 'wpc_input_type_textarea', $html, $this->getAttributes() );
309 }
310 }
311
312 class Wpeditor extends Input {
313 public function render(){
314 $settings = array(
315 'textarea_rows' => $this->getAttribute('textarea_rows'),
316 'textarea_name' => $this->getAttribute('name')
317 );
318 wp_editor( $this->getAttribute('value' ), $this->getAttribute('id' ), $settings );
319 }
320 }
321
322 class inProButton extends Input {
323 public function render(){
324 return flrt_unlock_in_pro();
325 }
326 }
327
328 class RangeList extends Input {
329 public function render(){
330 $html = '';
331 $disabled = '';
332 $range_list = $this->getAttribute('value');
333 if(!empty($range_list)){
334 foreach ($range_list as $key => $value) {
335 $html .= '<div class="range-list-value" data-list-number="' . $key .'">';
336 $html .= '<div class="wpc-range-list-inputs"><div><input class="wpc-range-list-min-value" type="number" name="' . $this->getAttribute('name' ) .'[' . $key . '][range_list_min_val]" placeholder="' . esc_html__('Min Value', 'filter-everything') . '" value="' . $value['range_list_min_val'] . '"></div>';
337 $html .= '<div><input class="wpc-range-list-max-value" type="number" name="' . $this->getAttribute('name' ) .'[' . $key . '][range_list_max_val]" placeholder="' . esc_html__('Max Value', 'filter-everything') . '" value="' . $value['range_list_max_val'] . '"></div>';
338 $html .= '<div><input type="text" class="wpc-range-list-range-text" name="' . $this->getAttribute('name' ) .'[' . $key . '][range_list_range_text]" placeholder="' . esc_html__('Label', 'filter-everything') . '" value="' . $value['range_list_range_text'] . '"></div>';
339 $html .= '<div><button class="button remove-range-list-value">' . esc_html__('Remove', 'filter-everything') . '</button></div>';
340 $html .= '</div></div>';
341 }
342 $total = (int) (defined('FLRT_RANGE_LIST_LIMIT') && FLRT_RANGE_LIST_LIMIT ) ? FLRT_RANGE_LIST_LIMIT : 20;
343 if(count($range_list) > $total){
344 $disabled = ' disabled';
345 }
346 }
347
348 if(!empty($this->getAttribute('button_name'))){
349 if(!empty($this->getAttribute('class' ))){
350 $id = ' id="'.$this->getAttribute('id' ).'"';
351 $class = ' class="'.$this->getAttribute('class' ).' button"';
352 }
353 $html .= '<button';
354 $html .= ' name="'.$this->getAttribute('name' ).'"';
355 $html .= $id;
356 $html .= $class;
357 $html .= $disabled;
358 $html .= '>' . $this->getAttribute('button_name' );
359 $html .= '</button>'."\r\n";
360 }
361 return apply_filters( 'wpc_input_type_button', $html, $this->getAttributes() );
362 }
363 }