async-upload.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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( __DIR__ ) . '/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. require 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'] ) && (int) $_REQUEST['attachment_id'] && $_REQUEST['fetch'] ) {
  33. $id = (int) $_REQUEST['attachment_id'];
  34. $post = get_post( $id );
  35. if ( 'attachment' !== $post->post_type ) {
  36. wp_die( __( 'Invalid post type.' ) );
  37. }
  38. switch ( $_REQUEST['fetch'] ) {
  39. case 3:
  40. ?>
  41. <div class="media-item-wrapper">
  42. <div class="attachment-details">
  43. <?php
  44. $thumb_url = wp_get_attachment_image_src( $id, 'thumbnail', true );
  45. if ( $thumb_url ) {
  46. echo '<img class="pinkynail" src="' . esc_url( $thumb_url[0] ) . '" alt="" />';
  47. }
  48. // Title shouldn't ever be empty, but use filename just in case.
  49. $file = get_attached_file( $post->ID );
  50. $file_url = wp_get_attachment_url( $post->ID );
  51. $title = $post->post_title ? $post->post_title : wp_basename( $file );
  52. ?>
  53. <div class="filename new">
  54. <span class="media-list-title"><strong><?php echo esc_html( wp_html_excerpt( $title, 60, '&hellip;' ) ); ?></strong></span>
  55. <span class="media-list-subtitle"><?php echo wp_basename( $file ); ?></span>
  56. </div>
  57. </div>
  58. <div class="attachment-tools">
  59. <span class="media-item-copy-container copy-to-clipboard-container edit-attachment">
  60. <button type="button" class="button button-small copy-attachment-url" data-clipboard-text="<?php echo $file_url; ?>"><?php _e( 'Copy URL to clipboard' ); ?></button>
  61. <span class="success hidden" aria-hidden="true"><?php _e( 'Copied!' ); ?></span>
  62. </span>
  63. <?php
  64. if ( current_user_can( 'edit_post', $id ) ) {
  65. echo '<a class="edit-attachment" href="' . esc_url( get_edit_post_link( $id ) ) . '">' . _x( 'Edit', 'media item' ) . '</a>';
  66. } else {
  67. echo '<span class="edit-attachment">' . _x( 'Success', 'media item' ) . '</span>';
  68. }
  69. ?>
  70. </div>
  71. </div>
  72. <?php
  73. break;
  74. case 2:
  75. add_filter( 'attachment_fields_to_edit', 'media_single_attachment_fields_to_edit', 10, 2 );
  76. echo get_media_item(
  77. $id,
  78. array(
  79. 'send' => false,
  80. 'delete' => true,
  81. )
  82. );
  83. break;
  84. default:
  85. add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 );
  86. echo get_media_item( $id );
  87. break;
  88. }
  89. exit;
  90. }
  91. check_admin_referer( 'media-form' );
  92. $post_id = 0;
  93. if ( isset( $_REQUEST['post_id'] ) ) {
  94. $post_id = absint( $_REQUEST['post_id'] );
  95. if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
  96. $post_id = 0;
  97. }
  98. }
  99. $id = media_handle_upload( 'async-upload', $post_id );
  100. if ( is_wp_error( $id ) ) {
  101. printf(
  102. '<div class="error-div error">%s <strong>%s</strong><br />%s</div>',
  103. sprintf(
  104. '<button type="button" class="dismiss button-link" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">%s</button>',
  105. __( 'Dismiss' )
  106. ),
  107. sprintf(
  108. /* translators: %s: Name of the file that failed to upload. */
  109. __( '&#8220;%s&#8221; has failed to upload.' ),
  110. esc_html( $_FILES['async-upload']['name'] )
  111. ),
  112. esc_html( $id->get_error_message() )
  113. );
  114. exit;
  115. }
  116. if ( $_REQUEST['short'] ) {
  117. // Short form response - attachment ID only.
  118. echo $id;
  119. } else {
  120. // Long form response - big chunk of HTML.
  121. $type = $_REQUEST['type'];
  122. /**
  123. * Filters the returned ID of an uploaded attachment.
  124. *
  125. * The dynamic portion of the hook name, `$type`, refers to the attachment type.
  126. *
  127. * Possible hook names include:
  128. *
  129. * - `async_upload_audio`
  130. * - `async_upload_file`
  131. * - `async_upload_image`
  132. * - `async_upload_video`
  133. *
  134. * @since 2.5.0
  135. *
  136. * @param int $id Uploaded attachment ID.
  137. */
  138. echo apply_filters( "async_upload_{$type}", $id );
  139. }