image.php
| 1 | <?php |
| 2 | |
| 3 | class acf_Image extends acf_Field |
| 4 | { |
| 5 | |
| 6 | /*-------------------------------------------------------------------------------------- |
| 7 | * |
| 8 | * Constructor |
| 9 | * |
| 10 | * @author Elliot Condon |
| 11 | * @since 1.0.0 |
| 12 | * @updated 2.2.0 |
| 13 | * |
| 14 | *-------------------------------------------------------------------------------------*/ |
| 15 | |
| 16 | function __construct($parent) |
| 17 | { |
| 18 | parent::__construct($parent); |
| 19 | |
| 20 | $this->name = 'image'; |
| 21 | $this->title = __('Image','acf'); |
| 22 | |
| 23 | add_action('admin_head-media-upload-popup', array($this, 'popup_head')); |
| 24 | add_filter('get_media_item_args', array($this, 'allow_img_insertion')); |
| 25 | add_action('wp_ajax_acf_get_preview_image', array($this, 'acf_get_preview_image')); |
| 26 | add_action('acf_head-update_attachment-image', array($this, 'acf_head_update_attachment')); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | /* |
| 31 | * acf_head_update_attachment |
| 32 | * |
| 33 | * @description: |
| 34 | * @since: 3.2.7 |
| 35 | * @created: 4/07/12 |
| 36 | */ |
| 37 | |
| 38 | function acf_head_update_attachment() |
| 39 | { |
| 40 | ?> |
| 41 | <script type="text/javascript"> |
| 42 | (function($){ |
| 43 | |
| 44 | // vars |
| 45 | var div = self.parent.acf_edit_attachment; |
| 46 | |
| 47 | |
| 48 | // add message |
| 49 | self.parent.acf.add_message("<?php _e("Image Updated.",'acf'); ?>", div); |
| 50 | |
| 51 | |
| 52 | })(jQuery); |
| 53 | </script> |
| 54 | <?php |
| 55 | } |
| 56 | |
| 57 | /*-------------------------------------------------------------------------------------- |
| 58 | * |
| 59 | * acf_get_preview_image |
| 60 | * |
| 61 | * @description Returns a json array of preview sized urls |
| 62 | * @author Elliot Condon |
| 63 | * @since 3.1.7 |
| 64 | * |
| 65 | *-------------------------------------------------------------------------------------*/ |
| 66 | |
| 67 | function acf_get_preview_image() |
| 68 | { |
| 69 | |
| 70 | // vars |
| 71 | $id_string = isset($_GET['id']) ? $_GET['id'] : false; |
| 72 | $preview_size = isset($_GET['preview_size']) ? $_GET['preview_size'] : 'thumbnail'; |
| 73 | $return = array(); |
| 74 | |
| 75 | |
| 76 | // attachment ID is required |
| 77 | if($id_string) |
| 78 | { |
| 79 | |
| 80 | // convert id_string into an array |
| 81 | $id_array = explode(',' , $id_string); |
| 82 | if(!is_array($id_array)) |
| 83 | { |
| 84 | $id_array = array( $id_string ); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | // find image preview url for each image |
| 89 | foreach($id_array as $id) |
| 90 | { |
| 91 | $file_src = wp_get_attachment_image_src($id, $preview_size); |
| 92 | $return[] = array( |
| 93 | 'id' => $id, |
| 94 | 'url' => $file_src[0], |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | // return json |
| 101 | echo json_encode( $return ); |
| 102 | die(); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /*-------------------------------------------------------------------------------------- |
| 107 | * |
| 108 | * admin_print_scripts / admin_print_styles |
| 109 | * |
| 110 | * @author Elliot Condon |
| 111 | * @since 3.0.1 |
| 112 | * |
| 113 | *-------------------------------------------------------------------------------------*/ |
| 114 | |
| 115 | function allow_img_insertion($vars) |
| 116 | { |
| 117 | $vars['send'] = true; |
| 118 | return($vars); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /*-------------------------------------------------------------------------------------- |
| 123 | * |
| 124 | * admin_print_scripts / admin_print_styles |
| 125 | * |
| 126 | * @author Elliot Condon |
| 127 | * @since 3.0.0 |
| 128 | * |
| 129 | *-------------------------------------------------------------------------------------*/ |
| 130 | |
| 131 | function admin_print_scripts() |
| 132 | { |
| 133 | wp_enqueue_script(array( |
| 134 | 'jquery', |
| 135 | 'jquery-ui-core', |
| 136 | 'jquery-ui-tabs', |
| 137 | |
| 138 | 'thickbox', |
| 139 | 'media-upload', |
| 140 | )); |
| 141 | } |
| 142 | |
| 143 | function admin_print_styles() |
| 144 | { |
| 145 | wp_enqueue_style(array( |
| 146 | 'thickbox', |
| 147 | )); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /*-------------------------------------------------------------------------------------- |
| 152 | * |
| 153 | * create_field |
| 154 | * |
| 155 | * @author Elliot Condon |
| 156 | * @since 2.0.5 |
| 157 | * @updated 2.2.0 |
| 158 | * |
| 159 | *-------------------------------------------------------------------------------------*/ |
| 160 | |
| 161 | function create_field($field) |
| 162 | { |
| 163 | // vars |
| 164 | $class = ""; |
| 165 | $file_src = ""; |
| 166 | $preview_size = isset($field['preview_size']) ? $field['preview_size'] : 'thumbnail'; |
| 167 | |
| 168 | // get image url |
| 169 | if($field['value'] != '' && is_numeric($field['value'])) |
| 170 | { |
| 171 | $file_src = wp_get_attachment_image_src($field['value'], $preview_size); |
| 172 | $file_src = $file_src[0]; |
| 173 | |
| 174 | if($file_src) |
| 175 | { |
| 176 | $class = "active"; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | ?> |
| 181 | <div class="acf-image-uploader clearfix <?php echo $class; ?>" data-preview_size="<?php echo $preview_size; ?>"> |
| 182 | <input class="value" type="hidden" name="<?php echo $field['name']; ?>" value="<?php echo $field['value']; ?>" /> |
| 183 | <div class="has-image"> |
| 184 | <div class="hover"> |
| 185 | <ul class="bl"> |
| 186 | <li><a class="remove-image ir" href="#"><?php _e("Remove",'acf'); ?></a></li> |
| 187 | <li><a class="edit-image ir" href="#"><?php _e("Edit",'acf'); ?></a></li> |
| 188 | </ul> |
| 189 | </div> |
| 190 | <img src="<?php echo $file_src; ?>" alt=""/> |
| 191 | </div> |
| 192 | <div class="no-image"> |
| 193 | <p><?php _e('No image selected','acf'); ?> <input type="button" class="button add-image" value="<?php _e('Add Image','acf'); ?>" /> |
| 194 | </div> |
| 195 | </div> |
| 196 | <?php |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /*-------------------------------------------------------------------------------------- |
| 201 | * |
| 202 | * create_options |
| 203 | * |
| 204 | * @author Elliot Condon |
| 205 | * @since 2.0.6 |
| 206 | * @updated 2.2.0 |
| 207 | * |
| 208 | *-------------------------------------------------------------------------------------*/ |
| 209 | |
| 210 | function create_options($key, $field) |
| 211 | { |
| 212 | // vars |
| 213 | $defaults = array( |
| 214 | 'save_format' => 'url', |
| 215 | 'preview_size' => 'thumbnail', |
| 216 | ); |
| 217 | |
| 218 | $field = array_merge($defaults, $field); |
| 219 | |
| 220 | ?> |
| 221 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 222 | <td class="label"> |
| 223 | <label><?php _e("Return Value",'acf'); ?></label> |
| 224 | </td> |
| 225 | <td> |
| 226 | <?php |
| 227 | $this->parent->create_field(array( |
| 228 | 'type' => 'radio', |
| 229 | 'name' => 'fields['.$key.'][save_format]', |
| 230 | 'value' => $field['save_format'], |
| 231 | 'layout' => 'horizontal', |
| 232 | 'choices' => array( |
| 233 | 'url' => __("Image URL",'acf'), |
| 234 | 'id' => __("Attachment ID",'acf') |
| 235 | ) |
| 236 | )); |
| 237 | ?> |
| 238 | </td> |
| 239 | </tr> |
| 240 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 241 | <td class="label"> |
| 242 | <label><?php _e("Preview Size",'acf'); ?></label> |
| 243 | </td> |
| 244 | <td> |
| 245 | <?php |
| 246 | |
| 247 | $image_sizes = $this->parent->get_all_image_sizes(); |
| 248 | |
| 249 | $this->parent->create_field(array( |
| 250 | 'type' => 'radio', |
| 251 | 'name' => 'fields['.$key.'][preview_size]', |
| 252 | 'value' => $field['preview_size'], |
| 253 | 'layout' => 'horizontal', |
| 254 | 'choices' => $image_sizes |
| 255 | )); |
| 256 | |
| 257 | ?> |
| 258 | </td> |
| 259 | </tr> |
| 260 | <?php |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | /*--------------------------------------------------------------------------------------------- |
| 266 | * popup_head - STYLES MEDIA THICKBOX |
| 267 | * |
| 268 | * @author Elliot Condon |
| 269 | * @since 1.1.4 |
| 270 | * |
| 271 | ---------------------------------------------------------------------------------------------*/ |
| 272 | function popup_head() |
| 273 | { |
| 274 | |
| 275 | // defults |
| 276 | $access = false; |
| 277 | $tab = "type"; |
| 278 | $preview_size = "thumbnail"; |
| 279 | |
| 280 | |
| 281 | // GET |
| 282 | if(isset($_GET["acf_type"]) && $_GET['acf_type'] == 'image') |
| 283 | { |
| 284 | $access = true; |
| 285 | if( isset($_GET['tab']) ) $tab = $_GET['tab']; |
| 286 | if( isset($_GET['acf_preview_size']) ) $preview_size = $_GET['acf_preview_size']; |
| 287 | |
| 288 | if( isset($_POST["attachments"]) ) |
| 289 | { |
| 290 | echo '<div class="updated"><p>' . __("Media attachment updated.") . '</p></div>'; |
| 291 | } |
| 292 | |
| 293 | } |
| 294 | |
| 295 | |
| 296 | if( $access ) |
| 297 | { |
| 298 | |
| 299 | ?><style type="text/css"> |
| 300 | #media-upload-header #sidemenu li#tab-type_url, |
| 301 | #media-upload-header #sidemenu li#tab-gallery, |
| 302 | #media-items .media-item a.toggle, |
| 303 | #media-items .media-item tr.image-size, |
| 304 | #media-items .media-item tr.align, |
| 305 | #media-items .media-item tr.url, |
| 306 | #media-items .media-item .slidetoggle { |
| 307 | display: none !important; |
| 308 | } |
| 309 | |
| 310 | #media-items .media-item { |
| 311 | min-height: 68px; |
| 312 | } |
| 313 | |
| 314 | #media-items .media-item .acf-checkbox { |
| 315 | float: left; |
| 316 | margin: 28px 10px 0; |
| 317 | } |
| 318 | |
| 319 | #media-items .media-item .pinkynail { |
| 320 | max-width: 64px; |
| 321 | max-height: 64px; |
| 322 | display: block !important; |
| 323 | } |
| 324 | |
| 325 | #media-items .media-item .filename.new { |
| 326 | min-height: 0; |
| 327 | padding: 20px 10px 10px 10px; |
| 328 | line-height: 15px; |
| 329 | } |
| 330 | |
| 331 | #media-items .media-item .title { |
| 332 | line-height: 14px; |
| 333 | } |
| 334 | |
| 335 | #media-items .media-item .acf-select { |
| 336 | float: right; |
| 337 | margin: 22px 12px 0 10px; |
| 338 | } |
| 339 | |
| 340 | #media-upload .ml-submit { |
| 341 | display: none !important; |
| 342 | } |
| 343 | |
| 344 | #media-upload .acf-submit { |
| 345 | margin: 1em 0; |
| 346 | padding: 1em 0; |
| 347 | position: relative; |
| 348 | overflow: hidden; |
| 349 | display: none; /* default is hidden */ |
| 350 | } |
| 351 | |
| 352 | #media-upload .acf-submit a { |
| 353 | float: left; |
| 354 | margin: 0 10px 0 0; |
| 355 | } |
| 356 | |
| 357 | </style> |
| 358 | <script type="text/javascript"> |
| 359 | (function($){ |
| 360 | |
| 361 | /* |
| 362 | * Select Image |
| 363 | * |
| 364 | * @created : 28/03/2012 |
| 365 | */ |
| 366 | |
| 367 | $('#media-items .media-item a.acf-select').live('click', function(){ |
| 368 | |
| 369 | var id = $(this).attr('href'); |
| 370 | |
| 371 | var data = { |
| 372 | action: 'acf_get_preview_image', |
| 373 | id: id, |
| 374 | preview_size : "<?php echo $preview_size; ?>" |
| 375 | }; |
| 376 | |
| 377 | // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php |
| 378 | $.ajax({ |
| 379 | url: ajaxurl, |
| 380 | data : data, |
| 381 | cache: false, |
| 382 | dataType: "json", |
| 383 | success: function( json ) { |
| 384 | |
| 385 | |
| 386 | // validate |
| 387 | if(!json) |
| 388 | { |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | // get item |
| 394 | var item = json[0], |
| 395 | div = self.parent.acf_div; |
| 396 | |
| 397 | |
| 398 | // update acf_div |
| 399 | div.find('input.value').val( item.id ); |
| 400 | div.find('img').attr( 'src', item.url ); |
| 401 | div.addClass('active'); |
| 402 | |
| 403 | |
| 404 | // validation |
| 405 | div.closest('.field').removeClass('error'); |
| 406 | |
| 407 | |
| 408 | // reset acf_div and return false |
| 409 | self.parent.acf_div = null; |
| 410 | self.parent.tb_remove(); |
| 411 | |
| 412 | |
| 413 | } |
| 414 | }); |
| 415 | |
| 416 | return false; |
| 417 | |
| 418 | }); |
| 419 | |
| 420 | |
| 421 | $('#acf-add-selected').live('click', function(){ |
| 422 | |
| 423 | // check total |
| 424 | var total = $('#media-items .media-item .acf-checkbox:checked').length; |
| 425 | if(total == 0) |
| 426 | { |
| 427 | alert("<?php _e("No images selected",'acf'); ?>"); |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | // generate id's |
| 433 | var attachment_ids = []; |
| 434 | $('#media-items .media-item .acf-checkbox:checked').each(function(){ |
| 435 | attachment_ids.push( $(this).val() ); |
| 436 | }); |
| 437 | |
| 438 | |
| 439 | // creae json data |
| 440 | var data = { |
| 441 | action: 'acf_get_preview_image', |
| 442 | id: attachment_ids.join(','), |
| 443 | preview_size : "<?php echo $preview_size; ?>" |
| 444 | }; |
| 445 | |
| 446 | |
| 447 | // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php |
| 448 | $.getJSON(ajaxurl, data, function( json ) { |
| 449 | |
| 450 | // validate |
| 451 | if(!json) |
| 452 | { |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | $.each(json, function(i ,item){ |
| 457 | |
| 458 | // update acf_div |
| 459 | self.parent.acf_div.find('input.value').val( item.id ); |
| 460 | self.parent.acf_div.find('img').attr('src', item.url ); |
| 461 | self.parent.acf_div.addClass('active'); |
| 462 | |
| 463 | |
| 464 | // validation |
| 465 | self.parent.acf_div.closest('.field').removeClass('error'); |
| 466 | |
| 467 | |
| 468 | if((i+1) < total) |
| 469 | { |
| 470 | // add row |
| 471 | self.parent.acf_div.closest('.repeater').find('.add-row-end').trigger('click'); |
| 472 | |
| 473 | // set acf_div to new row image |
| 474 | self.parent.acf_div = self.parent.acf_div.closest('.repeater').find('> table > tbody > tr:last-child .acf-image-uploader'); |
| 475 | } |
| 476 | else |
| 477 | { |
| 478 | // reset acf_div and return false |
| 479 | self.parent.acf_div = null; |
| 480 | self.parent.tb_remove(); |
| 481 | } |
| 482 | |
| 483 | }); |
| 484 | |
| 485 | |
| 486 | |
| 487 | }); |
| 488 | |
| 489 | return false; |
| 490 | |
| 491 | }); |
| 492 | |
| 493 | |
| 494 | // edit toggle |
| 495 | $('#media-items .media-item a.acf-toggle-edit').live('click', function(){ |
| 496 | |
| 497 | if( $(this).hasClass('active') ) |
| 498 | { |
| 499 | $(this).removeClass('active'); |
| 500 | $(this).closest('.media-item').find('.slidetoggle').attr('style', 'display: none !important'); |
| 501 | return false; |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | $(this).addClass('active'); |
| 506 | $(this).closest('.media-item').find('.slidetoggle').attr('style', 'display: table !important'); |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | }); |
| 511 | |
| 512 | |
| 513 | // set a interval function to add buttons to media items |
| 514 | function acf_add_buttons() |
| 515 | { |
| 516 | // vars |
| 517 | var is_sub_field = (self.parent.acf_div && self.parent.acf_div.closest('.repeater').length > 0) ? true : false; |
| 518 | |
| 519 | |
| 520 | // add submit after media items (on for sub fields) |
| 521 | if($('.acf-submit').length == 0 && is_sub_field) |
| 522 | { |
| 523 | $('#media-items').after('<div class="acf-submit"><a id="acf-add-selected" class="button"><?php _e("Add selected Images",'acf'); ?></a></div>'); |
| 524 | } |
| 525 | |
| 526 | |
| 527 | // add buttons to media items |
| 528 | $('#media-items .media-item:not(.acf-active)').each(function(){ |
| 529 | |
| 530 | // show the add all button |
| 531 | $('.acf-submit').show(); |
| 532 | |
| 533 | // needs attachment ID |
| 534 | if($(this).children('input[id*="type-of-"]').length == 0){ return false; } |
| 535 | |
| 536 | // only once! |
| 537 | $(this).addClass('acf-active'); |
| 538 | |
| 539 | // find id |
| 540 | var id = $(this).children('input[id*="type-of-"]').attr('id').replace('type-of-', ''); |
| 541 | |
| 542 | // if inside repeater, add checkbox |
| 543 | if(is_sub_field) |
| 544 | { |
| 545 | $(this).prepend('<input type="checkbox" class="acf-checkbox" value="' + id + '" <?php if($tab == "type"){echo 'checked="checked"';} ?> />'); |
| 546 | } |
| 547 | |
| 548 | // Add edit button |
| 549 | $(this).find('.filename.new').append('<br /><a href="#" class="acf-toggle-edit">Edit</a>'); |
| 550 | |
| 551 | // Add select button |
| 552 | $(this).find('.filename.new').before('<a href="' + id + '" class="button acf-select"><?php _e("Select Image",'acf'); ?></a>'); |
| 553 | |
| 554 | // add save changes button |
| 555 | $(this).find('tr.submit input.button').hide().before('<input type="submit" value="<?php _e("Update Image",'acf'); ?>" class="button savebutton" />'); |
| 556 | |
| 557 | |
| 558 | }); |
| 559 | } |
| 560 | <?php |
| 561 | |
| 562 | // run the acf_add_buttons ever 500ms when on the image upload tab |
| 563 | if($tab == 'type'): ?> |
| 564 | var acf_t = setInterval(function(){ |
| 565 | acf_add_buttons(); |
| 566 | }, 500); |
| 567 | <?php endif; ?> |
| 568 | |
| 569 | |
| 570 | // add acf input filters to allow for tab navigation |
| 571 | $(document).ready(function(){ |
| 572 | |
| 573 | setTimeout(function(){ |
| 574 | acf_add_buttons(); |
| 575 | }, 1); |
| 576 | |
| 577 | |
| 578 | $('form#filter').each(function(){ |
| 579 | |
| 580 | $(this).append('<input type="hidden" name="acf_preview_size" value="<?php echo $preview_size; ?>" />'); |
| 581 | $(this).append('<input type="hidden" name="acf_type" value="image" />'); |
| 582 | |
| 583 | }); |
| 584 | |
| 585 | $('form#image-form, form#library-form').each(function(){ |
| 586 | |
| 587 | var action = $(this).attr('action'); |
| 588 | action += "&acf_type=image&acf_preview_size=<?php echo $preview_size; ?>"; |
| 589 | $(this).attr('action', action); |
| 590 | |
| 591 | }); |
| 592 | }); |
| 593 | |
| 594 | })(jQuery); |
| 595 | </script><?php |
| 596 | |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | |
| 601 | /*-------------------------------------------------------------------------------------- |
| 602 | * |
| 603 | * get_value_for_api |
| 604 | * |
| 605 | * @author Elliot Condon |
| 606 | * @since 3.0.0 |
| 607 | * |
| 608 | *-------------------------------------------------------------------------------------*/ |
| 609 | |
| 610 | function get_value_for_api($post_id, $field) |
| 611 | { |
| 612 | // vars |
| 613 | $format = isset($field['save_format']) ? $field['save_format'] : 'url'; |
| 614 | |
| 615 | $value = parent::get_value($post_id, $field); |
| 616 | |
| 617 | if($format == 'url') |
| 618 | { |
| 619 | $value = wp_get_attachment_url($value); |
| 620 | } |
| 621 | |
| 622 | return $value; |
| 623 | } |
| 624 | |
| 625 | |
| 626 | |
| 627 | } |
| 628 | |
| 629 | ?> |