img
1 year ago
ARIAL.TTF
1 year ago
background.png
6 years ago
banner-sample.php
5 months ago
class-php-captcha.php
4 months ago
class-pisol-enquiry-quotation-woocommerce-activator.php
5 months ago
class-pisol-enquiry-quotation-woocommerce-deactivator.php
5 months ago
class-pisol-enquiry-quotation-woocommerce-i18n.php
5 months ago
class-pisol-enquiry-quotation-woocommerce-loader.php
5 months ago
class-pisol-enquiry-quotation-woocommerce.php
5 months ago
conflict-fixer.php
5 months ago
includes.php
5 months ago
index.php
7 years ago
pisol.class.form.php
5 months ago
pisol.class.promotion.php
5 months ago
review-icon.svg
4 years ago
review.php
4 months ago
pisol.class.form.php
611 lines
| 1 | <?php |
| 2 | /** |
| 3 | * version 3.10 |
| 4 | * work with bootstrap |
| 5 | */ |
| 6 | defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); |
| 7 | |
| 8 | if(!class_exists('pisol_class_form_eqw')): |
| 9 | class pisol_class_form_eqw{ |
| 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 'number': |
| 124 | $this->number_box(); |
| 125 | break; |
| 126 | |
| 127 | case 'text': |
| 128 | $this->text_box(); |
| 129 | break; |
| 130 | |
| 131 | case 'text_html': |
| 132 | $this->text_box(); |
| 133 | break; |
| 134 | |
| 135 | case 'textarea': |
| 136 | $this->textarea_box(); |
| 137 | break; |
| 138 | |
| 139 | case 'multiselect': |
| 140 | $this->multiselect_box(); |
| 141 | break; |
| 142 | |
| 143 | case 'color': |
| 144 | $this->color_box(); |
| 145 | break; |
| 146 | |
| 147 | case 'hidden': |
| 148 | $this->hidden_box(); |
| 149 | break; |
| 150 | |
| 151 | case 'switch': |
| 152 | $this->switch_display(); |
| 153 | break; |
| 154 | |
| 155 | case 'switch_category': |
| 156 | $this->switch_category_display(); |
| 157 | break; |
| 158 | |
| 159 | case 'setting_category': |
| 160 | $this->setting_category(); |
| 161 | break; |
| 162 | |
| 163 | case 'image': |
| 164 | $this->image(); |
| 165 | break; |
| 166 | } |
| 167 | endif; |
| 168 | } |
| 169 | |
| 170 | function bootstrap($label, $field, $desc = "", $links = "", $title_col = 5){ |
| 171 | $title_col = 6; |
| 172 | $setting_col = 12 - $title_col; |
| 173 | if($this->setting['type'] != 'hidden'){ |
| 174 | ?> |
| 175 | <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']) : ''; ?>"> |
| 176 | <div class="col-12 col-md-<?php echo esc_attr($title_col); ?>"> |
| 177 | <?php echo wp_kses($label, $this->allowed_tags); ?> |
| 178 | <?php echo wp_kses($desc != "" ? $desc: "", $this->allowed_tags); ?> |
| 179 | <?php echo wp_kses($links != "" ? $links: "", $this->allowed_tags); ?> |
| 180 | </div> |
| 181 | <div class="col-12 col-md-<?php echo esc_attr($setting_col); ?>"> |
| 182 | <?php echo wp_kses($field, $this->allowed_tags); ?> |
| 183 | </div> |
| 184 | </div> |
| 185 | <?php |
| 186 | }else{ |
| 187 | ?> |
| 188 | <div id="row_<?php echo esc_attr($this->setting['field']); ?>" class="row align-items-center <?php echo esc_attr($this->pro); ?>"> |
| 189 | <div class="col-12 col-md-12"> |
| 190 | <?php echo wp_kses($field, $this->allowed_tags); ?> |
| 191 | </div> |
| 192 | </div> |
| 193 | <?php |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | |
| 198 | function bootstrap_switch_category($label, $field, $desc = "", $links = ""){ |
| 199 | ?> |
| 200 | <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']) : "" ); ?>"> |
| 201 | <div class="col-9"> |
| 202 | <?php echo wp_kses($label, $this->allowed_tags) ; ?> |
| 203 | <?php echo wp_kses($desc != "" ? $desc.'<br>': "", $this->allowed_tags); ?> |
| 204 | <?php echo wp_kses($links != "" ? $links: "", $this->allowed_tags); ?> |
| 205 | </div> |
| 206 | <div class="col-3"> |
| 207 | <?php echo wp_kses($field, $this->allowed_tags); ?> |
| 208 | </div> |
| 209 | </div> |
| 210 | <?php |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | Field type: select box |
| 215 | */ |
| 216 | function select_box(){ |
| 217 | |
| 218 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>'; |
| 219 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 220 | |
| 221 | $field = '<select class="form-control '.esc_attr($this->pro).'" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'"' |
| 222 | .(isset($this->setting['multiple']) ? ' multiple="'.esc_attr($this->setting['multiple']).'"': '') |
| 223 | .'>'; |
| 224 | foreach($this->setting['value'] as $key => $val){ |
| 225 | $field .= '<option value="'.esc_attr($key).'" '.( ( $this->saved_value == $key) ? " selected=\"selected\" " : "" ).'>'.esc_html($val).'</option>'; |
| 226 | } |
| 227 | $field .= '</select>'; |
| 228 | |
| 229 | $links = $this->generateLinks($this->setting); |
| 230 | |
| 231 | $this->bootstrap($label, $field, $desc, $links, 7); |
| 232 | |
| 233 | } |
| 234 | |
| 235 | function generateLinks($setting){ |
| 236 | /* |
| 237 | 'links'=>array(array('name'=>"Video", 'url'=>"https://www.youtube.com/watch?v=KNC5lkoE2Fs", 'type'=>'iframe')) |
| 238 | 'links'=>array(array('name'=>"Video", 'url'=>"image url", 'type'=>'image')) |
| 239 | */ |
| 240 | |
| 241 | if(!isset($setting['links']) || !is_array($setting['links']) || empty($setting['links'])) return; |
| 242 | |
| 243 | $html = ''; |
| 244 | $links = $setting['links']; |
| 245 | foreach($links as $link){ |
| 246 | $class = 'pi-'.$link['type']; |
| 247 | $html .= '<a href="'.esc_url($link['url']).'" class="'.esc_attr($class).' pi-info-links" target="_blank">'.esc_html($link['name']).'</a> '; |
| 248 | } |
| 249 | return $html; |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | Field type: select box |
| 254 | */ |
| 255 | function multiselect_box(){ |
| 256 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>'; |
| 257 | $desc = ((isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""); |
| 258 | $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'. '>'; |
| 259 | foreach($this->setting['value'] as $key => $val){ |
| 260 | if(isset($this->saved_value) && $this->saved_value != false){ |
| 261 | $field .='<option value="'.esc_attr($key).'" '.( ( in_array($key, $this->saved_value) ) ? " selected=\"selected\" " : "" ).'>'.esc_html($val).'</option>'; |
| 262 | }else{ |
| 263 | $field .= '<option value="'.esc_attr($key).'">'.esc_html($val).'</option>'; |
| 264 | } |
| 265 | } |
| 266 | $field .= '</select>'; |
| 267 | |
| 268 | $links = $this->generateLinks($this->setting); |
| 269 | |
| 270 | $this->bootstrap($label, $field, $desc, $links); |
| 271 | |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | Field type: Number box |
| 276 | */ |
| 277 | function number_box(){ |
| 278 | |
| 279 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>'; |
| 280 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 281 | $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).'"' |
| 282 | .(isset($this->setting['min']) ? ' min="'.esc_attr($this->setting['min']).'"': '') |
| 283 | .(isset($this->setting['max']) ? ' max="'.esc_attr($this->setting['max']).'"': '') |
| 284 | .(isset($this->setting['step']) ? ' step="'.esc_attr($this->setting['step']).'"': '') |
| 285 | .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '') |
| 286 | .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '') |
| 287 | .'>'; |
| 288 | |
| 289 | $links = $this->generateLinks($this->setting); |
| 290 | |
| 291 | $this->bootstrap($label, $field, $desc, $links, 7); |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | Field type: Number box |
| 296 | */ |
| 297 | function text_box(){ |
| 298 | |
| 299 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>'; |
| 300 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 301 | $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).'"' |
| 302 | .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '') |
| 303 | .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '') |
| 304 | .'>'; |
| 305 | |
| 306 | $links = $this->generateLinks($this->setting); |
| 307 | |
| 308 | $this->bootstrap($label, $field, $desc, $links, 7); |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | Textarea field |
| 313 | */ |
| 314 | function textarea_box(){ |
| 315 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>'; |
| 316 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 317 | $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']).'"' |
| 318 | .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '') |
| 319 | .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '') |
| 320 | .'>'; |
| 321 | $field .= esc_textarea($this->saved_value); |
| 322 | $field .= '</textarea>'; |
| 323 | |
| 324 | $links = $this->generateLinks($this->setting); |
| 325 | |
| 326 | $this->bootstrap($label, $field, $desc, $links, 12); |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | Field type: color |
| 331 | */ |
| 332 | function color_box(){ |
| 333 | wp_enqueue_style( 'wp-color-picker'); |
| 334 | wp_enqueue_script( 'wp-color-picker'); |
| 335 | wp_add_inline_script('wp-color-picker',' |
| 336 | jQuery(document).ready(function($) { |
| 337 | $(".color-picker").wpColorPicker(); |
| 338 | }); |
| 339 | '); |
| 340 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>'; |
| 341 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 342 | $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).'"' |
| 343 | .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '') |
| 344 | .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '') |
| 345 | .'>'; |
| 346 | |
| 347 | $links = $this->generateLinks($this->setting); |
| 348 | |
| 349 | $this->bootstrap($label, $field, $desc, $links, 6); |
| 350 | } |
| 351 | |
| 352 | function hidden_box(){ |
| 353 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>'; |
| 354 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 355 | $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).'"' |
| 356 | .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '') |
| 357 | .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '') |
| 358 | .'>'; |
| 359 | |
| 360 | $links = $this->generateLinks($this->setting); |
| 361 | |
| 362 | $this->bootstrap($label, $field, $desc, $links); |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | Field type: switch |
| 367 | */ |
| 368 | function switch_display(){ |
| 369 | |
| 370 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>'; |
| 371 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 372 | |
| 373 | $field = '<div class="custom-control custom-switch"> |
| 374 | <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'": "").' > |
| 375 | <label class="custom-control-label" for="'.esc_attr($this->setting['field']).'"></label> |
| 376 | </div>'; |
| 377 | |
| 378 | $links = $this->generateLinks($this->setting); |
| 379 | |
| 380 | $this->bootstrap($label, $field, $desc, $links, 9); |
| 381 | } |
| 382 | |
| 383 | function switch_category_display(){ |
| 384 | |
| 385 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>'; |
| 386 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 387 | |
| 388 | $field = '<div class="custom-control custom-switch"> |
| 389 | <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'": "").' > |
| 390 | <label class="custom-control-label" for="'.esc_attr($this->setting['field']).'"></label> |
| 391 | </div>'; |
| 392 | |
| 393 | $links = $this->generateLinks($this->setting); |
| 394 | |
| 395 | $this->bootstrap_switch_category($label, $field, $desc, $links); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Category: is to devide setting in different part |
| 400 | */ |
| 401 | function setting_category(){ |
| 402 | if(isset($this->setting['label']) && $this->setting['label'] != ""): |
| 403 | ?> |
| 404 | <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']) : "" ); ?>"> |
| 405 | <div class="col-12"> |
| 406 | <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> |
| 407 | </div> |
| 408 | </div> |
| 409 | <?php |
| 410 | endif; |
| 411 | } |
| 412 | |
| 413 | function image(){ |
| 414 | wp_enqueue_media(); |
| 415 | add_action( 'admin_footer', array($this,'media_selector_scripts') ); |
| 416 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.wp_kses_post($this->setting['label']).'</label>'; |
| 417 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 418 | $field = ' |
| 419 | <div class="row align-items-center"> |
| 420 | <div class="col-6"> |
| 421 | <input id="'.esc_attr($this->setting['field']).'_button" type="button" class="button" value="'.esc_attr(__('Upload image', 'pisol-enquiry-quotation-woocommerce')).'" /> |
| 422 | <input type="hidden" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'" value="'.esc_attr($this->saved_value).'"> |
| 423 | </div> |
| 424 | <div class="col-6"> |
| 425 | <div class="image-preview-wrapper"> |
| 426 | <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;"> |
| 427 | <a href="javascript:void(0)" class="clear-image-'.esc_attr($this->setting['field']).'">Clear</a> |
| 428 | </div> |
| 429 | </div> |
| 430 | </div> |
| 431 | '; |
| 432 | $links = $this->generateLinks($this->setting); |
| 433 | |
| 434 | $this->bootstrap($label, $field, $desc, $links); |
| 435 | } |
| 436 | |
| 437 | function media_selector_scripts(){ |
| 438 | $my_saved_attachment_post_id = get_option($this->setting['field'], 0 ); |
| 439 | ?><script type='text/javascript'> |
| 440 | jQuery( document ).ready( function( $ ) { |
| 441 | // Uploading files |
| 442 | var file_frame; |
| 443 | var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id |
| 444 | 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 |
| 445 | jQuery('#<?php echo esc_attr($this->setting['field']); ?>_button').on('click', function( event ){ |
| 446 | event.preventDefault(); |
| 447 | // If the media frame already exists, reopen it. |
| 448 | if ( file_frame ) { |
| 449 | // Set the post ID to what we want |
| 450 | file_frame.uploader.uploader.param( 'post_id', set_to_post_id ); |
| 451 | // Open frame |
| 452 | file_frame.open(); |
| 453 | return; |
| 454 | } else { |
| 455 | // Set the wp.media post id so the uploader grabs the ID we want when initialised |
| 456 | wp.media.model.settings.post.id = set_to_post_id; |
| 457 | } |
| 458 | // Create the media frame. |
| 459 | file_frame = wp.media.frames.file_frame = wp.media({ |
| 460 | title: 'Select a image to upload', |
| 461 | button: { |
| 462 | text: 'Use this image', |
| 463 | }, |
| 464 | multiple: false // Set to true to allow multiple files to be selected |
| 465 | }); |
| 466 | // When an image is selected, run a callback. |
| 467 | file_frame.on( 'select', function() { |
| 468 | // We set multiple to false so only get one image from the uploader |
| 469 | attachment = file_frame.state().get('selection').first().toJSON(); |
| 470 | // Do something with attachment.id and/or attachment.url here |
| 471 | $( '#<?php echo esc_attr($this->setting['field']); ?>_preview' ).attr( 'src', attachment.url ).css( 'width', 'auto' ); |
| 472 | $( '#<?php echo esc_attr($this->setting['field']); ?>' ).val( attachment.id ); |
| 473 | // Restore the main post ID |
| 474 | wp.media.model.settings.post.id = wp_media_post_id; |
| 475 | }); |
| 476 | // Finally, open the modal |
| 477 | file_frame.open(); |
| 478 | }); |
| 479 | // Restore the main ID when the add media button is pressed |
| 480 | jQuery( 'a.add_media' ).on( 'click', function() { |
| 481 | wp.media.model.settings.post.id = wp_media_post_id; |
| 482 | }); |
| 483 | jQuery( 'a.clear-image-<?php echo esc_attr($this->setting['field']); ?>' ).on( 'click', function() { |
| 484 | $( '#<?php echo esc_attr($this->setting['field']); ?>_preview' ).attr("src",""); |
| 485 | $( '#<?php echo esc_attr($this->setting['field']); ?>' ).val(""); |
| 486 | }); |
| 487 | }); |
| 488 | </script> |
| 489 | <?php |
| 490 | } |
| 491 | |
| 492 | |
| 493 | /** |
| 494 | * if a field don't want to do any sanitization then they will set |
| 495 | * 'validation' => false |
| 496 | * and if they want to add there custom sanitization function then they will do |
| 497 | * 'sanitize_callback' => 'function_name' OR |
| 498 | * 'sanitize_callback' => array('class_name', 'function_name') |
| 499 | * if they want to use different sanitization function that is other then the one defined for them then they will use |
| 500 | * 'sanitize_callback' => 'sanitize_text_field' => directly add the sanitization function name |
| 501 | */ |
| 502 | static function register_setting($group, $setting){ |
| 503 | |
| 504 | $validation_function = self::getValidationFunction($setting); |
| 505 | |
| 506 | |
| 507 | if($validation_function !== false){ |
| 508 | if(!is_array($validation_function) && method_exists(__CLASS__, $validation_function)){ |
| 509 | register_setting($group, $setting['field'], [ |
| 510 | 'sanitize_callback' => [__CLASS__, $validation_function] |
| 511 | ]); |
| 512 | return; |
| 513 | }else{ |
| 514 | if(is_array($validation_function) && count($validation_function) == 2 && method_exists($validation_function[0], $validation_function[1])){ |
| 515 | register_setting($group, $setting['field'], [ |
| 516 | 'sanitize_callback' => $validation_function |
| 517 | ]); |
| 518 | return; |
| 519 | }elseif(!is_array($validation_function) && function_exists($validation_function)){ |
| 520 | register_setting($group, $setting['field'], [ |
| 521 | 'sanitize_callback' => $validation_function |
| 522 | ]); |
| 523 | return; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | register_setting($group, $setting['field']); |
| 529 | |
| 530 | } |
| 531 | |
| 532 | static function getValidationFunction($setting){ |
| 533 | if(isset($setting['validation']) && $setting['validation'] === false) return false; |
| 534 | |
| 535 | if(isset($setting['sanitize_callback'])){ |
| 536 | return $setting['sanitize_callback']; |
| 537 | } |
| 538 | |
| 539 | $sanitize_text_allow_basic_html_field_types = ['text_html']; |
| 540 | |
| 541 | $sanitize_text_field_types = ['select', 'text', 'multiselect', 'color', 'hidden', 'switch', 'switch_category']; |
| 542 | |
| 543 | $sanitize_textarea_field_types = ['textarea']; |
| 544 | |
| 545 | $sanitize_number_field_types = ['number']; |
| 546 | |
| 547 | if(isset($setting['type']) && in_array($setting['type'], $sanitize_text_field_types)){ |
| 548 | return 'sanitize_text_field'; |
| 549 | } |
| 550 | |
| 551 | if(isset($setting['type']) && in_array($setting['type'], $sanitize_textarea_field_types)){ |
| 552 | return 'sanitize_textarea_field'; |
| 553 | } |
| 554 | |
| 555 | if(isset($setting['type']) && in_array($setting['type'], $sanitize_number_field_types)){ |
| 556 | return 'sanitize_numeric_values'; |
| 557 | } |
| 558 | |
| 559 | if(isset($setting['type']) && in_array($setting['type'], $sanitize_text_allow_basic_html_field_types)){ |
| 560 | return 'sanitize_text_allow_basic_html'; |
| 561 | } |
| 562 | |
| 563 | return false; |
| 564 | |
| 565 | } |
| 566 | |
| 567 | static function sanitize_text_field($input) { |
| 568 | |
| 569 | $sanitized_input = is_array($input) ? array_map([__CLASS__,'sanitize_text_field'], $input) : sanitize_text_field($input); |
| 570 | |
| 571 | return $sanitized_input; |
| 572 | } |
| 573 | |
| 574 | static function sanitize_textarea_field($input) { |
| 575 | $sanitized_input = sanitize_textarea_field($input); |
| 576 | |
| 577 | return $sanitized_input; |
| 578 | |
| 579 | } |
| 580 | |
| 581 | static function sanitize_text_allow_basic_html($input) { |
| 582 | $allowed_tags = array( |
| 583 | 'span' => array(), |
| 584 | 'strong' => array(), |
| 585 | 'b' => array(), |
| 586 | 'i' => array(), |
| 587 | 'br' => array(), |
| 588 | ); |
| 589 | |
| 590 | $sanitized_input = wp_kses($input, $allowed_tags); |
| 591 | |
| 592 | return $sanitized_input; |
| 593 | } |
| 594 | |
| 595 | // Sanitize numeric input (supports both integer and float) |
| 596 | static function sanitize_numeric_values($input) { |
| 597 | if (is_numeric($input)) { |
| 598 | if (ctype_digit($input)) { |
| 599 | $sanitized_input = intval($input); |
| 600 | } else { |
| 601 | $sanitized_input = floatval($input); |
| 602 | } |
| 603 | } else { |
| 604 | $sanitized_input = 0; // You can change this to any default value |
| 605 | } |
| 606 | |
| 607 | return $sanitized_input; |
| 608 | } |
| 609 | |
| 610 | } |
| 611 | endif; |