async-upload.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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'] ) && intval( $_REQUEST['attachment_id'] ) && $_REQUEST['fetch'] ) {
  33. $id = intval( $_REQUEST['attachment_id'] );
  34. $post = get_post( $id );
  35. if ( 'attachment' != $post->post_type ) {
  36. wp_die( __( 'Invalid post type.' ) );
  37. }
  38. if ( ! current_user_can( 'edit_post', $id ) ) {
  39. wp_die( __( 'Sorry, you are not allowed to edit this item.' ) );
  40. }
  41. switch ( $_REQUEST['fetch'] ) {
  42. case 3:
  43. $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true );
  44. if ( $thumb_url ) {
  45. echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
  46. }
  47. echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '" target="_blank">' . _x( 'Edit', 'media item' ) . '</a>';
  48. // Title shouldn't ever be empty, but use filename just in case.
  49. $file = get_attached_file( $post->ID );
  50. $title = $post->post_title ? $post->post_title : wp_basename( $file );
  51. echo '<div class="filename new"><span class="title">' . esc_html( wp_html_excerpt( $title, 60, '&hellip;' ) ) . '</span></div>';
  52. break;
  53. case 2:
  54. add_filter( 'attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2 );
  55. echo get_media_item(
  56. $id,
  57. array(
  58. 'send' => false,
  59. 'delete' => true,
  60. )
  61. );
  62. break;
  63. default:
  64. add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 );
  65. echo get_media_item( $id );
  66. break;
  67. }
  68. exit;
  69. }
  70. check_admin_referer( 'media-form' );
  71. $post_id = 0;
  72. if ( isset( $_REQUEST['post_id'] ) ) {
  73. $post_id = absint( $_REQUEST['post_id'] );
  74. if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
  75. $post_id = 0;
  76. }
  77. }
  78. $id = media_handle_upload( 'async-upload', $post_id );
  79. if ( is_wp_error( $id ) ) {
  80. printf(
  81. '<div class="error-div error">%s <strong>%s</strong><br />%s</div>',
  82. sprintf(
  83. '<button type="button" class="dismiss button-link" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">%s</button>',
  84. __( 'Dismiss' )
  85. ),
  86. sprintf(
  87. /* translators: %s: Name of the file that failed to upload. */
  88. __( '&#8220;%s&#8221; has failed to upload.' ),
  89. esc_html( $_FILES['async-upload']['name'] )
  90. ),
  91. esc_html( $id->get_error_message() )
  92. );
  93. exit;
  94. }
  95. if ( $_REQUEST['short'] ) {
  96. // Short form response - attachment ID only.
  97. echo $id;
  98. } else {
  99. // Long form response - big chunk of html.
  100. $type = $_REQUEST['type'];
  101. /**
  102. * Filters the returned ID of an uploaded attachment.
  103. *
  104. * The dynamic portion of the hook name, `$type`, refers to the attachment type,
  105. * such as 'image', 'audio', 'video', 'file', etc.
  106. *
  107. * @since 2.5.0
  108. *
  109. * @param int $id Uploaded attachment ID.
  110. */
  111. echo apply_filters( "async_upload_{$type}", $id );
  112. }