PluginProbe ʕ •ᴥ•ʔ
PiWeb Delivery & Pickup Date Time for WooCommerce / 3.0.62
PiWeb Delivery & Pickup Date Time for WooCommerce v3.0.62
3.0.63 3.0.62 3.0.61 3.0.60 3.0.49.49 3.0.49.6 3.0.49.60 3.0.49.61 3.0.49.62 3.0.49.63 3.0.49.64 3.0.49.66 3.0.49.67 3.0.49.69 3.0.49.7 3.0.49.70 3.0.49.72 3.0.49.73 3.0.49.74 3.0.49.76 3.0.49.77 3.0.49.79 3.0.49.9 3.0.49.90 3.0.49.91 3.0.49.92 3.0.49.93 3.0.49.94 3.0.49.96 3.0.49.97 3.0.49.99 trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 2.9.17 3.0.47 3.0.49 3.0.49.1 3.0.49.10 3.0.49.11 3.0.49.12 3.0.49.13 3.0.49.16 3.0.49.17 3.0.49.19 3.0.49.2 3.0.49.20 3.0.49.21 3.0.49.22 3.0.49.23 3.0.49.24 3.0.49.26 3.0.49.27 3.0.49.29 3.0.49.3 3.0.49.30 3.0.49.31 3.0.49.32 3.0.49.33 3.0.49.34 3.0.49.36 3.0.49.37 3.0.49.39 3.0.49.4 3.0.49.40 3.0.49.41 3.0.49.42 3.0.49.43 3.0.49.44 3.0.49.46 3.0.49.47
pi-woocommerce-order-date-time-and-type / include / pisol.class.form.php
pi-woocommerce-order-date-time-and-type / include Last commit date
Pro_Warning.php 3 weeks ago background.png 3 weeks ago banner-sample.php 3 weeks ago class-get-values.php 3 weeks ago includes.php 3 weeks ago pisol.class.form.php 3 weeks ago pisol.class.promotion.php 3 weeks ago review-icon.svg 3 weeks ago review.php 3 weeks ago
pisol.class.form.php
633 lines
1 <?php
2 /**
3 * version 3.12
4 * work with bootstrap
5 */
6 defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
7
8 if(!class_exists('pisol_class_form')):
9 class pisol_class_form{
10
11 private $setting;
12 private $saved_value;
13 private $pro;
14 public $allowed_tags;
15 public $allowed_atts;
16
17 function __construct($setting){
18
19 $this->setting = $setting;
20
21 if(isset( $this->setting['default'] )){
22 $this->saved_value = get_option($this->setting['field'], $this->setting['default']);
23 }else{
24 $this->saved_value = get_option($this->setting['field']);
25 }
26
27 if(isset( $this->setting['pro'] )){
28 if($this->setting['pro']){
29 $this->pro = ' free-version ';
30 //$this->setting['desc'] = '<span style="color:#f00; font-weight:bold;">Workes in Pro version only / Without PRO version this setting will have no effect</span>';
31 }else{
32 $this->pro = ' paid-version ';
33 }
34 }else{
35 $this->pro = "";
36 }
37
38 $allowed_atts = array(
39 'align' => array(),
40 'class' => array(),
41 'selected' => array(),
42 'multiple' => array(),
43 'checked' => array(),
44 'type' => array(),
45 'id' => array(),
46 'dir' => array(),
47 'lang' => array(),
48 'style' => array(),
49 'xml:lang' => array(),
50 'src' => array(),
51 'alt' => array(),
52 'href' => array(),
53 'rel' => array(),
54 'rev' => array(),
55 'target' => array(),
56 'novalidate' => array(),
57 'type' => array(),
58 'value' => array(),
59 'name' => array(),
60 'tabindex' => array(),
61 'action' => array(),
62 'method' => array(),
63 'for' => array(),
64 'width' => array(),
65 'height' => array(),
66 'data' => array(),
67 'title' => array(),
68 'min' => array(),
69 'max' => array(),
70 'step' => array(),
71 'required' => array(),
72 'readonly' => array(),
73 );
74 $this->allowed_tags['form'] = $allowed_atts;
75 $this->allowed_tags['br'] = $allowed_atts;
76 $this->allowed_tags['label'] = $allowed_atts;
77 $this->allowed_tags['input'] = $allowed_atts;
78 $this->allowed_tags['select'] = $allowed_atts;
79 $this->allowed_tags['option'] = $allowed_atts;
80 $this->allowed_tags['textarea'] = $allowed_atts;
81 $this->allowed_tags['iframe'] = $allowed_atts;
82 $this->allowed_tags['script'] = $allowed_atts;
83 $this->allowed_tags['style'] = $allowed_atts;
84 $this->allowed_tags['strong'] = $allowed_atts;
85 $this->allowed_tags['small'] = $allowed_atts;
86 $this->allowed_tags['table'] = $allowed_atts;
87 $this->allowed_tags['span'] = $allowed_atts;
88 $this->allowed_tags['abbr'] = $allowed_atts;
89 $this->allowed_tags['code'] = $allowed_atts;
90 $this->allowed_tags['pre'] = $allowed_atts;
91 $this->allowed_tags['div'] = $allowed_atts;
92 $this->allowed_tags['img'] = $allowed_atts;
93 $this->allowed_tags['h1'] = $allowed_atts;
94 $this->allowed_tags['h2'] = $allowed_atts;
95 $this->allowed_tags['h3'] = $allowed_atts;
96 $this->allowed_tags['h4'] = $allowed_atts;
97 $this->allowed_tags['h5'] = $allowed_atts;
98 $this->allowed_tags['h6'] = $allowed_atts;
99 $this->allowed_tags['ol'] = $allowed_atts;
100 $this->allowed_tags['ul'] = $allowed_atts;
101 $this->allowed_tags['li'] = $allowed_atts;
102 $this->allowed_tags['em'] = $allowed_atts;
103 $this->allowed_tags['hr'] = $allowed_atts;
104 $this->allowed_tags['br'] = $allowed_atts;
105 $this->allowed_tags['tr'] = $allowed_atts;
106 $this->allowed_tags['td'] = $allowed_atts;
107 $this->allowed_tags['p'] = $allowed_atts;
108 $this->allowed_tags['a'] = $allowed_atts;
109 $this->allowed_tags['b'] = $allowed_atts;
110 $this->allowed_tags['i'] = $allowed_atts;
111
112
113 $this->check_field_type();
114 }
115
116 function check_field_type(){
117 if(isset($this->setting['type'])):
118 switch ($this->setting['type']){
119 case 'select':
120 $this->select_box();
121 break;
122
123 case 'radio':
124 $this->radio_group();
125 break;
126
127 case 'number':
128 $this->number_box();
129 break;
130
131 case 'text':
132 $this->text_box();
133 break;
134
135 case 'text_html':
136 $this->text_box();
137 break;
138
139 case 'textarea':
140 $this->textarea_box();
141 break;
142
143 case 'multiselect':
144 $this->multiselect_box();
145 break;
146
147 case 'color':
148 $this->color_box();
149 break;
150
151 case 'hidden':
152 $this->hidden_box();
153 break;
154
155 case 'switch':
156 $this->switch_display();
157 break;
158
159 case 'switch_category':
160 $this->switch_category_display();
161 break;
162
163 case 'setting_category':
164 $this->setting_category();
165 break;
166
167 case 'image':
168 $this->image();
169 break;
170 }
171 endif;
172 }
173
174 function bootstrap($label, $field, $desc = "", $links = "", $title_col = 5){
175 $setting_col = 12 - $title_col;
176 if($this->setting['type'] != 'hidden'){
177 ?>
178 <div id="row_<?php echo esc_attr($this->setting['field']); ?>" class="pisol-form-element-row row py-4 border-bottom align-items-center <?php echo esc_attr($this->pro); ?> <?php echo !empty($this->setting['class']) ? esc_attr($this->setting['class']) : ''; ?> field-type-<?php echo esc_attr($this->setting['type']); ?>">
179 <div class="col-12 col-md-<?php echo esc_attr($title_col); ?>">
180 <?php echo wp_kses($label, $this->allowed_tags); ?>
181 <?php echo wp_kses($desc != "" ? $desc : "", $this->allowed_tags); ?>
182 <?php echo wp_kses($links != "" ? $links: "", $this->allowed_tags); ?>
183 </div>
184 <div class="col-12 col-md-<?php echo esc_attr($setting_col); ?>">
185 <?php echo wp_kses($field, $this->allowed_tags); ?>
186 </div>
187 </div>
188 <?php
189 }else{
190 ?>
191 <div id="row_<?php echo esc_attr($this->setting['field']); ?>" class="row align-items-center <?php echo esc_attr($this->pro); ?>">
192 <div class="col-12 col-md-12">
193 <?php echo wp_kses($field, $this->allowed_tags); ?>
194 </div>
195 </div>
196 <?php
197 }
198 }
199
200
201 function bootstrap_switch_category($label, $field, $desc = "", $links = ""){
202 ?>
203 <div id="row_<?php echo esc_attr($this->setting['field']); ?>" class="row py-4 border-bottom align-items-center <?php echo ( isset($this->setting['class']) ? esc_attr($this->setting['class']) : "" ); ?>">
204 <div class="col-9">
205 <?php echo wp_kses($label, $this->allowed_tags) ; ?>
206 <?php echo wp_kses($desc != "" ? $desc.'<br>': "", $this->allowed_tags); ?>
207 <?php echo wp_kses($links != "" ? $links: "", $this->allowed_tags); ?>
208 </div>
209 <div class="col-3">
210 <?php echo wp_kses($field, $this->allowed_tags); ?>
211 </div>
212 </div>
213 <?php
214 }
215
216 /*
217 Field type: select box
218 */
219 function select_box(){
220
221 $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>';
222 $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : "";
223
224 $field = '<select class="form-control '.esc_attr($this->pro).'" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'"'
225 .(isset($this->setting['multiple']) ? ' multiple="'.esc_attr($this->setting['multiple']).'"': '')
226 .'>';
227 foreach($this->setting['value'] as $key => $val){
228 $field .= '<option value="'.esc_attr($key).'" '.( ( $this->saved_value == $key) ? " selected=\"selected\" " : "" ).'>'.esc_html($val).'</option>';
229 }
230 $field .= '</select>';
231
232 $links = $this->generateLinks($this->setting);
233
234 $this->bootstrap($label, $field, $desc, $links, 7);
235
236 }
237
238 function radio_group(){
239
240 $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>';
241 $desc = (isset($this->setting['desc'])) ? '<small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : "";
242
243 $field = '<div class="'.esc_attr($this->setting['radio_class'] ?? '').'">';
244 foreach($this->setting['value'] as $key => $val){
245 $is_last = ($key === array_key_last($this->setting['value']));
246 $mb_class = $is_last ? '' : ' mb-3';
247 $field .= '<div class="form-check ' . $mb_class . '">
248 <input class="form-check-input" type="radio" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field'].'_'.$key).'" value="'.esc_attr($key).'" '.checked($this->saved_value, $key, false).'>
249 <label class="form-check-label font-italic ml-2" for="'.esc_attr($this->setting['field'].'_'.$key).'">'.esc_html($val).'</label>
250 </div>';
251 }
252 $field .= '</div>';
253
254 $links = $this->generateLinks($this->setting);
255
256 $this->bootstrap($label, $field, $desc, $links, 7);
257
258 }
259
260
261 function generateLinks($setting){
262 /*
263 'links'=>array(array('name'=>"Video", 'url'=>"https://www.youtube.com/watch?v=KNC5lkoE2Fs", 'type'=>'iframe'))
264 'links'=>array(array('name'=>"Video", 'url'=>"image url", 'type'=>'image'))
265 */
266
267 if(!isset($setting['links']) || !is_array($setting['links']) || empty($setting['links'])) return;
268
269 $html = '';
270 $links = $setting['links'];
271 foreach($links as $link){
272 $class = 'pi-'.($link['type'] ?? 'link');
273 $html .= '<a href="'.esc_url($link['url']).'" class="'.esc_attr($class).' pi-info-links" target="_blank">'.esc_html($link['name']).'</a> ';
274 }
275 return $html;
276 }
277
278 /*
279 Field type: select box
280 */
281 function multiselect_box(){
282 $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>';
283 $desc = ((isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : "");
284 $field = '<select style="min-height:100px;" class="form-control multiselect '.esc_attr($this->pro).'" name="'.esc_attr($this->setting['field']).'[]" id="'.esc_attr($this->setting['field']).'" multiple'. '>';
285 foreach($this->setting['value'] as $key => $val){
286 if(isset($this->saved_value) && $this->saved_value != false){
287 $field .='<option value="'.esc_attr($key).'" '.( ( in_array($key, $this->saved_value) ) ? " selected=\"selected\" " : "" ).'>'.esc_html($val).'</option>';
288 }else{
289 $field .= '<option value="'.esc_attr($key).'">'.esc_html($val).'</option>';
290 }
291 }
292 $field .= '</select>';
293
294 $links = $this->generateLinks($this->setting);
295
296 $this->bootstrap($label, $field, $desc, $links);
297
298 }
299
300 /*
301 Field type: Number box
302 */
303 function number_box(){
304
305 $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>';
306 $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : "";
307 $field = '<input type="number" class="form-control '.esc_attr($this->pro).'" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'" value="'.esc_attr($this->saved_value).'"'
308 .(isset($this->setting['min']) ? ' min="'.esc_attr($this->setting['min']).'"': '')
309 .(isset($this->setting['max']) ? ' max="'.esc_attr($this->setting['max']).'"': '')
310 .(isset($this->setting['step']) ? ' step="'.esc_attr($this->setting['step']).'"': '')
311 .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
312 .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
313 .'>';
314
315 $links = $this->generateLinks($this->setting);
316
317 $this->bootstrap($label, $field, $desc, $links, 7);
318 }
319
320 /*
321 Field type: Number box
322 */
323 function text_box(){
324
325 $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>';
326 $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : "";
327 $field = '<input type="text" class="form-control '.esc_attr($this->pro).'" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'" value="'.esc_attr($this->saved_value).'"'
328 .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
329 .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
330 .'>';
331
332 $links = $this->generateLinks($this->setting);
333
334 $this->bootstrap($label, $field, $desc, $links, 7);
335 }
336
337 /*
338 Textarea field
339 */
340 function textarea_box(){
341 $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>';
342 $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : "";
343 $field = '<textarea style="height:auto !important; min-height:200px;" type="text" class="form-control '.esc_attr($this->pro).'" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'"'
344 .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
345 .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
346 .'>';
347 $field .= esc_textarea($this->saved_value);
348 $field .= '</textarea>';
349
350 $links = $this->generateLinks($this->setting);
351
352 $this->bootstrap($label, $field, $desc, $links, 12);
353 }
354
355 /*
356 Field type: color
357 */
358 function color_box(){
359 wp_enqueue_style( 'wp-color-picker');
360 wp_enqueue_script( 'wp-color-picker');
361 wp_add_inline_script('wp-color-picker','
362 jQuery(document).ready(function($) {
363 $(".color-picker").wpColorPicker();
364 });
365 ');
366 $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>';
367 $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : "";
368 $field = '<input type="text" class="color-picker pisol_select '.esc_attr($this->pro).'" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'" value="'.esc_attr($this->saved_value).'"'
369 .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
370 .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
371 .'>';
372
373 $links = $this->generateLinks($this->setting);
374
375 $this->bootstrap($label, $field, $desc, $links, 6);
376 }
377
378 function hidden_box(){
379 $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>';
380 $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : "";
381 $field ='<input type="hidden" class="pisol_select '.esc_attr($this->pro).'" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'" value="'.esc_attr($this->saved_value).'"'
382 .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
383 .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
384 .'>';
385
386 $links = $this->generateLinks($this->setting);
387
388 $this->bootstrap($label, $field, $desc, $links);
389 }
390
391 /*
392 Field type: switch
393 */
394 function switch_display(){
395
396 $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>';
397 $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : "";
398
399 $field = '<div class="custom-control custom-switch">
400 <input type="checkbox" value="1" class="custom-control-input" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'" '.(($this->saved_value == true) ? "checked='checked'": "").' >
401 <label class="custom-control-label" for="'.esc_attr($this->setting['field']).'"></label>
402 </div>';
403
404 $links = $this->generateLinks($this->setting);
405
406 $this->bootstrap($label, $field, $desc, $links, 9);
407 }
408
409 function switch_category_display(){
410
411 $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>';
412 $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : "";
413
414 $field = '<div class="custom-control custom-switch">
415 <input type="checkbox" value="1" class="custom-control-input" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'" '.(!empty($this->saved_value) ? "checked='checked'": "").' >
416 <label class="custom-control-label" for="'.esc_attr($this->setting['field']).'"></label>
417 </div>';
418
419 $links = $this->generateLinks($this->setting);
420
421 $this->bootstrap_switch_category($label, $field, $desc, $links);
422 }
423
424 /**
425 * Category: is to devide setting in different part
426 */
427 function setting_category(){
428 if(isset($this->setting['label']) && $this->setting['label'] != ""):
429 $links = $this->generateLinks($this->setting);
430 ?>
431 <div id="row_<?php echo esc_attr($this->setting['field']); ?>" class="row py-4 border-bottom align-items-center <?php echo ( isset($this->setting['class']) ? esc_attr($this->setting['class']) : "" ); ?> field-type-<?php echo esc_attr($this->setting['type']); ?>">
432 <div class="col-12">
433 <h2 class="mt-0 mb-0 <?php echo ( isset($this->setting['class_title']) ? esc_attr($this->setting['class_title']) : "" ); ?>"><?php echo wp_kses_post($this->setting['label']); ?></h2>
434 <?php echo wp_kses($links != "" ? $links: "", $this->allowed_tags); ?>
435 </div>
436 </div>
437 <?php
438 endif;
439 }
440
441 function image(){
442 wp_enqueue_media();
443 add_action( 'admin_footer', array($this,'media_selector_scripts') );
444 $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>';
445 $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : "";
446 $field = '
447 <div class="row align-items-center">
448 <div class="col-6">
449 <input id="'.esc_attr($this->setting['field']).'_button" type="button" class="button" value="'.esc_attr(__('Upload image','add-coupon-by-link-woocommerce')).'" />
450 <input type="hidden" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'" value="'.esc_attr($this->saved_value).'">
451 </div>
452 <div class="col-6">
453 <div class="image-preview-wrapper">
454 <img id="'.esc_attr($this->setting['field']).'_preview" '.($this->saved_value > 0 ? 'src="'.wp_get_attachment_url( get_option( $this->setting['field'] ) ).'"': '').' width="100" height="100" style="max-height: 100px; width: 100px;">
455 <a href="javascript:void(0)" class="clear-image-'.esc_attr($this->setting['field']).'">Clear</a>
456 </div>
457 </div>
458 </div>
459 ';
460 $links = $this->generateLinks($this->setting);
461
462 $this->bootstrap($label, $field, $desc, $links);
463 }
464
465 function media_selector_scripts(){
466 $my_saved_attachment_post_id = get_option($this->setting['field'], 0 );
467 ?><script type='text/javascript'>
468 jQuery( document ).ready( function( $ ) {
469 // Uploading files
470 var file_frame;
471 var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
472 var set_to_post_id = <?php echo esc_attr($my_saved_attachment_post_id == 0 || $my_saved_attachment_post_id =="" ? "0" : $my_saved_attachment_post_id) ; ?>; // Set this
473 jQuery('#<?php echo esc_attr($this->setting['field']); ?>_button').on('click', function( event ){
474 event.preventDefault();
475 // If the media frame already exists, reopen it.
476 if ( file_frame ) {
477 // Set the post ID to what we want
478 file_frame.uploader.uploader.param( 'post_id', set_to_post_id );
479 // Open frame
480 file_frame.open();
481 return;
482 } else {
483 // Set the wp.media post id so the uploader grabs the ID we want when initialised
484 wp.media.model.settings.post.id = set_to_post_id;
485 }
486 // Create the media frame.
487 file_frame = wp.media.frames.file_frame = wp.media({
488 title: 'Select a image to upload',
489 button: {
490 text: 'Use this image',
491 },
492 multiple: false // Set to true to allow multiple files to be selected
493 });
494 // When an image is selected, run a callback.
495 file_frame.on( 'select', function() {
496 // We set multiple to false so only get one image from the uploader
497 attachment = file_frame.state().get('selection').first().toJSON();
498 // Do something with attachment.id and/or attachment.url here
499 $( '#<?php echo esc_attr($this->setting['field']); ?>_preview' ).attr( 'src', attachment.url ).css( 'width', 'auto' );
500 $( '#<?php echo esc_attr($this->setting['field']); ?>' ).val( attachment.id );
501 // Restore the main post ID
502 wp.media.model.settings.post.id = wp_media_post_id;
503 });
504 // Finally, open the modal
505 file_frame.open();
506 });
507 // Restore the main ID when the add media button is pressed
508 jQuery( 'a.add_media' ).on( 'click', function() {
509 wp.media.model.settings.post.id = wp_media_post_id;
510 });
511 jQuery( 'a.clear-image-<?php echo esc_attr($this->setting['field']); ?>' ).on( 'click', function() {
512 $( '#<?php echo esc_attr($this->setting['field']); ?>_preview' ).attr("src","");
513 $( '#<?php echo esc_attr($this->setting['field']); ?>' ).val("");
514 });
515 });
516 </script>
517 <?php
518 }
519
520
521 /**
522 * if a field don't want to do any sanitization then they will set
523 * 'validation' => false
524 * and if they want to add there custom sanitization function then they will do
525 * 'sanitize_callback' => 'function_name' OR
526 * 'sanitize_callback' => array('class_name', 'function_name')
527 * if they want to use different sanitization function that is other then the one defined for them then they will use
528 * 'sanitize_callback' => 'sanitize_text_field' => directly add the sanitization function name
529 */
530 static function register_setting($group, $setting){
531
532 $validation_function = self::getValidationFunction($setting);
533
534
535 if($validation_function !== false){
536 if(!is_array($validation_function) && method_exists(__CLASS__, $validation_function)){
537 register_setting($group, $setting['field'], [
538 'sanitize_callback' => [__CLASS__, $validation_function]
539 ]);
540 return;
541 }else{
542 if(is_array($validation_function) && count($validation_function) == 2 && method_exists($validation_function[0], $validation_function[1])){
543 register_setting($group, $setting['field'], [
544 'sanitize_callback' => $validation_function
545 ]);
546 return;
547 }elseif(!is_array($validation_function) && function_exists($validation_function)){
548 register_setting($group, $setting['field'], [
549 'sanitize_callback' => $validation_function
550 ]);
551 return;
552 }
553 }
554 }
555
556 register_setting($group, $setting['field']);
557
558 }
559
560 static function getValidationFunction($setting){
561 if(isset($setting['validation']) && $setting['validation'] === false) return false;
562
563 if(isset($setting['sanitize_callback'])){
564 return $setting['sanitize_callback'];
565 }
566
567 $sanitize_text_allow_basic_html_field_types = ['text_html'];
568
569 $sanitize_text_field_types = ['select', 'text', 'multiselect', 'color', 'hidden', 'switch', 'switch_category','number'];
570
571 $sanitize_textarea_field_types = ['textarea'];
572
573 if(isset($setting['type']) && in_array($setting['type'], $sanitize_text_field_types)){
574 return 'sanitize_text_field';
575 }
576
577 if(isset($setting['type']) && in_array($setting['type'], $sanitize_textarea_field_types)){
578 return 'sanitize_textarea_field';
579 }
580
581 if(isset($setting['type']) && in_array($setting['type'], $sanitize_text_allow_basic_html_field_types)){
582 return 'sanitize_text_allow_basic_html';
583 }
584
585 return false;
586
587 }
588
589 static function sanitize_text_field($input) {
590
591 $sanitized_input = is_array($input) ? array_map([__CLASS__,'sanitize_text_field'], $input) : sanitize_text_field($input);
592
593 return $sanitized_input;
594 }
595
596 static function sanitize_textarea_field($input) {
597 $sanitized_input = sanitize_textarea_field($input);
598
599 return $sanitized_input;
600
601 }
602
603 static function sanitize_text_allow_basic_html($input) {
604 $allowed_tags = array(
605 'span' => array(),
606 'strong' => array(),
607 'b' => array(),
608 'i' => array(),
609 'br' => array(),
610 );
611
612 $sanitized_input = wp_kses($input, $allowed_tags);
613
614 return $sanitized_input;
615 }
616
617 // Sanitize numeric input (supports both integer and float)
618 static function sanitize_numeric_values($input) {
619 if (is_numeric($input)) {
620 if (ctype_digit($input)) {
621 $sanitized_input = intval($input);
622 } else {
623 $sanitized_input = floatval($input);
624 }
625 } else {
626 $sanitized_input = 0; // You can change this to any default value
627 }
628
629 return $sanitized_input;
630 }
631
632 }
633 endif;