async-upload.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Server-side file upload handler from wp-plupload or other asynchronous upload methods.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
  9. define( 'DOING_AJAX', true );
  10. }
  11. if ( ! defined( 'WP_ADMIN' ) ) {
  12. define( 'WP_ADMIN', true );
  13. }
  14. if ( defined( 'ABSPATH' ) ) {
  15. require_once( ABSPATH . 'wp-load.php' );
  16. } else {
  17. require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
  18. }
  19. require_once( ABSPATH . 'wp-admin/admin.php' );
  20. header( 'Content-Type: text/plain; charset=' . get_option( 'blog_charset' ) );
  21. if ( isset( $_REQUEST['action'] ) && 'upload-attachment' === $_REQUEST['action'] ) {
  22. include( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
  23. send_nosniff_header();
  24. nocache_headers();
  25. wp_ajax_upload_attachment();
  26. die( '0' );
  27. }
  28. if ( ! current_user_can( 'upload_files' ) ) {
  29. wp_die( __( 'Sorry, you are not allowed to upload files.' ) );
  30. }
  31. // just fetch the detail form for that attachment
  32. if ( isset($_REQUEST['attachment_id']) && ($id = intval($_REQUEST['attachment_id'])) && $_REQUEST['fetch'] ) {
  33. $post = get_post( $id );
  34. if ( 'attachment' != $post->post_type )
  35. wp_die( __( 'Invalid post type.' ) );
  36. if ( ! current_user_can( 'edit_post', $id ) )
  37. wp_die( __( 'Sorry, you are not allowed to edit this item.' ) );
  38. switch ( $_REQUEST['fetch'] ) {
  39. case 3 :
  40. if ( $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true ) )
  41. echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
  42. echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '" target="_blank">' . _x( 'Edit', 'media item' ) . '</a>';
  43. // Title shouldn't ever be empty, but use filename just in case.
  44. $file = get_attached_file( $post->ID );
  45. $title = $post->post_title ? $post->post_title : wp_basename( $file );
  46. echo '<div class="filename new"><span class="title">' . esc_html( wp_html_excerpt( $title, 60, '&hellip;' ) ) . '</span></div>';
  47. break;
  48. case 2 :
  49. add_filter('attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2);
  50. echo get_media_item($id, array( 'send' => false, 'delete' => true ));
  51. break;
  52. default:
  53. add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
  54. echo get_media_item($id);
  55. break;
  56. }
  57. exit;
  58. }
  59. check_admin_referer('media-form');
  60. $post_id = 0;
  61. if ( isset( $_REQUEST['post_id'] ) ) {
  62. $post_id = absint( $_REQUEST['post_id'] );
  63. if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) )
  64. $post_id = 0;
  65. }
  66. $id = media_handle_upload( 'async-upload', $post_id );
  67. if ( is_wp_error($id) ) {
  68. echo '<div class="error-div error">
  69. <a class="dismiss" href="#" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">' . __('Dismiss') . '</a>
  70. <strong>' . sprintf(__('&#8220;%s&#8221; has failed to upload.'), esc_html($_FILES['async-upload']['name']) ) . '</strong><br />' .
  71. esc_html($id->get_error_message()) . '</div>';
  72. exit;
  73. }
  74. if ( $_REQUEST['short'] ) {
  75. // Short form response - attachment ID only.
  76. echo $id;
  77. } else {
  78. // Long form response - big chunk o html.
  79. $type = $_REQUEST['type'];
  80. /**
  81. * Filters the returned ID of an uploaded attachment.
  82. *
  83. * The dynamic portion of the hook name, `$type`, refers to the attachment type,
  84. * such as 'image', 'audio', 'video', 'file', etc.
  85. *
  86. * @since 2.5.0
  87. *
  88. * @param int $id Uploaded attachment ID.
  89. */
  90. echo apply_filters( "async_upload_{$type}", $id );
  91. }