async-upload.php 4.1 KB

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