| 27 | | <?php if ($error) { ?> |
|---|
| 28 | | <div id="media-upload-error"> |
|---|
| 29 | | <?php echo $error->get_error_message(); ?> |
|---|
| 30 | | </div> |
|---|
| 31 | | <?php } ?> |
|---|
| | 28 | <div id="media-upload-error"> |
|---|
| | 29 | <?php if ($error) { |
|---|
| | 30 | echo $error->get_error_message(); |
|---|
| | 31 | } ?> |
|---|
| | 32 | </div> |
|---|
| | 33 | <script type="text/javascript"> |
|---|
| | 34 | <!-- |
|---|
| | 35 | |
|---|
| | 36 | jQuery(document).ready(function(){ |
|---|
| | 37 | var swfu = new SWFUpload({ |
|---|
| | 38 | upload_url : "<?php echo get_option('siteurl').'/wp-admin/async-upload.php'; ?>", |
|---|
| | 39 | flash_url : "<?php echo get_option('siteurl').'/wp-includes/js/swfupload/swfupload_f9.swf'; ?>", |
|---|
| | 40 | file_post_name: "async-upload", |
|---|
| | 41 | swfupload_element_id : "flash-upload-ui", // id of the element displayed when swfupload is available |
|---|
| | 42 | degraded_element_id : "html-upload-ui", // when swfupload is unavailable |
|---|
| | 43 | //file_types : "*.jpg;*.gif;*.png", |
|---|
| | 44 | file_size_limit : "<?php echo wp_max_upload_size(); ?> B", |
|---|
| | 45 | post_params : { |
|---|
| | 46 | "post_id" : "<?php echo $post_id; ?>", |
|---|
| | 47 | "auth_cookie" : "<?php echo $_COOKIE[AUTH_COOKIE]; ?>", |
|---|
| | 48 | "type" : "image", |
|---|
| | 49 | }, |
|---|
| | 50 | swfupload_loaded_handler : uploadLoadedImage, |
|---|
| | 51 | upload_progress_handler : uploadProgressImage, |
|---|
| | 52 | upload_success_handler : uploadSuccessImage, |
|---|
| | 53 | upload_error_handler: uploadError, |
|---|
| | 54 | file_queued_handler : fileQueuedImage, |
|---|
| | 55 | file_queue_error_handler : fileQueueError, |
|---|
| | 56 | file_dialog_complete_handler : fileDialogComplete, |
|---|
| | 57 | |
|---|
| | 58 | custom_settings : { |
|---|
| | 59 | progressTarget : "flash-upload-ui", |
|---|
| | 60 | cancelButtonId : "btnCancel2" |
|---|
| | 61 | }, |
|---|
| | 62 | |
|---|
| | 63 | debug: false, |
|---|
| | 64 | |
|---|
| | 65 | }); |
|---|
| | 66 | |
|---|
| | 67 | document.getElementById("flash-browse-button").onclick = function () { swfu.selectFile(); }; |
|---|
| | 68 | }); |
|---|
| | 69 | //--> |
|---|
| | 70 | </script> |
|---|
| | 147 | // this returns html to include in the single image upload form when the async flash upload has finished |
|---|
| | 148 | // i.e. show a thumb of the image, and include the attachment id as a hidden input |
|---|
| | 149 | function async_image_callback($id) { |
|---|
| | 150 | $thumb_url = wp_get_attachment_thumb_url($id); |
|---|
| | 151 | if ( empty($thumb_url) ) |
|---|
| | 152 | $thumb_url = wp_mime_type_icon($id); |
|---|
| | 153 | |
|---|
| | 154 | if ($thumb_url) { |
|---|
| | 155 | $out = '<p><input type="hidden" name="attachment_id" id="attachment_id" value="'.intval($id).'" />' |
|---|
| | 156 | . '<img src="'.wp_get_attachment_thumb_url($id).'" class="pinkynail" /> ' |
|---|
| | 157 | . basename(wp_get_attachment_url($id)).'</p>'; |
|---|
| | 158 | } |
|---|
| | 159 | else { |
|---|
| | 160 | $out = '<p><input type="hidden" name="attachment_id" id="attachment_id" value="'.intval($id).'" />' |
|---|
| | 161 | . basename(wp_get_attachment_url($id)).'</p>'; |
|---|
| | 162 | } |
|---|
| | 163 | |
|---|
| | 164 | $post = get_post($id); |
|---|
| | 165 | $title = addslashes($post->post_title); |
|---|
| | 166 | $alt = addslashes($post->post_content); |
|---|
| | 167 | |
|---|
| | 168 | // populate the input fields with post data (which in turn comes from exif/iptc) |
|---|
| | 169 | $out .= <<<EOF |
|---|
| | 170 | <script type="text/javascript"> |
|---|
| | 171 | <!-- |
|---|
| | 172 | jQuery('#image-alt').val('{$alt}').attr('disabled', false); |
|---|
| | 173 | jQuery('#image-title').val('{$title}').attr('disabled', false); |
|---|
| | 174 | jQuery('#image-url').attr('disabled', false); |
|---|
| | 175 | jQuery('#image-add').attr('disabled', false); |
|---|
| | 176 | --> |
|---|
| | 177 | </script> |
|---|
| | 178 | EOF; |
|---|
| | 179 | |
|---|
| | 180 | return $out; |
|---|
| | 181 | } |
|---|
| | 182 | |
|---|
| | 183 | add_filter('async_upload_image', 'async_image_callback'); |
|---|
| | 184 | |
|---|
| | 185 | |
|---|
| | 250 | return $id; |
|---|
| | 251 | } |
|---|
| | 252 | |
|---|
| | 253 | // this handles the file upload POST itself, creating the attachment post |
|---|
| | 254 | function media_handle_upload($file_id, $post_id, $post_data = array()) { |
|---|
| | 255 | $overrides = array('test_form'=>false); |
|---|
| | 256 | $file = wp_handle_upload($_FILES[$file_id], $overrides); |
|---|
| | 257 | |
|---|
| | 258 | if ( isset($file['error']) ) |
|---|
| | 259 | return new wp_error( 'upload_error', $file['error'] ); |
|---|
| | 260 | |
|---|
| | 261 | $url = $file['url']; |
|---|
| | 262 | $type = $file['type']; |
|---|
| | 263 | $file = $file['file']; |
|---|
| | 264 | $title = preg_replace('/\.[^.]+$/', '', basename($file)); |
|---|
| | 265 | $content = ''; |
|---|
| | 266 | |
|---|
| | 267 | // use image exif/iptc data for title and caption defaults if possible |
|---|
| | 268 | if ( $image_meta = @wp_read_image_metadata($file) ) { |
|---|
| | 269 | if ( trim($image_meta['title']) ) |
|---|
| | 270 | $title = $image_meta['title']; |
|---|
| | 271 | if ( trim($image_meta['caption']) ) |
|---|
| | 272 | $content = $image_meta['caption']; |
|---|
| | 273 | } |
|---|
| | 274 | |
|---|
| | 275 | // Construct the attachment array |
|---|
| | 276 | $attachment = array_merge( array( |
|---|
| | 277 | 'post_mime_type' => $type, |
|---|
| | 278 | 'guid' => $url, |
|---|
| | 279 | 'post_parent' => $post_id, |
|---|
| | 280 | 'post_title' => $title, |
|---|
| | 281 | 'post_content' => $content, |
|---|
| | 282 | ), $post_data ); |
|---|
| | 283 | |
|---|
| | 284 | // Save the data |
|---|
| | 285 | $id = wp_insert_attachment($attachment, $file, $post_parent); |
|---|
| | 286 | if ( !is_wp_error($id) ) { |
|---|
| | 287 | wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); |
|---|
| | 288 | } |
|---|
| | 289 | |
|---|
| | 369 | function multimedia_upload_handler() { |
|---|
| | 370 | if ( !current_user_can('upload_files') ) { |
|---|
| | 371 | return new wp_error( 'upload_not_allowed', __('You are not allowed to upload files.') ); |
|---|
| | 372 | } |
|---|
| | 373 | |
|---|
| | 374 | if ( empty($_POST) ) { |
|---|
| | 375 | // no button click, we're just displaying the form |
|---|
| | 376 | wp_iframe( 'multimedia_upload_form' ); |
|---|
| | 377 | } elseif ( empty($_POST['upload-button']) ) { |
|---|
| | 378 | // Insert multimedia button was clicked |
|---|
| | 379 | check_admin_referer('multimedia-form'); |
|---|
| | 380 | |
|---|
| | 381 | if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) { |
|---|
| | 382 | $post = $_post = get_post($attachment_id, ARRAY_A); |
|---|
| | 383 | $post['post_content'] = $attachment['post_content']; |
|---|
| | 384 | $post['post_title'] = $attachment['post_title']; |
|---|
| | 385 | if ( $post != $_post ) |
|---|
| | 386 | wp_update_post($post); |
|---|
| | 387 | |
|---|
| | 388 | if ( $taxonomies = get_object_taxonomies('attachment') ) foreach ( $taxonomies as $t ) |
|---|
| | 389 | if ( isset($attachment[$t]) ) |
|---|
| | 390 | wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false); |
|---|
| | 391 | } |
|---|
| | 392 | |
|---|
| | 393 | media_send_to_editor('[gallery]'); |
|---|
| | 394 | } else { |
|---|
| | 395 | // Upload File button was clicked |
|---|
| | 396 | |
|---|
| | 397 | $id = media_handle_upload('async-upload', $_REQUEST['post_id']); |
|---|
| | 398 | |
|---|
| | 399 | wp_iframe( 'multimedia_upload_form' ); |
|---|
| | 400 | } |
|---|
| | 401 | } |
|---|
| | 402 | |
|---|
| | 403 | function get_multimedia_items( $post_id ) { |
|---|
| | 404 | $attachments = get_children("post_parent=$post_id&post_type=attachment&orderby=\"menu_order ASC, ID ASC\""); |
|---|
| | 405 | |
|---|
| | 406 | if ( empty($attachments) ) |
|---|
| | 407 | return ''; |
|---|
| | 408 | |
|---|
| | 409 | foreach ( $attachments as $id => $attachment ) { |
|---|
| | 410 | $output .= "\n<div id='multimedia-item-$id' class='multimedia-item preloaded'><div id='media-upload-error-$id'></div><span class='filename'></span><div class='progress'><div class='bar'></div></div>"; |
|---|
| | 411 | $output .= get_multimedia_item($id); |
|---|
| | 412 | $output .= " <div class='progress clickmask'></div>\n</div>"; |
|---|
| | 413 | } |
|---|
| | 414 | |
|---|
| | 415 | return $output; |
|---|
| | 416 | } |
|---|
| | 417 | |
|---|
| | 418 | function get_multimedia_item( $attachment_id ) { |
|---|
| | 419 | $thumb_url = wp_get_attachment_thumb_url( $attachment_id ); |
|---|
| | 420 | if ( empty($thumb_url) ) |
|---|
| | 421 | $thumb_url = wp_mime_type_icon( $attachment_id ); |
|---|
| | 422 | |
|---|
| | 423 | $title_label = __('Title'); |
|---|
| | 424 | $description_label = __('Description'); |
|---|
| | 425 | $tags_label = __('Tags'); |
|---|
| | 426 | |
|---|
| | 427 | $toggle_on = __('Describe »'); |
|---|
| | 428 | $toggle_off = __('Describe «'); |
|---|
| | 429 | |
|---|
| | 430 | $post = get_post($attachment_id); |
|---|
| | 431 | |
|---|
| | 432 | $filename = basename($post->guid); |
|---|
| | 433 | $title = attribute_escape($post->post_title); |
|---|
| | 434 | $description = attribute_escape($post->post_content); |
|---|
| | 435 | if ( $_tags = get_the_tags($attachment_id) ) { |
|---|
| | 436 | foreach ( $_tags as $tag ) |
|---|
| | 437 | $tags[] = $tag->name; |
|---|
| | 438 | $tags = attribute_escape(join(', ', $tags)); |
|---|
| | 439 | } |
|---|
| | 440 | |
|---|
| | 441 | $delete_href = wp_nonce_url("post.php?action=delete-post&post=$attachment_id", 'delete-post_' . $attachment_id); |
|---|
| | 442 | $delete = __('Delete'); |
|---|
| | 443 | |
|---|
| | 444 | $item = " |
|---|
| | 445 | <a class='toggle describe-toggle-on' href='#'>$toggle_on</a> |
|---|
| | 446 | <a class='toggle describe-toggle-off' href='#'>$toggle_off</a> |
|---|
| | 447 | <span class='filename new'>$filename</span> |
|---|
| | 448 | <div class='slidetoggle describe'> |
|---|
| | 449 | <img class='thumbnail' src='$thumb_url' alt='' /> |
|---|
| | 450 | <fieldset> |
|---|
| | 451 | <p><label for='attachments[$attachment_id][post_title]'>$title_label</label><input type='text' id='attachments[$attachment_id][post_title]' name='attachments[$attachment_id][post_title]' value='$title' /></p> |
|---|
| | 452 | <p><label for='attachments[$attachment_id][post_content]'>$description_label</label><input type='text' id='attachments[$attachment_id][post_content]' name='attachments[$attachment_id][post_content]' value='$description' /></p> |
|---|
| | 453 | "; |
|---|
| | 454 | |
|---|
| | 455 | if ( $taxonomies = get_object_taxonomies('attachment') ) foreach ( $taxonomies as $t ) { |
|---|
| | 456 | $tax = get_taxonomy($t); |
|---|
| | 457 | $t_title = !empty($tax->title) ? $tax->title : $t; |
|---|
| | 458 | if ( false === $terms = get_object_term_cache( $attachment_id, $t ) ) |
|---|
| | 459 | $_terms = wp_get_object_terms($attachment_id, $t); |
|---|
| | 460 | if ( $_terms ) { |
|---|
| | 461 | foreach ( $_terms as $term ) |
|---|
| | 462 | $terms[] = $term->name; |
|---|
| | 463 | $terms = join(', ', $terms); |
|---|
| | 464 | } else |
|---|
| | 465 | $terms = ''; |
|---|
| | 466 | $item .= "<p><label for='attachments[$attachment_id][$t]'>$t_title</label><input type='text' id='attachments[$attachment_id][$t]' name='attachments[$attachment_id][$t]' value='$terms' /></p>\n"; |
|---|
| | 467 | } |
|---|
| | 468 | |
|---|
| | 469 | $item .= " |
|---|
| | 470 | </fieldset> |
|---|
| | 471 | <p><a id='del$attachment_id' class='delete' href='$delete_href'>$delete</a></p> |
|---|
| | 472 | </div> |
|---|
| | 473 | "; |
|---|
| | 474 | |
|---|
| | 475 | return $item; |
|---|
| | 476 | } |
|---|
| | 477 | |
|---|
| | 478 | function multimedia_upload_form( $error = null ) { |
|---|
| | 479 | $flash_action_url = get_option('siteurl') . '/wp-admin/async-upload.php?type=multimedia'; |
|---|
| | 480 | $form_action_url = get_option('siteurl') . '/wp-admin/media-upload.php?type=multimedia'; |
|---|
| | 481 | |
|---|
| | 482 | $post_id = intval($_REQUEST['post_id']); |
|---|
| | 483 | |
|---|
| | 485 | <div id="media-upload-header"> |
|---|
| | 486 | <h3>Add Images</h3> |
|---|
| | 487 | </div> |
|---|
| | 488 | <div id="media-upload-error"> |
|---|
| | 489 | <?php if ($error) { ?> |
|---|
| | 490 | <?php echo $error->get_error_message(); ?> |
|---|
| | 491 | <?php } ?> |
|---|
| | 492 | </div> |
|---|
| | 493 | <script type="text/javascript"> |
|---|
| | 494 | <!-- |
|---|
| | 495 | jQuery(function($){ |
|---|
| | 496 | swfu = new SWFUpload({ |
|---|
| | 497 | upload_url : "<?php echo attribute_escape( $flash_action_url ); ?>", |
|---|
| | 498 | flash_url : "<?php echo get_option('siteurl').'/wp-includes/js/swfupload/swfupload_f9.swf'; ?>", |
|---|
| | 499 | file_post_name: "async-upload", |
|---|
| | 500 | file_types: "*.*", |
|---|
| | 501 | post_params : { |
|---|
| | 502 | "post_id" : "<?php echo $post_id; ?>", |
|---|
| | 503 | "auth_cookie" : "<?php echo $_COOKIE[AUTH_COOKIE]; ?>", |
|---|
| | 504 | "type" : "multimedia" |
|---|
| | 505 | }, |
|---|
| | 506 | swfupload_element_id : "flash-upload-ui", // id of the element displayed when swfupload is available |
|---|
| | 507 | degraded_element_id : "html-upload-ui", // when swfupload is unavailable |
|---|
| | 508 | //upload_start_handler : uploadStart, |
|---|
| | 509 | upload_progress_handler : uploadProgressMultimedia, |
|---|
| | 510 | //upload_error_handler : uploadError, |
|---|
| | 511 | upload_success_handler : uploadSuccessMultimedia, |
|---|
| | 512 | upload_complete_handler : uploadCompleteMultimedia, |
|---|
| | 513 | file_dialog_start_handler : fileDialogStart, |
|---|
| | 514 | file_queued_handler : fileQueuedMultimedia, |
|---|
| | 515 | file_queue_error_handler : fileQueueError, |
|---|
| | 516 | file_dialog_complete_handler : fileDialogComplete, |
|---|
| | 517 | |
|---|
| | 518 | debug: false, |
|---|
| | 519 | }); |
|---|
| | 520 | $("#flash-browse-button").bind( "click", function(){swfu.selectFiles();}); |
|---|
| | 521 | $("#insert-multimedia").bind( "click", function(){jQuery(this).parents('form').get(0).submit();}); |
|---|
| | 522 | $(".multimedia-item.preloaded").each(function(){uploadSuccessMultimedia({id:this.id.replace(/[^0-9]/g, '')},'');jQuery('#insert-multimedia').attr('disabled', '');}); |
|---|
| | 523 | $("a.delete").bind('click',function(){$.ajax({url:'admin-ajax.php',type:'post',data:{id:this.id.replace(/del/,''),action:'delete-post',_ajax_nonce:this.href.replace(/^.*wpnonce=/,'')}});$(this).parents(".multimedia-item").eq(0).slideToggle(300, function(){$(this).remove();});return false;}); |
|---|
| | 524 | }); |
|---|
| | 525 | //--> |
|---|
| | 526 | </script> |
|---|
| | 527 | <p id="flash-upload-ui" style="display:none"> |
|---|
| | 528 | <input id="flash-browse-button" type="button" value="<?php _e('Choose Files'); ?>" /> |
|---|
| | 529 | <label for="image-file" class="form-help"><?php _e('Only PNG, JPG, GIF'); ?></label> |
|---|
| | 530 | </p> |
|---|
| | 531 | |
|---|
| | 532 | <form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form"> |
|---|
| | 533 | |
|---|
| | 534 | <div id="html-upload-ui"> |
|---|
| | 535 | <p><label for="async-upload"><?php _e('Choose image'); ?></label> |
|---|
| | 536 | <input type="file" name="async-upload" id="async-upload" /> |
|---|
| | 537 | <label for="image-file" class="form-help"><?php _e('Only PNG, JPG, GIF'); ?></label> |
|---|
| | 538 | </p> |
|---|
| | 539 | <p> |
|---|
| | 540 | <button id="upload-button" name="upload-button" value="1" class="button-ok"><?php _e('Add Image'); ?></button> |
|---|
| | 541 | <a href="#" onClick="return top.tb_remove();" id="image-cancel" class="button-cancel"><?php _e('Cancel'); ?></a> |
|---|
| | 542 | </p> |
|---|
| | 543 | <input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id; ?>" /> |
|---|
| | 544 | <br style="clear:both" /> |
|---|
| | 545 | </div> |
|---|
| | 546 | |
|---|
| | 547 | |
|---|
| | 548 | |
|---|
| | 549 | <div id="multimedia-items"> |
|---|
| | 550 | |
|---|
| | 551 | <?php echo get_multimedia_items($post_id); ?> |
|---|
| | 552 | |
|---|
| | 553 | </div> |
|---|
| | 554 | |
|---|
| | 555 | <p class="submit"> |
|---|
| | 556 | <a href="#" onClick="return top.tb_remove();" id="image-cancel" class="button-cancel"><?php _e('Cancel'); ?></a> |
|---|
| | 557 | <input type="button" class="submit" id="insert-multimedia" value="<?php _e('Insert gallery into post'); ?>" disabled="disabled" /> |
|---|
| | 558 | </p> |
|---|
| | 559 | |
|---|
| | 560 | <?php wp_nonce_field('multimedia-form'); ?> |
|---|
| | 561 | |
|---|
| | 562 | </form> |
|---|
| | 563 | |
|---|
| | 564 | <?php |
|---|
| | 565 | } |
|---|
| | 566 | |
|---|
| | 567 | add_action('admin_head_multimedia_upload_form', 'media_admin_css'); |
|---|
| | 568 | add_filter('async_upload_multimedia', 'get_multimedia_item'); |
|---|
| | 569 | add_filter('media_upload_multimedia', 'multimedia_upload_handler'); |
|---|
| | 570 | |
|---|
| | 571 | // Any 'attachment' taxonomy will be included in the description input form for the multi uploader |
|---|
| | 572 | // Example: |
|---|
| | 573 | //register_taxonomy('attachment_people', 'attachment', array('title' => 'People')); |
|---|
| | 574 | |
|---|
| | 575 | ?> |
|---|