background.png
2 years ago
banner-sample.php
2 years ago
class-pisol-enquiry-quotation-woocommerce-activator.php
2 years ago
class-pisol-enquiry-quotation-woocommerce-deactivator.php
2 years ago
class-pisol-enquiry-quotation-woocommerce-i18n.php
2 years ago
class-pisol-enquiry-quotation-woocommerce-loader.php
2 years ago
class-pisol-enquiry-quotation-woocommerce.php
2 years ago
conflict-fixer.php
2 years ago
includes.php
2 years ago
index.php
2 years ago
pisol.class.form.php
2 years ago
pisol.class.promotion.php
2 years ago
review-icon.svg
2 years ago
review.php
2 years ago
pisol.class.form.php
486 lines
| 1 | <?php |
| 2 | /** |
| 3 | * version 3.7 |
| 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 | |
| 16 | function __construct($setting){ |
| 17 | |
| 18 | $this->setting = $setting; |
| 19 | |
| 20 | if(isset( $this->setting['default'] )){ |
| 21 | $this->saved_value = get_option($this->setting['field'], $this->setting['default']); |
| 22 | }else{ |
| 23 | $this->saved_value = get_option($this->setting['field']); |
| 24 | } |
| 25 | |
| 26 | if(isset( $this->setting['pro'] )){ |
| 27 | if($this->setting['pro']){ |
| 28 | $this->pro = ' free-version '; |
| 29 | //$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>'; |
| 30 | }else{ |
| 31 | $this->pro = ' paid-version '; |
| 32 | } |
| 33 | }else{ |
| 34 | $this->pro = ""; |
| 35 | } |
| 36 | |
| 37 | $allowed_atts = array( |
| 38 | 'align' => array(), |
| 39 | 'class' => array(), |
| 40 | 'selected' => array(), |
| 41 | 'multiple' => array(), |
| 42 | 'checked' => array(), |
| 43 | 'type' => array(), |
| 44 | 'id' => array(), |
| 45 | 'dir' => array(), |
| 46 | 'lang' => array(), |
| 47 | 'style' => array(), |
| 48 | 'xml:lang' => array(), |
| 49 | 'src' => array(), |
| 50 | 'alt' => array(), |
| 51 | 'href' => array(), |
| 52 | 'rel' => array(), |
| 53 | 'rev' => array(), |
| 54 | 'target' => array(), |
| 55 | 'novalidate' => array(), |
| 56 | 'type' => array(), |
| 57 | 'value' => array(), |
| 58 | 'name' => array(), |
| 59 | 'tabindex' => array(), |
| 60 | 'action' => array(), |
| 61 | 'method' => array(), |
| 62 | 'for' => array(), |
| 63 | 'width' => array(), |
| 64 | 'height' => array(), |
| 65 | 'data' => array(), |
| 66 | 'title' => array(), |
| 67 | 'min' => array(), |
| 68 | 'max' => array(), |
| 69 | 'step' => array(), |
| 70 | 'required' => array(), |
| 71 | 'readonly' => array(), |
| 72 | ); |
| 73 | $this->allowed_tags['form'] = $allowed_atts; |
| 74 | $this->allowed_tags['label'] = $allowed_atts; |
| 75 | $this->allowed_tags['input'] = $allowed_atts; |
| 76 | $this->allowed_tags['textarea'] = $allowed_atts; |
| 77 | $this->allowed_tags['iframe'] = $allowed_atts; |
| 78 | $this->allowed_tags['script'] = $allowed_atts; |
| 79 | $this->allowed_tags['style'] = $allowed_atts; |
| 80 | $this->allowed_tags['strong'] = $allowed_atts; |
| 81 | $this->allowed_tags['small'] = $allowed_atts; |
| 82 | $this->allowed_tags['table'] = $allowed_atts; |
| 83 | $this->allowed_tags['span'] = $allowed_atts; |
| 84 | $this->allowed_tags['abbr'] = $allowed_atts; |
| 85 | $this->allowed_tags['code'] = $allowed_atts; |
| 86 | $this->allowed_tags['pre'] = $allowed_atts; |
| 87 | $this->allowed_tags['div'] = $allowed_atts; |
| 88 | $this->allowed_tags['img'] = $allowed_atts; |
| 89 | $this->allowed_tags['h1'] = $allowed_atts; |
| 90 | $this->allowed_tags['h2'] = $allowed_atts; |
| 91 | $this->allowed_tags['h3'] = $allowed_atts; |
| 92 | $this->allowed_tags['h4'] = $allowed_atts; |
| 93 | $this->allowed_tags['h5'] = $allowed_atts; |
| 94 | $this->allowed_tags['h6'] = $allowed_atts; |
| 95 | $this->allowed_tags['ol'] = $allowed_atts; |
| 96 | $this->allowed_tags['ul'] = $allowed_atts; |
| 97 | $this->allowed_tags['li'] = $allowed_atts; |
| 98 | $this->allowed_tags['em'] = $allowed_atts; |
| 99 | $this->allowed_tags['hr'] = $allowed_atts; |
| 100 | $this->allowed_tags['br'] = $allowed_atts; |
| 101 | $this->allowed_tags['tr'] = $allowed_atts; |
| 102 | $this->allowed_tags['td'] = $allowed_atts; |
| 103 | $this->allowed_tags['p'] = $allowed_atts; |
| 104 | $this->allowed_tags['a'] = $allowed_atts; |
| 105 | $this->allowed_tags['b'] = $allowed_atts; |
| 106 | $this->allowed_tags['i'] = $allowed_atts; |
| 107 | $this->allowed_tags['select'] = $allowed_atts; |
| 108 | $this->allowed_tags['option'] = $allowed_atts; |
| 109 | |
| 110 | $this->check_field_type(); |
| 111 | } |
| 112 | |
| 113 | |
| 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 'textarea': |
| 132 | $this->textarea_box(); |
| 133 | break; |
| 134 | |
| 135 | case 'multiselect': |
| 136 | $this->multiselect_box(); |
| 137 | break; |
| 138 | |
| 139 | case 'color': |
| 140 | $this->color_box(); |
| 141 | break; |
| 142 | |
| 143 | case 'hidden': |
| 144 | $this->hidden_box(); |
| 145 | break; |
| 146 | |
| 147 | case 'switch': |
| 148 | $this->switch_display(); |
| 149 | break; |
| 150 | |
| 151 | case 'switch_category': |
| 152 | $this->switch_category_display(); |
| 153 | break; |
| 154 | |
| 155 | case 'setting_category': |
| 156 | $this->setting_category(); |
| 157 | break; |
| 158 | |
| 159 | case 'image': |
| 160 | $this->image(); |
| 161 | break; |
| 162 | } |
| 163 | endif; |
| 164 | } |
| 165 | |
| 166 | function bootstrap($label, $field, $desc = "", $links = ""){ |
| 167 | if($this->setting['type'] != 'hidden'){ |
| 168 | ?> |
| 169 | <div id="row_<?php echo esc_attr($this->setting['field']); ?>" class="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']) : ''; ?>"> |
| 170 | <div class="col-12 col-md-5"> |
| 171 | <?php echo wp_kses($label, $this->allowed_tags); ?> |
| 172 | <?php echo wp_kses($desc != "" ? $desc.'<br>': "", $this->allowed_tags); ?> |
| 173 | <?php echo wp_kses($links != "" ? $links: "", $this->allowed_tags); ?> |
| 174 | </div> |
| 175 | <div class="col-12 col-md-7"> |
| 176 | <?php echo wp_kses($field, $this->allowed_tags,['javascript','http','https']); ?> |
| 177 | </div> |
| 178 | </div> |
| 179 | <?php |
| 180 | }else{ |
| 181 | ?> |
| 182 | <div id="row_<?php echo esc_attr($this->setting['field']); ?>" class="row align-items-center <?php echo esc_attr($this->pro); ?>"> |
| 183 | <div class="col-12 col-md-12"> |
| 184 | <?php echo wp_kses($field, $this->allowed_tags); ?> |
| 185 | </div> |
| 186 | </div> |
| 187 | <?php |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | function bootstrap_switch_category($label, $field, $desc = "", $links = ""){ |
| 193 | ?> |
| 194 | <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']) : "" ); ?>"> |
| 195 | <div class="col-9"> |
| 196 | <?php echo wp_kses($label, $this->allowed_tags) ; ?> |
| 197 | <?php echo wp_kses($desc != "" ? $desc.'<br>': "", $this->allowed_tags); ?> |
| 198 | <?php echo wp_kses($links != "" ? $links: "", $this->allowed_tags); ?> |
| 199 | </div> |
| 200 | <div class="col-3"> |
| 201 | <?php echo wp_kses($field, $this->allowed_tags); ?> |
| 202 | </div> |
| 203 | </div> |
| 204 | <?php |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | Field type: select box |
| 209 | */ |
| 210 | function select_box(){ |
| 211 | |
| 212 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.esc_html($this->setting['label']).'</label>'; |
| 213 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 214 | |
| 215 | $field = '<select class="form-control '.esc_attr($this->pro).'" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'"' |
| 216 | .(isset($this->setting['multiple']) ? ' multiple="'.esc_attr($this->setting['multiple']).'"': '') |
| 217 | .'>'; |
| 218 | foreach($this->setting['value'] as $key => $val){ |
| 219 | $field .= '<option value="'.esc_attr($key).'" '.( ( $this->saved_value == $key) ? " selected=\"selected\" " : "" ).'>'.esc_html($val).'</option>'; |
| 220 | } |
| 221 | $field .= '</select>'; |
| 222 | |
| 223 | $links = $this->generateLinks($this->setting); |
| 224 | |
| 225 | $this->bootstrap($label, $field, $desc, $links); |
| 226 | |
| 227 | } |
| 228 | |
| 229 | function generateLinks($setting){ |
| 230 | /* |
| 231 | 'links'=>array(array('name'=>"Video", 'url'=>"https://www.youtube.com/watch?v=KNC5lkoE2Fs", 'type'=>'iframe')) |
| 232 | 'links'=>array(array('name'=>"Video", 'url'=>"image url", 'type'=>'image')) |
| 233 | */ |
| 234 | |
| 235 | if(!isset($setting['links']) || !is_array($setting['links']) || empty($setting['links'])) return; |
| 236 | |
| 237 | $html = ''; |
| 238 | $links = $setting['links']; |
| 239 | foreach($links as $link){ |
| 240 | $class = 'pi-'.$link['type']; |
| 241 | $html .= '<a href="'.esc_url($link['url']).'" class="'.esc_attr($class).' pi-info-links" target="_blank">'.esc_html($link['name']).'</a> '; |
| 242 | } |
| 243 | return $html; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | Field type: select box |
| 248 | */ |
| 249 | function multiselect_box(){ |
| 250 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.esc_html($this->setting['label']).'</label>'; |
| 251 | $desc = ((isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""); |
| 252 | $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'. '>'; |
| 253 | foreach($this->setting['value'] as $key => $val){ |
| 254 | if(isset($this->saved_value) && $this->saved_value != false){ |
| 255 | $field .='<option value="'.esc_attr($key).'" '.( ( in_array($key, $this->saved_value) ) ? " selected=\"selected\" " : "" ).'>'.esc_html($val).'</option>'; |
| 256 | }else{ |
| 257 | $field .= '<option value="'.esc_attr($key).'">'.esc_html($val).'</option>'; |
| 258 | } |
| 259 | } |
| 260 | $field .= '</select>'; |
| 261 | |
| 262 | $links = $this->generateLinks($this->setting); |
| 263 | |
| 264 | $this->bootstrap($label, $field, $desc, $links); |
| 265 | |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | Field type: Number box |
| 270 | */ |
| 271 | function number_box(){ |
| 272 | |
| 273 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.esc_html($this->setting['label']).'</label>'; |
| 274 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 275 | $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).'"' |
| 276 | .(isset($this->setting['min']) ? ' min="'.esc_attr($this->setting['min']).'"': '') |
| 277 | .(isset($this->setting['max']) ? ' max="'.esc_attr($this->setting['max']).'"': '') |
| 278 | .(isset($this->setting['step']) ? ' step="'.esc_attr($this->setting['step']).'"': '') |
| 279 | .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '') |
| 280 | .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '') |
| 281 | .'>'; |
| 282 | |
| 283 | $links = $this->generateLinks($this->setting); |
| 284 | |
| 285 | $this->bootstrap($label, $field, $desc, $links); |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | Field type: Number box |
| 290 | */ |
| 291 | function text_box(){ |
| 292 | |
| 293 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.esc_html($this->setting['label']).'</label>'; |
| 294 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 295 | $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).'"' |
| 296 | .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '') |
| 297 | .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '') |
| 298 | .'>'; |
| 299 | |
| 300 | $links = $this->generateLinks($this->setting); |
| 301 | |
| 302 | $this->bootstrap($label, $field, $desc, $links); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | Textarea field |
| 307 | */ |
| 308 | function textarea_box(){ |
| 309 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.esc_html($this->setting['label']).'</label>'; |
| 310 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 311 | $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']).'"' |
| 312 | .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '') |
| 313 | .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '') |
| 314 | .'>'; |
| 315 | $field .= esc_textarea($this->saved_value); |
| 316 | $field .= '</textarea>'; |
| 317 | |
| 318 | $links = $this->generateLinks($this->setting); |
| 319 | |
| 320 | $this->bootstrap($label, $field, $desc, $links); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | Field type: color |
| 325 | */ |
| 326 | function color_box(){ |
| 327 | wp_enqueue_style( 'wp-color-picker'); |
| 328 | wp_enqueue_script( 'wp-color-picker'); |
| 329 | wp_add_inline_script('wp-color-picker',' |
| 330 | jQuery(document).ready(function($) { |
| 331 | $(".color-picker").wpColorPicker(); |
| 332 | }); |
| 333 | '); |
| 334 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.esc_html($this->setting['label']).'</label>'; |
| 335 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 336 | $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).'"' |
| 337 | .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '') |
| 338 | .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '') |
| 339 | .'>'; |
| 340 | |
| 341 | $links = $this->generateLinks($this->setting); |
| 342 | |
| 343 | $this->bootstrap($label, $field, $desc, $links); |
| 344 | } |
| 345 | |
| 346 | function hidden_box(){ |
| 347 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.esc_html($this->setting['label']).'</label>'; |
| 348 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 349 | $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).'"' |
| 350 | .(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '') |
| 351 | .(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '') |
| 352 | .'>'; |
| 353 | |
| 354 | $links = $this->generateLinks($this->setting); |
| 355 | |
| 356 | $this->bootstrap($label, $field, $desc, $links); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | Field type: switch |
| 361 | */ |
| 362 | function switch_display(){ |
| 363 | |
| 364 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.esc_html($this->setting['label']).'</label>'; |
| 365 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 366 | |
| 367 | $field = '<div class="custom-control custom-switch"> |
| 368 | <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'": "").' > |
| 369 | <label class="custom-control-label" for="'.esc_attr($this->setting['field']).'"></label> |
| 370 | </div>'; |
| 371 | |
| 372 | $links = $this->generateLinks($this->setting); |
| 373 | |
| 374 | $this->bootstrap($label, $field, $desc, $links); |
| 375 | } |
| 376 | |
| 377 | function switch_category_display(){ |
| 378 | |
| 379 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.esc_html($this->setting['label']).'</label>'; |
| 380 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 381 | |
| 382 | $field = '<div class="custom-control custom-switch"> |
| 383 | <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'": "").' > |
| 384 | <label class="custom-control-label" for="'.esc_attr($this->setting['field']).'"></label> |
| 385 | </div>'; |
| 386 | |
| 387 | $links = $this->generateLinks($this->setting); |
| 388 | |
| 389 | $this->bootstrap_switch_category($label, $field, $desc, $links); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Category: is to devide setting in different part |
| 394 | */ |
| 395 | function setting_category(){ |
| 396 | if(isset($this->setting['label']) && $this->setting['label'] != ""): |
| 397 | ?> |
| 398 | <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']) : "" ); ?>"> |
| 399 | <div class="col-12"> |
| 400 | <h2 class="mt-0 mb-0 <?php echo ( isset($this->setting['class_title']) ? esc_attr($this->setting['class_title']) : "" ); ?>"><?php echo $this->setting['label']; ?></h2> |
| 401 | </div> |
| 402 | </div> |
| 403 | <?php |
| 404 | endif; |
| 405 | } |
| 406 | |
| 407 | function image(){ |
| 408 | wp_enqueue_media(); |
| 409 | add_action( 'admin_footer', array($this,'media_selector_scripts') ); |
| 410 | $label = '<label class="h6 mb-0" for="'.esc_attr($this->setting['field']).'">'.esc_html($this->setting['label']).'</label>'; |
| 411 | $desc = (isset($this->setting['desc'])) ? '<br><small>'.wp_kses($this->setting['desc'], $this->allowed_tags).'</small>' : ""; |
| 412 | $field = ' |
| 413 | <div class="row align-items-center"> |
| 414 | <div class="col-6"> |
| 415 | <input id="'.esc_attr($this->setting['field']).'_button" type="button" class="button" value="'.esc_attr(__('Upload image','add-coupon-by-link-woocommerce')).'" /> |
| 416 | <input type="hidden" name="'.esc_attr($this->setting['field']).'" id="'.esc_attr($this->setting['field']).'" value="'.esc_attr($this->saved_value).'"> |
| 417 | </div> |
| 418 | <div class="col-6"> |
| 419 | <div class="image-preview-wrapper"> |
| 420 | <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;"> |
| 421 | <a href="javascript:void(0)" class="clear-image-'.esc_attr($this->setting['field']).'">Clear</a> |
| 422 | </div> |
| 423 | </div> |
| 424 | </div> |
| 425 | '; |
| 426 | $links = $this->generateLinks($this->setting); |
| 427 | |
| 428 | $this->bootstrap($label, $field, $desc, $links); |
| 429 | } |
| 430 | |
| 431 | function media_selector_scripts(){ |
| 432 | $my_saved_attachment_post_id = get_option($this->setting['field'], 0 ); |
| 433 | ?><script type='text/javascript'> |
| 434 | jQuery( document ).ready( function( $ ) { |
| 435 | // Uploading files |
| 436 | var file_frame; |
| 437 | var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id |
| 438 | 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 |
| 439 | jQuery('#<?php echo esc_attr($this->setting['field']); ?>_button').on('click', function( event ){ |
| 440 | event.preventDefault(); |
| 441 | // If the media frame already exists, reopen it. |
| 442 | if ( file_frame ) { |
| 443 | // Set the post ID to what we want |
| 444 | file_frame.uploader.uploader.param( 'post_id', set_to_post_id ); |
| 445 | // Open frame |
| 446 | file_frame.open(); |
| 447 | return; |
| 448 | } else { |
| 449 | // Set the wp.media post id so the uploader grabs the ID we want when initialised |
| 450 | wp.media.model.settings.post.id = set_to_post_id; |
| 451 | } |
| 452 | // Create the media frame. |
| 453 | file_frame = wp.media.frames.file_frame = wp.media({ |
| 454 | title: 'Select a image to upload', |
| 455 | button: { |
| 456 | text: 'Use this image', |
| 457 | }, |
| 458 | multiple: false // Set to true to allow multiple files to be selected |
| 459 | }); |
| 460 | // When an image is selected, run a callback. |
| 461 | file_frame.on( 'select', function() { |
| 462 | // We set multiple to false so only get one image from the uploader |
| 463 | attachment = file_frame.state().get('selection').first().toJSON(); |
| 464 | // Do something with attachment.id and/or attachment.url here |
| 465 | $( '#<?php echo esc_attr($this->setting['field']); ?>_preview' ).attr( 'src', attachment.url ).css( 'width', 'auto' ); |
| 466 | $( '#<?php echo esc_attr($this->setting['field']); ?>' ).val( attachment.id ); |
| 467 | // Restore the main post ID |
| 468 | wp.media.model.settings.post.id = wp_media_post_id; |
| 469 | }); |
| 470 | // Finally, open the modal |
| 471 | file_frame.open(); |
| 472 | }); |
| 473 | // Restore the main ID when the add media button is pressed |
| 474 | jQuery( 'a.add_media' ).on( 'click', function() { |
| 475 | wp.media.model.settings.post.id = wp_media_post_id; |
| 476 | }); |
| 477 | jQuery( 'a.clear-image-<?php echo esc_attr($this->setting['field']); ?>' ).on( 'click', function() { |
| 478 | $( '#<?php echo esc_attr($this->setting['field']); ?>_preview' ).attr("src",""); |
| 479 | $( '#<?php echo esc_attr($this->setting['field']); ?>' ).val(""); |
| 480 | }); |
| 481 | }); |
| 482 | </script> |
| 483 | <?php |
| 484 | } |
| 485 | } |
| 486 | endif; |